Files
pipelines/app/test/application/list_detail_consistency_test.dart
T

235 lines
9.1 KiB
Dart

import 'dart:ui' show Tristate;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pipelines_app/features/chat/screen.dart';
import 'package:pipelines_app/features/pipelines/screen.dart';
import 'package:pipelines_app/features/runs/screen.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/ui/theme/app_theme.dart';
import '../features/pipelines/fixtures.dart';
void main() {
for (final cupertino in [false, true]) {
for (final brightness in Brightness.values) {
for (final width in [640.0, 840.0, 1280.0]) {
testWidgets('shared equal panes and selected rows: $cupertino '
'$brightness at $width', (tester) async {
SharedPreferences.setMockInitialValues({});
tester.view.physicalSize = Size(width, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final fixture = PipelineFixture();
final gateway = FakeGateway(
(type, fields) => switch (type) {
'list_pipelines' => pipelineListReply(fixture.summaries),
'get_pipeline' => pipelineDetailReply(
fixture.detailFor(
fields['pipeline_id'] as int,
fields['version'] as int,
)!,
),
'list_runs' => {
'runs': [
for (final id in [1, 2])
{'id': id, 'title': 'Run item $id', 'status': 'succeeded'},
],
},
'get_run' => {
'run_detail': {
'id': fields['run_id'],
'title': 'Run item ${fields['run_id']}',
'status': 'succeeded',
'input': {'prompt': 'Recorded input'},
},
},
'chat_sessions' => {
'chat_sessions': [
{
'id': 'a',
'title': 'Chat item a',
'backend': 'claude',
'model': 'claude-opus',
'mode': 'auto',
'mode_name': 'Auto',
},
{
'id': 'b',
'title': 'Chat item b',
'backend': 'claude',
'model': 'claude-opus',
'mode': 'auto',
'mode_name': 'Auto',
},
],
},
'chat_messages' => {
'chat_history': {'messages': []},
},
_ => {},
},
);
final shell = AdaptiveShell(
cupertino: cupertino,
destinationBodies: {
AppDestination.pipelines: (_) =>
PipelinesScreen(gateway: gateway, embedded: true),
AppDestination.runs: (_) => RunsScreen(gateway: gateway),
AppDestination.chat: (_) => ChatScreen(gateway: gateway),
},
);
await tester.pumpWidget(
cupertino
? CupertinoApp(
theme: CupertinoThemeData(brightness: brightness),
home: shell,
)
: MaterialApp(
theme: brightness == Brightness.dark
? appDarkTheme()
: appLightTheme(),
home: shell,
),
);
await tester.pumpAndSettle();
final paneWidths = <double>[];
for (final destination in ['Pipelines', 'Runs', 'Chat']) {
if (destination != 'Pipelines') {
await tester.tap(find.text(destination).first);
await tester.pumpAndSettle();
expect(
tester.takeException(),
isNull,
reason: '$destination list',
);
}
final list = find.byKey(const ValueKey('list-detail-list'));
final detail = find.byKey(const ValueKey('list-detail-body'));
expect(list, findsOneWidget);
expect(detail, findsOneWidget);
final before = tester.getSize(list).width;
paneWidths.add(before);
expect(before, closeTo(tester.getSize(detail).width, 0.01));
final rowKeys = switch (destination) {
'Pipelines' => ['pipeline-row-1', 'pipeline-row-2'],
'Runs' => ['run-row-1', 'run-row-2'],
_ => ['chat-row-a', 'chat-row-b'],
};
for (final key in rowKeys) {
final target = find.byKey(ValueKey(key));
// In a narrow Cupertino list the row center can be a star/menu
// control. Open the chat through its title, not a trailing action.
await tester.tap(
destination == 'Chat'
? find.descendant(
of: target,
matching: find.text('Chat item ${key.split('-').last}'),
)
: target,
);
await tester.pumpAndSettle();
expect(
tester.takeException(),
isNull,
reason: '$destination after opening $key',
);
expect(tester.getSize(list).width, closeTo(before, 0.01));
expect(tester.getSize(detail).width, closeTo(before, 0.01));
expect(
tester.state<NavigatorState>(find.byType(Navigator)).canPop(),
isFalse,
);
for (final rowKey in rowKeys) {
final row = find.byKey(ValueKey(rowKey));
expect(
row,
findsOneWidget,
reason: 'the list must remain visible',
);
final selected = rowKey == key;
expect(
tester.getSemantics(row).flagsCollection.isSelected ==
Tristate.isTrue,
selected,
reason: '$destination: $rowKey after opening $key',
);
final context = tester.element(row);
if (cupertino) {
final color = destination == 'Runs'
? tester.widget<CupertinoButton>(row).color
: tester.widget<CupertinoListTile>(row).backgroundColor;
expect(
color,
selected
? CupertinoColors.tertiarySystemFill.resolveFrom(
context,
)
: isNull,
);
} else if (destination == 'Runs') {
final id = rowKey.split('-').last;
expect(
tester
.widget<Material>(
find.byKey(ValueKey('run-row-surface-$id')),
)
.color,
selected
? Theme.of(context).colorScheme.secondaryContainer
: Colors.transparent,
);
} else {
final tile = tester.widget<ListTile>(row);
expect(tile.selected, selected);
expect(
tile.selectedTileColor,
Theme.of(context).colorScheme.secondaryContainer,
);
}
}
}
// A phone still uses stock page navigation; returning to a wide
// window must keep the selected item without clearing its route.
tester.view.physicalSize = const Size(390, 900);
await tester.pumpAndSettle();
expect(
tester.state<NavigatorState>(find.byType(Navigator)).canPop(),
isTrue,
reason: '$destination should push its selected item on a phone',
);
expect(
find.byKey(const ValueKey('list-detail-list')),
findsNothing,
);
tester.view.physicalSize = Size(width, 900);
await tester.pumpAndSettle();
expect(
tester.state<NavigatorState>(find.byType(Navigator)).canPop(),
isFalse,
);
expect(tester.getSize(list).width, closeTo(before, 0.01));
expect(
tester
.getSemantics(find.byKey(ValueKey(rowKeys.last)))
.flagsCollection
.isSelected,
Tristate.isTrue,
);
}
expect(
paneWidths,
everyElement(closeTo(paneWidths.first, 0.01)),
reason: 'Pipelines, Runs and Chat must use identical pane widths',
);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox.shrink());
});
}
}
}
}