halkyon_app: instances, sign-in and sync plumbing for Halkyon's apps

What every Halkyon app's shell needs, taken from Halkyon Learn's and set
on halkyon_base's client: the instances a device keeps an account on,
the first start's tiles and where an instance keeps people's data, the
account switcher; signing in anonymously, with GitHub or an email code,
switching person and the start screen; the gates that build an app's
dependencies per instance and per person; and the sync service with its
status, Settings card and banner. Texts of its own in English and
German take the app's name; what only the app can say, it passes in.
Fakes for apps' tests are in testing.dart.

An instance's basics now name the app it serves (`app`, set by the
host's configuration), and an app accepts only an instance of its own.
SerialQueue and CoalescingRunner move into halkyon_core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-19 15:18:49 +02:00
commit e23cc77f7c
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64
3 changed files with 10761 additions and 10 deletions

View file

@ -0,0 +1,242 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:halkyon_app/halkyon_app.dart';
import 'package:halkyon_app/testing.dart';
void main() {
const anonymous = SignedIn(authUserId: 'anonymous-1', isIdentified: false);
late FakeAuthGateway gateway;
late MemorySignInAddresses addresses;
/// Whether this device holds content a switch would leave behind.
late bool holdsContent;
setUp(() {
gateway = FakeAuthGateway(status: anonymous);
addresses = MemorySignInAddresses();
holdsContent = false;
});
EmailSignInBloc newBloc({String language = 'en'}) {
final bloc = EmailSignInBloc(
AuthRepository(gateway, addresses),
deviceHoldsContent: () async => holdsContent,
language: language,
);
addTearDown(bloc.close);
return bloc;
}
/// A bloc waiting for the code mailed to [address].
Future<EmailSignInBloc> codeMailed(String address) async {
final bloc = newBloc()..add(EmailSignInEventCodeRequested(address));
await expectLater(
bloc.stream,
emitsThrough(isA<EmailSignInStateCode>()),
);
return bloc;
}
test('starts at the address', () {
expect(newBloc().state, const EmailSignInStateAddress());
});
test("mails a code in the app's language, and the code makes the address "
"the person's: they keep everything and become identified", () async {
final bloc = newBloc(language: 'de')
..add(const EmailSignInEventCodeRequested(' Anna@Example.org '));
await expectLater(
bloc.stream,
emitsInOrder([
const EmailSignInStateAddress(sending: true),
const EmailSignInStateCode(
address: 'anna@example.org',
request: 'request-1',
),
]),
);
expect(gateway.mailed.single.language, 'de');
bloc.add(const EmailSignInEventCodeEntered('code-1'));
await expectLater(
bloc.stream,
emitsInOrder([
const EmailSignInStateCode(
address: 'anna@example.org',
request: 'request-1',
checking: true,
),
const EmailSignInStateLinked(address: 'anna@example.org'),
]),
);
expect(
gateway.status,
const SignedIn(authUserId: 'anonymous-1', isIdentified: true),
);
expect(addresses.addresses, {'anonymous-1': 'anna@example.org'});
});
test('says what is wrong with an address, and with a code, and takes the '
'next try', () async {
final bloc = newBloc()..add(const EmailSignInEventCodeRequested('anna'));
await expectLater(
bloc.stream,
emitsInOrder([
const EmailSignInStateAddress(sending: true),
const EmailSignInStateAddress(
problem: EmailSignInProblem.notAnAddress,
),
]),
);
bloc.add(const EmailSignInEventCodeRequested('anna@example.org'));
await expectLater(bloc.stream, emitsThrough(isA<EmailSignInStateCode>()));
bloc.add(const EmailSignInEventCodeEntered('CODE-9'));
await expectLater(
bloc.stream,
emitsThrough(
const EmailSignInStateCode(
address: 'anna@example.org',
request: 'request-1',
problem: EmailSignInProblem.wrongCode,
),
),
);
bloc.add(const EmailSignInEventCodeEntered('CODE-1'));
await expectLater(
bloc.stream,
emitsThrough(const EmailSignInStateLinked(address: 'anna@example.org')),
);
});
test('mails a new code on request, and goes back to the address', () async {
final bloc = await codeMailed('anna@example.org');
bloc.add(const EmailSignInEventCodeResent());
await expectLater(
bloc.stream,
emitsInOrder([
const EmailSignInStateCode(
address: 'anna@example.org',
request: 'request-1',
checking: true,
),
const EmailSignInStateCode(
address: 'anna@example.org',
request: 'request-2',
resent: true,
),
]),
);
bloc.add(const EmailSignInEventAddressReopened());
await expectLater(
bloc.stream,
emits(const EmailSignInStateAddress()),
);
});
test("another person's address: a device holding nothing switches to "
'them straight away', () async {
gateway.addressOwners['ben@example.org'] = 'ben';
final bloc = await codeMailed('ben@example.org');
bloc.add(const EmailSignInEventCodeEntered('CODE-1'));
await expectLater(
bloc.stream,
emitsThrough(const EmailSignInStateSwitching()),
);
await pumpEventQueue();
expect(gateway.emailSwitches, 1);
expect(
gateway.status,
const SignedIn(authUserId: 'ben', isIdentified: true),
);
expect(addresses.addresses, {'ben': 'ben@example.org'});
});
test('a device holding content asks first: declining changes nothing, '
'confirming switches', () async {
gateway.addressOwners['ben@example.org'] = 'ben';
holdsContent = true;
final bloc = await codeMailed('ben@example.org');
bloc.add(const EmailSignInEventCodeEntered('CODE-1'));
await expectLater(
bloc.stream,
emitsThrough(
const EmailSignInStateConfirmSwitch(
address: 'ben@example.org',
switchToken: 'switch:ben@example.org',
),
),
);
bloc.add(const EmailSignInEventSwitchDeclined());
await expectLater(bloc.stream, emits(const EmailSignInStateAddress()));
expect(gateway.emailSwitches, 0);
expect(gateway.status, anonymous);
bloc.add(const EmailSignInEventCodeRequested('ben@example.org'));
await expectLater(bloc.stream, emitsThrough(isA<EmailSignInStateCode>()));
bloc.add(const EmailSignInEventCodeEntered('CODE-2'));
await expectLater(
bloc.stream,
emitsThrough(isA<EmailSignInStateConfirmSwitch>()),
);
bloc.add(const EmailSignInEventSwitchConfirmed());
await expectLater(bloc.stream, emits(const EmailSignInStateSwitching()));
await pumpEventQueue();
expect(gateway.emailSwitches, 1);
});
test("someone identified whose address is another person's is told so, "
'never switched: their own account stays theirs', () async {
gateway = FakeAuthGateway(
status: const SignedIn(authUserId: 'carla', isIdentified: true),
)..addressOwners['ben@example.org'] = 'ben';
final bloc = await codeMailed('ben@example.org');
bloc.add(const EmailSignInEventCodeEntered('CODE-1'));
await expectLater(
bloc.stream,
emitsThrough(const EmailSignInStateTaken(address: 'ben@example.org')),
);
expect(gateway.emailSwitches, 0);
expect(
gateway.status,
const SignedIn(authUserId: 'carla', isIdentified: true),
);
});
test("says when the instance couldn't send the mail", () async {
gateway.emailError = const EmailSignInFailed(
EmailSignInProblem.mailFailed,
);
final bloc = newBloc()
..add(const EmailSignInEventCodeRequested('anna@example.org'));
await expectLater(
bloc.stream,
emitsInOrder([
const EmailSignInStateAddress(sending: true),
const EmailSignInStateAddress(problem: EmailSignInProblem.mailFailed),
]),
);
});
test("says when the instance can't be reached", () async {
gateway.emailError = Exception('offline');
final bloc = newBloc()
..add(const EmailSignInEventCodeRequested('anna@example.org'));
await expectLater(
bloc.stream,
emitsInOrder([
const EmailSignInStateAddress(sending: true),
const EmailSignInStateAddress(problem: EmailSignInProblem.failed),
]),
);
});
}

