import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/changes/domain/change_capture.dart'; import 'package:pipelines_app/features/changes/domain/change_kind.dart'; import 'package:pipelines_app/features/changes/domain/change_wording.dart'; import 'package:pipelines_app/features/changes/domain/file_change.dart'; import 'package:pipelines_app/features/changes/domain/run_changes.dart'; import 'fixtures.dart'; // Thin map-override wrappers over the shared server-shaped fixtures; the // canonical field shapes live in fixtures.dart. JsonMap file(JsonMap overrides) => { ...fileChangeFixture(path: 'a.txt'), ...overrides, }; JsonMap step(JsonMap overrides) => {...stepFixture(stage: 's'), ...overrides}; JsonMap capture({ String status = 'complete', List files = const [], int skipped = 0, int omitted = 0, String diagnostic = '', String scope = 'Git tracked and nonignored files in the run directory', }) => captureFixture( status: status, files: files, skipped: skipped, omitted: omitted, diagnostic: diagnostic, scope: scope, ); void main() { test( 'a null capture is pending while running/waiting, otherwise missing', () { final running = runChanges([ step({'status': 'running'}), step({'status': 'waiting'}), ]); expect(running.pending, isTrue); expect(running.missingCaptures, 0); expect(running.stepsWithChanges, 0); final missing = runChanges([ step({'status': 'completed'}), step({'status': 'failed'}), ]); expect(missing.pending, isFalse); expect(missing.missingCaptures, 2); expect(missing.stepsWithChanges, 0); }, ); test('a non-null malformed capture is unavailable, not a no-change', () { final changes = runChanges([ step({'file_changes': 'not-an-object'}), ]); expect(changes.stepsWithChanges, 1); expect(changes.missingCaptures, 0); expect(changes.unavailable, isTrue); expect(changes.files, isEmpty); expect(runChangesHeader(changes), 'Changes · capture unavailable'); expect(runChangesNote(changes), malformedCaptureDetail); }); test('a present capture with no files is a captured no-change', () { final changes = runChanges([ step({'stage': 'a', 'file_changes': capture()}), ]); expect(changes.stepsWithChanges, 1); expect(changes.files, isEmpty); expect(changes.partial, isFalse); expect(runChangesHeader(changes), 'Changes · no changes observed'); expect(noChangeDetail(changes), emptyCaptureDetail); }); test('a later visit wins the kind, including an unrecognised later kind', () { final changes = runChanges([ step({ 'stage': 'a', 'file_changes': capture( files: [ file({'path': 'x', 'kind': 'modified'}), ], ), }), step({ 'stage': 'b', 'file_changes': capture( files: [ file({'path': 'x', 'kind': 'added'}), ], ), }), ]); expect(changes.files.single.kind, ChangeKind.added); expect(changes.files.single.steps, ['a', 'b']); final unknown = runChanges([ step({ 'stage': 'a', 'file_changes': capture( files: [ file({'path': 'x', 'kind': 'added'}), ], ), }), step({ 'stage': 'b', 'file_changes': capture( files: [ file({'path': 'x', 'kind': 'copied'}), ], ), }), ]); expect(unknown.files.single.kind, ChangeKind.other); }); test('three visits report only the last visit facts', () { final changes = runChanges([ step({ 'stage': 'a', 'sequence': 1, 'file_changes': capture( files: [ file({ 'path': 'x', 'kind': 'modified', 'patch': '@@ -1 +1 @@\n-old\n+one', 'before_mode': '0644', 'after_mode': '0644', }), ], ), }), step({ 'stage': 'b', 'sequence': 2, 'file_changes': capture( files: [ file({ 'path': 'x', 'patch': '@@ -1 +1 @@\n-one\n+two', 'before_mode': '0644', 'after_mode': '0644', }), ], ), }), step({ 'stage': 'c', 'sequence': 3, 'file_changes': capture( files: [ file({ 'path': 'x', 'kind': 'added', 'patch': '@@ -1 +2 @@\n-two\n+three\n+four', 'before_mode': '0755', 'after_mode': '0600', }), ], ), }), ]); final entry = changes.files.single; expect(entry.kind, ChangeKind.added); expect(entry.added, 2); expect(entry.removed, 1); expect(entry.patch, '@@ -1 +2 @@\n-two\n+three\n+four'); expect(entry.beforeMode, '0755'); expect(entry.afterMode, '0600'); expect(entry.modeChanged, isTrue); expect(entry.steps, ['a', 'b', 'c']); expect(changeRowMeta(entry), 'changed by a → b → c · mode 0755→0600'); expect(changes.added, 2); expect(changes.removed, 1); }); test('later binary/truncated flags replace earlier rows', () { final changes = runChanges([ step({ 'stage': 'a', 'file_changes': capture( files: [ file({ 'path': 'x', 'patch': '@@ -1 +1 @@\n-old\n+new', 'binary': true, 'truncated': true, 'before_mode': '0644', 'after_mode': '0755', }), ], ), }), step({ 'stage': 'b', 'file_changes': capture( files: [ file({ 'path': 'x', 'patch': '', 'binary': false, 'truncated': false, 'before_mode': '0755', 'after_mode': '0755', }), ], ), }), ]); final entry = changes.files.single; expect(entry.binary, isFalse); expect(entry.truncated, isFalse); expect(entry.patch, isNull); expect(entry.modeChanged, isFalse); expect(entry.added, 0); expect(entry.removed, 0); // A truncated row anywhere in the run still makes the capture partial. expect(changes.partial, isTrue); }); test('paths keep first-seen order while ordering follows sequence', () { final changes = runChanges([ step({ 'stage': 'c', 'sequence': 3, 'file_changes': capture( files: [ file({'path': 'y'}), ], ), }), step({ 'stage': 'a', 'sequence': 1, 'file_changes': capture( files: [ file({'path': 'y'}), ], ), }), step({ 'stage': 'b', 'sequence': 2, 'file_changes': capture( files: [ file({'path': 'x'}), ], ), }), ]); expect(changes.files.map((entry) => entry.path), ['y', 'x']); expect(changes.files.first.kind, ChangeKind.modified); expect(changes.files.first.steps, ['a', 'c']); expect(changeRowMeta(changes.files.first), 'changed by a → c'); }); test( 'stage names are deduped in run order; blank names/paths are handled', () { final changes = runChanges([ step({ 'stage': 'implementer', 'file_changes': capture( files: [ file({'path': 'x'}), ], ), }), step({ 'stage': 'reviewer', 'file_changes': capture( files: [ file({'path': 'x'}), ], ), }), step({ 'stage': 'implementer', 'file_changes': capture( files: [ file({'path': 'x'}), file({'path': ' '}), ], ), }), ]); expect(changes.files.single.steps, ['implementer', 'reviewer']); final blank = runChanges([ step({ 'stage': ' ', 'file_changes': capture( files: [ file({'path': 'x'}), ], ), }), ]); expect(blank.files.single.steps, ['stage']); }, ); test('partial is OR-ed from status, a truncated file or omitted count', () { expect( runChanges([ step({'file_changes': capture(status: 'partial')}), ]).partial, isTrue, ); expect( runChanges([ step({ 'file_changes': capture( files: [ file({'truncated': true}), ], ), }), ]).partial, isTrue, ); expect( runChanges([ step({'file_changes': capture(omitted: 2)}), ]).partial, isTrue, ); }); test('diagnostics from several captures are deduped and joined', () { final changes = runChanges([ step({'file_changes': capture(status: 'partial', diagnostic: 'why one')}), step({'file_changes': capture(status: 'partial', diagnostic: 'why one')}), step({'file_changes': capture(status: 'partial', diagnostic: 'why two')}), ]); expect(changes.diagnostic, 'why one why two'); expect(changes.stepsWithChanges, 3); }); test( 'stageCaptures keeps every visit, including a missing one, in order', () { final visits = stageCaptures([ step({'stage': 'reviewer', 'sequence': 2, 'file_changes': capture()}), step({'stage': 'implementer', 'sequence': 1, 'file_changes': null}), ]); expect(visits.map((visit) => visit.stage), ['implementer', 'reviewer']); expect(visits.first.missing, isTrue); expect(visits.last.capture?.status, 'complete'); }, ); test( 'visit is control.visit when positive, else the position in its stage', () { final visits = stageCaptures([ stepFixture(stage: 'implementer', sequence: 1), stepFixture(stage: 'reviewer', sequence: 2), stepFixture(stage: 'implementer', sequence: 3), stepFixture(stage: 'implementer', sequence: 4, visit: 7), stepFixture(stage: 'reviewer', sequence: 5, visit: 0), ]); expect(visits.map((visit) => '${visit.stage}:${visit.visit}'), [ 'implementer:1', 'reviewer:1', 'implementer:2', 'implementer:7', 'reviewer:2', ]); }, ); test('equal sequences keep their input order for stage positions', () { final visits = stageCaptures([ stepFixture(stage: 's', sequence: 1), stepFixture(stage: 's', sequence: 1), stepFixture(stage: 'other', sequence: 0), ]); expect(visits.map((visit) => '${visit.stage}:${visit.visit}'), [ 'other:1', 's:1', 's:2', ]); }); test('capture parsing keeps scope, counters and diagnostic', () { final parsed = ChangeCapture.fromJson( capture( status: 'partial', skipped: 2, omitted: 1, diagnostic: 'Excluded protected files.', files: [ file({'path': 'x'}), file({'path': ''}), ], ), ); expect(parsed.scope, contains('Git tracked')); expect(parsed.skipped, 2); expect(parsed.omitted, 1); expect(parsed.diagnostic, 'Excluded protected files.'); expect(parsed.files.map((entry) => entry.path), ['x']); expect( fileChangesSummary(parsed), 'partial (1 files), 2 skipped, 1 omitted · Excluded protected files.', ); }); test('showRunChangesSummary is true only for a settled succeeded run', () { expect(showRunChangesSummary('succeeded'), isTrue); expect(showRunChangesSummary(' Succeeded '), isTrue); for (final status in ['failed', 'running', 'waiting', 'canceled', '']) { expect(showRunChangesSummary(status), isFalse, reason: status); } expect(showRunChangesSummary(null), isFalse); }); test('header covers every empty-capture variant', () { expect( runChangesHeader(_run(unavailable: true, files: const [])), 'Changes · capture unavailable', ); expect(runChangesHeader(_run(pending: true)), 'Changes · capture pending'); expect( runChangesHeader(_run(partial: true, stepsWithChanges: 1)), 'Changes · partial capture', ); expect(runChangesHeader(_run()), 'Changes · not recorded for this run'); expect( runChangesHeader(_run(stepsWithChanges: 1, missingCaptures: 1)), 'Changes · capture incomplete', ); expect( runChangesHeader(_run(stepsWithChanges: 2)), 'Changes · no changes observed', ); }); test('note spells partial, missing and unavailable wording', () { expect( runChangesNote(_run(unavailable: true)), 'The workspace snapshot could not be compared; the changes below may be ' 'incomplete.', ); expect( runChangesNote(_run(unavailable: true, diagnostic: 'server says so')), 'server says so', ); expect( runChangesNote( _run( partial: true, skipped: 8, diagnostic: 'Excluded protected files.', missingCaptures: 1, ), ), 'Partial capture: some changes may be missing. (8 files skipped) ' 'Excluded protected files. Missing capture: 1 stage did not record ' 'changes.', ); expect( runChangesNote( _run( partial: true, omitted: 3, missingCaptures: 2, files: [ FileChange(path: 'x', kind: ChangeKind.modified, truncated: true), FileChange(path: 'y', kind: ChangeKind.modified, truncated: true), ], ), ), 'Partial capture: some changes may be missing. (3 omitted, 2 truncated) ' 'Missing capture: 2 stages did not record changes.', ); expect(runChangesNote(_run()), ''); }); test('tags, row meta and unavailable reasons are exact', () { final binary = FileChange( path: 'x', kind: ChangeKind.modified, binary: true, truncated: false, beforeMode: '0644', afterMode: '0755', steps: const ['a'], ); expect(changeEntryTags(binary), ['binary']); expect(changeRowMeta(binary), 'changed by a · binary'); final truncated = FileChange( path: 'x', kind: ChangeKind.modified, truncated: true, steps: const ['a', 'b'], ); expect(changeEntryTags(truncated), ['truncated']); expect(changeRowMeta(truncated), 'changed by a → b · truncated'); final modeOnly = FileChange( path: 'x', kind: ChangeKind.modified, beforeMode: '0644', afterMode: '0755', steps: const ['a'], ); expect(changeEntryTags(modeOnly), ['mode change']); expect( changeRowMeta(modeOnly), 'changed by a · mode change · mode 0644→0755', ); expect( patchUnavailableReason(modeOnly), "Only the file's mode changed (0644→0755); the server recorded no text " 'diff.', ); expect( patchUnavailableReason(truncated), "The server truncated this file's patch at its capture limit; no diff " 'text was recorded.', ); expect( patchUnavailableReason(FileChange(path: 'x', kind: ChangeKind.modified)), 'The server recorded this change without a patch.', ); }); test('changed filenames filter is case-insensitive and preserves order', () { final files = [ FileChange(path: 'lib/Changes_Viewer.dart', kind: ChangeKind.modified), FileChange(path: 'docs/notes.md', kind: ChangeKind.added), FileChange(path: 'lib/file_diff_viewer.dart', kind: ChangeKind.added), ]; expect(filterFileChanges(files, 'VIEWER').map((file) => file.path), [ 'lib/Changes_Viewer.dart', 'lib/file_diff_viewer.dart', ]); expect(filterFileChanges(files, ' '), files); expect(filterFileChanges(files, 'absent'), isEmpty); }); test('a real-shaped partial run aggregates once and keeps the gap visible', () { const diagnostic = 'Excluded protected, symlinked, oversized or unreadable files; this is ' 'not a complete workspace diff.'; final changes = runChanges([ step({ 'stage': 'implementer', 'sequence': 1, 'file_changes': capture( status: 'partial', skipped: 2, diagnostic: diagnostic, files: [ file({ 'path': 'docs/notes.md', 'kind': 'added', 'patch': '@@ -0,0 +1 @@\n+notes', }), file({ 'path': 'internal/server/retry_test.go', 'patch': '@@ -1 +1 @@\n-old\n+new', }), ], ), }), step({ 'stage': 'reviewer', 'sequence': 2, 'file_changes': capture( status: 'partial', skipped: 2, diagnostic: diagnostic, ), }), step({'stage': 'ui_reviewer', 'sequence': 3}), ]); expect(changes.files, hasLength(2)); expect(changes.stepsWithChanges, 2); expect(changes.missingCaptures, 1); expect(changes.partial, isTrue); expect(changes.skipped, 4); expect( runChangesHeader(changes), '2 files changed · +2 -1 · partial capture', ); expect( runChangesNote(changes), 'Partial capture: some changes may be missing. (4 files skipped) ' '$diagnostic Missing capture: 1 stage did not record changes.', ); expect( runChangesHeader(_run(unavailable: true, files: changes.files)), 'Changes · capture unavailable', ); }); } RunChanges _run({ List files = const [], bool partial = false, bool unavailable = false, bool pending = false, int stepsWithChanges = 0, int missingCaptures = 0, int skipped = 0, int omitted = 0, String diagnostic = '', }) => RunChanges( files: files, stepsWithChanges: stepsWithChanges, stepsPending: 0, added: files.fold(0, (sum, file) => sum + file.added), removed: files.fold(0, (sum, file) => sum + file.removed), partial: partial, unavailable: unavailable, pending: pending, missingCaptures: missingCaptures, skipped: skipped, omitted: omitted, diagnostic: diagnostic, );