feat(app): follow streamed chat and restore its read position

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.
This commit is contained in:
2026-10-22 17:15:14 +01:00
parent cf6da4acaa
commit e4c196d75b
5 changed files with 1063 additions and 142 deletions
@@ -0,0 +1,142 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_scroll.dart';
void main() {
group('follow geometry', () {
test('a transcript with no content has no distance or follow state', () {
expect(
chatBottomDistance(maxScrollExtent: 0, pixels: 0, itemCount: 0),
isNull,
);
expect(chatFollowsAt(null, itemCount: 0), isFalse);
});
test('the distance never goes negative past the end', () {
expect(
chatBottomDistance(maxScrollExtent: 800, pixels: 300, itemCount: 3),
500,
);
expect(
chatBottomDistance(maxScrollExtent: 800, pixels: 900, itemCount: 3),
0,
);
});
test('the follow threshold includes its boundary and needs an item', () {
expect(chatFollowsAt(0, itemCount: 3), isTrue);
expect(chatFollowsAt(32, itemCount: 3), isTrue);
expect(chatFollowsAt(33, itemCount: 3), isFalse);
expect(chatFollowsAt(null, itemCount: 3), isFalse);
expect(chatFollowsAt(0, itemCount: 0), isFalse);
});
});
group('jump control', () {
test('is offered only when not following with content below the fold', () {
expect(
chatShowsJumpToLatest(
following: false,
distanceFromBottom: 400,
itemCount: 5,
),
isTrue,
);
expect(
chatShowsJumpToLatest(
following: true,
distanceFromBottom: 400,
itemCount: 5,
),
isFalse,
);
// The end is in view, so there is nothing to return to.
expect(
chatShowsJumpToLatest(
following: false,
distanceFromBottom: 0,
itemCount: 5,
),
isFalse,
);
expect(
chatShowsJumpToLatest(
following: false,
distanceFromBottom: 400,
itemCount: 0,
),
isFalse,
);
});
});
group('arrival count', () {
test('counts only messages after the release point', () {
expect(
chatNewMessageCount(['m1', 'm2', 'm3'], 'm2'),
1,
);
expect(
chatNewMessageCount(['older', 'm1', 'm2', 'live', 'newer'], 'm2'),
2,
);
// A growing delta keeps the same id and is not a new message.
expect(chatNewMessageCount(['m1', 'm2'], 'm2'), 0);
});
test('reports nothing without a release point or a surviving message', () {
expect(chatNewMessageCount(['m1'], null), 0);
expect(chatNewMessageCount(['m1'], 'replaced'), 0);
expect(chatNewMessageCount(const [], 'm1'), 0);
});
test('the label keeps the action and states the singular/plural count', () {
expect(chatJumpToLatestLabel(0), 'Jump to latest');
expect(chatJumpToLatestLabel(1), 'Jump to latest · 1 new message');
expect(chatJumpToLatestLabel(2), 'Jump to latest · 2 new messages');
});
});
group('restore', () {
test('no saved position lands on the newest end', () {
expect(
chatRestoreOffset(savedOffset: null, maxScrollExtent: 640),
640,
);
});
test('a saved position is kept within the current extent', () {
expect(
chatRestoreOffset(savedOffset: 120, maxScrollExtent: 640),
120,
);
expect(chatRestoreOffset(savedOffset: 0, maxScrollExtent: 640), 0);
expect(
chatRestoreOffset(savedOffset: 900, maxScrollExtent: 640),
640,
);
});
});
group('store', () {
test('remembers one conversation without touching another', () {
final store = MemoryChatScrollStore();
store.write('a', 120);
store.write('b', 30);
expect(store.read('a'), 120);
expect(store.read('b'), 30);
expect(store.read('c'), isNull);
store.clear('a');
expect(store.read('a'), isNull);
expect(store.entries, {'b': 30.0});
});
test('ignores an unusable offset instead of inventing a position', () {
final store = MemoryChatScrollStore();
store.write('a', double.nan);
store.write('a', double.infinity);
store.write('a', -5);
expect(store.read('a'), isNull);
});
});
}
@@ -0,0 +1,284 @@
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);
});
}