feat(app): add the notification capability behind a platform seam
An abstract NotificationBridge with a dart:js_interop web implementation and an honest unsupported stub; a NotificationController that reports only the platform's permission word, asks only from an explicit gesture, never re-prompts a denied answer, and notifies once per run+kind+finished marker under its own persisted ledger key. One reusable native NotificationPermissionTile for Settings, an isolated browser probe, and focused tests. No shell/settings wiring.
This commit is contained in:
@@ -0,0 +1,403 @@
|
||||
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 {
|
||||
final attention = attentionController();
|
||||
final bridge = FakeNotificationBridge();
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
|
||||
attention.seedRows([
|
||||
runSummary(1, 'failed', diagnostic: 'Stage 3 stopped'),
|
||||
runSummary(2, 'waiting'),
|
||||
]);
|
||||
|
||||
expect(attention.transitions, isEmpty);
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('a live transition notifies once', () {
|
||||
test('the frame\'s own words and identity are used', () 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();
|
||||
expect(notify.canNotify, isTrue);
|
||||
|
||||
attention.seedRows([runSummary(1, 'running', name: 'nightly')]);
|
||||
attention.observeFrame(
|
||||
runFrame(
|
||||
1,
|
||||
'failed',
|
||||
name: 'nightly',
|
||||
title: 'Nightly audit',
|
||||
diagnostic: 'Stage 3 stopped',
|
||||
finishedAt: '2026-10-22T11:00:00Z',
|
||||
),
|
||||
);
|
||||
|
||||
expect(notify.publishTransitions(attention.transitions), 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.runId, 1);
|
||||
});
|
||||
|
||||
test('a duplicate frame and a republished list stay silent', () 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')]);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(notify.publishTransitions(attention.transitions), 1);
|
||||
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(attention.transitions, hasLength(1));
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, hasLength(1));
|
||||
});
|
||||
|
||||
test('a reconnect reseed cannot replay the notification', () 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')]);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(notify.publishTransitions(attention.transitions), 1);
|
||||
|
||||
attention.resetForReconnect();
|
||||
attention.seedRows([runSummary(1, 'failed')], reconnect: true);
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, hasLength(1));
|
||||
});
|
||||
|
||||
test('a rebuilt controller with the same store stays silent', () async {
|
||||
final store = MemoryNotificationStore();
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final attention = attentionController();
|
||||
final notify = NotificationController(store: store, bridge: bridge);
|
||||
addTearDown(attention.dispose);
|
||||
addTearDown(notify.dispose);
|
||||
await attention.load();
|
||||
await notify.load();
|
||||
|
||||
attention.seedRows([runSummary(1, 'running')]);
|
||||
attention.observeFrame(runFrame(1, 'failed'));
|
||||
expect(notify.publishTransitions(attention.transitions), 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(bridge.shown, hasLength(1));
|
||||
});
|
||||
|
||||
test('a genuinely new failure of the same run notifies again', () 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')]);
|
||||
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.observeFrame(runFrame(1, 'failed'));
|
||||
|
||||
expect(attention.transitions, isEmpty);
|
||||
expect(notify.publishTransitions(attention.transitions), 0);
|
||||
expect(bridge.shown, isEmpty);
|
||||
});
|
||||
|
||||
test('the publish guard records but does not show a dismissed key', () async {
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
||||
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');
|
||||
}
|
||||
Reference in New Issue
Block a user