Files
pipelines/app/test/features/attention/notify/notification_controller_test.dart
T

182 lines
6.4 KiB
Dart

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';
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(publish(notify, attention), 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(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');
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(publish(notify, attention), 1);
attention.observeFrame(runFrame(1, 'failed'));
expect(attention.transitions, hasLength(1));
expect(publish(notify, attention), 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(publish(notify, attention), 1);
attention.resetForReconnect();
attention.seedRows([runSummary(1, 'failed')], reconnect: true);
expect(publish(notify, attention), 0);
attention.observeFrame(runFrame(1, 'failed'));
expect(publish(notify, attention), 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(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'));
expect(publish(restarted, attention), 0);
expect(bridge.shown, hasLength(1));
});
test('a later failure of another run notifies independently', () 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'), runSummary(2, 'running')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(publish(notify, attention), 1);
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('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();
attention.seedRows([runSummary(1, 'running')]);
attention.observeFrame(runFrame(1, 'failed'));
expect(publish(notify, attention), 1);
// 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));
});
});
}