View file

@ -0,0 +1,246 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:halkyon_app/halkyon_app.dart';
import 'package:halkyon_app/testing.dart';
void main() {
const anonymous = SignedIn(authUserId: 'anonymous-1', isIdentified: false);
late FakeAuthGateway gateway;
/// Whether this device holds content a switch would leave behind.
late bool holdsContent;
setUp(() {
gateway = FakeAuthGateway(status: anonymous);
holdsContent = false;
});
/// A bloc as the account dialog starts it, once it knows whether this
/// instance offers GitHub sign-in.
Future<GitHubSignInBloc> ready() async {
final bloc = GitHubSignInBloc(
AuthRepository(gateway, MemorySignInAddresses()),
deviceHoldsContent: () async => holdsContent,
)..add(const GitHubSignInEventStarted());
addTearDown(bloc.close);
await expectLater(bloc.stream, emits(const GitHubSignInStateReady()));
return bloc;
}
test('offers nothing where the instance has no GitHub sign-in', () async {
gateway.offersGitHub = false;
final bloc = GitHubSignInBloc(
AuthRepository(gateway, MemorySignInAddresses()),
deviceHoldsContent: () async => holdsContent,
)..add(const GitHubSignInEventStarted());
addTearDown(bloc.close);
await expectLater(bloc.stream, emits(const GitHubSignInStateUnavailable()));
});
test('links the GitHub account to the person', () async {
final bloc = await ready();
bloc.add(const GitHubSignInEventRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const GitHubSignInStateBusy(),
const GitHubSignInStateLinked(),
]),
);
expect(
gateway.status,
const SignedIn(authUserId: 'anonymous-1', isIdentified: true),
);
});
test('changes nothing when the person cancels on GitHub', () async {
gateway.gitHubLinkOutcome = GitHubLinkOutcome.cancelled;
final bloc = await ready();
bloc.add(const GitHubSignInEventRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const GitHubSignInStateBusy(),
const GitHubSignInStateReady(),
]),
);
expect(gateway.status, anonymous);
});
test(
"the person can cancel while GitHub's page is open; nothing changes",
() async {
gateway
..canCancelGitHub = true
..gitHubPageOpens = true;
final bloc = await ready();
bloc.add(const GitHubSignInEventRequested());
await expectLater(
bloc.stream,
emits(const GitHubSignInStateBusy(cancellable: true)),
);
bloc.add(const GitHubSignInEventCancelled());
await expectLater(bloc.stream, emits(const GitHubSignInStateReady()));
expect(gateway.gitHubCancels, 1);
expect(gateway.status, anonymous);
},
);
test(
"offers no cancel where the system's sign-in screen has its own",
() async {
gateway.gitHubPageOpens = true;
final bloc = await ready();
bloc.add(const GitHubSignInEventRequested());
await expectLater(
bloc.stream,
emits(
isA<GitHubSignInStateBusy>().having(
(state) => state.cancellable,
'cancellable',
isFalse,
),
),
);
},
);
test(
"closing the dialog while GitHub's page is open cancels the sign-in",
() async {
gateway
..canCancelGitHub = true
..gitHubPageOpens = true;
final bloc = await ready();
bloc.add(const GitHubSignInEventRequested());
await expectLater(
bloc.stream,
emits(const GitHubSignInStateBusy(cancellable: true)),
);
await bloc.close();
expect(gateway.gitHubCancels, 1);
expect(gateway.status, anonymous);
},
);
test('closing the dialog otherwise cancels nothing', () async {
final bloc = await ready();
await bloc.close();
expect(gateway.gitHubCancels, 0);
});
test("switches right away when the GitHub account is someone else's and "
'this device holds nothing', () async {
gateway.gitHubLinkOutcome = GitHubLinkOutcome.takenByAnotherPerson;
final bloc = await ready();
bloc.add(const GitHubSignInEventRequested());
await expectLater(bloc.stream, emits(const GitHubSignInStateBusy()));
await pumpEventQueue();
expect(gateway.gitHubSwitches, 1);
});
test("someone identified whose GitHub account is another person's is told "
'so, never switched: their own account stays theirs', () async {
gateway = FakeAuthGateway(
status: const SignedIn(authUserId: 'carla', isIdentified: true),
)..gitHubLinkOutcome = GitHubLinkOutcome.takenByAnotherPerson;
final bloc = await ready();
bloc.add(const GitHubSignInEventRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const GitHubSignInStateBusy(),
const GitHubSignInStateTaken(),
]),
);
expect(gateway.gitHubSwitches, 0);
expect(
gateway.status,
const SignedIn(authUserId: 'carla', isIdentified: true),
);
});
group("when the GitHub account is someone else's and this device holds "
'content', () {
late GitHubSignInBloc bloc;
setUp(() async {
gateway.gitHubLinkOutcome = GitHubLinkOutcome.takenByAnotherPerson;
holdsContent = true;
bloc = (await ready())..add(const GitHubSignInEventRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const GitHubSignInStateBusy(),
const GitHubSignInStateConfirmSwitch(),
]),
);
});
test('asks first; staying anonymous changes nothing', () async {
bloc.add(const GitHubSignInEventSwitchDeclined());
await expectLater(bloc.stream, emits(const GitHubSignInStateReady()));
expect(gateway.gitHubSwitches, 0);
expect(gateway.status, anonymous);
});
test('switches once confirmed', () async {
bloc.add(const GitHubSignInEventSwitchConfirmed());
await expectLater(bloc.stream, emits(const GitHubSignInStateBusy()));
await pumpEventQueue();
expect(gateway.gitHubSwitches, 1);
});
test("cancelling GitHub's page for the switch stays anonymous", () async {
gateway
..canCancelGitHub = true
..gitHubPageOpens = true;
bloc.add(const GitHubSignInEventSwitchConfirmed());
await expectLater(
bloc.stream,
emits(const GitHubSignInStateBusy(cancellable: true)),
);
bloc.add(const GitHubSignInEventCancelled());
await expectLater(bloc.stream, emits(const GitHubSignInStateReady()));
expect(gateway.gitHubSwitches, 0);
expect(gateway.status, anonymous);
});
});
test('says so when signing in fails', () async {
final bloc = await ready();
gateway.gitHubError = Exception('No connection');
bloc.add(const GitHubSignInEventRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const GitHubSignInStateBusy(),
const GitHubSignInStateReady(failed: true),
]),
);
expect(gateway.status, anonymous);
});
}

