Mark chats seen after user interaction

Prevent passive open, restored, or unfocused chats from clearing notifications. Track qualifying focused interactions and cover passive and active seen-watermark behavior with tests and notes.
This commit is contained in:
2026-11-12 02:25:09 +00:00
parent e4f3ebdcab
commit 746e23788d
10 changed files with 1013 additions and 65 deletions
@@ -0,0 +1,220 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/servers/server_catalog.dart';
import 'package:pipelines_app/core/servers/server_catalog_store.dart';
import 'package:pipelines_app/core/servers/server_context.dart';
import 'package:pipelines_app/features/chat/conversation/chat_seen_interaction.dart';
import 'package:pipelines_app/features/settings/server_connections.dart';
/// One open chat's pane: a field and a long transcript, both inside the region.
Widget _pane({
required VoidCallback onInteract,
ScrollController? scroll,
FocusNode? focus,
}) => MaterialApp(
home: Scaffold(
body: ChatSeenInteraction(
sessionId: 'ses_a',
onInteract: onInteract,
child: Column(
children: [
TextField(focusNode: focus),
Expanded(
child: ListView.builder(
controller: scroll,
itemCount: 200,
itemBuilder: (context, index) =>
SizedBox(height: 40, child: Text('row $index')),
),
),
],
),
),
),
);
void main() {
testWidgets('showing the pane reports nothing', (tester) async {
var count = 0;
await tester.pumpWidget(_pane(onInteract: () => count++));
await tester.pump();
await tester.pump(const Duration(seconds: 2));
expect(count, 0);
});
testWidgets('a programmatic scroll reports nothing', (tester) async {
var count = 0;
final scroll = ScrollController();
addTearDown(scroll.dispose);
await tester.pumpWidget(_pane(onInteract: () => count++, scroll: scroll));
scroll.jumpTo(600);
await tester.pump();
expect(count, 0);
});
testWidgets('a click or tap in the pane reports once per press', (
tester,
) async {
var count = 0;
await tester.pumpWidget(_pane(onInteract: () => count++));
await tester.tap(find.text('row 3'));
await tester.pump();
expect(count, greaterThanOrEqualTo(1));
});
testWidgets('a user drag of the transcript reports', (tester) async {
var count = 0;
await tester.pumpWidget(_pane(onInteract: () => count++));
await tester.drag(find.text('row 0'), const Offset(0, -300));
await tester.pumpAndSettle();
expect(count, greaterThanOrEqualTo(1));
});
testWidgets('a mouse-wheel scroll of the transcript reports', (tester) async {
var count = 0;
final scroll = ScrollController();
addTearDown(scroll.dispose);
await tester.pumpWidget(_pane(onInteract: () => count++, scroll: scroll));
final pointer = TestPointer(1, PointerDeviceKind.mouse);
await tester.sendEventToBinding(
pointer.hover(tester.getCenter(find.text('row 0'))),
);
await tester.sendEventToBinding(pointer.scroll(const Offset(0, 200)));
await tester.pumpAndSettle();
expect(scroll.offset, greaterThan(0));
expect(count, greaterThanOrEqualTo(1));
});
testWidgets('a key press while focus is in the pane reports, with no click', (
tester,
) async {
var count = 0;
final focus = FocusNode();
addTearDown(focus.dispose);
await tester.pumpWidget(_pane(onInteract: () => count++, focus: focus));
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
await tester.pump();
expect(count, 0, reason: 'a key with no focus in the pane is not its input');
focus.requestFocus();
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.keyA);
await tester.pump();
expect(count, 1);
});
testWidgets('code-driven focus into the pane reports nothing', (tester) async {
var count = 0;
final focus = FocusNode();
addTearDown(focus.dispose);
await tester.pumpWidget(_pane(onInteract: () => count++, focus: focus));
focus.requestFocus();
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(focus.hasFocus, isTrue);
expect(count, 0, reason: 'focus moved in code, with no key press, is passive');
});
testWidgets('Tab from outside the pane into it reports once', (tester) async {
var count = 0;
final outside = FocusNode();
final focus = FocusNode();
addTearDown(outside.dispose);
addTearDown(focus.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Column(
children: [
TextField(focusNode: outside),
Expanded(
child: ChatSeenInteraction(
sessionId: 'ses_a',
onInteract: () => count++,
child: Column(
children: [
TextField(focusNode: focus),
const Expanded(child: SizedBox()),
],
),
),
),
],
),
),
),
);
outside.requestFocus();
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pump();
expect(focus.hasFocus, isTrue, reason: 'Tab moved focus into the pane');
expect(count, 1);
});
testWidgets('a notification tap that opened this chat reports once', (
tester,
) async {
var count = 0;
final connections = ServerConnections(
store: MemoryServerCatalogStore(
catalog: ServerCatalog(
servers: [
SavedServer.validated(
id: 'mac',
name: 'Mac',
endpoints: const ['ws://mac.test/connect'],
),
],
selectedId: 'mac',
),
),
onSelected: (_) async {},
);
addTearDown(connections.dispose);
await connections.load();
await tester.pumpWidget(
ServerContext(
connections: connections,
serverId: 'mac',
child: _pane(onInteract: () => count++),
),
);
await tester.pump();
expect(count, 0);
// Another chat's notification is not this pane's open.
connections.requestChatOpen('ses_other');
await tester.pump();
await tester.pump();
expect(count, 0);
connections.requestChatOpen('ses_a');
await tester.pump();
await tester.pump();
expect(count, 1);
// The tap is consumed: a later rebuild does not report it again.
connections.requestChatOpen('ses_other');
await tester.pump();
await tester.pump();
expect(count, 1);
});
}
@@ -0,0 +1,376 @@
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/attention/runtime/attention_ack_queue.dart';
import 'package:pipelines_app/features/attention/runtime/attention_ack_sync.dart';
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_seen_focus.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_screen.dart';
import '../../../attention/runtime/fake_ack_server.dart';
import 'chat_test_harness.dart';
/// A chat reply in [id], as the server pushes it while the chat is live.
JsonMap _reply(String id) => {
'type': 'chat_event',
'chat_event': {'session_id': id, 'kind': 'text', 'delta': 'hello'},
};
/// One device's chat list, its shared acknowledgements and its window focus.
class _Device {
_Device(this.server, {required this.clock, bool focused = true})
: client = server.connect(),
local = MemoryChatSessionLocalStore(),
focus = ChatSeenFocus(focused: focused) {
sync = AttentionAckSync(gateway: client, queue: MemoryAttentionAckQueue());
bindAttentionAckSync(client, sync);
list = ChatSessionsController(
ChatRepository(client),
localStore: local,
nowMillis: () => clock(),
seenFocus: focus,
// Short enough to test quiet periods, long enough to settle under load.
activeAckDelay: const Duration(milliseconds: 60),
);
}
final FakeAckServer server;
final FakeAckClient client;
final MemoryChatSessionLocalStore local;
final ChatSeenFocus focus;
final int Function() clock;
late final AttentionAckSync sync;
late final ChatSessionsController list;
Future<void> start() async {
await sync.connected();
await list.refresh();
await settle();
}
JsonMap row(String id) => list.sessions.firstWhere((row) => row['id'] == id);
/// Whether [id] shows a New badge in the list (an open chat never does).
bool isNew(String id) => list.isUnseen(row(id));
/// Whether this device holds [id] as unread, whether or not it is open.
bool holdsUnread(String id) => local.state?.isUnseen(id) ?? false;
void dispose() {
list.dispose();
sync.dispose();
focus.dispose();
}
}
/// Lets a pending quiet-period acknowledgement fire (60 ms here) and settle.
Future<void> quietPeriod() async {
await Future<void>.delayed(const Duration(milliseconds: 200));
await settle();
}
void main() {
late FakeAckServer server;
var now = 5000;
setUp(() {
now = 5000;
server = FakeAckServer()
..chatSessions = [
{'id': 'ses_a', 'title': 'A', 'directory': '/w', 'updated_at': 1000},
{'id': 'ses_b', 'title': 'B', 'directory': '/w', 'updated_at': 1000},
];
});
void pushToAll(JsonMap frame) {
for (final client in server.clients) {
client.pushed.add(frame);
}
}
group('passive states never mark a chat seen', () {
test('a reply in an unfocused window stays New', () async {
final device = _Device(server, clock: () => now, focused: false);
addTearDown(device.dispose);
await device.start();
// Restored as the open chat, then the window is in the background.
device.list.activeSessionId = 'ses_a';
pushToAll(_reply('ses_a'));
await quietPeriod();
expect(server.state.chatSeen, isEmpty);
expect(device.holdsUnread('ses_a'), isTrue);
device.list.activeSessionId = null;
expect(device.isNew('ses_a'), isTrue);
});
test('a restored open chat with no interaction stays New', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
// A layout restored on start sets the open chat without any open.
device.list.activeSessionId = 'ses_a';
await settle();
pushToAll(_reply('ses_a'));
await quietPeriod();
expect(server.state.chatSeen, isEmpty);
expect(device.holdsUnread('ses_a'), isTrue);
});
testWidgets(
'a layout restored with a chat open, then a reply, leaves it New',
(tester) async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await tester.runAsync(device.start);
// The list screen is shown with the restored chat as its open conversation.
await tester.pumpWidget(
MaterialApp(
home: ChatSessionsScreen(
gateway: device.client,
onOpen: (_) {},
controller: device.list,
activeSessionId: 'ses_a',
localStore: device.local,
),
),
);
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)),
);
await tester.pump();
await tester.runAsync(() async {
pushToAll(_reply('ses_a'));
await quietPeriod();
});
await tester.pump();
expect(server.state.chatSeen, isEmpty);
expect(device.holdsUnread('ses_a'), isTrue);
},
);
test('an open chat left idle long after a reply stays New', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
await settle();
pushToAll(_reply('ses_a'));
now += 600000; // Ten minutes later, nothing touched it.
await quietPeriod();
expect(server.state.chatSeen, isEmpty);
expect(device.holdsUnread('ses_a'), isTrue);
});
test('a reply after backgrounding an engaged chat stays New, even '
'once the window is focused again without interaction', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
device.list.reportInteraction('ses_a');
await settle();
expect(server.state.chatSeen['ses_a'], 5000);
device.focus.focused = false;
now = 6000;
pushToAll(_reply('ses_a'));
await quietPeriod();
expect(server.state.chatSeen['ses_a'], 5000);
device.focus.focused = true;
now = 7000;
pushToAll(_reply('ses_a'));
await quietPeriod();
expect(server.state.chatSeen['ses_a'], 5000);
expect(device.holdsUnread('ses_a'), isTrue);
});
test('a reply in another chat while this one is open stays New', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
device.list.reportInteraction('ses_a');
await settle();
now = 6000;
pushToAll(_reply('ses_b'));
await quietPeriod();
expect(server.state.chatSeen.containsKey('ses_b'), isFalse);
expect(device.isNew('ses_b'), isTrue);
});
test('a reply after leaving an engaged chat stays New', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
device.list.reportInteraction('ses_a');
await settle();
device.list.activeSessionId = 'ses_b';
now = 6000;
pushToAll(_reply('ses_a'));
await quietPeriod();
expect(server.state.chatSeen['ses_a'], 5000);
expect(device.isNew('ses_a'), isTrue);
});
test('a reply still in its quiet period when the window loses focus '
'stays New, not seen', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
device.list.reportInteraction('ses_a');
await settle();
expect(server.state.chatSeen['ses_a'], 5000);
now = 6000;
pushToAll(_reply('ses_a'));
await settle(); // The reply is held in its quiet period, not yet acked.
device.focus.focused = false;
await quietPeriod();
expect(server.state.chatSeen['ses_a'], 5000);
expect(device.holdsUnread('ses_a'), isTrue);
});
test('a reply still in its quiet period when the user opens another '
'chat stays New, not seen', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
device.list.reportInteraction('ses_a');
await settle();
now = 6000;
pushToAll(_reply('ses_a'));
await settle();
device.list.activeSessionId = 'ses_b';
await quietPeriod();
expect(server.state.chatSeen['ses_a'], 5000);
expect(device.isNew('ses_a'), isTrue);
});
test('an interaction while the window is unfocused is ignored', () async {
final device = _Device(server, clock: () => now, focused: false);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
device.list.reportInteraction('ses_a');
await quietPeriod();
expect(server.state.chatSeen, isEmpty);
});
});
group('user interactions mark the chat seen', () {
test(
'an interaction in an unread open chat marks it seen up to now',
() async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
pushToAll(_reply('ses_a'));
await settle();
expect(device.holdsUnread('ses_a'), isTrue);
now = 6000;
device.list.reportInteraction('ses_a');
await settle();
expect(server.state.chatSeen['ses_a'], 6000);
expect(device.holdsUnread('ses_a'), isFalse);
expect(device.sync.pending.chatSeen, isEmpty);
},
);
test('opening a chat from the list marks it seen', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
pushToAll(_reply('ses_a'));
await settle();
now = 6000;
await device.list.markOpened(device.row('ses_a'));
await settle();
expect(server.state.chatSeen['ses_a'], 6000);
expect(device.holdsUnread('ses_a'), isFalse);
});
test(
'a reply after an interaction is marked seen once the chat is quiet',
() async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
device.list.reportInteraction('ses_a');
await settle();
now = 6000;
pushToAll(_reply('ses_a'));
await quietPeriod();
expect(server.state.chatSeen['ses_a'], 6000);
expect(device.holdsUnread('ses_a'), isFalse);
},
);
test(
'repeated interactions in an engaged, read chat send nothing more',
() async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
device.list.reportInteraction('ses_a');
await settle();
final sent = device.client.count('ack_attention');
// Scroll ticks and clicks after the first engagement.
for (var i = 0; i < 5; i++) {
device.list.reportInteraction('ses_a');
}
await settle();
expect(device.client.count('ack_attention'), sent);
},
);
test('an interaction in a visible chat that is not the open one marks it '
'seen', () async {
final device = _Device(server, clock: () => now);
addTearDown(device.dispose);
await device.start();
device.list.activeSessionId = 'ses_a';
pushToAll(_reply('ses_b'));
await settle();
expect(device.holdsUnread('ses_b'), isTrue);
now = 6000;
device.list.reportInteraction('ses_b');
await settle();
expect(server.state.chatSeen['ses_b'], 6000);
expect(device.holdsUnread('ses_b'), isFalse);
});
});
}