feat(app): add run change captures and native diff viewer
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
import 'dart:io';
|
||||
|
||||
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';
|
||||
|
||||
double _addGreenMinusRed(Color color) => color.g - color.r;
|
||||
|
||||
double _removeRedMinusGreen(Color color) => color.r - color.g;
|
||||
|
||||
void main() {
|
||||
test('patch text uses the bundled code face, not a web fallback', () {
|
||||
final style = monospaceText(const TextStyle(fontSize: 12));
|
||||
expect(style.fontFamily, codeFontFamily);
|
||||
expect(style.fontFamilyFallback, contains('Menlo'));
|
||||
// Flutter web can only render a monospace diff if the build carries one.
|
||||
expect(File('assets/fonts/RobotoMono.ttf').existsSync(), isTrue);
|
||||
expect(
|
||||
File('pubspec.yaml').readAsStringSync(),
|
||||
contains('family: $codeFontFamily'),
|
||||
);
|
||||
});
|
||||
|
||||
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, isNotNull, reason: '$brightness add tint');
|
||||
expect(remove.background, isNotNull, reason: '$brightness remove tint');
|
||||
expect(add.text, isNot(remove.text), reason: '$brightness text');
|
||||
expect(
|
||||
_addGreenMinusRed(add.background!),
|
||||
greaterThan(0.02),
|
||||
reason: '$brightness additions read as green, not another red',
|
||||
);
|
||||
expect(
|
||||
_removeRedMinusGreen(remove.background!),
|
||||
greaterThan(0.02),
|
||||
reason: '$brightness removals read as red, not another green',
|
||||
);
|
||||
expect(
|
||||
_removeRedMinusGreen(remove.text),
|
||||
greaterThan(0.02),
|
||||
reason: '$brightness removal text is a red hue',
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('every diff line tint spans the hunk width, not its own text', (
|
||||
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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user