View file

@ -0,0 +1,206 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:halkyon_app/halkyon_app.dart';
import 'package:halkyon_app/testing.dart';
void main() {
late FakeAuthGateway gateway;
late MemorySignedOutMark mark;
late SessionBloc bloc;
SessionStateActive anonymous(int number) => SessionStateActive(
SignedIn(authUserId: 'anonymous-$number', isIdentified: false),
);
void start({AuthStatus status = const SignedOut(), bool signedOut = false}) {
gateway = FakeAuthGateway(status: status);
mark = MemorySignedOutMark(signedOut);
bloc = SessionBloc(AuthRepository(gateway, MemorySignInAddresses()), mark);
}
tearDown(() => bloc.close());
test('signs in anonymously at a first start, nobody having signed out '
'here', () async {
start();
bloc.add(const SessionEventStarted());
await expectLater(
bloc.stream,
emitsInOrder([const SessionStateLoading(), anonymous(1)]),
);
expect(gateway.signIns, 1);
});
test('keeps a stored session instead of creating a second account', () async {
const stored = SignedIn(authUserId: 'stored', isIdentified: false);
start(status: stored);
bloc.add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateActive(stored)));
expect(gateway.signIns, 0);
});
test('reports a failed sign-in and signs in when asked again', () async {
start();
gateway.signInError = Exception('No connection');
bloc.add(const SessionEventStarted());
await expectLater(
bloc.stream,
emitsInOrder([const SessionStateLoading(), const SessionStateFailed()]),
);
gateway.signInError = null;
bloc.add(const SessionEventSignInRequested());
await expectLater(
bloc.stream,
emitsInOrder([const SessionStateLoading(), anonymous(2)]),
);
});
test('after signing out, asks how to start, making no account; the next '
'start of the app asks again', () async {
const stored = SignedIn(authUserId: 'stored', isIdentified: false);
start(status: stored);
bloc.add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateActive(stored)));
bloc.add(const SessionEventSignOutRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const SessionStateLoading(),
const SessionStateSignedOut(),
]),
);
// Signing out of an anonymous account deletes it.
expect(gateway.accountDeletions, 1);
expect(gateway.signIns, 0);
expect(mark.signedOut, isTrue);
await bloc.close();
bloc = SessionBloc(AuthRepository(gateway, MemorySignInAddresses()), mark)
..add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
expect(gateway.signIns, 0);
});
test('starting without an account from there signs in anonymously, and '
'the device is no longer signed out', () async {
start(signedOut: true);
bloc.add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
bloc.add(const SessionEventStartedAnonymously());
await expectLater(
bloc.stream,
emitsInOrder([const SessionStateLoading(), anonymous(1)]),
);
expect(mark.signedOut, isFalse);
});
test('signing in with GitHub from there switches to its person; '
'cancelled, it asks again', () async {
start(signedOut: true);
bloc.add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
bloc.add(const SessionEventGitHubRequested());
await expectLater(
bloc.stream,
emitsThrough(
const SessionStateActive(
SignedIn(authUserId: 'github-person', isIdentified: true),
),
),
);
expect(gateway.signIns, 0);
expect(mark.signedOut, isFalse);
});
test('a GitHub account nobody here signs in with yet is said so; any '
'other refusal fails, and nothing is made either way', () async {
start(signedOut: true);
bloc.add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
gateway.gitHubError = const GitHubSignInFailed(
GitHubSignInProblem.unknownAccount,
);
bloc.add(const SessionEventGitHubRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const SessionStateSignedOut(busy: true),
const SessionStateSignedOut(problem: SignedOutProblem.noGitHubAccount),
]),
);
gateway.gitHubError = const GitHubSignInFailed(GitHubSignInProblem.refused);
bloc.add(const SessionEventGitHubRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const SessionStateSignedOut(busy: true),
const SessionStateSignedOut(problem: SignedOutProblem.failed),
]),
);
expect(gateway.signIns, 0);
expect(gateway.gitHubSwitches, 0);
});
test('with an email address from there, it starts without an account and '
'asks for the address', () async {
start(signedOut: true);
bloc.add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
bloc.add(const SessionEventEmailRequested());
await expectLater(
bloc.stream,
emitsThrough(
const SessionStateActive(
SignedIn(authUserId: 'anonymous-1', isIdentified: false),
signsInWithEmail: true,
),
),
);
});
test('a session the server ended asks how to start too', () async {
const stored = SignedIn(authUserId: 'github-1', isIdentified: true);
start(status: stored);
bloc.add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateActive(stored)));
gateway.revokeSession();
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
expect(gateway.signIns, 0);
});
test('deleting the account asks how to start', () async {
const stored = SignedIn(authUserId: 'github-1', isIdentified: true);
start(status: stored);
bloc.add(const SessionEventStarted());
await expectLater(bloc.stream, emits(const SessionStateActive(stored)));
bloc.add(const SessionEventDeleteRequested());
await expectLater(
bloc.stream,
emitsInOrder([
const SessionStateLoading(),
const SessionStateSignedOut(),
]),
);
expect(gateway.accountDeletions, 1);
expect(gateway.signIns, 0);
});
}