Files
pipelines/app/test/features/chat/conversation/chat_thread_scroll_test.dart
T
alan fbe1928f59 Remove Cupertino chat transcript branches
Keep the existing Material transcript path while preserving public Cupertino parameters for callers. Narrow transcript tests to the Material-only behavior and remove Cupertino-only fixtures.
2026-11-11 11:46:13 +00:00

274 lines
8.6 KiB
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 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.dart';
import 'chat_thread_fixtures.dart';
Widget host(Widget child) => MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(body: 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<CustomScrollView>(
find.descendant(
of: find.byKey(const Key('chat-thread-s')),
matching: find.byType(CustomScrollView),
),
)
.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);
});
testWidgets(
'an upward scroll releases follow and the control counts arrivals',
(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);
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);
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, lessThan(scroll.position.maxScrollExtent - 300));
expect(positions.read('s')!.offset, 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')!.offset, 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 cached reopen restores the position before the read replies', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => tallPage();
final positions = MemoryChatScrollStore();
final transcripts = ChatTranscriptCache();
Widget thread() => host(
ChatThreadView(
gateway: gateway,
sessionId: 's',
scrollPositions: positions,
transcripts: transcripts,
onBack: () {},
),
);
await tester.pumpWidget(thread());
await tester.pumpAndSettle();
await tester.drag(
find.byKey(const Key('chat-thread-s')),
const Offset(0, 360),
);
await tester.pumpAndSettle();
final saved = transcriptScroll(tester).position.pixels;
await tester.pumpWidget(host(const SizedBox()));
expect(transcripts.sessionIds, ['s']);
// The reopened read stays pending: the position is restored on the
// cached transcript alone.
gateway.history = null;
await tester.pumpWidget(thread());
await tester.pumpAndSettle();
final reopened = transcriptScroll(tester);
expect(find.byKey(const Key('chat-thread-loading')), findsNothing);
expect(reopened.position.pixels, closeTo(saved, 1));
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
// The same transcript from the server keeps the operator's position.
gateway.historyRequests.last.complete(tallPage());
await tester.pumpAndSettle();
expect(reopened.position.pixels, closeTo(saved, 1));
});
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);
});
}