merge(app): land recorded changes viewer with native-default corrections

This commit is contained in:
2026-10-21 18:40:55 +01:00
22 changed files with 4511 additions and 0 deletions
@@ -0,0 +1,473 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/changes/changes.dart';
import 'fixtures.dart';
import 'widget_hosts.dart';
void main() {
testWidgets(
'a missing capture is an evidence gap, not an observed no-change',
(tester) async {
await tester.pumpWidget(
materialHost(ChangesViewer(steps: [stepFixture(fileChanges: null)])),
);
expect(find.text('Changes · not recorded for this run'), findsOneWidget);
expect(find.text('Capture unavailable'), findsOneWidget);
expect(find.text(missingCaptureDetail), findsOneWidget);
expect(find.text(emptyCaptureDetail), findsNothing);
expect(find.text('No changes observed'), findsNothing);
},
);
testWidgets('a running step with no capture is pending, not unavailable', (
tester,
) async {
await tester.pumpWidget(
materialHost(
ChangesViewer(
steps: [stepFixture(status: 'running', fileChanges: null)],
),
),
);
expect(find.text('Changes · capture pending'), findsOneWidget);
expect(find.text('Capture pending'), findsOneWidget);
expect(find.text(pendingCaptureDetail), findsOneWidget);
expect(find.text('Capture unavailable'), findsNothing);
});
testWidgets('a differently cased running status agrees with the header', (
tester,
) async {
await tester.pumpWidget(
materialHost(
ChangesViewer(
steps: [stepFixture(status: 'Running', fileChanges: null)],
),
),
);
expect(find.text('Changes · capture pending'), findsOneWidget);
expect(find.text('Capture pending'), findsOneWidget);
expect(find.text('Capture unavailable'), findsNothing);
});
testWidgets('an empty complete capture says the workspace was unchanged', (
tester,
) async {
await tester.pumpWidget(
materialHost(
ChangesViewer(steps: [stepFixture(fileChanges: captureFixture())]),
),
);
expect(find.text('Changes · no changes observed'), findsOneWidget);
expect(find.text('No changes observed'), findsOneWidget);
expect(find.text(emptyCaptureDetail), findsOneWidget);
expect(find.text(missingCaptureDetail), findsNothing);
});
testWidgets('an unavailable capture never claims there were no changes', (
tester,
) async {
await tester.pumpWidget(
materialHost(
ChangesViewer(
steps: [
stepFixture(
fileChanges: captureFixture(
status: 'unavailable',
diagnostic:
'Workspace snapshot unavailable; changes could not be '
'compared.',
),
),
],
),
),
);
expect(find.text('Changes · capture unavailable'), findsOneWidget);
expect(find.textContaining('Workspace snapshot unavailable'), findsWidgets);
expect(find.text(emptyCaptureDetail), findsNothing);
});
testWidgets('stage/visit sections keep partial status, counters and scope', (
tester,
) async {
final diagnostic = 'Excluded protected files.';
await tester.pumpWidget(
materialHost(
ChangesViewer(
steps: [
stepFixture(
stage: 'implementer',
fileChanges: captureFixture(
status: 'partial',
skipped: 1,
omitted: 2,
diagnostic: diagnostic,
files: [
fileChangeFixture(path: 'lib/a.dart', patch: multiHunkPatch),
],
),
),
stepFixture(
stage: 'reviewer',
sequence: 2,
status: 'failed',
fileChanges: null,
),
],
),
),
);
expect(find.text('implementer'), findsOneWidget);
expect(find.text('Visit 1 · completed'), findsOneWidget);
// The reviewer's first visit is Visit 1, not the run-wide step ordinal 2.
expect(find.text('Visit 1 · failed'), findsOneWidget);
expect(find.text('Visit 2 · failed'), findsNothing);
expect(
find.text(
'partial (1 files), 1 skipped, 2 omitted · Excluded protected files.',
),
findsOneWidget,
);
expect(
find.text('Scope: Git tracked and nonignored files in the run directory'),
findsOneWidget,
);
expect(find.text('reviewer'), findsOneWidget);
expect(find.text(missingCaptureDetail), findsOneWidget);
expect(find.textContaining('partial capture'), findsWidgets);
});
testWidgets(
'repeated stages number their own visits and honour control.visit',
(tester) async {
// Tall enough that the lazy list builds every section.
tester.view.physicalSize = const Size(800, 1400);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
materialHost(
ChangesViewer(
steps: [
stepFixture(
stage: 'implementer',
sequence: 1,
fileChanges: captureFixture(),
),
stepFixture(
stage: 'reviewer',
sequence: 2,
fileChanges: captureFixture(),
),
stepFixture(
stage: 'implementer',
sequence: 3,
fileChanges: captureFixture(),
),
stepFixture(
stage: 'reviewer',
sequence: 4,
visit: 6,
fileChanges: captureFixture(),
),
],
),
),
);
// Both stages' first recorded visits are Visit 1; the run-wide step
// ordinals (1-4) must not leak into the label.
expect(find.text('Visit 1 · completed'), findsNWidgets(2));
expect(find.text('Visit 2 · completed'), findsOneWidget);
// The resumed reviewer visit records control.visit 6.
expect(find.text('Visit 6 · completed'), findsOneWidget);
expect(find.text('Visit 3 · completed'), findsNothing);
expect(find.text('Visit 4 · completed'), findsNothing);
},
);
testWidgets('filtering changed filenames keeps matching rows and says so', (
tester,
) async {
await tester.pumpWidget(
materialHost(
ChangesViewer(
steps: [
stepFixture(
fileChanges: captureFixture(
files: [
fileChangeFixture(path: 'assets/logo.bin', binary: true),
fileChangeFixture(
path: 'lib/feature.dart',
patch: multiHunkPatch,
),
fileChangeFixture(
path: 'docs/notes.md',
kind: 'added',
patch: '@@ -0,0 +1 @@\n+x',
),
],
),
),
],
),
),
);
expect(find.text('assets/logo.bin'), findsOneWidget);
expect(find.text('lib/feature.dart'), findsOneWidget);
await tester.enterText(find.byType(TextField), 'feature');
await tester.pump();
expect(find.text('lib/feature.dart'), findsOneWidget);
expect(find.text('assets/logo.bin'), findsNothing);
expect(find.text('docs/notes.md'), findsNothing);
expect(find.text('1 of 3 changed paths match'), findsOneWidget);
await tester.enterText(find.byType(TextField), 'nothing-here');
await tester.pump();
expect(
find.text('No changed filenames match "nothing-here".'),
findsOneWidget,
);
await tester.enterText(find.byType(TextField), '');
await tester.pump();
expect(find.text('assets/logo.bin'), findsOneWidget);
expect(find.textContaining('changed files match'), findsNothing);
});
testWidgets(
'opening a file shows the rendered patch and copies exact bytes',
(tester) async {
final calls = <MethodCall>[];
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, (call) async {
if (call.method == 'Clipboard.setData') calls.add(call);
return null;
});
addTearDown(
() => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, null),
);
await tester.pumpWidget(
materialHost(
ChangesViewer(
steps: [
stepFixture(
fileChanges: captureFixture(
files: [
fileChangeFixture(
path: 'lib/feature.dart',
patch: multiHunkPatch,
),
],
),
),
],
),
),
);
await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart')));
await tester.pumpAndSettle();
expect(find.byType(Dialog), findsOneWidget);
expect(find.text('Rendered'), findsOneWidget);
expect(find.text('Raw patch'), findsOneWidget);
final rendered = tester
.widgetList<RichText>(find.byType(RichText))
.map((rich) => rich.text.toPlainText())
.join('\n');
expect(rendered, contains('+ new line'));
expect(rendered, contains('- old line'));
expect(rendered, contains('@@ -20,3 +21,2 @@'));
await tester.tap(find.text('Raw patch'));
await tester.pumpAndSettle();
final raw = tester.widget<Text>(find.text(multiHunkPatch));
expect(raw.data, multiHunkPatch);
expect(raw.softWrap, isFalse);
await tester.tap(find.text('Copy patch'));
await tester.pump();
expect(calls, hasLength(1));
expect(calls.single.arguments, {'text': multiHunkPatch});
expect(find.text('Copied'), findsOneWidget);
await tester.tap(find.byTooltip('Close diff'));
await tester.pumpAndSettle();
expect(find.byType(Dialog), findsNothing);
},
);
testWidgets('binary, truncated and mode-only entries explain the missing diff', (
tester,
) async {
await tester.pumpWidget(
materialHost(
SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: 300,
child: FileDiffViewer(
file: FileChange(
path: 'assets/logo.bin',
kind: ChangeKind.modified,
binary: true,
),
),
),
SizedBox(
height: 300,
child: FileDiffViewer(
file: FileChange(
path: 'logs/build.log',
kind: ChangeKind.modified,
patch: '@@ -1 +1 @@\n-a\n+b',
truncated: true,
),
),
),
SizedBox(
height: 300,
child: FileDiffViewer(
file: FileChange(
path: 'scripts/run.sh',
kind: ChangeKind.modified,
beforeMode: '0644',
afterMode: '0755',
),
),
),
],
),
),
),
);
expect(find.text(binaryDiffMessage), findsOneWidget);
expect(find.text(truncatedPatchNote), findsOneWidget);
expect(
find.text(
"Only the file's mode changed (0644→0755); the server recorded no text "
'diff.',
),
findsOneWidget,
);
final copyButtons = <TextButton>[];
for (var index = 0; index < 3; index++) {
final button = find
.ancestor(
of: find.text('Copy patch').at(index),
matching: find.byWidgetPredicate((widget) => widget is TextButton),
)
.first;
copyButtons.add(tester.widget<TextButton>(button));
}
// Binary and mode-only have nothing to copy; the truncated patch still can.
expect(
copyButtons.where((button) => button.onPressed == null),
hasLength(2),
);
expect(
copyButtons.where((button) => button.onPressed != null),
hasLength(1),
);
});
testWidgets(
'Cupertino uses native list tiles, search field and segmented control',
(tester) async {
await tester.pumpWidget(
cupertinoHost(
ChangesViewer(
steps: [
stepFixture(
fileChanges: captureFixture(
files: [
fileChangeFixture(
path: 'lib/feature.dart',
patch: multiHunkPatch,
),
],
),
),
],
),
),
);
expect(find.byType(CupertinoListTile), findsOneWidget);
expect(find.byType(CupertinoSearchTextField), findsOneWidget);
expect(find.byType(TextField), findsNothing);
await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart')));
await tester.pumpAndSettle();
expect(
find.byType(CupertinoSlidingSegmentedControl<DiffForm>),
findsOneWidget,
);
expect(find.text('Rendered'), findsOneWidget);
expect(find.text('Raw patch'), findsOneWidget);
await tester.tap(find.byIcon(CupertinoIcons.xmark));
await tester.pumpAndSettle();
expect(
find.byType(CupertinoSlidingSegmentedControl<DiffForm>),
findsNothing,
);
},
);
testWidgets('narrow viewport and large text keep every control reachable', (
tester,
) async {
tester.view.physicalSize = const Size(320, 720);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
materialHost(
ChangesViewer(
steps: [
stepFixture(
fileChanges: captureFixture(
files: [
fileChangeFixture(
path: 'lib/a_rather_long_feature_file_name.dart',
patch: multiHunkPatch,
),
fileChangeFixture(path: 'assets/logo.bin', binary: true),
],
),
),
],
),
scaler: const TextScaler.linear(2),
),
);
expect(tester.takeException(), isNull);
expect(
find.text('lib/a_rather_long_feature_file_name.dart'),
findsOneWidget,
);
// The viewer itself at the same narrow width and 2x text: every action
// stays on screen and the patch body scrolls instead of overflowing.
await tester.pumpWidget(
materialHost(
FileDiffViewer(
file: FileChange(
path: 'lib/a_rather_long_feature_file_name.dart',
kind: ChangeKind.modified,
patch: multiHunkPatch,
),
),
scaler: const TextScaler.linear(2),
),
);
expect(tester.takeException(), isNull);
expect(find.text('Rendered'), findsOneWidget);
expect(find.text('Copy patch'), findsOneWidget);
expect(find.byTooltip('Close diff'), findsOneWidget);
});
}
@@ -0,0 +1,206 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/changes/changes.dart';
import 'package:pipelines_app/features/changes/widgets/change_style.dart';
import 'fixtures.dart';
import 'widget_hosts.dart';
/// The patch has one short context line and one much longer added line, so a
/// text-width tint and a full-hunk-width tint are visibly different sizes.
const _raggedPatch =
'@@ -1,4 +1,4 @@\n'
' short\n'
'+this added line is much longer than every other recorded line here\n'
'-x\n'
' tail\n';
void main() {
test(
'patch typography preserves the native host style without overrides',
() {
const native = TextStyle(fontSize: 19, fontFamily: 'host-font');
expect(nativeDiffText(native), same(native));
},
);
testWidgets(
'light and dark themes give additions and removals distinct hues',
(tester) async {
for (final brightness in [Brightness.light, Brightness.dark]) {
await tester.pumpWidget(
materialHost(const SizedBox.shrink(), brightness: brightness),
);
final context = tester.element(find.byType(Scaffold));
final add = diffLineColors(context, ChangeLineKind.add);
final remove = diffLineColors(context, ChangeLineKind.remove);
expect(add.background, isNull);
expect(remove.background, isNull);
expect(add.text, isNot(remove.text), reason: '$brightness text');
expect(add.text, Theme.of(context).colorScheme.primary);
expect(remove.text, Theme.of(context).colorScheme.error);
}
},
);
testWidgets('every diff line shares the hunk width, not its own text width', (
tester,
) async {
await tester.pumpWidget(
materialHost(
SizedBox(
height: 400,
child: FileDiffViewer(
file: FileChange(
path: 'lib/ragged.dart',
kind: ChangeKind.modified,
patch: _raggedPatch,
),
),
),
),
);
await tester.pumpAndSettle();
final widths = [
for (var index = 0; index < 4; index++)
tester.getSize(find.byKey(ValueKey('diff-line-$index'))).width,
];
for (final width in widths) {
expect(width, closeTo(widths.first, 0.01));
}
// The widest recorded line sets the hunk width, well beyond a short line.
expect(widths.first, greaterThan(300));
});
testWidgets('the stat line colours additions and removals apart', (
tester,
) async {
await tester.pumpWidget(
materialHost(
SizedBox(
height: 400,
child: FileDiffViewer(
file: FileChange(
path: 'lib/feature.dart',
kind: ChangeKind.modified,
patch: multiHunkPatch,
),
),
),
),
);
await tester.pumpAndSettle();
final stat = tester.widgetList<RichText>(find.byType(RichText)).firstWhere((
rich,
) {
final span = rich.text;
return span is TextSpan &&
span.children != null &&
span.toPlainText() == '+2 -2';
});
final root = (stat.text as TextSpan).children!.single as TextSpan;
final spans = root.children!.cast<TextSpan>();
final added = spans.firstWhere((span) => span.text == '+2');
final removed = spans.firstWhere((span) => span.text == '-2');
expect(added.style!.color, isNot(removed.style!.color));
});
testWidgets('the raw view scrolls long lines instead of wrapping them', (
tester,
) async {
await tester.pumpWidget(
materialHost(
SizedBox(
height: 400,
child: FileDiffViewer(
file: FileChange(
path: 'lib/feature.dart',
kind: ChangeKind.modified,
patch: multiHunkPatch,
),
),
),
),
);
await tester.tap(find.text('Raw patch'));
await tester.pumpAndSettle();
expect(find.text(multiHunkPatch), findsOneWidget);
expect(
find.ancestor(
of: find.text(multiHunkPatch),
matching: find.byWidgetPredicate(
(widget) =>
widget is SingleChildScrollView &&
widget.scrollDirection == Axis.horizontal,
),
),
findsOneWidget,
);
});
testWidgets('a Cupertino row wraps its recorded path instead of clipping it', (
tester,
) async {
tester.view.physicalSize = const Size(320, 720);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
const path =
'lib/features/changes/domain/a_rather_long_unified_patch_file_name.dart';
await tester.pumpWidget(
cupertinoHost(
ChangesViewer(
steps: [
stepFixture(
fileChanges: captureFixture(
files: [fileChangeFixture(path: path, patch: multiHunkPatch)],
),
),
],
),
),
);
final rich = tester
.widgetList<RichText>(find.byType(RichText))
.firstWhere((widget) => widget.text.toPlainText() == path);
// CupertinoListTile caps a title at one ellipsized line; the row must lift
// that cap so the filename stays visible.
expect(rich.maxLines, isNull);
expect(rich.overflow, TextOverflow.visible);
expect(tester.getSize(find.text(path)).height, greaterThan(30));
expect(tester.takeException(), isNull);
});
testWidgets('a Cupertino phone dialog is inset from the screen edge', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
cupertinoHost(
ChangesViewer(
steps: [
stepFixture(
fileChanges: captureFixture(
files: [
fileChangeFixture(
path: 'lib/feature.dart',
patch: multiHunkPatch,
),
],
),
),
],
),
),
);
await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart')));
await tester.pumpAndSettle();
final rect = tester.getRect(find.byType(CupertinoPopupSurface));
expect(rect.width, lessThanOrEqualTo(390 - 24));
expect(rect.left, greaterThan(8));
expect(rect.right, lessThan(382));
expect(tester.takeException(), isNull);
});
}
+67
View File
@@ -0,0 +1,67 @@
import 'package:pipelines_app/core/api_gateway.dart';
/// Server-shaped fixtures shared by the Changes tests. These mirror the JSON
/// `internal/protocol.StageFileChanges` / `FileChange` writer, including quoted
/// unified headers and optional fields.
const multiHunkPatch =
'--- "a/lib/feature.dart"\n'
'+++ "b/lib/feature.dart"\n'
'@@ -1,4 +1,5 @@\n'
' context one\n'
'-old line\n'
'+new line\n'
'+added line\n'
' context two\n'
' context three\n'
'@@ -20,3 +21,2 @@\n'
' context twenty\n'
'-removed line\n'
' context twenty-two\n';
JsonMap fileChangeFixture({
required String path,
String kind = 'modified',
String patch = '',
bool binary = false,
bool truncated = false,
String? beforeMode,
String? afterMode,
}) => {
'path': path,
'kind': kind,
'patch': patch,
if (binary) 'binary': true,
if (truncated) 'truncated': true,
'before_mode': ?beforeMode,
'after_mode': ?afterMode,
};
JsonMap captureFixture({
String status = 'complete',
List<JsonMap> files = const [],
int skipped = 0,
int omitted = 0,
String diagnostic = '',
String scope = 'Git tracked and nonignored files in the run directory',
}) => {
'status': status,
'scope': scope,
'files': files,
'skipped': skipped,
'omitted': omitted,
'diagnostic': diagnostic,
};
JsonMap stepFixture({
String stage = 'implementer',
int sequence = 1,
String status = 'completed',
JsonMap? fileChanges,
int? visit,
}) => {
'stage': stage,
'sequence': sequence,
'status': status,
'file_changes': fileChanges,
if (visit != null) 'control': {'visit': visit},
};
@@ -0,0 +1,624 @@
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<JsonMap> 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('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<FileChange> 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,
);
@@ -0,0 +1,176 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/changes/domain/unified_patch.dart';
void main() {
test('a blank or absent patch parses to an empty result', () {
for (final blank in <dynamic>['', ' ', '\n', null]) {
final parsed = parseUnifiedPatch(blank);
expect(parsed.raw, '');
expect(parsed.headerLines, isEmpty);
expect(parsed.hunks, isEmpty);
expect(parsed.added, 0);
expect(parsed.removed, 0);
}
});
test('a single hunk is numbered and counted', () {
final patch = [
'--- "a/x.txt"',
'+++ "b/x.txt"',
'@@ -1,3 +1,3 @@',
' ctx',
'-old',
'+new',
' tail',
].join('\n');
final parsed = parseUnifiedPatch(patch);
expect(parsed.raw, patch);
expect(parsed.headerLines, ['--- "a/x.txt"', '+++ "b/x.txt"']);
expect(parsed.hunks, hasLength(1));
final hunk = parsed.hunks.single;
expect(hunk.header, '@@ -1,3 +1,3 @@');
expect(
[hunk.oldStart, hunk.oldCount, hunk.newStart, hunk.newCount],
[1, 3, 1, 3],
);
expect(
[
for (final line in hunk.lines)
[line.kind, line.text, line.oldLine, line.newLine],
],
[
[ChangeLineKind.context, 'ctx', 1, 1],
[ChangeLineKind.remove, 'old', 2, 0],
[ChangeLineKind.add, 'new', 0, 2],
[ChangeLineKind.context, 'tail', 3, 3],
],
);
expect(parsed.added, 1);
expect(parsed.removed, 1);
});
test('a missing count defaults to 1', () {
final hunk = parseUnifiedPatch('@@ -5 +7 @@\n-old\n+new').hunks.single;
expect(
[hunk.oldStart, hunk.oldCount, hunk.newStart, hunk.newCount],
[5, 1, 7, 1],
);
});
test('a multi-hunk patch keeps each hunk header and its own numbering', () {
final parsed = parseUnifiedPatch(
[
'--- "a/lib/x.dart"',
'+++ "b/lib/x.dart"',
'@@ -1,2 +1,3 @@',
' one',
'+two',
' three',
'@@ -40,3 +41,2 @@',
' forty',
'-forty-one',
' forty-two',
].join('\n'),
);
expect(parsed.hunks, hasLength(2));
expect(parsed.hunks[0].lines.map((line) => line.newLine), [1, 2, 3]);
expect(parsed.hunks[1].lines.map((line) => line.oldLine), [40, 41, 42]);
expect(parsed.added, 1);
expect(parsed.removed, 1);
expect(parsed.headerLines, ['--- "a/lib/x.dart"', '+++ "b/lib/x.dart"']);
});
test('a no-newline marker is shown verbatim and never numbered', () {
final lines = parseUnifiedPatch(
[
'@@ -1 +1 @@',
'-one',
r'\ No newline at end of file',
'+two',
].join('\n'),
).hunks.single.lines;
expect(lines[1].noNewlineMarker, isTrue);
expect(lines[1].text, r'\ No newline at end of file');
expect(lines[1].oldLine, 0);
expect(lines[1].newLine, 0);
expect(lines[1].kind, ChangeLineKind.context);
});
test('a second file header closes the current hunk', () {
final parsed = parseUnifiedPatch(
[
'--- "a/x"',
'+++ "b/x"',
'@@ -1 +1 @@',
'-old',
'+new',
'--- "a/y"',
'+++ "b/y"',
'@@ -1 +1 @@',
'-alt',
'+neu',
].join('\n'),
);
expect(parsed.hunks, hasLength(2));
expect(parsed.hunks[0].lines.map((line) => line.text), ['old', 'new']);
expect(parsed.hunks[1].lines.map((line) => line.text), ['alt', 'neu']);
expect(
parsed.headerLines.where((line) => line.startsWith('--- ')),
hasLength(2),
);
expect(parsed.added, 2);
expect(parsed.removed, 2);
});
test('metadata before a hunk is kept; a truncated tail is unnumbered', () {
final parsed = parseUnifiedPatch(
[
'index abc..def 100644',
'--- "a/x"',
'+++ "b/x"',
'@@ -1 +1 @@',
'-old',
'+new',
'trailing trunc',
].join('\n'),
);
expect(parsed.headerLines.first, 'index abc..def 100644');
final tail = parsed.hunks.single.lines.last;
expect(tail.kind, ChangeLineKind.context);
expect(tail.text, 'trailing trunc');
expect([tail.oldLine, tail.newLine], [0, 0]);
});
test('a trailing newline becomes a numbered empty context line', () {
final lines = parseUnifiedPatch('@@ -1 +1 @@\n ctx\n').hunks.single.lines;
expect(lines, hasLength(2));
expect([lines[1].text, lines[1].oldLine, lines[1].newLine], ['', 2, 2]);
});
test('gutter width, gutter text, symbols and stat match the web wording', () {
expect(
gutterWidth(
const PatchHunk(
header: 'h',
oldStart: 0,
oldCount: 0,
newStart: 0,
newCount: 0,
lines: [],
),
),
1,
);
final hunk = parseUnifiedPatch('@@ -9,1 +12,1 @@\n-a\n+b').hunks.single;
expect(gutterWidth(hunk), 2);
expect(gutterText(0, 2), ' ');
expect(gutterText(7, 2), ' 7');
expect(diffLineSymbol(ChangeLineKind.add), '+');
expect(diffLineSymbol(ChangeLineKind.remove), '-');
expect(diffLineSymbol(ChangeLineKind.context), ' ');
expect(changeStat(3, 2), '+3 -2');
expect(changeStat(3, 0), '+3');
expect(changeStat(0, 2), '-2');
expect(changeStat(0, 0), '');
});
}
@@ -0,0 +1,41 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/// The two platform hosts the Changes widget tests share: Material for
/// Android/desktop/web and Cupertino for iOS/macOS. They state the platform
/// explicitly instead of mutating a global debug override.
Widget materialHost(
Widget child, {
TextScaler scaler = TextScaler.noScaling,
Brightness brightness = Brightness.light,
}) {
return MaterialApp(
theme: ThemeData(brightness: brightness),
builder: (context, app) => MediaQuery(
data: MediaQuery.of(context).copyWith(textScaler: scaler),
child: app!,
),
home: Scaffold(body: child),
);
}
Widget cupertinoHost(
Widget child, {
TextScaler scaler = TextScaler.noScaling,
Brightness brightness = Brightness.light,
}) {
return CupertinoApp(
theme: CupertinoThemeData(brightness: brightness),
builder: (context, app) => Theme(
// The real app selects Cupertino by platform; the test host states it
// explicitly without changing a global debug override. The Theme wraps
// the Navigator so dialogs opened later resolve the same platform.
data: ThemeData(platform: TargetPlatform.iOS),
child: MediaQuery(
data: MediaQuery.of(context).copyWith(textScaler: scaler),
child: app!,
),
),
home: CupertinoPageScaffold(child: SafeArea(child: child)),
);
}