This repository has been archived on 2026-10-23. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
halkyon_shared/packages/halkyon_app/test/base/base_client_test.dart
Tizian 「ティツィアーン」 e23cc77f7c
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>
2026-10-19 15:31:40 +02:00

106 lines
3.2 KiB
Dart

import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:halkyon_app/halkyon_app.dart';
import 'package:halkyon_base_client/halkyon_base_client.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
/// [refusal] as an instance sends it: named by its module.
String sent(SerializableException refusal) => jsonEncode({
'className':
'halkyon_base.${switch (refusal) {
EmailSignInRefusedException() => 'EmailSignInRefusedException',
GitHubSignInRefusedException() => 'GitHubSignInRefusedException',
_ => throw ArgumentError.value(refusal),
}}',
'data': (refusal as SerializableModel).toJson(),
});
void main() {
test("reads the refusals an instance sends under halkyon_base's name, as "
"an app's own generated protocol does", () {
final protocol = BaseProtocol();
expect(
protocol.decodeWithType(
sent(
EmailSignInRefusedException(reason: EmailSignInRefusal.wrongCode),
),
),
isA<EmailSignInRefusedException>().having(
(refusal) => refusal.reason,
'reason',
EmailSignInRefusal.wrongCode,
),
);
expect(
protocol.decodeWithType(
sent(
GitHubSignInRefusedException(
reason: GitHubSignInRefusal.unknownAccount,
),
),
),
isA<GitHubSignInRefusedException>().having(
(refusal) => refusal.reason,
'reason',
GitHubSignInRefusal.unknownAccount,
),
);
});
test("calls halkyon_base's endpoints at the instance, and passes its "
'refusals on as themselves', () async {
final asked = <String>[];
final client = BaseClient(
'https://learn.example.org/',
httpClientOverride: MockClient((request) async {
asked.add(request.url.toString());
return switch (request.url.path) {
'/halkyon_base.instance/basics' => http.Response(
jsonEncode(
InstanceBasics(
app: 'learn',
emailSignIn: true,
sourceCodeUrl: 'https://code.example.org/learn',
minimumAppBuild: 21,
dataLocation: 'EU',
).toJson(),
),
200,
),
'/halkyon_base.emailSignIn/signIn' => http.Response(
sent(
EmailSignInRefusedException(
reason: EmailSignInRefusal.codeExpired,
),
),
400,
),
_ => http.Response('', 404),
};
}),
);
addTearDown(client.close);
final basics = await client.modules.instance.basics();
expect(basics.emailSignIn, isTrue);
expect(basics.minimumAppBuild, 21);
expect(basics.dataLocation, 'EU');
await expectLater(
client.modules.emailSignIn.signIn(switchToken: 'token'),
throwsA(
isA<EmailSignInRefusedException>().having(
(refusal) => refusal.reason,
'reason',
EmailSignInRefusal.codeExpired,
),
),
);
expect(asked, [
'https://learn.example.org/halkyon_base.instance/basics',
'https://learn.example.org/halkyon_base.emailSignIn/signIn',
]);
});
}