Sync Needs-you dismissals and chat read state across devices
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
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_session_local.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
|
||||
|
||||
import '../../../attention/runtime/fake_ack_server.dart';
|
||||
import 'chat_test_harness.dart';
|
||||
|
||||
JsonMap _text(String id) => {
|
||||
'type': 'chat_event',
|
||||
'chat_event': {'session_id': id, 'kind': 'text', 'delta': 'hello'},
|
||||
};
|
||||
|
||||
/// One device's chat list sharing read state through [FakeAckServer].
|
||||
class _Device {
|
||||
_Device(FakeAckServer server, {required this.clock})
|
||||
: client = server.connect(),
|
||||
local = MemoryChatSessionLocalStore() {
|
||||
sync = AttentionAckSync(gateway: client, queue: MemoryAttentionAckQueue());
|
||||
bindAttentionAckSync(client, sync);
|
||||
list = ChatSessionsController(
|
||||
ChatRepository(client),
|
||||
localStore: local,
|
||||
nowMillis: () => clock(),
|
||||
activeAckDelay: const Duration(milliseconds: 20),
|
||||
);
|
||||
}
|
||||
|
||||
final FakeAckClient client;
|
||||
final MemoryChatSessionLocalStore local;
|
||||
final int Function() clock;
|
||||
late final AttentionAckSync sync;
|
||||
late final ChatSessionsController list;
|
||||
|
||||
Future<void> start() async {
|
||||
await sync.connected();
|
||||
await list.refresh();
|
||||
await settle();
|
||||
}
|
||||
|
||||
bool isNew(String id) =>
|
||||
list.isUnseen(list.sessions.firstWhere((row) => row['id'] == id));
|
||||
|
||||
JsonMap row(String id) => list.sessions.firstWhere((row) => row['id'] == id);
|
||||
|
||||
void dispose() {
|
||||
list.dispose();
|
||||
sync.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
late FakeAckServer server;
|
||||
late _Device web;
|
||||
late _Device phone;
|
||||
var now = 5000;
|
||||
|
||||
setUp(() async {
|
||||
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},
|
||||
];
|
||||
web = _Device(server, clock: () => now);
|
||||
phone = _Device(server, clock: () => now);
|
||||
await web.start();
|
||||
await phone.start();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
web.dispose();
|
||||
phone.dispose();
|
||||
});
|
||||
|
||||
void pushToAll(JsonMap frame) {
|
||||
for (final client in server.clients) {
|
||||
client.pushed.add(frame);
|
||||
}
|
||||
}
|
||||
|
||||
test(
|
||||
'opening a chat on one device clears its New badge everywhere',
|
||||
() async {
|
||||
pushToAll(_text('ses_a'));
|
||||
await settle();
|
||||
expect(web.isNew('ses_a'), isTrue);
|
||||
expect(phone.isNew('ses_a'), isTrue);
|
||||
|
||||
now = 6000;
|
||||
await web.list.markOpened(web.row('ses_a'));
|
||||
await settle();
|
||||
|
||||
expect(server.state.chatSeen['ses_a'], 6000);
|
||||
expect(phone.isNew('ses_a'), isFalse);
|
||||
expect(phone.local.state?.watermark('ses_a'), 6000);
|
||||
// Only the opened chat is affected.
|
||||
pushToAll(_text('ses_b'));
|
||||
await settle();
|
||||
expect(phone.isNew('ses_b'), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
test('an earlier acknowledgement never clears later content', () async {
|
||||
now = 6000;
|
||||
await web.list.markOpened(web.row('ses_a'));
|
||||
web.list.activeSessionId = null;
|
||||
await settle();
|
||||
// New content after the open: the phone marks it New and a reload of the
|
||||
// same shared watermark must not clear it.
|
||||
pushToAll(_text('ses_a'));
|
||||
await settle();
|
||||
expect(phone.isNew('ses_a'), isTrue);
|
||||
await phone.sync.connected();
|
||||
await settle();
|
||||
expect(phone.isNew('ses_a'), isTrue);
|
||||
});
|
||||
|
||||
test('content arriving in a chat open elsewhere is read there', () async {
|
||||
now = 6000;
|
||||
await web.list.markOpened(web.row('ses_a'));
|
||||
await settle();
|
||||
now = 7000;
|
||||
pushToAll(_text('ses_a'));
|
||||
await settle();
|
||||
expect(phone.isNew('ses_a'), isTrue);
|
||||
expect(web.isNew('ses_a'), isFalse);
|
||||
|
||||
// The open chat acknowledges once its stream goes quiet (20 ms here).
|
||||
for (var i = 0; i < 100 && server.state.chatSeen['ses_a'] != 7000; i++) {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
||||
}
|
||||
await settle();
|
||||
expect(server.state.chatSeen['ses_a'], 7000);
|
||||
expect(phone.isNew('ses_a'), isFalse);
|
||||
});
|
||||
|
||||
test('a chat opened offline is synced and cleared on reconnect', () async {
|
||||
pushToAll(_text('ses_a'));
|
||||
await settle();
|
||||
web.client.online = false;
|
||||
now = 6000;
|
||||
await web.list.markOpened(web.row('ses_a'));
|
||||
await settle();
|
||||
expect(phone.isNew('ses_a'), isTrue);
|
||||
expect(web.sync.pending.chatSeen, {'ses_a': 6000});
|
||||
|
||||
web.client.online = true;
|
||||
await web.sync.connected();
|
||||
await settle();
|
||||
expect(phone.isNew('ses_a'), isFalse);
|
||||
|
||||
// A device that was not connected for the push applies the stored set
|
||||
// on its next connection.
|
||||
final late = _Device(server, clock: () => now);
|
||||
addTearDown(late.dispose);
|
||||
late.local.state = const ChatSessionLocal(
|
||||
seenAt: {'ses_a': 1000},
|
||||
unseen: ['ses_a'],
|
||||
);
|
||||
await late.start();
|
||||
expect(late.isNew('ses_a'), isFalse);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user