feat(app): notification capability with transition dedupe

This commit is contained in:
2026-10-22 02:05:20 +01:00
parent 7718604268
commit b8e5c3e7e3
13 changed files with 871 additions and 393 deletions
@@ -1,16 +1,9 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
import 'package:pipelines_app/features/attention/notify/notification_bridge.dart';
import 'package:pipelines_app/features/attention/notify/notification_controller.dart';
import 'package:pipelines_app/features/attention/notify/notification_ledger.dart';
import 'package:pipelines_app/features/attention/notify/notification_models.dart';
import 'package:pipelines_app/features/attention/notify/notification_store.dart';
import 'notification_test_support.dart';
/// Let the controller's fire-and-forget ledger write land.
Future<void> settle() => Future<void>.delayed(Duration.zero);
void main() {
group('the seed never notifies', () {
test('an already failed and waiting list is silent', () async {
@@ -31,7 +24,7 @@ void main() {
]);
expect(attention.transitions, isEmpty);
expect(notify.publishTransitions(attention.transitions), 0);
expect(publish(notify, attention), 0);
expect(bridge.shown, isEmpty);
});
});
@@ -62,11 +55,11 @@ void main() {
),
);
expect(notify.publishTransitions(attention.transitions), 1);
expect(publish(notify, attention), 1);
final message = bridge.shown.single;
expect(message.title, 'A run failed');
expect(message.body, 'Nightly audit \u00b7 nightly');
expect(message.tag, '1:Failed@2026-10-22T11:00:00Z');
expect(message.tag, '1:Failed');
expect(message.runId, 1);
});
@@ -84,11 +77,11 @@ void main() {
attention.seedRows([runSummary(1, 'running')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(notify.publishTransitions(attention.transitions), 1);
expect(publish(notify, attention), 1);
attention.observeFrame(runFrame(1, 'failed'));
expect(attention.transitions, hasLength(1));
expect(notify.publishTransitions(attention.transitions), 0);
expect(publish(notify, attention), 0);
expect(bridge.shown, hasLength(1));
});
@@ -106,13 +99,13 @@ void main() {
attention.seedRows([runSummary(1, 'running')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(notify.publishTransitions(attention.transitions), 1);
expect(publish(notify, attention), 1);
attention.resetForReconnect();
attention.seedRows([runSummary(1, 'failed')], reconnect: true);
expect(notify.publishTransitions(attention.transitions), 0);
expect(publish(notify, attention), 0);
attention.observeFrame(runFrame(1, 'failed'));
expect(notify.publishTransitions(attention.transitions), 0);
expect(publish(notify, attention), 0);
expect(bridge.shown, hasLength(1));
});
@@ -128,18 +121,18 @@ void main() {
attention.seedRows([runSummary(1, 'running')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(notify.publishTransitions(attention.transitions), 1);
expect(publish(notify, attention), 1);
await settle();
final restarted = NotificationController(store: store, bridge: bridge);
addTearDown(restarted.dispose);
await restarted.load();
expect(restarted.notified, contains('1:Failed@${at(const Duration(minutes: 1))}'));
expect(restarted.publishTransitions(attention.transitions), 0);
expect(restarted.notified, contains('1:Failed'));
expect(publish(restarted, attention), 0);
expect(bridge.shown, hasLength(1));
});
test('a genuinely new failure of the same run notifies again', () async {
test('a later failure of another run notifies independently', () async {
final attention = attentionController();
final bridge = FakeNotificationBridge(permission: 'granted');
final notify = NotificationController(
@@ -151,253 +144,38 @@ void main() {
await attention.load();
await notify.load();
attention.seedRows([runSummary(1, 'running')]);
attention.observeFrame(
runFrame(1, 'failed', finishedAt: '2026-10-22T09:10:00Z'),
);
expect(notify.publishTransitions(attention.transitions), 1);
attention.observeFrame(runFrame(1, 'running'));
attention.observeFrame(
runFrame(1, 'failed', finishedAt: '2026-10-22T11:30:00Z'),
);
expect(notify.publishTransitions(attention.transitions), 1);
expect(bridge.shown, hasLength(2));
expect(bridge.shown.map((m) => m.tag).toSet(), hasLength(2));
});
});
group('a dismissed entry never notifies', () {
test('the detector suppresses it and nothing is posted', () async {
final attention = attentionController();
final bridge = FakeNotificationBridge(permission: 'granted');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(attention.dispose);
addTearDown(notify.dispose);
await attention.load();
await notify.load();
attention.seedRows([runSummary(1, 'running')]);
await attention.dismiss(attentionEntryFor(runSummary(1, 'failed'))!);
attention.seedRows([runSummary(1, 'running'), runSummary(2, 'running')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(publish(notify, attention), 1);
expect(attention.transitions, isEmpty);
expect(notify.publishTransitions(attention.transitions), 0);
expect(bridge.shown, isEmpty);
attention.observeFrame(runFrame(2, 'failed'));
expect(publish(notify, attention), 1);
expect(bridge.shown, hasLength(2));
expect(bridge.shown.map((m) => m.tag), ['1:Failed', '2:Failed']);
});
test('the publish guard records but does not show a dismissed key', () async {
test('a transition for a run that left is never re-posted', () async {
final attention = attentionController();
final bridge = FakeNotificationBridge(permission: 'granted');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(attention.dispose);
addTearDown(notify.dispose);
await attention.load();
await notify.load();
final transition = transitionFor(4);
expect(
notify.publishTransitions([transition], dismissed: {transition.key}),
0,
);
expect(notify.notified, contains(notificationIdentity(transition)));
expect(bridge.shown, isEmpty);
});
});
attention.seedRows([runSummary(1, 'running')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(publish(notify, attention), 1);
group('permission is only ever the platform\'s answer', () {
test('an unsupported platform is never asked and never posts', () async {
final bridge = FakeNotificationBridge(supported: false);
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.unsupported);
expect(
await notify.requestPermission(),
NotificationPermissionState.unsupported,
);
expect(bridge.requests, 0);
expect(notify.publishTransitions([transitionFor(1)]), 0);
expect(bridge.shown, isEmpty);
});
test('an insecure page reports needsSecureContext', () async {
final bridge = FakeNotificationBridge(secureContext: false);
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.needsSecureContext);
await notify.requestPermission();
expect(bridge.requests, 0);
});
test('a default page is asked once and follows the real answer', () async {
final store = MemoryNotificationStore();
final bridge = FakeNotificationBridge(answer: 'granted');
final notify = NotificationController(store: store, bridge: bridge);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.notRequested);
expect(notify.canNotify, isFalse);
final state = await notify.requestPermission();
expect(bridge.requests, 1);
expect(state, NotificationPermissionState.granted);
expect(notify.canNotify, isTrue);
expect(notify.asked, isTrue);
await settle();
expect(store.stored, contains('"asked":true'));
});
test('a denied answer is never re-prompted', () async {
final bridge = FakeNotificationBridge(permission: 'denied');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.denied);
expect(
await notify.requestPermission(),
NotificationPermissionState.denied,
);
expect(bridge.requests, 0);
expect(notify.publishTransitions([transitionFor(1)]), 0);
expect(bridge.shown, isEmpty);
});
test('a prompt that grants nothing keeps the honest default wording', () async {
final bridge = FakeNotificationBridge(answer: 'default');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
final state = await notify.requestPermission();
expect(state, NotificationPermissionState.notRequested);
expect(notify.asked, isTrue);
expect(notify.canNotify, isFalse);
});
});
group('batching and the tap seam', () {
test('a burst posts a bounded batch in order, then the rest', () async {
final bridge = FakeNotificationBridge(permission: 'granted');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
final transitions = [for (var id = 1; id <= 7; id++) transitionFor(id)];
expect(notify.publishTransitions(transitions), 5);
expect(bridge.shown.map((m) => m.runId), [1, 2, 3, 4, 5]);
expect(notify.publishTransitions(transitions), 2);
expect(bridge.shown.map((m) => m.runId), [1, 2, 3, 4, 5, 6, 7]);
});
test('a tap reports the run and never navigates here', () async {
final bridge = FakeNotificationBridge(permission: 'granted');
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
final opened = <int>[];
notify.onOpenRun = opened.add;
expect(notify.publishTransitions([transitionFor(3)]), 1);
bridge.taps.single!.call();
expect(opened, [3]);
});
});
group('an unavailable store cannot reach the UI', () {
test('a failed read and write still notify and never throw', () async {
final store = MemoryNotificationStore()
..failReads = true
..failWrites = true;
final bridge = FakeNotificationBridge(permission: 'granted');
final notify = NotificationController(store: store, bridge: bridge);
addTearDown(notify.dispose);
await expectLater(notify.load(), completes);
expect(notify.loaded, isTrue);
expect(notify.notified, isEmpty);
expect(notify.publishTransitions([transitionFor(1)]), 1);
await settle();
// Publishing the leaving frame forgets the identity; republishing the
// accumulated list afterwards must not resurrect the old alert.
attention.observeFrame(runFrame(1, 'running'));
expect(publish(notify, attention), 0);
expect(publish(notify, attention), 0);
expect(bridge.shown, hasLength(1));
});
test('a throwing platform post is reported, not propagated', () async {
final bridge = _ThrowingBridge();
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(notify.publishTransitions([transitionFor(1)]), 0);
expect(notify.notified, contains('1:Failed@${at(const Duration(minutes: 1))}'));
});
});
group('the platform seam', () {
test('the non-web bridge reports unsupported honestly', () async {
final bridge = createNotificationBridge();
expect(bridge.environment().supported, isFalse);
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
addTearDown(notify.dispose);
await notify.load();
expect(notify.permission, NotificationPermissionState.unsupported);
expect(
bridge.show(
const NotificationMessage(
runId: 1,
title: 'A run failed',
body: 'release',
tag: '1:Failed@x',
),
),
isFalse,
);
});
});
}
class _ThrowingBridge implements NotificationBridge {
@override
NotificationEnvironment environment() => const NotificationEnvironment(
supported: true,
secureContext: true,
permission: 'granted',
);
@override
Future<String> requestPermission() async => 'granted';
@override
bool show(NotificationMessage message, {void Function()? onTap}) =>
throw StateError('the browser refused to construct a notification');
}