fix(app): live New marks clear the active chat on leave; lint and regression tests
This commit is contained in:
@@ -128,6 +128,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
syncToken: widget.syncToken,
|
||||
onOpen: _open,
|
||||
onSessions: _adoptRows,
|
||||
activeSessionId: selected,
|
||||
),
|
||||
),
|
||||
if (selected != null)
|
||||
|
||||
@@ -55,8 +55,9 @@ class ChatSessionLocal {
|
||||
ChatSessionLocal read(String id, int updatedAt) {
|
||||
final nextUnseen = unseen.where((entry) => entry != id).toList();
|
||||
if (id.isEmpty ||
|
||||
(updatedAt <= watermark(id) && nextUnseen.length == unseen.length))
|
||||
(updatedAt <= watermark(id) && nextUnseen.length == unseen.length)) {
|
||||
return this;
|
||||
}
|
||||
return ChatSessionLocal(
|
||||
hidden: hidden,
|
||||
seenAt: updatedAt > watermark(id) ? {...seenAt, id: updatedAt} : seenAt,
|
||||
|
||||
@@ -23,6 +23,7 @@ class ChatSessionsScreen extends StatefulWidget {
|
||||
this.syncToken = 0,
|
||||
this.onSessions,
|
||||
this.localStore,
|
||||
this.activeSessionId,
|
||||
});
|
||||
final ApiGateway gateway;
|
||||
final void Function(JsonMap session) onOpen;
|
||||
@@ -39,6 +40,10 @@ class ChatSessionsScreen extends StatefulWidget {
|
||||
/// title and model facts as a tapped row without a second list request.
|
||||
final ValueChanged<List<JsonMap>>? onSessions;
|
||||
|
||||
/// The conversation open above this list, or null when the list is showing.
|
||||
/// Pushed activity for it is being read, so it never raises a New mark.
|
||||
final String? activeSessionId;
|
||||
|
||||
/// Overrides the device-local hidden/read store in tests.
|
||||
final ChatSessionLocalStore? localStore;
|
||||
|
||||
@@ -47,10 +52,13 @@ class ChatSessionsScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
late final ChatSessionsController _controller = ChatSessionsController(
|
||||
ChatRepository(widget.gateway),
|
||||
localStore: widget.localStore,
|
||||
)..refresh();
|
||||
late final ChatSessionsController _controller =
|
||||
ChatSessionsController(
|
||||
ChatRepository(widget.gateway),
|
||||
localStore: widget.localStore,
|
||||
)
|
||||
..activeSessionId = widget.activeSessionId
|
||||
..refresh();
|
||||
final _search = TextEditingController();
|
||||
Timer? _undoTimer;
|
||||
|
||||
@@ -73,6 +81,7 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
@override
|
||||
void didUpdateWidget(covariant ChatSessionsScreen oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_controller.activeSessionId = widget.activeSessionId;
|
||||
if (oldWidget.gateway != widget.gateway) {
|
||||
_controller.replaceRepository(ChatRepository(widget.gateway));
|
||||
} else if (oldWidget.syncToken != widget.syncToken) {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
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/ui/sessions/chat_sessions_screen.dart';
|
||||
|
||||
import 'chat_test_harness.dart';
|
||||
import 'fake_chat_gateway.dart';
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
FakeChatGateway gateway, {
|
||||
String? active,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
home: ChatSessionsScreen(
|
||||
gateway: gateway,
|
||||
onOpen: (_) {},
|
||||
localStore: _store,
|
||||
activeSessionId: active,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
late MemoryChatSessionLocalStore _store;
|
||||
|
||||
JsonMap _event(String id, {String kind = 'text'}) => {
|
||||
'type': 'chat_event',
|
||||
'chat_event': {'session_id': id, 'kind': kind},
|
||||
};
|
||||
|
||||
void main() {
|
||||
setUp(() => _store = MemoryChatSessionLocalStore());
|
||||
|
||||
FakeChatGateway gateway() => FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [
|
||||
{'id': 's1', 'title': 'First', 'directory': '/w', 'updated_at': 1000},
|
||||
],
|
||||
};
|
||||
|
||||
testWidgets('a pushed event marks a background chat New without a refresh', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = gateway();
|
||||
await _pump(tester, fake);
|
||||
expect(find.text('New'), findsNothing);
|
||||
|
||||
fake.pushed.add(_event('s1'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('New'), findsOneWidget);
|
||||
expect(fake.count('chat_sessions'), 1);
|
||||
|
||||
await tester.tap(find.byKey(const Key('chat-row-s1')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('New'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('usage frames and the open chat never raise New', (tester) async {
|
||||
final fake = gateway();
|
||||
await _pump(tester, fake, active: 's1');
|
||||
fake.pushed.add(_event('s1'));
|
||||
fake.pushed.add(_event('s1', kind: 'usage'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('New'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('leaving the conversation lets later activity mark it New', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = gateway();
|
||||
await _pump(tester, fake);
|
||||
await tester.tap(find.byKey(const Key('chat-row-s1')));
|
||||
await _pump(tester, fake, active: 's1');
|
||||
await _pump(tester, fake);
|
||||
|
||||
fake.pushed.add({
|
||||
'type': 'chat_permission',
|
||||
'chat_permission': {'session_id': 's1'},
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('New'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -19,8 +19,11 @@ class FakeChatGateway implements ApiGateway {
|
||||
Future<JsonMap> Function()? directoriesHandler;
|
||||
Future<JsonMap> Function()? backendsHandler;
|
||||
|
||||
/// Server-pushed frames; tests add `chat_event` or `chat_permission` here.
|
||||
final pushed = StreamController<JsonMap>.broadcast();
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
Stream<JsonMap> get frames => pushed.stream;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
|
||||
Reference in New Issue
Block a user