import 'package:flutter_test/flutter_test.dart'; import 'package:halkyon_app/halkyon_app.dart'; import 'package:halkyon_app/testing.dart'; void main() { final builtIn = Instance.parse('learn.halkyon.eu')!; final other = Instance.parse('learn.example.org')!; late MemoryInstanceStore store; late List forgotten; InstanceChoice choice() => InstanceChoice( store, builtIn: builtIn, forget: (instance) async { forgotten.add(instance); }, ); setUp(() { store = MemoryInstanceStore(); forgotten = []; }); test('a new install has no instance until one is chosen', () async { final instances = choice(); await instances.load(usedBefore: false); expect(instances.current, isNull); expect(instances.known, isEmpty); await instances.use(other); expect(instances.current, other); expect(store.kept?.current, other); expect(store.kept?.known, [other]); }); test('an install from before there was a choice goes on with the ' "build's instance", () async { final instances = choice(); await instances.load(usedBefore: true); expect(instances.current, builtIn); expect(store.kept?.current, builtIn); expect(store.kept?.known, [builtIn]); }); test('what was chosen is read back', () async { store = MemoryInstanceStore((current: other, known: [builtIn, other])); final instances = choice(); await instances.load(usedBefore: true); expect(instances.current, other); expect(instances.known, [builtIn, other]); }); test('switching keeps every account; removing one forgets what the ' 'device holds of it, never of the one in use', () async { final instances = choice(); await instances.load(usedBefore: true); await instances.use(other); await instances.use(builtIn); expect(instances.known, [builtIn, other]); await instances.remove(builtIn); expect(forgotten, isEmpty, reason: 'in use'); await instances.remove(other); expect(forgotten, [other]); expect(instances.known, [builtIn]); expect(store.kept?.current, builtIn); expect(store.kept?.known, [builtIn]); }); test('tells its listeners of every change', () async { final instances = choice(); var changes = 0; instances.addListener(() => changes++); await instances.load(usedBefore: false); await instances.use(other); await instances.use(builtIn); await instances.remove(other); expect(changes, 4); }); }