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'); }); }