Unattended test: build the subscription the current way
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -143,6 +143,10 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
/// Another device changed a chat's shared colour, star or title: the row
|
||||
/// takes the server's values at once, with no re-list. Each field the
|
||||
/// registry row omits is cleared, since omitted means unset on the wire.
|
||||
/// The server sends these pushes concurrently, so they can arrive out of
|
||||
/// order: one whose `revision` is older than the row held here is stale and
|
||||
/// ignored (a row or push without a revision, from an older server, is
|
||||
/// always applied).
|
||||
///
|
||||
/// A row naming `previous_id` is a chat another device moved to a fresh
|
||||
/// session (`/new`): it replaces that row in place and [onSessionReplaced]
|
||||
@@ -165,8 +169,14 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
if ('${row['id'] ?? ''}' == id)
|
||||
() {
|
||||
found = true;
|
||||
if (chatSessionPushIsStale(row, changed)) return row;
|
||||
final next = <String, dynamic>{...row};
|
||||
for (final key in const ['color', 'starred', 'starred_at']) {
|
||||
for (final key in const [
|
||||
'color',
|
||||
'starred',
|
||||
'starred_at',
|
||||
'revision',
|
||||
]) {
|
||||
if (changed.containsKey(key)) {
|
||||
next[key] = changed[key];
|
||||
} else {
|
||||
@@ -404,8 +414,14 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
final runningRevision = _runningRevision;
|
||||
final sessions = await repository.sessions();
|
||||
if (generation != _generation) return;
|
||||
// A push applied while this list was read can be newer than its row.
|
||||
final held = {for (final row in _sessions) '${row['id'] ?? ''}': row};
|
||||
_sessions = [
|
||||
for (final row in sessions) _withActivity(row, runningRevision),
|
||||
for (final row in sessions)
|
||||
_withActivity(
|
||||
newerChatSessionMetadata(row, held['${row['id'] ?? ''}']),
|
||||
runningRevision,
|
||||
),
|
||||
];
|
||||
_seedLocal();
|
||||
_loading = false;
|
||||
@@ -644,3 +660,41 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// The registry revision a chat list row or push carries; 0 when absent.
|
||||
int chatSessionRevision(Map<dynamic, dynamic> row) {
|
||||
final value = row['revision'];
|
||||
return value is num ? value.toInt() : 0;
|
||||
}
|
||||
|
||||
/// Whether [pushed] is older than [held]: both carry a revision and the
|
||||
/// push's is lower. A push is never stale against a row with no revision.
|
||||
bool chatSessionPushIsStale(
|
||||
Map<dynamic, dynamic> held,
|
||||
Map<dynamic, dynamic> pushed,
|
||||
) {
|
||||
final incoming = chatSessionRevision(pushed);
|
||||
return incoming > 0 && incoming < chatSessionRevision(held);
|
||||
}
|
||||
|
||||
/// [fetched] with the shared metadata (colour, star, title, revision) of
|
||||
/// [held] when [held] is newer: a list read that started before a push was
|
||||
/// applied must not revert it.
|
||||
JsonMap newerChatSessionMetadata(JsonMap fetched, JsonMap? held) {
|
||||
if (held == null || !chatSessionPushIsStale(held, fetched)) return fetched;
|
||||
final next = <String, dynamic>{...fetched};
|
||||
for (final key in const [
|
||||
'color',
|
||||
'starred',
|
||||
'starred_at',
|
||||
'title',
|
||||
'revision',
|
||||
]) {
|
||||
if (held.containsKey(key)) {
|
||||
next[key] = held[key];
|
||||
} else {
|
||||
next.remove(key);
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
@@ -266,6 +266,45 @@ void main() {
|
||||
expect(gateway.count('chat_sessions'), lists);
|
||||
});
|
||||
|
||||
test('a push older than the row held is ignored', () async {
|
||||
JsonMap push(int revision, String color) => {
|
||||
'type': 'chat_session_changed',
|
||||
'chat_session': {
|
||||
'id': 's2',
|
||||
'title': 'Plain chat',
|
||||
'color': color,
|
||||
'revision': revision,
|
||||
},
|
||||
};
|
||||
// The server sends pushes concurrently: revision 7 lands before 6.
|
||||
gateway.pushed.add(push(7, 'pink'));
|
||||
gateway.pushed.add(push(6, 'blue'));
|
||||
await pumpEventQueue();
|
||||
expect(row('s2')['color'], 'pink');
|
||||
expect(row('s2')['revision'], 7);
|
||||
gateway.pushed.add(push(8, 'amber'));
|
||||
await pumpEventQueue();
|
||||
expect(row('s2')['color'], 'amber');
|
||||
// A list read older than the applied push does not revert it.
|
||||
var listed = <String, dynamic>{
|
||||
'id': 's2',
|
||||
'title': 'Plain chat',
|
||||
'directory': '/w',
|
||||
'revision': 5,
|
||||
};
|
||||
gateway.sessionsHandler = () async => {
|
||||
'chat_sessions': [listed],
|
||||
};
|
||||
await controller.refresh();
|
||||
expect(row('s2')['color'], 'amber');
|
||||
expect(row('s2')['revision'], 8);
|
||||
// A newer list row wins.
|
||||
listed = {...listed, 'color': 'teal', 'revision': 9};
|
||||
await controller.refresh();
|
||||
expect(row('s2')['color'], 'teal');
|
||||
expect(row('s2')['revision'], 9);
|
||||
});
|
||||
|
||||
test('a refused colour is undone and the reason shown', () async {
|
||||
gateway.failures['chat_color'] = 'unknown chat colour';
|
||||
await controller.setColor('s2', 'blue');
|
||||
|
||||
Reference in New Issue
Block a user