feat(app): notification capability with transition dedupe
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
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_models.dart';
|
||||
import 'package:pipelines_app/features/attention/notify/notification_store.dart';
|
||||
|
||||
import 'notification_test_support.dart';
|
||||
|
||||
void main() {
|
||||
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(publish(notify, attention), 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],
|
||||
qualifying: {transition.key},
|
||||
dismissed: {transition.key},
|
||||
),
|
||||
0,
|
||||
);
|
||||
expect(notify.notified, contains(transition.key));
|
||||
expect(bridge.shown, isEmpty);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
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)];
|
||||
final qualifying = {for (final t in transitions) t.key};
|
||||
expect(notify.publishTransitions(transitions, qualifying: qualifying), 5);
|
||||
expect(bridge.shown.map((m) => m.runId), [1, 2, 3, 4, 5]);
|
||||
expect(notify.publishTransitions(transitions, qualifying: qualifying), 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)], qualifying: {'3:Failed'}),
|
||||
1,
|
||||
);
|
||||
bridge.taps.single!.call();
|
||||
expect(opened, [3]);
|
||||
});
|
||||
});
|
||||
|
||||
group('the ledger cannot be read into by a caller', () {
|
||||
test('notified is an unmodifiable copy', () async {
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(
|
||||
store: MemoryNotificationStore(),
|
||||
bridge: bridge,
|
||||
);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
|
||||
expect(
|
||||
notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'}),
|
||||
1,
|
||||
);
|
||||
expect(notify.notified, {'1:Failed'});
|
||||
expect(() => notify.notified.add('9:Failed'), throwsUnsupportedError);
|
||||
expect(notify.notified, {'1:Failed'});
|
||||
});
|
||||
});
|
||||
|
||||
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)], qualifying: {'1:Failed'}),
|
||||
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)], qualifying: {'1:Failed'}),
|
||||
0,
|
||||
);
|
||||
expect(notify.notified, contains('1:Failed'));
|
||||
});
|
||||
|
||||
test('ledger writes land in call order, one at a time', () async {
|
||||
final store = _SlowStore();
|
||||
final bridge = FakeNotificationBridge(permission: 'granted');
|
||||
final notify = NotificationController(store: store, bridge: bridge);
|
||||
addTearDown(notify.dispose);
|
||||
await notify.load();
|
||||
|
||||
notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'});
|
||||
notify.publishTransitions(
|
||||
[transitionFor(2)],
|
||||
qualifying: {'1:Failed', '2:Failed'},
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 40));
|
||||
|
||||
expect(store.maxActive, 1, reason: 'the writes must be serialized');
|
||||
expect(store.stored, contains('"1:Failed"'));
|
||||
expect(store.stored, contains('"2:Failed"'));
|
||||
expect(store.writes.last, store.stored);
|
||||
});
|
||||
});
|
||||
|
||||
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',
|
||||
),
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// A store whose writes take a tick, so a racing implementation would overlap
|
||||
/// them and the last value to land would be the older snapshot.
|
||||
class _SlowStore implements NotificationStore {
|
||||
String? stored;
|
||||
int active = 0;
|
||||
int maxActive = 0;
|
||||
final List<String> writes = [];
|
||||
|
||||
@override
|
||||
Future<String?> read() async => stored;
|
||||
|
||||
@override
|
||||
Future<bool> write(String value) async {
|
||||
active++;
|
||||
if (active > maxActive) maxActive = active;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 2));
|
||||
stored = value;
|
||||
writes.add(value);
|
||||
active--;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
@@ -4,21 +4,17 @@ import 'package:pipelines_app/features/attention/notify/notification_ledger.dart
|
||||
import 'notification_test_support.dart';
|
||||
|
||||
void main() {
|
||||
group('the identity carries the finished marker', () {
|
||||
test('a recorded finish time distinguishes two failures of one run', () {
|
||||
group('the identity is the run and kind', () {
|
||||
test('two failures of one run are the same announced state', () {
|
||||
final first = transitionFor(7, finishedAt: '2026-10-22T09:10:00Z');
|
||||
final second = transitionFor(7, finishedAt: '2026-10-22T11:30:00Z');
|
||||
expect(notificationIdentity(first), '7:Failed@2026-10-22T09:10:00Z');
|
||||
expect(notificationIdentity(second), '7:Failed@2026-10-22T11:30:00Z');
|
||||
expect(notificationIdentity(first), isNot(notificationIdentity(second)));
|
||||
expect(notificationIdentity(first), '7:Failed');
|
||||
expect(notificationIdentity(second), '7:Failed');
|
||||
});
|
||||
|
||||
test('a run without a finish time falls back to its creation time', () {
|
||||
test('a waiting run needs no finish time to have an identity', () {
|
||||
final entry = transitionFor(7, status: 'waiting', finishedAt: '');
|
||||
expect(
|
||||
notificationIdentity(entry),
|
||||
'7:Decision@${attentionNow.subtract(const Duration(hours: 1)).toIso8601String()}',
|
||||
);
|
||||
expect(notificationIdentity(entry), '7:Decision');
|
||||
});
|
||||
|
||||
test('the kind separates a decision from a failure of one run', () {
|
||||
@@ -29,31 +25,88 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('the qualifying set is the attention surface\'s own keys', () {
|
||||
test('only failed and human-waiting runs qualify', () {
|
||||
expect(
|
||||
notificationQualifyingKeys([
|
||||
runSummary(1, 'failed'),
|
||||
runSummary(2, 'waiting'),
|
||||
runSummary(3, 'running'),
|
||||
runSummary(4, 'succeeded'),
|
||||
runSummary(5, 'queued'),
|
||||
runSummary(6, 'waiting', exportPending: 2),
|
||||
]),
|
||||
{'1:Failed', '2:Decision'},
|
||||
);
|
||||
});
|
||||
|
||||
test('a duplicate row and a malformed id add nothing', () {
|
||||
expect(
|
||||
notificationQualifyingKeys([
|
||||
runSummary(1, 'failed'),
|
||||
runSummary(1, 'failed'),
|
||||
{'id': 0, 'status': 'failed'},
|
||||
{'id': 'not a run', 'status': 'failed'},
|
||||
]),
|
||||
{'1:Failed'},
|
||||
);
|
||||
expect(notificationQualifyingKeys(null), isEmpty);
|
||||
});
|
||||
|
||||
test('the frame key form is read too', () {
|
||||
expect(notificationQualifyingKeys([runFrame(9, 'failed')]), {'9:Failed'});
|
||||
});
|
||||
});
|
||||
|
||||
group('the ledger records identities', () {
|
||||
test('recording is idempotent and never mutates the original', () {
|
||||
const base = NotificationLedger();
|
||||
final once = base.recording('7:Failed@a');
|
||||
final twice = once.recording('7:Failed@a');
|
||||
final once = base.recording('7:Failed');
|
||||
final twice = once.recording('7:Failed');
|
||||
expect(base.notified, isEmpty);
|
||||
expect(once.hasNotified('7:Failed@a'), isTrue);
|
||||
expect(twice.notified, {'7:Failed@a'});
|
||||
expect(once.hasNotified('7:Failed'), isTrue);
|
||||
expect(twice.notified, {'7:Failed'});
|
||||
});
|
||||
|
||||
test('asked is carried without touching the identities', () {
|
||||
final asked = const NotificationLedger(
|
||||
notified: {'a'},
|
||||
).withAsked(true);
|
||||
final asked = const NotificationLedger(notified: {'a'}).withAsked(true);
|
||||
expect(asked.asked, isTrue);
|
||||
expect(asked.notified, {'a'});
|
||||
});
|
||||
});
|
||||
|
||||
group('pruning keeps only the current qualifying run', () {
|
||||
test('a run that left the state is forgotten; asked survives', () {
|
||||
final ledger = const NotificationLedger(
|
||||
notified: {'7:Decision', '8:Failed'},
|
||||
).withAsked(true);
|
||||
final pruned = ledger.pruning({'8:Failed'});
|
||||
expect(pruned.notified, {'8:Failed'});
|
||||
expect(pruned.asked, isTrue);
|
||||
});
|
||||
|
||||
test('nothing to drop returns the same ledger', () {
|
||||
const ledger = NotificationLedger(notified: {'7:Failed'});
|
||||
expect(identical(ledger.pruning({'7:Failed'}), ledger), isTrue);
|
||||
});
|
||||
|
||||
test('a timestamped identity from an older build is dropped', () {
|
||||
final old = decodeNotificationLedger(
|
||||
'{"asked":false,"notified":["7:Decision@2026-10-22T11:00:00.000"]}',
|
||||
);
|
||||
expect(old.notified, hasLength(1));
|
||||
expect(old.pruning({'7:Decision'}).notified, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('persistence is total', () {
|
||||
test('a round trip keeps the asked flag and the identities', () {
|
||||
final ledger = const NotificationLedger(
|
||||
notified: {'b', 'a'},
|
||||
).withAsked(true);
|
||||
final decoded = decodeNotificationLedger(encodeNotificationLedger(ledger));
|
||||
final decoded = decodeNotificationLedger(
|
||||
encodeNotificationLedger(ledger),
|
||||
);
|
||||
expect(decoded.asked, isTrue);
|
||||
expect(decoded.notified, {'a', 'b'});
|
||||
});
|
||||
@@ -75,8 +128,9 @@ void main() {
|
||||
expect(decodeNotificationLedger('[]').notified, isEmpty);
|
||||
expect(decodeNotificationLedger('"text"').notified, isEmpty);
|
||||
expect(
|
||||
decodeNotificationLedger('{"asked":"yes","notified":[1,null,"a",""]}')
|
||||
.notified,
|
||||
decodeNotificationLedger(
|
||||
'{"asked":"yes","notified":[1,null,"a",""]}',
|
||||
).notified,
|
||||
{'a'},
|
||||
);
|
||||
expect(
|
||||
|
||||
@@ -128,7 +128,10 @@ void main() {
|
||||
'created_at': '2026-10-22T09:00:00Z',
|
||||
'finished_at': '2026-10-22T09:10:00Z',
|
||||
})!;
|
||||
expect(notificationAlertBody(entry), 'Nightly audit \u00b7 nightly-audit');
|
||||
expect(
|
||||
notificationAlertBody(entry),
|
||||
'Nightly audit \u00b7 nightly-audit',
|
||||
);
|
||||
});
|
||||
|
||||
test('a blank or equal pipeline is never repeated', () {
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/attention/notify/notification_controller.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';
|
||||
|
||||
void main() {
|
||||
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)], qualifying: {'1:Failed'}),
|
||||
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)], qualifying: {'1:Failed'}),
|
||||
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);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/attention/notify/notification_controller.dart';
|
||||
import 'package:pipelines_app/features/attention/notify/notification_store.dart';
|
||||
|
||||
import 'notification_test_support.dart';
|
||||
|
||||
/// A run that leaves a qualifying state and enters it again is a new event.
|
||||
///
|
||||
/// A waiting run has no `finished_at`, and its creation time never changes, so
|
||||
/// both gates of one run carry exactly the same timestamps. These tests pin the
|
||||
/// behaviour that was silently dropped when the identity included that marker.
|
||||
void main() {
|
||||
test('a second decision gate 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(7, 'running')]);
|
||||
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
|
||||
expect(publish(notify, attention), 1);
|
||||
expect(bridge.shown.single.tag, '7:Decision');
|
||||
|
||||
attention.observeFrame(runFrame(7, 'running'));
|
||||
expect(publish(notify, attention), 0);
|
||||
|
||||
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
|
||||
expect(attention.transitions, hasLength(2));
|
||||
expect(publish(notify, attention), 1);
|
||||
expect(bridge.shown, hasLength(2));
|
||||
// The browser coalesces by run+kind, so both alerts share one tag.
|
||||
expect(bridge.shown.map((m) => m.tag).toSet(), {'7:Decision'});
|
||||
});
|
||||
|
||||
test(
|
||||
'a second decision gate notifies even without the leaving publish',
|
||||
() 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(7, 'running')]);
|
||||
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
|
||||
expect(publish(notify, attention), 1);
|
||||
|
||||
// The caller misses the frame that leaves the state; the detector still
|
||||
// creates a new instance for the next gate, so it must notify again.
|
||||
attention.observeFrame(runFrame(7, 'running'));
|
||||
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
|
||||
expect(publish(notify, attention), 1);
|
||||
expect(bridge.shown, hasLength(2));
|
||||
expect(bridge.shown.map((m) => m.tag).toSet(), {'7:Decision'});
|
||||
},
|
||||
);
|
||||
|
||||
test('a coalesced backlog of two gates notifies once', () 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(7, 'running')]);
|
||||
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
|
||||
attention.observeFrame(runFrame(7, 'running'));
|
||||
attention.observeFrame(runFrame(7, 'waiting', finishedAt: ''));
|
||||
expect(attention.transitions, hasLength(2));
|
||||
|
||||
// Both gates were folded before the caller published; the current state is
|
||||
// what the operator should be told about, once.
|
||||
expect(publish(notify, attention), 1);
|
||||
expect(bridge.shown, hasLength(1));
|
||||
expect(bridge.shown.single.tag, '7:Decision');
|
||||
});
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
|
||||
import 'package:pipelines_app/features/attention/domain/attention_transitions.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/runtime/attention_controller.dart';
|
||||
import 'package:pipelines_app/features/attention/runtime/attention_store.dart';
|
||||
@@ -27,13 +29,19 @@ Map<String, dynamic> runSummary(
|
||||
'status': status,
|
||||
'diagnostic': diagnostic,
|
||||
'created_at': at(age),
|
||||
'finished_at': at(age - const Duration(minutes: 5)),
|
||||
// The server leaves finished_at empty for a waiting run (and for one still in
|
||||
// flight); only a settled run carries a finish time.
|
||||
'finished_at': status == 'waiting' || status == 'running'
|
||||
? ''
|
||||
: at(age - const Duration(minutes: 5)),
|
||||
'export_pending_sequence': ?exportPending,
|
||||
...extra,
|
||||
};
|
||||
|
||||
/// One qualifying pushed frame for a run, as `run_event` carries it. A blank
|
||||
/// [finishedAt] leaves the marker to fall back to the creation time.
|
||||
/// One qualifying pushed frame for a run, as `run_event` carries it.
|
||||
///
|
||||
/// A waiting or running frame has no `finished_at`, matching the server; pass
|
||||
/// [finishedAt] only to describe a settled run.
|
||||
Map<String, dynamic> runFrame(
|
||||
int id,
|
||||
String status, {
|
||||
@@ -49,7 +57,11 @@ Map<String, dynamic> runFrame(
|
||||
'status': status,
|
||||
'diagnostic': diagnostic,
|
||||
'created_at': at(const Duration(hours: 1)),
|
||||
'finished_at': finishedAt ?? at(const Duration(minutes: 1)),
|
||||
'finished_at':
|
||||
finishedAt ??
|
||||
(status == 'waiting' || status == 'running'
|
||||
? ''
|
||||
: at(const Duration(minutes: 1))),
|
||||
};
|
||||
|
||||
/// One transition for a run that just entered [status], for batch tests.
|
||||
@@ -65,6 +77,18 @@ AttentionTransition transitionFor(
|
||||
)!,
|
||||
);
|
||||
|
||||
/// Let the controller's fire-and-forget ledger write land.
|
||||
Future<void> settle() => Future<void>.delayed(Duration.zero);
|
||||
|
||||
/// Publish exactly the way the manager wires it: the accumulated transitions,
|
||||
/// the current rows and the dismissed set from the same attention snapshot.
|
||||
int publish(NotificationController notify, AttentionController attention) =>
|
||||
notify.publishTransitions(
|
||||
attention.transitions,
|
||||
qualifying: notificationQualifyingKeys(attention.rows),
|
||||
dismissed: attention.dismissed,
|
||||
);
|
||||
|
||||
AttentionController attentionController() =>
|
||||
AttentionController(store: MemoryAttentionStore(), now: () => attentionNow);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user