e4c196d75b
Transcripts pin to the newest end while following, release on the operator's upward scroll, and offer a native Jump to latest control carrying the count of messages that arrived meanwhile. The chat destination keeps each conversation's saved position so reopening restores it.
285 lines
9.1 KiB
Dart
285 lines
9.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/chat/conversation/chat_scroll.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
|
|
|
import 'chat_thread_fixtures.dart';
|
|
|
|
Widget host(Widget child) => MaterialApp(
|
|
theme: ThemeData(platform: TargetPlatform.android),
|
|
home: Scaffold(body: child),
|
|
);
|
|
|
|
Widget cupertinoHost(Widget child) => CupertinoApp(
|
|
theme: const CupertinoThemeData(brightness: Brightness.dark),
|
|
home: CupertinoPageScaffold(child: child),
|
|
);
|
|
|
|
/// A history tall enough that the transcript overflows the test viewport.
|
|
JsonMap tallPage() => chatPage([
|
|
for (var index = 0; index < 14; index++)
|
|
chatMessage(
|
|
'm$index',
|
|
index.isEven ? 'user' : 'assistant',
|
|
parts: [
|
|
{
|
|
'type': 'text',
|
|
'text':
|
|
'Message $index. '
|
|
'${List.filled(16, 'A sentence that wraps across the transcript width.').join(' ')}',
|
|
},
|
|
],
|
|
),
|
|
]);
|
|
|
|
ScrollController transcriptScroll(WidgetTester tester) =>
|
|
tester
|
|
.widget<ListView>(find.byKey(const Key('chat-thread-s')))
|
|
.controller!;
|
|
|
|
void main() {
|
|
testWidgets('a streaming transcript stays pinned to its newest end', (
|
|
tester,
|
|
) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => tallPage();
|
|
|
|
await tester.pumpWidget(
|
|
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
final scroll = transcriptScroll(tester);
|
|
expect(scroll.position.maxScrollExtent, greaterThan(0));
|
|
expect(
|
|
scroll.position.pixels,
|
|
closeTo(scroll.position.maxScrollExtent, 0.5),
|
|
);
|
|
final heldExtent = scroll.position.maxScrollExtent;
|
|
|
|
// A new message and further deltas grow the turn; every frame keeps the
|
|
// newest end visible without an animation the operator has to chase.
|
|
gateway.emitEvent(chatTextFrame('One streamed paragraph. ', messageId: 'live'));
|
|
await tester.pump();
|
|
await tester.pump();
|
|
gateway.emitEvent(chatTextFrame('And another delta. ', messageId: 'live'));
|
|
await tester.pump();
|
|
await tester.pump();
|
|
|
|
expect(scroll.position.maxScrollExtent, greaterThan(heldExtent));
|
|
expect(
|
|
scroll.position.pixels,
|
|
closeTo(scroll.position.maxScrollExtent, 0.5),
|
|
);
|
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsNothing);
|
|
});
|
|
|
|
for (final cupertino in [false, true]) {
|
|
testWidgets(
|
|
'an upward scroll releases follow and the control counts arrivals '
|
|
'(${cupertino ? 'cupertino' : 'material'})',
|
|
(tester) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => tallPage();
|
|
|
|
await tester.pumpWidget(
|
|
(cupertino ? cupertinoHost : host)(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
final scroll = transcriptScroll(tester);
|
|
await tester.drag(
|
|
find.byKey(const Key('chat-thread-s')),
|
|
const Offset(0, 360),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
final releasedAt = scroll.position.pixels;
|
|
expect(
|
|
releasedAt,
|
|
lessThan(scroll.position.maxScrollExtent - chatFollowThresholdPx),
|
|
);
|
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
|
expect(find.text('Jump to latest'), findsOneWidget);
|
|
|
|
gateway.emitEvent(chatTextFrame('A new answer. ', messageId: 'live-1'));
|
|
await tester.pump();
|
|
await tester.pump();
|
|
|
|
// The operator's position is not moved by an arrival; only the
|
|
// control's count changes.
|
|
expect(scroll.position.pixels, closeTo(releasedAt, 0.5));
|
|
expect(
|
|
find.text('Jump to latest · 1 new message'),
|
|
findsOneWidget,
|
|
);
|
|
|
|
gateway.emitEvent(chatTextFrame('A second answer. ', messageId: 'live-2'));
|
|
await tester.pump();
|
|
await tester.pump();
|
|
expect(scroll.position.pixels, closeTo(releasedAt, 0.5));
|
|
expect(
|
|
find.text('Jump to latest · 2 new messages'),
|
|
findsOneWidget,
|
|
);
|
|
|
|
if (cupertino) {
|
|
// The affordance is a standard Cupertino control; no Material
|
|
// ancestor is introduced into the bare Cupertino host.
|
|
expect(find.byType(Material), findsNothing);
|
|
}
|
|
|
|
await tester.tap(find.byKey(const Key('chat-jump-to-latest')));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(
|
|
scroll.position.pixels,
|
|
closeTo(scroll.position.maxScrollExtent, 0.5),
|
|
);
|
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsNothing);
|
|
|
|
// Returning to the end re-arms following for later frames.
|
|
gateway.emitEvent(chatTextFrame('Pinned again. ', messageId: 'live-3'));
|
|
await tester.pump();
|
|
await tester.pump();
|
|
expect(
|
|
scroll.position.pixels,
|
|
closeTo(scroll.position.maxScrollExtent, 0.5),
|
|
);
|
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsNothing);
|
|
},
|
|
);
|
|
}
|
|
|
|
testWidgets('a reopened conversation restores the position it was left at', (
|
|
tester,
|
|
) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => tallPage();
|
|
final positions = MemoryChatScrollStore();
|
|
|
|
await tester.pumpWidget(
|
|
host(
|
|
ChatThreadView(
|
|
gateway: gateway,
|
|
sessionId: 's',
|
|
scrollPositions: positions,
|
|
onBack: () {},
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
final scroll = transcriptScroll(tester);
|
|
await tester.drag(
|
|
find.byKey(const Key('chat-thread-s')),
|
|
const Offset(0, 360),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
final saved = scroll.position.pixels;
|
|
expect(saved, greaterThan(0));
|
|
expect(positions.read('s'), closeTo(saved, 0.5));
|
|
|
|
// Leaving the conversation disposes its view but not the destination's
|
|
// store.
|
|
await tester.pumpWidget(host(const SizedBox()));
|
|
await tester.pumpAndSettle();
|
|
expect(positions.read('s'), closeTo(saved, 0.5));
|
|
|
|
await tester.pumpWidget(
|
|
host(
|
|
ChatThreadView(
|
|
gateway: gateway,
|
|
sessionId: 's',
|
|
scrollPositions: positions,
|
|
onBack: () {},
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
final reopened = transcriptScroll(tester);
|
|
expect(reopened.position.pixels, closeTo(saved, 1));
|
|
expect(
|
|
reopened.position.pixels,
|
|
lessThan(reopened.position.maxScrollExtent - chatFollowThresholdPx),
|
|
);
|
|
// The operator left a position, so the control offers the way back.
|
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a conversation that fits its viewport offers no control', (
|
|
tester,
|
|
) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => chatPage([
|
|
chatMessage(
|
|
'm1',
|
|
'user',
|
|
parts: [
|
|
{'type': 'text', 'text': 'Short.'},
|
|
],
|
|
),
|
|
]);
|
|
|
|
await tester.pumpWidget(
|
|
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
final scroll = transcriptScroll(tester);
|
|
expect(scroll.position.maxScrollExtent, 0);
|
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsNothing);
|
|
});
|
|
|
|
testWidgets('the control fits a 320px Cupertino viewport at 2x text', (
|
|
tester,
|
|
) async {
|
|
tester.view.physicalSize = const Size(640, 1200);
|
|
tester.view.devicePixelRatio = 2;
|
|
tester.platformDispatcher.textScaleFactorTestValue = 2;
|
|
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
|
|
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
|
|
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => tallPage();
|
|
|
|
await tester.pumpWidget(
|
|
cupertinoHost(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.drag(
|
|
find.byKey(const Key('chat-thread-s')),
|
|
const Offset(0, 360),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
gateway.emitEvent(chatTextFrame('Arrival. ', messageId: 'live'));
|
|
await tester.pump();
|
|
await tester.pump();
|
|
|
|
expect(tester.takeException(), isNull);
|
|
final control = find.byKey(const Key('chat-jump-to-latest'));
|
|
expect(control, findsOneWidget);
|
|
expect(find.text('Jump to latest · 1 new message'), findsOneWidget);
|
|
final rect = tester.getRect(control);
|
|
expect(rect.left, greaterThanOrEqualTo(0));
|
|
expect(rect.right, lessThanOrEqualTo(320));
|
|
expect(find.byType(Material), findsNothing);
|
|
});
|
|
}
|