App: chat colours, Ctrl+1-9 opens the Nth list item, starred chats alphabetical
A chat's colour is picked from its row menu (Colour...), a long-press or the chat information sheet. The list shows it as a leading bar, a faint row tint and a coloured avatar; the open chat shows a faint wash under its top bar. Shades are Cupertino's dynamic system colours and Material swatches tuned per brightness. Changes from other devices apply live. Ctrl+1...Ctrl+9 open the Nth item of the open tab's list (chats, runs, pipelines) in on-screen order, also while typing. Starred chats are sorted by name, case-insensitively, as one alphabetical group.
This commit is contained in:
@@ -0,0 +1,269 @@
|
||||
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/chat/data/chat_repository.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_screen.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/session_row_content.dart';
|
||||
import 'package:pipelines_app/ui/layout/app_list_avatar.dart';
|
||||
import 'package:pipelines_app/ui/layout/app_list_row.dart';
|
||||
|
||||
import 'chat_test_harness.dart';
|
||||
import 'fake_chat_gateway.dart';
|
||||
|
||||
/// A server whose list keeps each accepted colour, as the registry does.
|
||||
FakeChatGateway _gateway() {
|
||||
final rows = <JsonMap>[
|
||||
{'id': 's1', 'title': 'Teal chat', 'directory': '/w', 'color': 'teal'},
|
||||
{'id': 's2', 'title': 'Plain chat', 'directory': '/w'},
|
||||
];
|
||||
final gateway = FakeChatGateway();
|
||||
gateway.sessionsHandler = () async => {
|
||||
'chat_sessions': [
|
||||
for (final row in rows) {...row},
|
||||
],
|
||||
};
|
||||
gateway.actionHandler = (type, fields) async {
|
||||
if (type != 'chat_color') return const {};
|
||||
final query = fields['chat_query'] as JsonMap;
|
||||
final row = rows.firstWhere((r) => r['id'] == query['session_id']);
|
||||
final color = query['color'];
|
||||
color == null ? row.remove('color') : row['color'] = color;
|
||||
return {'chat_session': row};
|
||||
};
|
||||
return gateway;
|
||||
}
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
FakeChatGateway gateway, {
|
||||
required bool cupertino,
|
||||
required Brightness brightness,
|
||||
}) async {
|
||||
final screen = ChatSessionsScreen(
|
||||
gateway: gateway,
|
||||
onOpen: (_) {},
|
||||
localStore: MemoryChatSessionLocalStore(),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(
|
||||
theme: CupertinoThemeData(brightness: brightness),
|
||||
home: MediaQuery(
|
||||
data: MediaQueryData(platformBrightness: brightness),
|
||||
child: screen,
|
||||
),
|
||||
)
|
||||
: MaterialApp(
|
||||
theme: ThemeData(
|
||||
platform: TargetPlatform.android,
|
||||
brightness: brightness,
|
||||
),
|
||||
home: screen,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Finder _accent(String id) => find.descendant(
|
||||
of: find.byKey(Key('chat-row-$id')),
|
||||
matching: find.byKey(appListRowAccentKey),
|
||||
);
|
||||
|
||||
Color _accentColor(WidgetTester tester, String id) {
|
||||
final box = tester.widget<DecoratedBox>(_accent(id));
|
||||
final border = (box.decoration as BoxDecoration).border as BorderDirectional;
|
||||
return border.start.color;
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
for (final brightness in Brightness.values) {
|
||||
final host = '${cupertino ? 'Cupertino' : 'Material'} ${brightness.name}';
|
||||
|
||||
testWidgets('$host: a coloured chat wears its colour, a plain one none', (
|
||||
tester,
|
||||
) async {
|
||||
await _pump(
|
||||
tester,
|
||||
_gateway(),
|
||||
cupertino: cupertino,
|
||||
brightness: brightness,
|
||||
);
|
||||
expect(_accent('s1'), findsOneWidget);
|
||||
expect(_accent('s2'), findsNothing);
|
||||
final accent = _accentColor(tester, 's1');
|
||||
// The avatar takes the same colour as the bar.
|
||||
final avatar = tester.widget<AppListAvatar>(
|
||||
find.descendant(
|
||||
of: find.byKey(const Key('chat-row-s1')),
|
||||
matching: find.byType(AppListAvatar),
|
||||
),
|
||||
);
|
||||
expect(avatar.foreground, accent);
|
||||
// A teal shade, not the host's accent or a grey.
|
||||
expect(accent.g, greaterThan(accent.r));
|
||||
expect(accent.b, greaterThan(accent.r));
|
||||
// The row tint is faint: the selected fill stays the stronger mark.
|
||||
final tint = cupertino
|
||||
? tester
|
||||
.widget<ColoredBox>(
|
||||
find.descendant(
|
||||
of: find.byKey(const Key('chat-row-s1')),
|
||||
matching: find.byKey(appListRowSurfaceKey),
|
||||
),
|
||||
)
|
||||
.color
|
||||
: tester
|
||||
.widget<Material>(
|
||||
find.descendant(
|
||||
of: find.byKey(const Key('chat-row-s1')),
|
||||
matching: find.byKey(appListRowSurfaceKey),
|
||||
),
|
||||
)
|
||||
.color!;
|
||||
expect(tint.a, inInclusiveRange(0.05, 0.13));
|
||||
// Shades are tuned per brightness.
|
||||
final shade = accent.computeLuminance();
|
||||
if (brightness == Brightness.dark) {
|
||||
expect(shade, greaterThan(0.2));
|
||||
} else {
|
||||
expect(shade, lessThan(0.55));
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('$host: the row menu sets and clears the colour', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = _gateway();
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
cupertino: cupertino,
|
||||
brightness: brightness,
|
||||
);
|
||||
await tester.tap(
|
||||
find.descendant(
|
||||
of: find.byKey(const Key('chat-actions-s2')),
|
||||
matching: find.byType(cupertino ? CupertinoButton : IconButton),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Colour…'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('chat-color-sheet')), findsOneWidget);
|
||||
await tester.tap(find.byKey(const Key('chat-color-purple')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.lastFields('chat_color'), {
|
||||
'chat_query': {'session_id': 's2', 'color': 'purple'},
|
||||
});
|
||||
// The row shows it at once, before the list re-reads.
|
||||
expect(_accent('s2'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byKey(const Key('chat-color-none')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.lastFields('chat_color'), {
|
||||
'chat_query': {'session_id': 's2', 'color': null},
|
||||
});
|
||||
await tester.tap(find.byKey(const Key('chat-color-done')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('chat-color-sheet')), findsNothing);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
testWidgets('a long-press on a row opens the colour choice', (tester) async {
|
||||
final gateway = _gateway();
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
cupertino: false,
|
||||
brightness: Brightness.light,
|
||||
);
|
||||
await tester.longPress(find.text('Plain chat'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('chat-color-sheet')), findsOneWidget);
|
||||
await tester.tap(find.byKey(const Key('chat-color-red')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.lastFields('chat_color'), {
|
||||
'chat_query': {'session_id': 's2', 'color': 'red'},
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('the selected coloured row keeps its colour and selection', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
home: Scaffold(
|
||||
body: ChatSessionRow(
|
||||
session: const {'id': 's1', 'title': 'T', 'color': 'amber'},
|
||||
selected: true,
|
||||
onOpen: (_) {},
|
||||
onAction: (_) {},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
final surface = tester.widget<Material>(find.byKey(appListRowSurfaceKey));
|
||||
final context = tester.element(find.byType(ChatSessionRow));
|
||||
expect(
|
||||
surface.color,
|
||||
AppListRow.selectedColor(context, cupertino: false),
|
||||
reason: 'The selected fill, not the tint, marks the open row',
|
||||
);
|
||||
expect(find.byKey(appListRowAccentKey), findsOneWidget);
|
||||
});
|
||||
|
||||
group('controller', () {
|
||||
late FakeChatGateway gateway;
|
||||
late ChatSessionsController controller;
|
||||
|
||||
setUp(() async {
|
||||
gateway = _gateway();
|
||||
controller = ChatSessionsController(
|
||||
ChatRepository(gateway),
|
||||
localStore: MemoryChatSessionLocalStore(),
|
||||
);
|
||||
await controller.refresh();
|
||||
});
|
||||
tearDown(() => controller.dispose());
|
||||
|
||||
JsonMap row(String id) =>
|
||||
controller.sessions.firstWhere((s) => s['id'] == id);
|
||||
|
||||
test('another device\'s change arrives live without a re-list', () async {
|
||||
final lists = gateway.count('chat_sessions');
|
||||
gateway.pushed.add({
|
||||
'type': 'chat_session_changed',
|
||||
'chat_session': {
|
||||
'id': 's2',
|
||||
'title': 'Plain chat',
|
||||
'color': 'pink',
|
||||
'starred': true,
|
||||
'starred_at': 5,
|
||||
},
|
||||
});
|
||||
await pumpEventQueue();
|
||||
expect(row('s2')['color'], 'pink');
|
||||
expect(row('s2')['starred'], isTrue);
|
||||
// An omitted colour is a cleared one.
|
||||
gateway.pushed.add({
|
||||
'type': 'chat_session_changed',
|
||||
'chat_session': {'id': 's1', 'title': 'Teal chat'},
|
||||
});
|
||||
await pumpEventQueue();
|
||||
expect(row('s1').containsKey('color'), isFalse);
|
||||
expect(gateway.count('chat_sessions'), lists);
|
||||
});
|
||||
|
||||
test('a refused colour is undone and the reason shown', () async {
|
||||
gateway.failures['chat_color'] = 'unknown chat colour';
|
||||
await controller.setColor('s2', 'blue');
|
||||
expect(row('s2')['color'], isNull);
|
||||
expect(controller.errorFor('s2'), contains('unknown chat colour'));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user