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,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);
});
});
}