import 'package:flutter_test/flutter_test.dart'; import 'package:friendly_map/modules/layout/layout.dart'; void main() { group('홈 구성', () { test('처음엔 기본 구성, 바꾸면 저장되어 다시 읽어도 같다', () async { final store = MemoryTextStore(); final home = homeLayoutStore(store); expect((await home.load()).first.id, 'chats'); await home.move(0, 3); await home.remove('family'); final items = await home.load(); expect(items.map((e) => e.id).take(3).toList(), ['network', 'gathering', 'chats']); expect(items.any((e) => e.id == 'family'), false); expect((await homeLayoutStore(store).load()).length, items.length); }); test('업데이트로 생긴 기본 항목은 기존 사용자에게도 한 번만 끼워 주고, 지운 항목은 되살리지 않는다', () async { final store = MemoryTextStore(); // 옛 버전이 저장한 홈: 가족 모임이 없고 학교를 지운 상태 final old = ['chats', 'network', 'family', 'company', 'group', 'location', 'discover', 'cards']; await store.write('layout_home', '[${old.map((i) => '{"id":"$i","title":"$i","visible":true}').join(',')}]'); final items = await homeLayoutStore(store).load(); final ids = items.map((e) => e.id).toList(); expect(ids.indexOf('gathering'), ids.indexOf('family') + 1); expect(ids.contains('school'), false); // 사용자가 새 항목을 지우면 다시 나타나지 않는다 await homeLayoutStore(store).remove('gathering'); expect((await homeLayoutStore(store).load()).any((e) => e.id == 'gathering'), false); }); test('숨기기·이름 바꾸기는 upsert 로, 끝 밖으로 옮겨도 안전', () async { final home = homeLayoutStore(MemoryTextStore()); final first = (await home.load()).first; await home.upsert(first.copyWith(title: '대화', visible: false)); final again = (await home.load()).first; expect(again.title, '대화'); expect(again.visible, false); await home.move(0, 999); expect((await home.load()).last.id, 'chats'); await home.move(99, 0); expect((await home.load()).length, defaultHomeItems().length); }); test('처음으로 되돌리기, 깨진 저장은 기본으로', () async { final store = MemoryTextStore(); final home = homeLayoutStore(store); await home.remove('chats'); await home.reset(); expect((await home.load()).first.id, 'chats'); store.data['layout_home'] = '{깨짐'; expect((await home.load()).length, defaultHomeItems().length); }); }); group('명함 틀', () { test('종류마다 기본 틀이 있고 항목이 있다', () { final t = defaultCardTemplates(); expect(t.map((e) => e.kind).toSet(), {'personal', 'company', 'school', 'group', 'family'}); expect(t.every((e) => e.fields.isNotEmpty), true); }); test('알려진 항목은 중복 없이 모든 기본 틀의 항목을 포함', () { final known = {for (final f in knownFieldDefs()) f.key}; expect(known.length, knownFieldDefs().length); for (final t in defaultCardTemplates()) { for (final f in t.fields) { expect(known.contains(f.key), true); } } }); test('항목을 고치고 새 틀을 만들고 지운다', () async { final tpl = cardTemplateStore(MemoryTextStore()); final company = (await tpl.load()).firstWhere((e) => e.kind == 'company'); await tpl.upsert(company.copyWith(fields: [...company.fields, const FieldDef('dept', '부서')])); await tpl.upsert(const CardTemplate(id: 'mine', kind: 'group', title: '동호회', fields: [FieldDef('club', '동호회')])); var list = await tpl.load(); expect(list.firstWhere((e) => e.id == company.id).fields.last.key, 'dept'); expect(list.last.id, 'mine'); list = await tpl.remove(company.id); expect(list.any((e) => e.id == company.id), false); expect(list.last.fields.single.label, '동호회'); }); }); }