import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/runs/domain/run_events.dart'; void main() { group('event fold', () { test('terminal status and resource wait reset are monotonic', () { final initial = [ { 'id': 1, 'status': 'succeeded', 'executing': false, 'completed_steps': 4, }, ]; final reset = foldRunEvent(initial, { 'run_id': 1, 'status': 'queued', 'resource_wait_set': true, 'resource_wait': null, }); expect(reset.runs.single['status'], 'succeeded'); expect(reset.runs.single['executing'], false); final competing = foldRunEvent(initial, { 'run_id': 1, 'status': 'failed', 'resource_wait_set': true, }); expect(competing.runs.single['status'], 'succeeded'); expect(competing.runs.single['resource_wait'], isNull); }); test( 'resource wait reset advances only nonterminal rows, even on stale status', () { final rows = [ { 'id': 2, 'status': 'running', 'executing': true, 'resource_wait': {'resource': 'gpu'}, 'completed_steps': 3, }, ]; final result = foldRunEvent(rows, { 'run_id': 2, 'status': 'queued', 'resource_wait_set': true, 'resource_wait': null, 'completed_steps': 1, }); expect(result.runs.single['status'], 'queued'); expect(result.runs.single['resource_wait'], isNull); expect(result.runs.single['completed_steps'], 3); }, ); test( 'new waiting and terminal rows are not executing; identity-less frame resyncs', () { for (final status in ['waiting', 'succeeded']) { final result = foldRunEvent([], { 'run_id': 8, 'pipeline_id': 4, 'name': 'Build', 'status': status, }); expect(result.runs.single['executing'], false); } expect( foldRunEvent([], {'run_id': 9, 'status': 'running'}).needsResync, isTrue, ); }, ); test( 'blank sparse step fields preserve graph evidence and terminal visit', () { final detail = { 'id': 1, 'status': 'running', 'steps': [ { 'sequence': 2, 'stage': 'review', 'status': 'completed', 'outcome': 'approve', 'target': 'publish', 'diagnostic': 'kept', }, ], }; final next = foldRunEventDetail(detail, { 'run_id': 1, 'status': 'running', 'step': { 'sequence': 2, 'stage': '', 'status': 'running', 'outcome': ' ', 'target': '', 'diagnostic': '', }, })!; final step = (next['steps'] as List).single as JsonMap; expect(step, containsPair('stage', 'review')); expect(step, containsPair('status', 'completed')); expect(step, containsPair('outcome', 'approve')); expect(step, containsPair('target', 'publish')); expect(step, containsPair('diagnostic', 'kept')); }, ); test( 'cancel and cleanup flags stick; resource wait can clear; terminal clears it', () { final rows = [ { 'id': 3, 'status': 'running', 'resource_wait': {'resource': 'gpu'}, }, ]; final first = foldRunEvent(rows, { 'run_id': 3, 'status': 'running', 'cancel_requested': true, 'cleanup_unconfirmed': true, }); final second = foldRunEvent(first.runs, { 'run_id': 3, 'status': 'running', 'cancel_requested': false, 'cleanup_unconfirmed': false, 'resource_wait_set': true, 'resource_wait': null, }); expect(second.runs.single['cancel_requested'], true); expect(second.runs.single['cleanup_unconfirmed'], true); expect(second.runs.single['resource_wait'], isNull); final terminal = foldRunEvent(second.runs, { 'run_id': 3, 'status': 'failed', }); expect(terminal.runs.single['resource_wait'], isNull); }, ); test( 'full sync triggers and list/detail folds agree across stale and current frames', () { expect(runDetailNeedsFullSync({'status': 'waiting'}), true); expect( runDetailNeedsFullSync({ 'status': 'running', 'step': {'status': 'completed'}, }), true, ); final summary = [ {'id': 1, 'status': 'running', 'completed_steps': 1}, ]; final frame = { 'run_id': 1, 'status': 'succeeded', 'completed_steps': 2, 'outcome': 'done', }; final list = foldRunEvent(summary, frame).runs.single; final detail = foldRunEventDetail({ 'id': 1, 'status': 'running', 'completed_steps': 1, 'steps': [], }, frame)!; expect(list['status'], detail['status']); expect(list['completed_steps'], detail['completed_steps']); final old = foldRunEventDetail(detail, { 'run_id': 1, 'status': 'running', 'outcome': '', })!; expect(old['status'], 'succeeded'); expect(old['outcome'], 'done'); }, ); }); }