@wherever-dev/client 0.8.0 → 0.8.2
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.
- package/CHANGELOG.md +12 -0
- package/README.md +63 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.js +102 -44
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.js.map +1 -1
- package/package.json +23 -3
- package/src/client.ts +105 -49
- package/src/types.ts +3 -0
- package/test/agent-pending.test.ts +0 -107
- package/test/chunked-upload.test.ts +0 -483
- package/test/conversation-mode-flag.test.ts +0 -111
- package/test/creating-session-watchdog.test.ts +0 -82
- package/test/folder-conflict-read-only.test.ts +0 -145
- package/test/folder-missing-read-only.test.ts +0 -114
- package/test/harness.ts +0 -133
- package/test/hydration-seam.test.ts +0 -400
- package/test/liveness-probe.test.ts +0 -183
- package/test/message-delivery-confirmation.test.ts +0 -320
- package/test/mid-stream-steer.test.ts +0 -91
- package/test/new-session-clone-progress.test.ts +0 -263
- package/test/optimistic-force-command.test.ts +0 -127
- package/test/queued-steer-restored.test.ts +0 -175
- package/test/reconnect-rejoins-session.test.ts +0 -136
- package/test/restore-panel.test.ts +0 -324
- package/test/resume-keeps-session.test.ts +0 -118
- package/test/session-load-by-id.test.ts +0 -100
- package/test/session-reload.test.ts +0 -82
- package/test/skills-autocomplete.test.ts +0 -113
- package/test/steer-queue-cancel.test.ts +0 -130
- package/test/superseded-connection-key.test.ts +0 -70
- package/test/superseded-session-load.test.ts +0 -180
- package/test/tool-aborted-render.test.ts +0 -170
- package/test/tool-duration-history.test.ts +0 -93
- package/test/tool-duration.test.ts +0 -86
- package/test/tool-interrupted-history.test.ts +0 -294
- package/test/upload-survives-reconnect.test.ts +0 -482
- package/tsconfig.json +0 -16
|
@@ -1,400 +0,0 @@
|
|
|
1
|
-
import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
|
|
2
|
-
import {get} from 'sveltore';
|
|
3
|
-
import {WhereverClient, serverKey} from '../src/client.js';
|
|
4
|
-
import type {HydrationSnapshot, WhereverState} from '../src/types.js';
|
|
5
|
-
import {makeWSFactory} from './harness.js';
|
|
6
|
-
|
|
7
|
-
// The client half of instant resume (ADR 0013): a conversation read from the
|
|
8
|
-
// web's on-device cache is hydrated into the store BEFORE the socket exists, the
|
|
9
|
-
// normal rejoin replaces it with the authoritative history, a refused load
|
|
10
|
-
// evicts it, and the store is stamped with the server that delivered it so a
|
|
11
|
-
// connect to a different server never preserves or rejoins it.
|
|
12
|
-
|
|
13
|
-
function installLocalStorage() {
|
|
14
|
-
const store = new Map<string, string>();
|
|
15
|
-
(globalThis as any).localStorage = {
|
|
16
|
-
getItem: (k: string) => (store.has(k) ? store.get(k)! : null),
|
|
17
|
-
setItem: (k: string, v: string) => void store.set(k, v),
|
|
18
|
-
removeItem: (k: string) => void store.delete(k),
|
|
19
|
-
clear: () => store.clear(),
|
|
20
|
-
};
|
|
21
|
-
return store;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const FILE = '/tmp/project/session.jsonl';
|
|
25
|
-
const CFG = {host: 'localhost', port: 1234, token: 'secret', secure: false};
|
|
26
|
-
|
|
27
|
-
function snapshot(overrides: Partial<HydrationSnapshot> = {}): HydrationSnapshot {
|
|
28
|
-
return {
|
|
29
|
-
serverKey: serverKey(CFG),
|
|
30
|
-
sessionId: 'sid-1',
|
|
31
|
-
sessionFile: FILE,
|
|
32
|
-
cwd: '/tmp/project',
|
|
33
|
-
model: 'fake:model',
|
|
34
|
-
messages: [
|
|
35
|
-
{id: 'c1', role: 'user', content: 'cached question', timestamp: 1, isStreaming: false},
|
|
36
|
-
{id: 'c2', role: 'assistant', content: 'cached answer', timestamp: 2, isStreaming: false},
|
|
37
|
-
],
|
|
38
|
-
historyTotalCount: 40,
|
|
39
|
-
historyOffset: 38,
|
|
40
|
-
...overrides,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const created = {
|
|
45
|
-
type: 'session_created',
|
|
46
|
-
sessionId: 'sid-1',
|
|
47
|
-
sessionFile: FILE,
|
|
48
|
-
cwd: '/tmp/project',
|
|
49
|
-
model: 'fake:model',
|
|
50
|
-
isStreaming: false,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const history = {
|
|
54
|
-
type: 'message_history',
|
|
55
|
-
sessionId: 'sid-1',
|
|
56
|
-
messages: [
|
|
57
|
-
{role: 'user', content: 'fresh question', timestamp: 1},
|
|
58
|
-
{role: 'assistant', content: 'fresh answer', timestamp: 2},
|
|
59
|
-
],
|
|
60
|
-
totalCount: 40,
|
|
61
|
-
offset: 38,
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
describe('serverKey', () => {
|
|
65
|
-
it('is synchronous, stable and host:port prefixed', () => {
|
|
66
|
-
const a = serverKey(CFG);
|
|
67
|
-
expect(typeof a).toBe('string');
|
|
68
|
-
expect(a).toBe(serverKey({...CFG}));
|
|
69
|
-
expect(a.startsWith('localhost:1234')).toBe(true);
|
|
70
|
-
expect(a).not.toContain('secret');
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('differs by host, port and token', () => {
|
|
74
|
-
const a = serverKey(CFG);
|
|
75
|
-
expect(serverKey({...CFG, host: 'other'})).not.toBe(a);
|
|
76
|
-
expect(serverKey({...CFG, port: 4321})).not.toBe(a);
|
|
77
|
-
expect(serverKey({...CFG, token: 'other'})).not.toBe(a);
|
|
78
|
-
expect(serverKey({...CFG, token: undefined})).not.toBe(a);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('ignores an http(s):// prefix on the host, like connect()', () => {
|
|
82
|
-
expect(serverKey({...CFG, host: 'http://localhost'})).toBe(serverKey(CFG));
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe('hydrateCachedSession', () => {
|
|
87
|
-
let ws: ReturnType<typeof makeWSFactory>;
|
|
88
|
-
let client: WhereverClient;
|
|
89
|
-
const state = () => get<WhereverState>(client.stateStore);
|
|
90
|
-
|
|
91
|
-
beforeEach(() => {
|
|
92
|
-
vi.useFakeTimers();
|
|
93
|
-
installLocalStorage();
|
|
94
|
-
ws = makeWSFactory();
|
|
95
|
-
client = new WhereverClient({...CFG, WebSocketCtor: ws.ctor});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
afterEach(() => {
|
|
99
|
-
client.disconnect(true);
|
|
100
|
-
vi.useRealTimers();
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('paints the cached session as resyncing, stamped, non-authoritative', () => {
|
|
104
|
-
client.hydrateCachedSession(snapshot());
|
|
105
|
-
const s = state();
|
|
106
|
-
expect(s.session).toBe(FILE);
|
|
107
|
-
expect(s.sessionId).toBe('sid-1');
|
|
108
|
-
expect(s.activeSessionFile).toBe(FILE);
|
|
109
|
-
expect(s.activeCwd).toBe('/tmp/project');
|
|
110
|
-
expect(s.activeModel).toBe('fake:model');
|
|
111
|
-
expect(s.messages.map((m) => m.content)).toEqual(['cached question', 'cached answer']);
|
|
112
|
-
expect(s.resyncing).toBe(true);
|
|
113
|
-
expect(s.isStreaming).toBe(false);
|
|
114
|
-
expect(s.serverKey).toBe(serverKey(CFG));
|
|
115
|
-
expect(s.historyAuthoritative).toBe(false);
|
|
116
|
-
expect(s.agentPending).toBe(false);
|
|
117
|
-
expect(s.folderConflict).toBe(null);
|
|
118
|
-
expect(s.sudoPrompt).toBe(null);
|
|
119
|
-
expect(s.readOnly).toBe(false);
|
|
120
|
-
expect(s.loadingSession).toBe(false);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('then connect(cfg, true): keeps the messages, rejoins, and the history replaces them', () => {
|
|
124
|
-
client.hydrateCachedSession(snapshot());
|
|
125
|
-
client.connect(undefined, true);
|
|
126
|
-
expect(state().messages.length).toBe(2);
|
|
127
|
-
ws.last().open();
|
|
128
|
-
expect(state().messages.length).toBe(2);
|
|
129
|
-
expect(ws.last().lastSentOfType('session_load')?.sessionFile).toBe(FILE);
|
|
130
|
-
ws.last().receive(created);
|
|
131
|
-
expect(state().serverKey).toBe(serverKey(CFG));
|
|
132
|
-
ws.last().receive(history);
|
|
133
|
-
const s = state();
|
|
134
|
-
expect(s.messages.map((m) => m.content)).toEqual(['fresh question', 'fresh answer']);
|
|
135
|
-
expect(s.resyncing).toBe(false);
|
|
136
|
-
expect(s.historyAuthoritative).toBe(true);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('is a no-op after session_created', () => {
|
|
140
|
-
client.connect();
|
|
141
|
-
ws.last().open();
|
|
142
|
-
client.joinSession(FILE);
|
|
143
|
-
ws.last().receive(created);
|
|
144
|
-
const before = state();
|
|
145
|
-
client.hydrateCachedSession(snapshot({sessionId: 'other', sessionFile: '/x.jsonl'}));
|
|
146
|
-
expect(state()).toEqual(before);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('is a no-op while connected', () => {
|
|
150
|
-
client.connect();
|
|
151
|
-
ws.last().open();
|
|
152
|
-
client.hydrateCachedSession(snapshot());
|
|
153
|
-
expect(state().activeSessionFile).toBe(null);
|
|
154
|
-
expect(state().messages).toEqual([]);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('is a no-op while a load is pending', () => {
|
|
158
|
-
client.joinSession(FILE);
|
|
159
|
-
client.hydrateCachedSession(snapshot());
|
|
160
|
-
expect(state().activeSessionFile).toBe(null);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('leaves undelivered-message recovery unchanged', () => {
|
|
164
|
-
// A previous tab persisted one send that never reached the server.
|
|
165
|
-
localStorage.setItem(
|
|
166
|
-
'wherever-pending:sid-1',
|
|
167
|
-
JSON.stringify([{id: 'p1', content: 'lost send', timestamp: 3, delivery: 'failed'}]),
|
|
168
|
-
);
|
|
169
|
-
client.hydrateCachedSession(snapshot());
|
|
170
|
-
// The cached view never shows it: recovery belongs to the history reply.
|
|
171
|
-
expect(state().messages.some((m) => m.delivery !== undefined)).toBe(false);
|
|
172
|
-
client.connect(undefined, true);
|
|
173
|
-
ws.last().open();
|
|
174
|
-
ws.last().receive(created);
|
|
175
|
-
ws.last().receive(history);
|
|
176
|
-
const failed = state().messages.filter((m) => m.delivery === 'failed');
|
|
177
|
-
expect(failed.map((m) => m.content)).toEqual(['lost send']);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('drops delivery-tagged messages from the snapshot itself', () => {
|
|
181
|
-
client.hydrateCachedSession(
|
|
182
|
-
snapshot({
|
|
183
|
-
messages: [
|
|
184
|
-
{id: 'c1', role: 'user', content: 'cached', timestamp: 1, isStreaming: false},
|
|
185
|
-
{id: 'p1', role: 'user', content: 'lost send', timestamp: 3, isStreaming: false, delivery: 'failed'},
|
|
186
|
-
],
|
|
187
|
-
}),
|
|
188
|
-
);
|
|
189
|
-
expect(state().messages.map((m) => m.content)).toEqual(['cached']);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe('hydration_rejected', () => {
|
|
194
|
-
let ws: ReturnType<typeof makeWSFactory>;
|
|
195
|
-
let client: WhereverClient;
|
|
196
|
-
let seen: any[];
|
|
197
|
-
const state = () => get<WhereverState>(client.stateStore);
|
|
198
|
-
|
|
199
|
-
beforeEach(() => {
|
|
200
|
-
vi.useFakeTimers();
|
|
201
|
-
installLocalStorage();
|
|
202
|
-
ws = makeWSFactory();
|
|
203
|
-
client = new WhereverClient({...CFG, WebSocketCtor: ws.ctor});
|
|
204
|
-
seen = [];
|
|
205
|
-
client.onMessage((m) => seen.push(m));
|
|
206
|
-
client.hydrateCachedSession(snapshot());
|
|
207
|
-
client.connect(undefined, true);
|
|
208
|
-
ws.last().open();
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
afterEach(() => {
|
|
212
|
-
client.disconnect(true);
|
|
213
|
-
vi.useRealTimers();
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
const rejected = () => seen.filter((m) => m.type === 'hydration_rejected');
|
|
217
|
-
|
|
218
|
-
it('an untagged session_error answering the hydrated load emits it and clears the session', () => {
|
|
219
|
-
ws.last().receive({type: 'session_error', error: 'Session not found'});
|
|
220
|
-
expect(rejected()).toEqual([{type: 'hydration_rejected', sessionId: 'sid-1'}]);
|
|
221
|
-
const s = state();
|
|
222
|
-
expect(s.activeSessionFile).toBe(null);
|
|
223
|
-
expect(s.sessionId).toBe(null);
|
|
224
|
-
expect(s.session).toBe(null);
|
|
225
|
-
expect(s.messages).toEqual([]);
|
|
226
|
-
expect(s.resyncing).toBe(false);
|
|
227
|
-
expect(s.serverKey).toBe(null);
|
|
228
|
-
expect(s.sessionError).toBe('Session not found');
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it('a tagged session_error does not', () => {
|
|
232
|
-
ws.last().receive({type: 'session_error', sessionId: 'sid-1', error: 'boom'});
|
|
233
|
-
expect(rejected()).toEqual([]);
|
|
234
|
-
expect(state().activeSessionFile).toBe(FILE);
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it('folder_missing after session_created does not', () => {
|
|
238
|
-
ws.last().receive({...created, folderMissing: true});
|
|
239
|
-
ws.last().receive({type: 'folder_missing', sessionId: 'sid-1', cwd: '/tmp/project'});
|
|
240
|
-
ws.last().receive(history);
|
|
241
|
-
expect(rejected()).toEqual([]);
|
|
242
|
-
expect(state().activeSessionFile).toBe(FILE);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it('a cold-build session_error after session_created does not', () => {
|
|
246
|
-
ws.last().receive({...created, pending: true});
|
|
247
|
-
ws.last().receive(history);
|
|
248
|
-
ws.last().receive({type: 'session_error', error: 'agent build failed'});
|
|
249
|
-
expect(rejected()).toEqual([]);
|
|
250
|
-
const s = state();
|
|
251
|
-
expect(s.activeSessionFile).toBe(FILE);
|
|
252
|
-
expect(s.messages.length).toBe(2);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
it('an untagged session_error for a later, different load does not', () => {
|
|
256
|
-
ws.last().receive(created);
|
|
257
|
-
ws.last().receive(history);
|
|
258
|
-
client.switchSession('/tmp/other.jsonl');
|
|
259
|
-
ws.last().receive({type: 'session_error', error: 'Session not found'});
|
|
260
|
-
expect(rejected()).toEqual([]);
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
describe('stamped server key on reconnect', () => {
|
|
265
|
-
let ws: ReturnType<typeof makeWSFactory>;
|
|
266
|
-
let client: WhereverClient;
|
|
267
|
-
const state = () => get<WhereverState>(client.stateStore);
|
|
268
|
-
|
|
269
|
-
beforeEach(() => {
|
|
270
|
-
vi.useFakeTimers();
|
|
271
|
-
installLocalStorage();
|
|
272
|
-
ws = makeWSFactory();
|
|
273
|
-
client = new WhereverClient({...CFG, WebSocketCtor: ws.ctor});
|
|
274
|
-
client.connect();
|
|
275
|
-
ws.last().open();
|
|
276
|
-
client.joinSession(FILE);
|
|
277
|
-
ws.last().receive(created);
|
|
278
|
-
ws.last().receive(history);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
afterEach(() => {
|
|
282
|
-
client.disconnect(true);
|
|
283
|
-
vi.useRealTimers();
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
it('session_created stamps the key of the socket that delivered it', () => {
|
|
287
|
-
expect(state().serverKey).toBe(serverKey(CFG));
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
it('an automatic reconnect to the same server preserves and rejoins', () => {
|
|
291
|
-
ws.last().close();
|
|
292
|
-
vi.advanceTimersByTime(30_000);
|
|
293
|
-
expect(state().activeSessionFile).toBe(FILE);
|
|
294
|
-
ws.last().open();
|
|
295
|
-
expect(ws.last().lastSentOfType('session_load')?.sessionFile).toBe(FILE);
|
|
296
|
-
expect(state().historyAuthoritative).toBe(false);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
it('an automatic reconnect after host/port were edited wipes, clears the stamp, and does not rejoin', () => {
|
|
300
|
-
client.setConfig({host: 'elsewhere', port: 9999});
|
|
301
|
-
ws.last().close();
|
|
302
|
-
vi.advanceTimersByTime(30_000);
|
|
303
|
-
const s = state();
|
|
304
|
-
expect(s.activeSessionFile).toBe(null);
|
|
305
|
-
expect(s.messages).toEqual([]);
|
|
306
|
-
expect(s.serverKey).toBe(null);
|
|
307
|
-
ws.last().open();
|
|
308
|
-
expect(ws.last().lastSentOfType('session_load')).toBeUndefined();
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
it('suspend -> resume against a dead socket -> edit host -> automatic reconnect sends no session_load', () => {
|
|
312
|
-
client.suspend();
|
|
313
|
-
expect(client.hasSuspendedSession()).toBe(true);
|
|
314
|
-
client.resume();
|
|
315
|
-
// The resumed socket never opens: it dies.
|
|
316
|
-
ws.last().close();
|
|
317
|
-
client.setConfig({host: 'elsewhere'});
|
|
318
|
-
vi.advanceTimersByTime(30_000);
|
|
319
|
-
expect(client.hasSuspendedSession()).toBe(false);
|
|
320
|
-
expect(state().serverKey).toBe(null);
|
|
321
|
-
ws.last().open();
|
|
322
|
-
expect(ws.last().lastSentOfType('session_load')).toBeUndefined();
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
it('an explicit connect with a new config also refuses to preserve', () => {
|
|
326
|
-
client.connect({token: 'rotated'}, true);
|
|
327
|
-
expect(state().activeSessionFile).toBe(null);
|
|
328
|
-
ws.last().open();
|
|
329
|
-
expect(ws.last().lastSentOfType('session_load')).toBeUndefined();
|
|
330
|
-
});
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
describe('historyAuthoritative gates loadMoreHistory', () => {
|
|
334
|
-
let ws: ReturnType<typeof makeWSFactory>;
|
|
335
|
-
let client: WhereverClient;
|
|
336
|
-
const state = () => get<WhereverState>(client.stateStore);
|
|
337
|
-
|
|
338
|
-
beforeEach(() => {
|
|
339
|
-
vi.useFakeTimers();
|
|
340
|
-
installLocalStorage();
|
|
341
|
-
ws = makeWSFactory();
|
|
342
|
-
client = new WhereverClient({...CFG, WebSocketCtor: ws.ctor});
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
afterEach(() => {
|
|
346
|
-
client.disconnect(true);
|
|
347
|
-
vi.useRealTimers();
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it('starts false', () => {
|
|
351
|
-
expect(state().historyAuthoritative).toBe(false);
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
it('loadMoreHistory is a no-op while false and works once the history lands', () => {
|
|
355
|
-
client.hydrateCachedSession(snapshot());
|
|
356
|
-
client.connect(undefined, true);
|
|
357
|
-
ws.last().open();
|
|
358
|
-
ws.last().receive(created);
|
|
359
|
-
client.loadMoreHistory();
|
|
360
|
-
expect(ws.last().lastSentOfType('history_load_more')).toBeUndefined();
|
|
361
|
-
expect(state().loadingMoreHistory).toBe(false);
|
|
362
|
-
ws.last().receive(history);
|
|
363
|
-
client.loadMoreHistory();
|
|
364
|
-
expect(ws.last().lastSentOfType('history_load_more')).toMatchObject({
|
|
365
|
-
sessionId: 'sid-1',
|
|
366
|
-
beforeOffset: 38,
|
|
367
|
-
});
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
it('a stale history for another session does not make it authoritative', () => {
|
|
371
|
-
client.hydrateCachedSession(snapshot());
|
|
372
|
-
client.connect(undefined, true);
|
|
373
|
-
ws.last().open();
|
|
374
|
-
ws.last().receive(created);
|
|
375
|
-
ws.last().receive({...history, sessionId: 'other'});
|
|
376
|
-
expect(state().historyAuthoritative).toBe(false);
|
|
377
|
-
});
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
describe('resume() with hydrated data', () => {
|
|
381
|
-
it('raises no timeout error while the socket is stuck CONNECTING', () => {
|
|
382
|
-
vi.useFakeTimers();
|
|
383
|
-
installLocalStorage();
|
|
384
|
-
const ws = makeWSFactory();
|
|
385
|
-
const client = new WhereverClient({...CFG, WebSocketCtor: ws.ctor});
|
|
386
|
-
try {
|
|
387
|
-
client.hydrateCachedSession(snapshot());
|
|
388
|
-
client.resume();
|
|
389
|
-
expect(ws.instances.length).toBe(1);
|
|
390
|
-
vi.advanceTimersByTime(60_000);
|
|
391
|
-
const s = get<WhereverState>(client.stateStore);
|
|
392
|
-
expect(s.sessionError).toBe(null);
|
|
393
|
-
expect(s.messages.length).toBe(2);
|
|
394
|
-
expect(s.activeSessionFile).toBe(FILE);
|
|
395
|
-
} finally {
|
|
396
|
-
client.disconnect(true);
|
|
397
|
-
vi.useRealTimers();
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
});
|
|
@@ -1,183 +0,0 @@
|
|
|
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
|
-
// ADR 0011: the client never tears a socket down on silence alone. When a
|
|
7
|
-
// backgrounded page thaws (most often after the file picker / camera), timers
|
|
8
|
-
// fire BEFORE the frames queued during the freeze are dispatched, so a raw
|
|
9
|
-
// "silent for 45/60 s" verdict is routinely false. Past the threshold the
|
|
10
|
-
// watchdog now pings and waits a short grace period; only a socket that stays
|
|
11
|
-
// silent through it is torn down. The page also triggers the probe on every
|
|
12
|
-
// return to visibility via noteVisible().
|
|
13
|
-
|
|
14
|
-
const pings = (sock: {sent: any[]}) => sock.sent.filter((m) => m.type === 'ping').length;
|
|
15
|
-
|
|
16
|
-
describe('liveness watchdog probes before tearing a socket down', () => {
|
|
17
|
-
let ws: ReturnType<typeof makeWSFactory>;
|
|
18
|
-
let client: WhereverClient;
|
|
19
|
-
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
vi.useFakeTimers();
|
|
22
|
-
ws = makeWSFactory();
|
|
23
|
-
client = new WhereverClient({
|
|
24
|
-
host: 'localhost',
|
|
25
|
-
port: 1234,
|
|
26
|
-
secure: false,
|
|
27
|
-
WebSocketCtor: ws.ctor,
|
|
28
|
-
});
|
|
29
|
-
client.connect();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
afterEach(() => {
|
|
33
|
-
client.disconnect(true);
|
|
34
|
-
vi.useRealTimers();
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// A freeze: wall-clock time passes but no timer and no frame runs.
|
|
38
|
-
function freeze(ms: number) {
|
|
39
|
-
vi.setSystemTime(Date.now() + ms);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
it('a thaw (timers fire before queued frames) does not tear down a healthy socket', () => {
|
|
43
|
-
const sock = ws.last();
|
|
44
|
-
sock.open();
|
|
45
|
-
freeze(120_000);
|
|
46
|
-
// The first tick after the thaw sees 120 s of "silence": it probes.
|
|
47
|
-
vi.advanceTimersByTime(25_000);
|
|
48
|
-
expect(pings(sock)).toBeGreaterThan(0);
|
|
49
|
-
expect(sock.readyState).toBe(1);
|
|
50
|
-
// Then the frames queued during the freeze are dispatched.
|
|
51
|
-
sock.receive({type: 'pong', timestamp: Date.now()});
|
|
52
|
-
vi.advanceTimersByTime(10_000);
|
|
53
|
-
expect(ws.instances).toHaveLength(1);
|
|
54
|
-
expect(sock.readyState).toBe(1);
|
|
55
|
-
expect(get(client.stateStore).connected).toBe(true);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('after a thaw-like jump, noteVisible() probes at once and a reply keeps the socket', () => {
|
|
59
|
-
const sock = ws.last();
|
|
60
|
-
sock.open();
|
|
61
|
-
freeze(120_000);
|
|
62
|
-
const before = pings(sock);
|
|
63
|
-
client.noteVisible();
|
|
64
|
-
expect(pings(sock)).toBe(before + 1);
|
|
65
|
-
sock.receive({type: 'pong', timestamp: Date.now()});
|
|
66
|
-
vi.advanceTimersByTime(30_000);
|
|
67
|
-
expect(ws.instances).toHaveLength(1);
|
|
68
|
-
expect(sock.readyState).toBe(1);
|
|
69
|
-
expect(get(client.stateStore).connected).toBe(true);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('after a thaw-like jump, noteVisible() with NO reply tears down after the grace and reconnects', () => {
|
|
73
|
-
const sock = ws.last();
|
|
74
|
-
sock.open();
|
|
75
|
-
freeze(120_000);
|
|
76
|
-
client.noteVisible();
|
|
77
|
-
vi.advanceTimersByTime(4_000);
|
|
78
|
-
expect(sock.readyState).toBe(1);
|
|
79
|
-
vi.advanceTimersByTime(1_000);
|
|
80
|
-
expect(sock.readyState).toBe(3);
|
|
81
|
-
expect(get(client.stateStore).connected).toBe(false);
|
|
82
|
-
vi.advanceTimersByTime(2_000);
|
|
83
|
-
expect(ws.instances).toHaveLength(2);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('noteVisible() resets the stale baseline: the frozen time alone does not count as silence', () => {
|
|
87
|
-
const sock = ws.last();
|
|
88
|
-
sock.open();
|
|
89
|
-
freeze(120_000);
|
|
90
|
-
client.noteVisible();
|
|
91
|
-
sock.receive({type: 'pong', timestamp: Date.now()});
|
|
92
|
-
// The next tick must not re-probe for the frozen 120 s.
|
|
93
|
-
const before = pings(sock);
|
|
94
|
-
vi.advanceTimersByTime(25_000);
|
|
95
|
-
// Only the ordinary heartbeat ping goes out, no extra probe ping.
|
|
96
|
-
expect(pings(sock) - before).toBe(1);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('an ordinary idle stale socket is still detected and torn down (one grace later)', () => {
|
|
100
|
-
const sock = ws.last();
|
|
101
|
-
sock.open();
|
|
102
|
-
// Ticks at 25/50/75 s: 75 s of silence crosses STALE_SOCKET_MS (60 s).
|
|
103
|
-
vi.advanceTimersByTime(75_000);
|
|
104
|
-
expect(sock.readyState).toBe(1);
|
|
105
|
-
vi.advanceTimersByTime(4_999);
|
|
106
|
-
expect(sock.readyState).toBe(1);
|
|
107
|
-
vi.advanceTimersByTime(1);
|
|
108
|
-
expect(sock.readyState).toBe(3);
|
|
109
|
-
expect(get(client.stateStore).connected).toBe(false);
|
|
110
|
-
vi.advanceTimersByTime(2_000);
|
|
111
|
-
expect(ws.instances).toHaveLength(2);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('a frame arriving during the grace period cancels the teardown', () => {
|
|
115
|
-
const sock = ws.last();
|
|
116
|
-
sock.open();
|
|
117
|
-
vi.advanceTimersByTime(75_000); // probe sent
|
|
118
|
-
vi.advanceTimersByTime(3_000);
|
|
119
|
-
sock.receive({type: 'pong', timestamp: Date.now()});
|
|
120
|
-
vi.advanceTimersByTime(10_000);
|
|
121
|
-
expect(sock.readyState).toBe(1);
|
|
122
|
-
expect(ws.instances).toHaveLength(1);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it('a real mid-turn stall is still surfaced as "Connection to relay stalled"', () => {
|
|
126
|
-
const sock = ws.last();
|
|
127
|
-
sock.open();
|
|
128
|
-
sock.receive({type: 'agent_start'});
|
|
129
|
-
expect(get(client.stateStore).isStreaming).toBe(true);
|
|
130
|
-
// Ticks at 25/50 s: 50 s of silence crosses TURN_STALL_MS (45 s).
|
|
131
|
-
vi.advanceTimersByTime(50_000 + 5_000);
|
|
132
|
-
expect(sock.readyState).toBe(3);
|
|
133
|
-
const s = get(client.stateStore);
|
|
134
|
-
expect(s.sessionError).toMatch(/Connection to relay stalled/);
|
|
135
|
-
expect(s.isStreaming).toBe(false);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it('a grace check delayed by a freeze re-probes instead of tearing down', () => {
|
|
139
|
-
const sock = ws.last();
|
|
140
|
-
sock.open();
|
|
141
|
-
vi.advanceTimersByTime(75_000); // probe sent
|
|
142
|
-
const before = pings(sock);
|
|
143
|
-
freeze(60_000); // the page freezes during the grace period
|
|
144
|
-
vi.advanceTimersByTime(5_000); // the grace check fires, very late
|
|
145
|
-
expect(sock.readyState).toBe(1);
|
|
146
|
-
expect(pings(sock)).toBe(before + 1);
|
|
147
|
-
sock.receive({type: 'pong', timestamp: Date.now()});
|
|
148
|
-
vi.advanceTimersByTime(10_000);
|
|
149
|
-
expect(sock.readyState).toBe(1);
|
|
150
|
-
expect(ws.instances).toHaveLength(1);
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('noteVisible() on a CONNECTING socket does nothing', () => {
|
|
154
|
-
const sock = ws.last();
|
|
155
|
-
expect(sock.readyState).toBe(0);
|
|
156
|
-
client.noteVisible();
|
|
157
|
-
expect(sock.sent).toHaveLength(0);
|
|
158
|
-
vi.advanceTimersByTime(10_000);
|
|
159
|
-
expect(sock.readyState).toBe(0);
|
|
160
|
-
expect(ws.instances).toHaveLength(1);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('noteVisible() with no socket at all does nothing', () => {
|
|
164
|
-
client.disconnect(false);
|
|
165
|
-
expect(() => client.noteVisible()).not.toThrow();
|
|
166
|
-
vi.advanceTimersByTime(10_000);
|
|
167
|
-
expect(ws.instances).toHaveLength(1);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('a pending probe is cancelled when the socket changes', () => {
|
|
171
|
-
const first = ws.last();
|
|
172
|
-
first.open();
|
|
173
|
-
client.noteVisible(); // probe pending on the first socket
|
|
174
|
-
first.close(); // dropped before the grace check
|
|
175
|
-
vi.advanceTimersByTime(2_000); // reconnect
|
|
176
|
-
const second = ws.last();
|
|
177
|
-
expect(second).not.toBe(first);
|
|
178
|
-
second.open();
|
|
179
|
-
vi.advanceTimersByTime(10_000); // well past the first socket's grace
|
|
180
|
-
expect(second.readyState).toBe(1);
|
|
181
|
-
expect(ws.instances).toHaveLength(2);
|
|
182
|
-
});
|
|
183
|
-
});
|