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 'package:pipelines_app/features/changes/domain/diff_search.dart'; FileChange _file(String path, {int lines = 3, String word = 'alpha'}) { final body = [for (var i = 0; i < lines; i++) '+$word line $i'].join('\n'); return FileChange( path: path, kind: ChangeKind.modified, patch: '--- a/$path\n+++ b/$path\n@@ -0,0 +1,$lines @@\n$body\n', ); } Future _pump(WidgetTester tester, List files) async { tester.view.physicalSize = const Size(1000, 700); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); diffShowsAllFiles.value = false; await tester.pumpWidget( MaterialApp( home: Builder( builder: (context) => Scaffold( body: Center( child: TextButton( onPressed: () => showFileDiffViewer(context, files.first, files: files), child: const Text('open'), ), ), ), ), ), ); await tester.tap(find.text('open')); await tester.pumpAndSettle(); } Future _key(WidgetTester tester, LogicalKeyboardKey key) async { await tester.sendKeyEvent(key); await tester.pumpAndSettle(); } ScrollPosition _scrollOf(WidgetTester tester, Finder within) { final scrollable = tester.widgetList( find.descendant(of: within, matching: find.byType(Scrollable)), ); final state = tester.state( find.byWidget( scrollable.firstWhere((s) => s.axisDirection == AxisDirection.down), ), ); return state.position; } void main() { test('search finds every case-insensitive occurrence in reading order', () { final matches = diffMatches([ _file('a', lines: 2, word: 'Alpha alpha'), _file('b', lines: 1, word: 'beta'), ], 'ALPHA'); expect(matches, hasLength(4)); expect(matches.first.file, 0); expect(diffMatches([_file('a')], ' '), isEmpty); expect(firstMatchFrom(matches, 1), 0); }); testWidgets('h and l step between files like the arrows', (tester) async { await _pump(tester, [_file('lib/one.dart'), _file('lib/two.dart')]); expect(find.text('one.dart'), findsWidgets); await _key(tester, LogicalKeyboardKey.keyL); expect(find.text('two.dart'), findsWidgets); await _key(tester, LogicalKeyboardKey.keyH); expect(find.text('one.dart'), findsWidgets); }); testWidgets('j, k and the up/down arrows scroll the diff', (tester) async { await _pump(tester, [_file('lib/long.dart', lines: 200)]); final viewer = find.byType(FileDiffViewer); expect(_scrollOf(tester, viewer).pixels, 0); await _key(tester, LogicalKeyboardKey.keyJ); await _key(tester, LogicalKeyboardKey.arrowDown); expect(_scrollOf(tester, viewer).pixels, diffScrollStep * 2); await _key(tester, LogicalKeyboardKey.keyK); expect(_scrollOf(tester, viewer).pixels, diffScrollStep); await _key(tester, LogicalKeyboardKey.arrowUp); expect(_scrollOf(tester, viewer).pixels, 0); }); testWidgets('/ searches across files; typed letters stay in the field', ( tester, ) async { await _pump(tester, [ _file('lib/one.dart', word: 'alpha'), _file('lib/two.dart', word: 'hello'), ]); await _key(tester, LogicalKeyboardKey.slash); expect(find.byKey(const ValueKey('diff-search-field')), findsOneWidget); await tester.enterText( find.byKey(const ValueKey('diff-search-field')), 'hello', ); await tester.pumpAndSettle(); // The only matches are in the second file, so the page moves there. expect(find.text('two.dart'), findsWidgets); expect(find.text('1 of 3'), findsOneWidget); // An h typed into the field must not change file. await _key(tester, LogicalKeyboardKey.keyH); expect(find.text('two.dart'), findsWidgets); await tester.testTextInput.receiveAction(TextInputAction.done); await tester.pumpAndSettle(); expect(find.text('2 of 3'), findsOneWidget); // Escape closes the search first, then leaves the page. await _key(tester, LogicalKeyboardKey.escape); expect(find.byKey(const ValueKey('diff-search-field')), findsNothing); await _key(tester, LogicalKeyboardKey.keyN); await _key(tester, LogicalKeyboardKey.escape); expect(find.byType(FileDiffPage), findsNothing); }); testWidgets('all files stacks every file with collapsible headers', ( tester, ) async { await _pump(tester, [ _file('lib/one.dart', word: 'alpha'), _file('lib/two.dart', word: 'gamma'), ]); await tester.tap(find.byKey(const ValueKey('diff-layout-toggle'))); await tester.pumpAndSettle(); expect(find.text('2 files changed'), findsWidgets); expect(find.text('lib/one.dart'), findsOneWidget); expect(find.text('lib/two.dart'), findsOneWidget); expect(find.byKey(const ValueKey('file-diff-pager')), findsNothing); expect( find.textContaining('gamma line 0', findRichText: true), findsOneWidget, ); await tester.tap(find.text('lib/two.dart')); await tester.pumpAndSettle(); expect( find.textContaining('gamma line 0', findRichText: true), findsNothing, ); // Searching reopens the collapsed file that holds the match. await _key(tester, LogicalKeyboardKey.slash); await tester.enterText( find.byKey(const ValueKey('diff-search-field')), 'gamma', ); await tester.pumpAndSettle(); expect(find.textContaining('gamma', findRichText: true), findsWidgets); await tester.tap(find.byKey(const ValueKey('diff-collapse-all'))); await tester.pumpAndSettle(); expect( find.textContaining('alpha line 0', findRichText: true), findsNothing, ); diffShowsAllFiles.value = false; }); }