frindlymap/test/group_service_test.dart

128 lines
6.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'package:flutter_test/flutter_test.dart';
import 'package:friendly_map/modules/app/app.dart';
import 'package:friendly_map/modules/core/core.dart';
import 'package:friendly_map/modules/transport/transport.dart';
import 'support/app_device.dart';
void main() {
late AppDevice me;
late GroupService groups;
setUp(() async {
me = await AppDevice.create(MemoryHub(), '010-1111-2222', '+821011112222', '나');
groups = me.app.groups;
});
/// 시계를 [minutes] 분 앞으로.
void later(int minutes) => me.app.deps.clock.offset += minutes * 6000;
test('그룹 정의: 처음 네 개, 더하고 이름·종류 바꾸고 지우기', () async {
expect([for (final v in await groups.all()) if (v.depth == 1) v.group.name], ['가족', '학교', '회사', '그룹·여행']);
final g = (await groups.add('등산 모임', CardKinds.group))!;
expect(await groups.add('등산 모임', CardKinds.group), isNull);
expect(await groups.rename(g.id, '산악회'), isTrue);
expect(await groups.setKind(g.id, CardKinds.personal), isTrue);
final v = (await groups.view(g.id))!;
expect(v.group.name, '산악회');
expect(v.group.kind, CardKinds.personal);
expect(await groups.remove(g.id), isTrue);
expect(await groups.view(g.id), isNull);
});
test('잠그면 닫힌 동안 구성원을 볼 수 없고, 비밀번호로 열면 보인다', () async {
final a = (await me.app.friends.add('+821033334444', '김민수'))!;
expect(await groups.addMembers(CardKinds.family, [a.id]), 1);
expect(await groups.setPin(CardKinds.family, '123'), isNotNull, reason: '너무 짧음');
expect(await groups.setPin(CardKinds.family, '2580'), isNull);
expect((await groups.view(CardKinds.family))!.open, isTrue, reason: '건 직후에는 열려 있다');
groups.lock(CardKinds.family);
final closed = (await groups.view(CardKinds.family))!;
expect(closed.open, isFalse);
expect(closed.memberCount, isNull);
expect(await groups.members(CardKinds.family), isNull);
expect(await groups.hiddenPeople(), {a.id});
expect(await groups.groupsOf(a.id), isEmpty);
expect(await groups.addMembers(CardKinds.family, ['x']), 0, reason: '닫힌 그룹에는 넣을 수 없다');
expect(await groups.rename(CardKinds.family, '우리 집'), isFalse);
expect(await groups.remove(CardKinds.family), isFalse);
final wrong = await groups.unlock(CardKinds.family, '0000');
expect(wrong.status, UnlockStatus.wrong);
expect(wrong.triesLeft, 4);
expect((await groups.unlock(CardKinds.family, '2580')).status, UnlockStatus.opened);
expect((await groups.members(CardKinds.family))!.map((p) => p.name), ['김민수']);
expect(await groups.hiddenPeople(), isEmpty);
});
test('연 뒤 정한 시간이 지나면 다시 잠긴다', () async {
await groups.setPin(CardKinds.school, 'abcd');
await groups.setRelockMinutes(3);
later(2);
expect((await groups.view(CardKinds.school))!.open, isTrue);
later(2);
expect((await groups.view(CardKinds.school))!.open, isFalse);
expect(await groups.relockMinutes(), 3);
});
test('5번 틀리면 기다려야 하고, 기다리는 동안은 맞는 비밀번호도 안 된다', () async {
await groups.setPin(CardKinds.company, '9999');
groups.lockAll();
for (var i = 0; i < 4; i++) {
expect((await groups.unlock(CardKinds.company, '1111')).status, UnlockStatus.wrong);
}
final r = await groups.unlock(CardKinds.company, '1111');
expect(r.status, UnlockStatus.waiting);
expect(r.waitSeconds, 30);
expect((await groups.unlock(CardKinds.company, '9999')).status, UnlockStatus.waiting);
later(1);
expect((await groups.unlock(CardKinds.company, '9999')).status, UnlockStatus.opened);
});
test('비밀번호 바꾸기·풀기는 지금 비밀번호가 맞아야 한다', () async {
await groups.setPin(CardKinds.group, '1234');
expect(await groups.setPin(CardKinds.group, '5678', current: '0000'), isNotNull);
expect(await groups.setPin(CardKinds.group, '5678', current: '1234'), isNull);
expect(await groups.clearPin(CardKinds.group, '1234'), isNotNull);
expect(await groups.clearPin(CardKinds.group, '5678'), isNull);
groups.lockAll();
expect((await groups.view(CardKinds.group))!.open, isTrue, reason: '푼 그룹은 늘 열려 있다');
expect((await groups.view(CardKinds.group))!.group.locked, isFalse);
});
test('잠금은 저장되어 앱을 다시 열어도 유지된다 (연 상태는 남지 않음)', () async {
await groups.setPin(CardKinds.family, '2468');
final again = GroupService(me.app.deps.storage, me.app.deps.clock, me.app.bus);
final v = (await again.view(CardKinds.family))!;
expect(v.group.locked, isTrue);
expect(v.open, isFalse);
});
test('하위 그룹: 열린 부모 아래에만 만들고, 부모가 잠기면 하위도 닫힌다', () async {
final hb = (await groups.add('한빛전자', CardKinds.company, parent: CardKinds.company))!;
final team = (await groups.add('결제팀', CardKinds.company, parent: hb.id))!;
final a = (await me.app.friends.add('+821033334444', '김민수'))!;
await groups.addMembers(team.id, [a.id]);
final v = (await groups.view(CardKinds.company))!;
expect(v.totalCount, 1);
expect((await groups.view(team.id))!.path, '회사 › 한빛전자 › 결제팀');
expect((await groups.children(CardKinds.company)).map((c) => c.group.name), ['한빛전자']);
await groups.setPin(CardKinds.company, '7777');
groups.lockAll();
expect((await groups.view(team.id))!.open, isFalse, reason: '부모가 잠기면 하위도 닫힘');
expect(await groups.members(team.id), isNull);
expect(await groups.children(CardKinds.company), isEmpty);
expect(await groups.hiddenPeople(), {a.id});
expect(await groups.add('새 팀', CardKinds.company, parent: hb.id), isNull);
expect((await groups.all()).any((x) => x.group.id == team.id), isFalse, reason: '닫힌 부모의 하위는 목록에서 뺀다');
await groups.unlock(CardKinds.company, '7777');
expect((await groups.members(team.id))!.single.name, '김민수');
expect(await groups.remove(CardKinds.company), isTrue);
expect(await groups.view(team.id), isNull, reason: '하위 그룹도 함께 지워진다');
expect(await me.app.friends.person(a.id), isNotNull, reason: '사람은 남는다');
});
}