Files
pipelines/app/test/features/chat/conversation/chat_floating_chrome_test.dart
T

278 lines
9.4 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_reading_width.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
import 'package:pipelines_app/ui/adaptive/segmented_bar_pieces.dart';
import 'chat_thread_fixtures.dart';
/// Floating composer, reading width and header controls of a conversation.
Widget _host(Widget child, {required bool cupertino}) => cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: child))
: MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(body: child),
);
void _size(WidgetTester tester, Size size) {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
}
List<Map<String, dynamic>> _messages(int count) => [
for (var index = 0; index < count; index++)
chatMessage(
'm$index',
index.isEven ? 'user' : 'assistant',
parts: [
{'type': 'text', 'text': 'Message number $index of the transcript.'},
],
),
];
Future<FakeChatGateway> _pump(
WidgetTester tester, {
required bool cupertino,
int messages = 30,
Map<String, dynamic>? session,
}) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(_messages(messages));
gateway.subscribe = () async => const {};
await tester.pumpWidget(
_host(
ChatThreadView(
gateway: gateway,
sessionId: 's',
title: 'Floating chrome',
session: session,
onBack: () {},
),
cupertino: cupertino,
),
);
await tester.pumpAndSettle();
return gateway;
}
void main() {
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('$host transcript viewport ends at the composer top', (
tester,
) async {
_size(tester, const Size(560, 800));
await _pump(tester, cupertino: cupertino);
final pill = find.byKey(const Key('chat-composer-pill'));
expect(tester.widget(pill), isA<GlassPill>());
Rect list() =>
tester.getRect(find.byKey(const ValueKey('chat-thread-s')));
Rect composer() => tester.getRect(find.byKey(const Key('chat-composer')));
Rect header() =>
tester.getRect(find.byKey(const Key('chat-thread-header')));
// The transcript has its own viewport below the complete header.
expect(list().top, greaterThanOrEqualTo(header().bottom));
// Nothing renders behind the composer: the viewport stops at its top.
expect(list().bottom, composer().top);
expect(composer().bottom, lessThanOrEqualTo(800));
expect(tester.getRect(pill).top, greaterThanOrEqualTo(list().bottom));
// Following the end, the last message rests fully above the composer.
final last = find.text('Message number 29 of the transcript.');
expect(tester.getRect(last).bottom, lessThanOrEqualTo(composer().top));
// A multi-line draft grows the composer; the viewport shrinks with it
// and the newest message stays in view above it.
final oneLine = composer().top;
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
List.generate(6, (index) => 'Draft line $index').join('\n'),
);
await tester.pumpAndSettle();
expect(composer().top, lessThan(oneLine));
expect(list().bottom, composer().top);
expect(tester.getRect(last).bottom, lessThanOrEqualTo(composer().top));
// Scrolling back up moves history down inside the shortened viewport.
final rested = tester.getRect(last).top;
await tester.drag(
find.byKey(const ValueKey('chat-thread-s')),
const Offset(0, 120),
);
await tester.pumpAndSettle();
if (last.evaluate().isNotEmpty) {
expect(tester.getRect(last).top, greaterThan(rested));
}
expect(list().bottom, composer().top);
expect(list().top, greaterThanOrEqualTo(header().bottom));
expect(tester.takeException(), isNull);
});
testWidgets('$host transcript and composer share a centred reading width', (
tester,
) async {
_size(tester, const Size(1200, 800));
await _pump(tester, cupertino: cupertino, messages: 4);
final pill = tester.getRect(find.byKey(const Key('chat-composer-pill')));
expect(pill.width, lessThanOrEqualTo(kChatReadingWidth));
expect(pill.center.dx, closeTo(600, 1));
final message = tester.getRect(
find.text('Message number 0 of the transcript.'),
);
final inset = chatReadingInset(1200);
expect(message.left, greaterThanOrEqualTo(inset));
expect(message.right, lessThanOrEqualTo(1200 - inset));
// The list itself still spans the window, so its margins scroll it.
expect(
tester.getRect(find.byKey(const ValueKey('chat-thread-s'))).width,
1200,
);
// Narrow, the column fills the width less the usual margins.
_size(tester, const Size(560, 800));
await tester.pumpAndSettle();
expect(
tester.getRect(find.byKey(const Key('chat-composer-pill'))).width,
560 - 32,
);
});
testWidgets('$host header draws no empty pill without model or mode', (
tester,
) async {
_size(tester, const Size(560, 800));
await _pump(
tester,
cupertino: cupertino,
messages: 1,
session: const {'id': 's', 'backend': 'pi', 'directory': '/srv'},
);
final header = find.byKey(const Key('chat-thread-header'));
expect(find.byKey(const Key('chat-mode')), findsNothing);
// The round Back, the title pill and the actions pill, which always
// carries the Files toggle, so it is never drawn empty.
expect(
find.descendant(of: header, matching: find.byType(SegmentedPill)),
findsNWidgets(3),
);
expect(
find.descendant(
of: header,
matching: find.bySemanticsIdentifier('chat-files-toggle'),
),
findsOneWidget,
);
});
testWidgets('$host header controls keep their natural width when wide', (
tester,
) async {
_size(tester, const Size(1200, 800));
await _pump(
tester,
cupertino: cupertino,
messages: 1,
session: const {
'id': 's',
'backend': 'opencode',
'model': 'p/sonnet-4-5',
'mode': 'default',
'mode_name': 'Ask for permission',
},
);
final mode = find.byKey(const Key('chat-mode'));
final model = find.byKey(const Key('chat-model'));
expect(mode, findsOneWidget);
expect(model, findsOneWidget);
for (final control in [mode, model]) {
final texts = find.descendant(of: control, matching: find.byType(Text));
for (final element in texts.evaluate()) {
final paragraph = element.renderObject! as RenderParagraph;
expect(
paragraph.didExceedMaxLines,
isFalse,
reason: 'truncated at 1200',
);
}
}
// Neutral like the bar's other actions, not the primary tint.
if (!cupertino) {
final button = tester.widget<TextButton>(mode);
final theme = Theme.of(tester.element(mode));
expect(
button.style!.foregroundColor!.resolve({}),
theme.colorScheme.onSurfaceVariant,
);
} else {
final label = tester.widget<RichText>(
find.descendant(
of: mode,
matching: find.byWidgetPredicate(
(widget) =>
widget is RichText &&
widget.text.toPlainText() == 'Ask for permission',
),
),
);
expect(
label.text.style?.color,
CupertinoColors.label.resolveFrom(tester.element(mode)),
);
}
});
testWidgets(
'Cupertino narrow header exposes the current permission on its icon',
(tester) async {
if (!cupertino) return;
final semantics = tester.ensureSemantics();
_size(tester, const Size(560, 800));
await _pump(
tester,
cupertino: true,
messages: 1,
session: const {
'id': 's',
'backend': 'opencode',
'model': 'p/sonnet-4-5',
'mode': 'default',
'mode_name': 'Ask for permission',
},
);
final mode = find.byKey(const Key('chat-mode'));
expect(mode, findsOneWidget);
expect(
find.descendant(of: mode, matching: find.text('Ask for permission')),
findsNothing,
);
expect(
tester.getSemantics(mode).label,
contains('Change mode, currently Ask for permission'),
);
await tester.tap(mode);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-mode-sheet')), findsOneWidget);
// The fixture has no chat_modes catalog, but the recorded session mode
// remains visible in the sheet.
expect(
find.byKey(const ValueKey('chat-mode-option-default')),
findsOneWidget,
);
expect(find.text('Ask for permission'), findsWidgets);
semantics.dispose();
},
);
}
}