@wherever-dev/client 0.5.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,180 @@
1
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
+ import {get} from 'sveltore';
3
+ import {WhereverClient} from '../src/client.js';
4
+ import {makeWSFactory} from './harness.js';
5
+
6
+ // Bug: while a session was slow to load, tapping a DIFFERENT session in the
7
+ // sidebar could result in the old, slow session suddenly replacing the one the
8
+ // user had switched to. The user is looking at session B, then session A's late
9
+ // reply lands and clobbers B.
10
+ //
11
+ // Cause: session_load's reply (session_created + message_history) was applied
12
+ // UNCONDITIONALLY. A switch to B reset the store and issued a fresh load, but
13
+ // A's in-flight replies were still on the wire; when they arrived they set
14
+ // activeSessionFile/sessionId/messages back to A.
15
+ //
16
+ // Fix: stamp pendingLoadFile on every session_load and reject a session_created
17
+ // whose sessionFile no longer matches (the latest tap wins). message_history for
18
+ // a non-active sessionId is likewise dropped, and it must not disarm the
19
+ // watchdog still guarding B's in-flight load.
20
+
21
+ function newClient(ws: ReturnType<typeof makeWSFactory>) {
22
+ const client = new WhereverClient({
23
+ host: 'localhost',
24
+ port: 1234,
25
+ secure: false,
26
+ WebSocketCtor: ws.ctor,
27
+ });
28
+ client.connect();
29
+ ws.last().open();
30
+ return client;
31
+ }
32
+
33
+ describe('a superseded (slow) session load never clobbers the switched-to session', () => {
34
+ let ws: ReturnType<typeof makeWSFactory>;
35
+ let client: WhereverClient;
36
+
37
+ beforeEach(() => {
38
+ vi.useFakeTimers();
39
+ ws = makeWSFactory();
40
+ client = newClient(ws);
41
+ });
42
+
43
+ afterEach(() => {
44
+ client.disconnect(true);
45
+ vi.useRealTimers();
46
+ });
47
+
48
+ it("drops session A's late session_created after the user switched to B", () => {
49
+ // Start loading the slow session A.
50
+ client.joinSession('/tmp/project/A.jsonl');
51
+ expect(get(client.stateStore).loadingSession).toBe(true);
52
+
53
+ // Before A replies, the user taps session B in the sidebar.
54
+ client.switchSession('/tmp/project/B.jsonl');
55
+
56
+ // B loads normally.
57
+ ws.last().receive({
58
+ type: 'session_created',
59
+ sessionId: 'sid-B',
60
+ sessionFile: '/tmp/project/B.jsonl',
61
+ cwd: '/tmp/project',
62
+ model: 'fake:model',
63
+ });
64
+ ws.last().receive({
65
+ type: 'message_history',
66
+ sessionId: 'sid-B',
67
+ messages: [{role: 'user', content: 'work on B', timestamp: 1}],
68
+ totalCount: 1,
69
+ offset: 0,
70
+ });
71
+
72
+ expect(get(client.stateStore).activeSessionFile).toBe('/tmp/project/B.jsonl');
73
+ expect(get(client.stateStore).sessionId).toBe('sid-B');
74
+
75
+ // NOW session A's slow reply finally arrives. It must be ignored: the user
76
+ // is on B, and A should not suddenly take over.
77
+ ws.last().receive({
78
+ type: 'session_created',
79
+ sessionId: 'sid-A',
80
+ sessionFile: '/tmp/project/A.jsonl',
81
+ cwd: '/tmp/project',
82
+ model: 'fake:model',
83
+ });
84
+ ws.last().receive({
85
+ type: 'message_history',
86
+ sessionId: 'sid-A',
87
+ messages: [{role: 'user', content: 'work on A', timestamp: 1}],
88
+ totalCount: 1,
89
+ offset: 0,
90
+ });
91
+
92
+ const s = get(client.stateStore);
93
+ expect(s.activeSessionFile).toBe('/tmp/project/B.jsonl');
94
+ expect(s.sessionId).toBe('sid-B');
95
+ expect(s.messages.map((m) => m.content)).toContain('work on B');
96
+ expect(s.messages.map((m) => m.content)).not.toContain('work on A');
97
+ });
98
+
99
+ it('accepts the switched-to session B normally (the happy path is unaffected)', () => {
100
+ client.joinSession('/tmp/project/A.jsonl');
101
+ client.switchSession('/tmp/project/B.jsonl');
102
+
103
+ ws.last().receive({
104
+ type: 'session_created',
105
+ sessionId: 'sid-B',
106
+ sessionFile: '/tmp/project/B.jsonl',
107
+ cwd: '/tmp/project',
108
+ model: 'fake:model',
109
+ });
110
+ ws.last().receive({
111
+ type: 'message_history',
112
+ sessionId: 'sid-B',
113
+ messages: [{role: 'user', content: 'work on B', timestamp: 1}],
114
+ totalCount: 1,
115
+ offset: 0,
116
+ });
117
+
118
+ const s = get(client.stateStore);
119
+ expect(s.activeSessionFile).toBe('/tmp/project/B.jsonl');
120
+ expect(s.loadingSession).toBe(false);
121
+ });
122
+
123
+ it("a stale session A history does not disarm the watchdog guarding B's load", () => {
124
+ client.joinSession('/tmp/project/A.jsonl');
125
+ client.switchSession('/tmp/project/B.jsonl');
126
+
127
+ // B is accepted...
128
+ ws.last().receive({
129
+ type: 'session_created',
130
+ sessionId: 'sid-B',
131
+ sessionFile: '/tmp/project/B.jsonl',
132
+ cwd: '/tmp/project',
133
+ model: 'fake:model',
134
+ });
135
+ // ...but B's own message_history has NOT arrived yet, so B is still loading
136
+ // and its load watchdog is armed. A stale A history now shows up.
137
+ ws.last().receive({
138
+ type: 'message_history',
139
+ sessionId: 'sid-A',
140
+ messages: [{role: 'user', content: 'work on A', timestamp: 1}],
141
+ totalCount: 1,
142
+ offset: 0,
143
+ });
144
+
145
+ // The stale A history must NOT have resolved B's load: still loading, and
146
+ // the watchdog is still able to fire as a last resort.
147
+ expect(get(client.stateStore).loadingSession).toBe(true);
148
+
149
+ // B's real history finally lands and resolves the load.
150
+ ws.last().receive({
151
+ type: 'message_history',
152
+ sessionId: 'sid-B',
153
+ messages: [{role: 'user', content: 'work on B', timestamp: 1}],
154
+ totalCount: 1,
155
+ offset: 0,
156
+ });
157
+ expect(get(client.stateStore).loadingSession).toBe(false);
158
+ });
159
+
160
+ it('a brand-new session (session_new) is never rejected by a stale pendingLoadFile', () => {
161
+ // A slow load is in flight...
162
+ client.joinSession('/tmp/project/A.jsonl');
163
+ // ...then the user creates a NEW session instead. pendingLoadFile must be
164
+ // cleared so the new session's created (with an unrelated sessionFile) is
165
+ // accepted.
166
+ client.createSession('/tmp/project');
167
+ ws.last().receive({
168
+ type: 'session_created',
169
+ sessionId: 'sid-N',
170
+ sessionFile: '/tmp/project/new.jsonl',
171
+ cwd: '/tmp/project',
172
+ model: 'fake:model',
173
+ });
174
+
175
+ const s = get(client.stateStore);
176
+ expect(s.activeSessionFile).toBe('/tmp/project/new.jsonl');
177
+ expect(s.sessionId).toBe('sid-N');
178
+ expect(s.creatingSession).toBe(false);
179
+ });
180
+ });