259 lines
8.1 KiB
Dart
259 lines
8.1 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,
|
|
);
|
|
|
|
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 = _nodeRect(tester, _longKey);
|
|
tester.view.physicalSize = const Size(320, 700);
|
|
await tester.pump();
|
|
await _settle(tester);
|
|
final narrow = _nodeRect(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('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 = _nodeRect(tester, 'plan');
|
|
final oneXLabel = tester.getRect(find.text('error*'));
|
|
|
|
await tester.pumpWidget(_materialHost(graph, scale: 2));
|
|
await _settle(tester);
|
|
|
|
final twoX = _nodeRect(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('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.byType(CupertinoButton), findsNWidgets(5));
|
|
expect(find.byType(Card), findsNothing);
|
|
_expectNoOverlaps(tester);
|
|
|
|
await _tapNode(tester, '#failure');
|
|
expect(picked, ['#failure']);
|
|
});
|
|
}
|