Files
pipelines/app/test/features/graph/widgets/definition_graph_view_test.dart
T

342 lines
11 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/graph/painting/graph_edge_layer.dart';
import 'package:pipelines_app/features/graph/widgets/definition_graph_view.dart';
import 'package:pipelines_app/features/graph/widgets/graph_stage_node.dart';
import '../domain/graph_fixtures.dart';
const String _longKey = 'implementer-with-an-unusually-long-stage-label';
JsonMap _longKeyGraph() => <String, dynamic>{
'stages': <dynamic>[
stage(
'plan',
edges: <String, String?>{'success': _longKey, 'error': '#'},
fallbackError: true,
),
stage(
_longKey,
edges: <String, String?>{'success': '#success', 'error': '#failure'},
),
],
};
JsonMap _emptyGraph() => <String, dynamic>{'stages': <dynamic>[]};
/// One bounded viewport, with an optional real text scale applied below the
/// app so the graph measures text exactly as a device would.
Widget _materialHost(
JsonMap graph, {
String? selected,
ValueChanged<String>? onSelected,
double scale = 1,
}) {
return MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(scale)),
child: SizedBox(
width: 900,
height: 700,
child: DefinitionGraphView(
graph: graph,
selectedStage: selected,
onSelected: onSelected ?? (_) {},
),
),
),
),
),
);
}
Widget _cupertinoHost(JsonMap graph, {ValueChanged<String>? onSelected}) {
return CupertinoApp(
home: CupertinoPageScaffold(
child: SizedBox(
width: 900,
height: 700,
child: DefinitionGraphView(
graph: graph,
onSelected: onSelected ?? (_) {},
),
),
),
);
}
/// Settles the two-frame measure-then-place contract.
Future<void> _settle(WidgetTester tester) async {
await tester.pump();
await tester.pump();
}
Finder _node(String key) => find.byWidgetPredicate(
(widget) => widget is GraphStageNode && widget.node.key == key,
);
/// The graph-space extent of a placed node.
///
/// The viewport fits the measured graph into the available box, so an onstage
/// node's rectangle is its measured extent times the current scale. Dividing
/// the rectangle by that scale keeps these assertions about measurement rather
/// than about the viewport transform.
Size _measuredNodeSize(WidgetTester tester, String key) {
final scale = tester
.widget<InteractiveViewer>(find.byType(InteractiveViewer))
.transformationController!
.value
.getMaxScaleOnAxis();
final rect = _nodeRect(tester, key);
return Size(rect.width / scale, rect.height / scale);
}
Rect _nodeRect(WidgetTester tester, String key) => tester.getRect(_node(key));
Future<void> _tapNode(WidgetTester tester, String key) async {
final viewport = tester.getRect(find.byType(InteractiveViewer));
final node = _nodeRect(tester, key);
if (!viewport.contains(node.center)) {
await tester.drag(
find.byType(InteractiveViewer),
viewport.center - node.center,
);
await tester.pumpAndSettle();
}
await tester.tap(_node(key));
await tester.pump();
}
void _expectNoOverlaps(WidgetTester tester) {
final rects = <Rect>[
for (final element in find.byType(GraphStageNode).evaluate())
tester.getRect(find.byWidget(element.widget)),
];
for (var a = 0; a < rects.length; a += 1) {
for (var b = a + 1; b < rects.length; b += 1) {
expect(
rects[a].overlaps(rects[b]),
isFalse,
reason: 'nodes $a and $b overlap: ${rects[a]} and ${rects[b]}',
);
}
}
}
void main() {
testWidgets(
'viewport changes remeasure node width instead of imposing a fixed cap',
(tester) async {
tester.view.physicalSize = const Size(900, 700);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(_materialHost(_longKeyGraph()));
await _settle(tester);
final wide = _measuredNodeSize(tester, _longKey);
tester.view.physicalSize = const Size(320, 700);
await tester.pump();
await _settle(tester);
final narrow = _measuredNodeSize(tester, _longKey);
expect(narrow.width, lessThan(wide.width));
expect(narrow.width, lessThanOrEqualTo(320));
expect(narrow.height, greaterThan(wide.height));
_expectNoOverlaps(tester);
expect(tester.takeException(), isNull);
},
);
testWidgets('selection reports the exact definition key', (tester) async {
final picked = <String>[];
await tester.pumpWidget(
_materialHost(linearGraph(), selected: 'build', onSelected: picked.add),
);
await _settle(tester);
expect(find.byType(GraphStageNode), findsNWidgets(5));
expect(find.byType(InteractiveViewer), findsOneWidget);
expect(tester.widget<GraphStageNode>(_node('build')).selected, isTrue);
expect(tester.widget<GraphStageNode>(_node('plan')).selected, isFalse);
await tester.tap(_node('plan'));
await tester.pump();
expect(picked, ['plan']);
await _tapNode(tester, '#success');
expect(picked, ['plan', '#success']);
});
testWidgets('a tap after zoom selects the node under the pointer', (
tester,
) async {
final picked = <String>[];
await tester.pumpWidget(
_materialHost(linearGraph(), onSelected: picked.add),
);
await _settle(tester);
final controller = tester
.widget<InteractiveViewer>(find.byType(InteractiveViewer))
.transformationController!;
final fitted = controller.value.getMaxScaleOnAxis();
// Scale about the node's own on-screen point, as a pinch there would.
final viewportRect = tester.getRect(find.byType(InteractiveViewer));
final anchor = _nodeRect(tester, 'build').center - viewportRect.topLeft;
final zoom = Matrix4.identity()
..translateByDouble(anchor.dx, anchor.dy, 0, 1)
..scaleByDouble(1.6, 1.6, 1.6, 1)
..translateByDouble(-anchor.dx, -anchor.dy, 0, 1);
controller.value = zoom * controller.value;
await tester.pump();
expect(controller.value.getMaxScaleOnAxis(), greaterThan(fitted));
// The node's on-screen rectangle already carries the transform, so this
// tap lands where the node is drawn rather than at its graph coordinate.
final build = _nodeRect(tester, 'build');
expect(
tester.getRect(find.byType(InteractiveViewer)).contains(build.center),
isTrue,
);
await tester.tapAt(build.center);
await tester.pump();
expect(picked, ['build']);
});
testWidgets('nodes use measured native sizes and never overlap', (
tester,
) async {
tester.view.physicalSize = const Size(320, 700);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(_materialHost(_longKeyGraph()));
await _settle(tester);
expect(find.byType(GraphStageNode), findsNWidgets(4));
_expectNoOverlaps(tester);
final plan = _nodeRect(tester, 'plan');
final long = _nodeRect(tester, _longKey);
// The wrapped key is bounded by the graph's measurement bound and is both
// wider and taller than the single-line node: a fixed node size could not
// produce either difference.
expect(long.width, lessThanOrEqualTo(320));
expect(long.width, greaterThan(plan.width));
expect(long.height, greaterThan(plan.height * 1.5));
final layer = tester.widget<GraphEdgeLayer>(find.byType(GraphEdgeLayer));
expect(layer.routes, isNotEmpty);
expect(layer.canvasSize.width, greaterThan(0));
// Every routed edge gets its native outcome label; `error*` is the
// fallback marker carried verbatim from the definition.
expect(find.text('error*'), findsOneWidget);
expect(find.text('success'), findsNWidgets(2));
expect(find.textContaining('no placed node'), findsNothing);
expect(tester.takeException(), isNull);
});
testWidgets('2x text scale re-measures instead of reusing 1x extents', (
tester,
) async {
final graph = linearGraph();
await tester.pumpWidget(_materialHost(graph));
await _settle(tester);
final oneX = _measuredNodeSize(tester, 'plan');
final oneXLabel = tester.getRect(find.text('error*'));
await tester.pumpWidget(_materialHost(graph, scale: 2));
await _settle(tester);
final twoX = _measuredNodeSize(tester, 'plan');
expect(twoX.height, greaterThan(oneX.height));
expect(twoX.width, greaterThan(oneX.width));
expect(
tester.getRect(find.text('error*')).height,
greaterThan(oneXLabel.height),
);
_expectNoOverlaps(tester);
});
testWidgets('stages run in one row with Failure and Success below it', (
tester,
) async {
await tester.pumpWidget(_materialHost(linearGraph()));
await _settle(tester);
// As the web and the TUI: the stages read left to right on one row.
final stages = [
for (final key in ['plan', 'build', 'ship']) _nodeRect(tester, key),
];
for (final rect in stages.skip(1)) {
expect(rect.center.dy, moreOrLessEquals(stages.first.center.dy));
}
expect(stages[0].right, lessThan(stages[1].left));
expect(stages[1].right, lessThan(stages[2].left));
// The ends share a lower row, Failure on the left.
final failure = _nodeRect(tester, '#failure');
final success = _nodeRect(tester, '#success');
expect(failure.top, moreOrLessEquals(success.top));
expect(failure.top, greaterThan(stages.first.bottom));
expect(failure.right, lessThan(success.left));
_expectNoOverlaps(tester);
});
testWidgets('unknown endpoints stay honest and invent no node', (
tester,
) async {
await tester.pumpWidget(_materialHost(incompleteGraph()));
await _settle(tester);
expect(find.byType(GraphStageNode), findsNWidgets(4));
expect(_node('ghost'), findsNothing);
// Two routes are drawn; the empty and unknown targets are reported, not
// guessed into nodes.
expect(
tester.widget<GraphEdgeLayer>(find.byType(GraphEdgeLayer)).routes,
hasLength(2),
);
expect(find.textContaining('ghost'), findsOneWidget);
expect(find.textContaining('edge target is empty'), findsOneWidget);
_expectNoOverlaps(tester);
expect(tester.takeException(), isNull);
});
testWidgets('an empty definition states its empty message', (tester) async {
await tester.pumpWidget(_materialHost(_emptyGraph()));
await tester.pump();
expect(find.text(definitionGraphEmpty), findsOneWidget);
expect(find.byType(GraphStageNode), findsNothing);
expect(find.byType(InteractiveViewer), findsNothing);
});
testWidgets('cupertino host composes native buttons and keeps identity', (
tester,
) async {
final picked = <String>[];
await tester.pumpWidget(
_cupertinoHost(linearGraph(), onSelected: picked.add),
);
await _settle(tester);
expect(
find.descendant(
of: find.byType(GraphStageNode),
matching: find.byType(CupertinoButton),
),
findsNWidgets(5),
);
expect(find.byType(Card), findsNothing);
_expectNoOverlaps(tester);
await _tapNode(tester, '#failure');
expect(picked, ['#failure']);
});
}