import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/attention/domain/attention_models.dart'; import 'package:pipelines_app/features/attention/domain/attention_transitions.dart'; Map run([Map overrides = const {}]) => { 'id': 1, 'name': 'Plan and land', 'title': null, 'status': 'failed', 'created_at': '2026-10-19T10:00:00Z', 'finished_at': '2026-10-19T10:05:00Z', 'diagnostic': '', ...overrides, }; void main() { test('a seeded failed/waiting run raises nothing', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 1, 'status': 'failed'}), run({'id': 2, 'status': 'waiting'}), ]); expect(detector.observe(run({'id': 1, 'status': 'failed'})), isNull); expect(detector.observe(run({'id': 2, 'status': 'waiting'})), isNull); expect(detector.emittedKeys, isEmpty); }); test('a first-seen run raises nothing and becomes the baseline', () { final detector = AttentionTransitionDetector(); expect(detector.observe(run({'id': 5, 'status': 'failed'})), isNull); expect(detector.statusOf(5), 'failed'); expect(detector.observe(run({'id': 5, 'status': 'failed'})), isNull); expect(detector.emittedKeys, isEmpty); }); test('entering failed from a known running run emits exactly once', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 5, 'status': 'running'}), ]); final first = detector.observe(run({'id': 5, 'status': 'failed'})); expect(first, isNotNull); expect(first!.runId, 5); expect(first.kind, AttentionKind.failed); expect(first.key, '5:Failed'); expect(first.entry.label, 'Plan and land'); expect(first.entry.detail, 'Run failed'); expect(detector.observe(run({'id': 5, 'status': 'failed'})), isNull); expect(detector.emittedKeys, {'5:Failed'}); }); test('entering a human waiting state emits a Decision', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 6, 'status': 'running'}), ]); final transition = detector.observe( run({'id': 6, 'status': 'waiting', 'title': ' Decide '}), ); expect(transition, isNotNull); expect(transition!.kind, AttentionKind.decision); expect(transition.key, '6:Decision'); expect(transition.entry.label, 'Decide'); expect(transition.entry.detail, 'Waiting for your decision'); }); test('waiting to failed is a new kind and emits once', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 7, 'status': 'waiting'}), ]); final transition = detector.observe(run({'id': 7, 'status': 'failed'})); expect(transition!.key, '7:Failed'); expect(detector.emittedKeys, {'7:Failed'}); }); test('a run that leaves the state and returns may emit again', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 8, 'status': 'running'}), ]); expect(detector.observe(run({'id': 8, 'status': 'failed'})), isNotNull); expect(detector.observe(run({'id': 8, 'status': 'running'})), isNull); expect(detector.emittedKeys, isEmpty); final again = detector.observe(run({'id': 8, 'status': 'failed'})); expect(again, isNotNull); expect(detector.observe(run({'id': 8, 'status': 'failed'})), isNull); }); test('nonqualifying statuses never emit', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 9, 'status': 'queued'}), ]); for (final status in [ 'running', 'succeeded', 'canceled', 'interrupted', 'unknown', ]) { expect( detector.observe(run({'id': 9, 'status': status})), isNull, reason: 'status $status must not emit', ); } expect(detector.emittedKeys, isEmpty); }); test( 'an export wait never emits a Decision even though status is waiting', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 10, 'status': 'running'}), ]); expect( detector.observe( run({'id': 10, 'status': 'waiting', 'export_pending_sequence': 2}), ), isNull, ); expect(detector.emittedKeys, isEmpty); }, ); test('dismissed identities are marked seen but do not emit', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 11, 'status': 'running'}), ]); expect( detector.observe( run({'id': 11, 'status': 'failed'}), dismissed: {'11:Failed'}, ), isNull, ); expect(detector.emittedKeys, {'11:Failed'}); expect(detector.observe(run({'id': 11, 'status': 'failed'})), isNull); }); test('a reconnect reseed produces no transition and no replay', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 12, 'status': 'failed'}), ]); detector.resetForReconnect(); detector.reseed([ run({'id': 12, 'status': 'failed'}), ]); expect(detector.observe(run({'id': 12, 'status': 'failed'})), isNull); expect(detector.emittedKeys, isEmpty); }); test('a reseed keeps still-qualifying keys and prunes the rest', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 20, 'status': 'running'}), run({'id': 21, 'status': 'running'}), ]); expect(detector.observe(run({'id': 20, 'status': 'failed'})), isNotNull); expect(detector.observe(run({'id': 21, 'status': 'waiting'})), isNotNull); expect(detector.emittedKeys, {'20:Failed', '21:Decision'}); detector.reseed([ run({'id': 20, 'status': 'failed'}), run({'id': 21, 'status': 'succeeded'}), ]); // 20 still qualifies, so its key survives; 21 no longer asks, so its key is // pruned and a genuine later transition can be raised once more. expect(detector.emittedKeys, {'20:Failed'}); expect(detector.observe(run({'id': 20, 'status': 'failed'})), isNull); expect(detector.observe(run({'id': 21, 'status': 'waiting'})), isNotNull); expect(detector.observe(run({'id': 21, 'status': 'waiting'})), isNull); }); test('a reseed keeps the key of a run absent from the read', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 22, 'status': 'running'}), ]); expect(detector.observe(run({'id': 22, 'status': 'failed'})), isNotNull); detector.reseed([ run({'id': 23, 'status': 'running'}), ]); expect(detector.emittedKeys, {'22:Failed'}); // Absence says nothing, so seeing the same state again is not a transition. expect(detector.observe(run({'id': 22, 'status': 'failed'})), isNull); }); test('a snapshot bracketed as a seed does not emit or replay', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 13, 'status': 'running'}), ]) ..beginSeed(); expect(detector.observe(run({'id': 13, 'status': 'waiting'})), isNull); detector.endSeed(); expect(detector.observe(run({'id': 13, 'status': 'waiting'})), isNull); expect(detector.emittedKeys, isEmpty); }); test('the transition keeps the whole frame, including unknown fields', () { final detector = AttentionTransitionDetector() ..seed([ run({'id': 14, 'status': 'running'}), ]); final frame = run({ 'id': 14, 'run_id': 14, 'status': 'failed', 'diagnostic': ' boom ', 'unknown_future_field': ['keep'], }); final transition = detector.observe(frame); expect(transition!.entry.detail, 'boom'); expect(transition.run, same(frame)); expect(transition.run['unknown_future_field'], ['keep']); }); }