@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.
Files changed (39) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +63 -0
  3. package/dist/client.d.ts +5 -0
  4. package/dist/client.js +102 -44
  5. package/dist/client.js.map +1 -1
  6. package/dist/types.d.ts +1 -0
  7. package/dist/types.js.map +1 -1
  8. package/package.json +23 -3
  9. package/src/client.ts +105 -49
  10. package/src/types.ts +3 -0
  11. package/test/agent-pending.test.ts +0 -107
  12. package/test/chunked-upload.test.ts +0 -483
  13. package/test/conversation-mode-flag.test.ts +0 -111
  14. package/test/creating-session-watchdog.test.ts +0 -82
  15. package/test/folder-conflict-read-only.test.ts +0 -145
  16. package/test/folder-missing-read-only.test.ts +0 -114
  17. package/test/harness.ts +0 -133
  18. package/test/hydration-seam.test.ts +0 -400
  19. package/test/liveness-probe.test.ts +0 -183
  20. package/test/message-delivery-confirmation.test.ts +0 -320
  21. package/test/mid-stream-steer.test.ts +0 -91
  22. package/test/new-session-clone-progress.test.ts +0 -263
  23. package/test/optimistic-force-command.test.ts +0 -127
  24. package/test/queued-steer-restored.test.ts +0 -175
  25. package/test/reconnect-rejoins-session.test.ts +0 -136
  26. package/test/restore-panel.test.ts +0 -324
  27. package/test/resume-keeps-session.test.ts +0 -118
  28. package/test/session-load-by-id.test.ts +0 -100
  29. package/test/session-reload.test.ts +0 -82
  30. package/test/skills-autocomplete.test.ts +0 -113
  31. package/test/steer-queue-cancel.test.ts +0 -130
  32. package/test/superseded-connection-key.test.ts +0 -70
  33. package/test/superseded-session-load.test.ts +0 -180
  34. package/test/tool-aborted-render.test.ts +0 -170
  35. package/test/tool-duration-history.test.ts +0 -93
  36. package/test/tool-duration.test.ts +0 -86
  37. package/test/tool-interrupted-history.test.ts +0 -294
  38. package/test/upload-survives-reconnect.test.ts +0 -482
  39. package/tsconfig.json +0 -16
@@ -1,483 +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 type {UploadProgress} from '../src/types.js';
5
- import {makeWSFactory, type FakeWebSocket} from './harness.js';
6
-
7
- // Resumable chunked uploads (upload.v1, docs/adr/0012): the file goes out as
8
- // binary chunks, progress comes from the server's acks, a dropped socket pauses
9
- // the upload, and the rejoin resumes it from the server's offset.
10
-
11
- const KiB = 1024;
12
- const MiB = 1024 * KiB;
13
-
14
- function makeSource(size: number) {
15
- const data = new Uint8Array(size);
16
- for (let i = 0; i < size; i++) data[i] = i % 251;
17
- const reads: Array<[number, number]> = [];
18
- return {
19
- data,
20
- reads,
21
- source: {
22
- size,
23
- read: async (offset: number, length: number) => {
24
- reads.push([offset, length]);
25
- return data.slice(offset, offset + length);
26
- },
27
- },
28
- };
29
- }
30
-
31
- // Byte-for-byte equality without vitest's slow per-element deep diff.
32
- function sameBytes(a: Uint8Array, b: Uint8Array) {
33
- return Buffer.from(a.buffer, a.byteOffset, a.byteLength).equals(Buffer.from(b.buffer, b.byteOffset, b.byteLength));
34
- }
35
-
36
- // Let the async byte-source reads (and the sends after them) run.
37
- async function flush() {
38
- for (let i = 0; i < 50; i++) await Promise.resolve();
39
- }
40
-
41
- const SESSION = {
42
- type: 'session_created',
43
- sessionId: 'sid-1',
44
- sessionFile: '/tmp/project/session.jsonl',
45
- cwd: '/tmp/project',
46
- model: 'fake:model',
47
- };
48
-
49
- describe('chunked uploads (upload.v1)', () => {
50
- let ws: ReturnType<typeof makeWSFactory>;
51
- let client: WhereverClient;
52
-
53
- beforeEach(() => {
54
- vi.useFakeTimers();
55
- ws = makeWSFactory();
56
- client = new WhereverClient({
57
- host: 'localhost',
58
- port: 1234,
59
- secure: false,
60
- WebSocketCtor: ws.ctor,
61
- });
62
- client.connect();
63
- });
64
-
65
- afterEach(() => {
66
- client.disconnect(true);
67
- vi.useRealTimers();
68
- });
69
-
70
- function connected(sock: FakeWebSocket, capabilities?: string[]) {
71
- sock.receive({type: 'connected', clientId: 'c1', serverVersion: 'x', capabilities});
72
- }
73
-
74
- function openAndJoin() {
75
- const sock = ws.last();
76
- sock.open();
77
- connected(sock, ['upload.v1']);
78
- client.joinSession('/tmp/project/session.jsonl');
79
- sock.receive(SESSION);
80
- sock.receive({type: 'message_history', sessionId: 'sid-1', messages: [], totalCount: 0, offset: 0});
81
- return sock;
82
- }
83
-
84
- // `null`: an older server, whose `connected` carries no capabilities.
85
- function dropAndReconnect(capabilities: string[] | null = ['upload.v1']) {
86
- const first = ws.last();
87
- first.close();
88
- vi.advanceTimersByTime(5000);
89
- const second = ws.last();
90
- expect(second).not.toBe(first);
91
- second.open();
92
- connected(second, capabilities ?? undefined);
93
- return second;
94
- }
95
-
96
- // Ack every chunk this socket has sent beyond `fromIndex`, as a server would.
97
- async function ackChunks(sock: FakeWebSocket, fromIndex: number) {
98
- let i = fromIndex;
99
- while (i < sock.binary.length) {
100
- const c = sock.binary[i++];
101
- sock.receive({type: 'upload_offset', uploadId: c.uploadId, offset: c.offset + c.payload.length});
102
- await flush();
103
- }
104
- return i;
105
- }
106
-
107
- async function ackUntilFinish(sock: FakeWebSocket, fromIndex = 0) {
108
- let i = fromIndex;
109
- for (let guard = 0; guard < 1000 && !sock.lastSentOfType('upload_finish'); guard++) {
110
- i = await ackChunks(sock, i);
111
- if (i >= sock.binary.length && !sock.lastSentOfType('upload_finish')) await flush();
112
- }
113
- return i;
114
- }
115
-
116
- function bytesOf(sock: FakeWebSocket) {
117
- return sock.binary.map((c) => [c.offset, c.payload.length]);
118
- }
119
-
120
- it('uploads in binary chunks; progress reflects acked bytes only', async () => {
121
- const sock = openAndJoin();
122
- const {data, source} = makeSource(600 * KiB);
123
- const progress: UploadProgress[] = [];
124
- const p = client.uploadFileViaWebSocket('sid-1', 'clip.mp4', source, {
125
- mime: 'video/mp4',
126
- onProgress: (pr) => progress.push(pr),
127
- });
128
- const begin = sock.lastSentOfType('upload_begin');
129
- expect(begin).toMatchObject({sessionId: 'sid-1', filename: 'clip.mp4', size: 600 * KiB, mime: 'video/mp4'});
130
- expect(begin.uploadId).toMatch(/^[A-Za-z0-9_-]{22,64}$/);
131
- expect(sock.lastSentOfType('file_upload')).toBeUndefined();
132
- expect(progress.at(-1)).toMatchObject({state: 'sending', acked: 0, size: 600 * KiB});
133
- // Nothing goes out before the server says where to start.
134
- await flush();
135
- expect(sock.binary).toHaveLength(0);
136
-
137
- sock.receive({type: 'upload_offset', uploadId: begin.uploadId, offset: 0});
138
- await flush();
139
- expect(sock.binary.length).toBeGreaterThan(0);
140
- const first = sock.binary[0];
141
- expect(first).toMatchObject({kind: 1, version: 1, uploadId: begin.uploadId, offset: 0});
142
- // Sent but not acked: progress has not moved.
143
- expect(progress.at(-1)).toMatchObject({acked: 0});
144
- expect(get(client.uploadsStore)[begin.uploadId]).toMatchObject({acked: 0, state: 'sending'});
145
-
146
- sock.receive({type: 'upload_offset', uploadId: begin.uploadId, offset: first.payload.length});
147
- await flush();
148
- expect(progress.at(-1)).toMatchObject({acked: first.payload.length, state: 'sending'});
149
-
150
- await ackUntilFinish(sock, 1);
151
- const finish = sock.lastSentOfType('upload_finish');
152
- expect(finish).toMatchObject({uploadId: begin.uploadId, sessionId: 'sid-1'});
153
- expect(progress.at(-1)).toMatchObject({acked: 600 * KiB, state: 'finishing'});
154
-
155
- // The bytes on the wire are exactly the file, in order, each byte once.
156
- const joined = new Uint8Array(600 * KiB);
157
- let at = 0;
158
- for (const c of sock.binary) {
159
- expect(c.offset).toBe(at);
160
- joined.set(c.payload, c.offset);
161
- at += c.payload.length;
162
- }
163
- expect(at).toBe(600 * KiB);
164
- expect(sameBytes(joined, data)).toBe(true);
165
- // Every acked value reported was one the server sent (monotonic).
166
- const acks = progress.map((pr) => pr.acked);
167
- expect([...acks].sort((a, b) => a - b)).toEqual(acks);
168
-
169
- sock.receive({type: 'file_uploaded', uploadId: begin.uploadId, sessionId: 'sid-1', filename: 'clip.mp4', savedPath: '/up/1_clip.mp4'});
170
- await expect(p).resolves.toEqual({savedPath: '/up/1_clip.mp4', filename: 'clip.mp4'});
171
- expect(progress.at(-1)).toMatchObject({acked: 600 * KiB, state: 'done'});
172
- expect(get(client.uploadsStore)[begin.uploadId]).toMatchObject({state: 'done'});
173
- expect(client.pendingUploadCount()).toBe(0);
174
- });
175
-
176
- it('paces by bytes in flight: 64 KiB at first, at most 256 KiB chunks and 1 MiB in flight', async () => {
177
- const sock = openAndJoin();
178
- const {source} = makeSource(4 * MiB);
179
- void client.uploadFileViaWebSocket('sid-1', 'big.bin', source).catch(() => {});
180
- const {uploadId} = sock.lastSentOfType('upload_begin');
181
- sock.receive({type: 'upload_offset', uploadId, offset: 0});
182
- await flush();
183
- // No throughput measured yet: the minimum window, one 64 KiB chunk.
184
- expect(bytesOf(sock)).toEqual([[0, 64 * KiB]]);
185
-
186
- // A fast ack (no time elapsed): the window opens to its 1 MiB maximum,
187
- // filled with 256 KiB chunks.
188
- sock.receive({type: 'upload_offset', uploadId, offset: 64 * KiB});
189
- await flush();
190
- const after = sock.binary.slice(1);
191
- expect(after.length).toBeGreaterThan(1);
192
- for (const c of after) expect(c.payload.length).toBeLessThanOrEqual(256 * KiB);
193
- const inFlight = after.reduce((n, c) => n + c.payload.length, 0);
194
- expect(inFlight).toBeLessThanOrEqual(1 * MiB);
195
- expect(inFlight).toBeGreaterThan(512 * KiB);
196
- });
197
-
198
- it('a slow link keeps the window at its 64 KiB floor', async () => {
199
- const sock = openAndJoin();
200
- const {source} = makeSource(1 * MiB);
201
- void client.uploadFileViaWebSocket('sid-1', 'slow.bin', source).catch(() => {});
202
- const {uploadId} = sock.lastSentOfType('upload_begin');
203
- sock.receive({type: 'upload_offset', uploadId, offset: 0});
204
- await flush();
205
- for (let i = 0; i < 4; i++) {
206
- // 64 KiB every 4 s: 16 KiB/s, so 2 s worth is far below the floor.
207
- // Pongs keep the liveness watchdog satisfied meanwhile.
208
- sock.receive({type: 'pong', timestamp: i});
209
- vi.advanceTimersByTime(4000);
210
- const c = sock.binary.at(-1)!;
211
- sock.receive({type: 'upload_offset', uploadId, offset: c.offset + c.payload.length});
212
- await flush();
213
- }
214
- for (const c of sock.binary) expect(c.payload.length).toBe(64 * KiB);
215
- expect(sock.binary).toHaveLength(5);
216
- });
217
-
218
- it('a drop mid-upload pauses it; the rejoin resumes from the server offset without re-sending acked bytes', async () => {
219
- openAndJoin();
220
- const {data, source} = makeSource(1 * MiB);
221
- const progress: UploadProgress[] = [];
222
- const p = client.uploadFileViaWebSocket('sid-1', 'r.bin', source, {onProgress: (pr) => progress.push(pr)});
223
- const first = ws.last();
224
- const {uploadId} = first.lastSentOfType('upload_begin');
225
- first.receive({type: 'upload_offset', uploadId, offset: 0});
226
- await flush();
227
- // Ack the first chunk only, then lose the socket with more in flight.
228
- const c0 = first.binary[0];
229
- first.receive({type: 'upload_offset', uploadId, offset: c0.payload.length});
230
- await flush();
231
- const acked = c0.payload.length;
232
-
233
- const second = dropAndReconnect();
234
- expect(progress.at(-1)).toMatchObject({state: 'paused', acked});
235
- // Held until the rejoin is answered: session_load first, no begin yet.
236
- expect(second.lastSentOfType('session_load')).toBeTruthy();
237
- expect(second.lastSentOfType('upload_begin')).toBeUndefined();
238
-
239
- second.receive(SESSION);
240
- const begin = second.lastSentOfType('upload_begin');
241
- expect(begin).toMatchObject({uploadId, sessionId: 'sid-1', size: 1 * MiB, filename: 'r.bin'});
242
- expect(second.binary).toHaveLength(0);
243
-
244
- // The server kept exactly the acked bytes.
245
- second.receive({type: 'upload_offset', uploadId, offset: acked});
246
- await flush();
247
- expect(second.binary[0].offset).toBe(acked);
248
- await ackUntilFinish(second);
249
- for (const c of second.binary) expect(c.offset).toBeGreaterThanOrEqual(acked);
250
- const joined = new Uint8Array(1 * MiB);
251
- joined.set(data.subarray(0, acked));
252
- for (const c of second.binary) joined.set(c.payload, c.offset);
253
- expect(sameBytes(joined, data)).toBe(true);
254
-
255
- second.receive({type: 'file_uploaded', uploadId, sessionId: 'sid-1', filename: 'r.bin', savedPath: '/up/r.bin'});
256
- await expect(p).resolves.toMatchObject({savedPath: '/up/r.bin'});
257
- });
258
-
259
- it('an offset mismatch realigns: in-flight acks drain, then it continues from the true offset', async () => {
260
- const sock = openAndJoin();
261
- const {source} = makeSource(2 * MiB);
262
- void client.uploadFileViaWebSocket('sid-1', 'm.bin', source).catch(() => {});
263
- const {uploadId} = sock.lastSentOfType('upload_begin');
264
- sock.receive({type: 'upload_offset', uploadId, offset: 0});
265
- await flush();
266
- sock.receive({type: 'upload_offset', uploadId, offset: 64 * KiB});
267
- await flush();
268
- const inFlight = sock.binary.slice(1);
269
- expect(inFlight.length).toBeGreaterThan(1);
270
- const sentBefore = sock.binary.length;
271
-
272
- // The server's true offset is NOT where the first in-flight chunk began
273
- // (e.g. it lost a write): it answers with its offset, 32 KiB.
274
- sock.receive({type: 'upload_offset', uploadId, offset: 32 * KiB});
275
- await flush();
276
- // Nothing new until the other in-flight chunks are answered too.
277
- expect(sock.binary).toHaveLength(sentBefore);
278
- for (let i = 1; i < inFlight.length; i++) {
279
- sock.receive({type: 'upload_offset', uploadId, offset: 32 * KiB});
280
- await flush();
281
- if (i < inFlight.length - 1) expect(sock.binary).toHaveLength(sentBefore);
282
- }
283
- expect(sock.binary.length).toBeGreaterThan(sentBefore);
284
- expect(sock.binary[sentBefore].offset).toBe(32 * KiB);
285
- expect(get(client.uploadsStore)[uploadId]).toMatchObject({acked: 32 * KiB});
286
- });
287
-
288
- it('done: true on the resume begin resolves without re-sending anything', async () => {
289
- openAndJoin();
290
- const {source} = makeSource(100 * KiB);
291
- const p = client.uploadFileViaWebSocket('sid-1', 'd.png', source);
292
- const first = ws.last();
293
- const {uploadId} = first.lastSentOfType('upload_begin');
294
- first.receive({type: 'upload_offset', uploadId, offset: 0});
295
- await flush();
296
- await ackUntilFinish(first);
297
- expect(first.lastSentOfType('upload_finish')).toBeTruthy();
298
- // file_uploaded is lost with the socket.
299
- const second = dropAndReconnect();
300
- second.receive(SESSION);
301
- expect(second.lastSentOfType('upload_begin')).toMatchObject({uploadId});
302
- second.receive({type: 'upload_offset', uploadId, offset: 100 * KiB, done: true, savedPath: '/up/d.png'});
303
- await expect(p).resolves.toEqual({savedPath: '/up/d.png', filename: 'd.png'});
304
- await flush();
305
- expect(second.binary).toHaveLength(0);
306
- expect(second.lastSentOfType('upload_finish')).toBeUndefined();
307
- expect(get(client.uploadsStore)[uploadId]).toMatchObject({state: 'done', acked: 100 * KiB});
308
- });
309
-
310
- it('an empty file finishes right after the begin', async () => {
311
- const sock = openAndJoin();
312
- const {source} = makeSource(0);
313
- const p = client.uploadFileViaWebSocket('sid-1', 'empty.txt', source);
314
- const {uploadId} = sock.lastSentOfType('upload_begin');
315
- sock.receive({type: 'upload_offset', uploadId, offset: 0});
316
- await flush();
317
- expect(sock.binary).toHaveLength(0);
318
- expect(sock.lastSentOfType('upload_finish')).toMatchObject({uploadId});
319
- sock.receive({type: 'file_uploaded', uploadId, sessionId: 'sid-1', filename: 'empty.txt', savedPath: '/up/e'});
320
- await expect(p).resolves.toMatchObject({savedPath: '/up/e'});
321
- });
322
-
323
- it('cancel sends upload_abort for an in-flight chunked upload and stops sending', async () => {
324
- const sock = openAndJoin();
325
- const {source} = makeSource(1 * MiB);
326
- const controller = new AbortController();
327
- const progress: UploadProgress[] = [];
328
- const p = client.uploadFileViaWebSocket('sid-1', 'c.bin', source, {
329
- signal: controller.signal,
330
- onProgress: (pr) => progress.push(pr),
331
- });
332
- const settled = expect(p).rejects.toThrow(/cancelled/);
333
- const {uploadId} = sock.lastSentOfType('upload_begin');
334
- sock.receive({type: 'upload_offset', uploadId, offset: 0});
335
- await flush();
336
- controller.abort();
337
- await settled;
338
- expect(sock.lastSentOfType('upload_abort')).toEqual({type: 'upload_abort', uploadId, sessionId: 'sid-1'});
339
- expect(progress.at(-1)).toMatchObject({state: 'aborted'});
340
- const sent = sock.binary.length;
341
- sock.receive({type: 'upload_offset', uploadId, offset: 64 * KiB});
342
- await flush();
343
- expect(sock.binary).toHaveLength(sent);
344
- expect(client.pendingUploadCount()).toBe(0);
345
- });
346
-
347
- it('a NotReadableError fails the upload with the "pick it again" message and code', async () => {
348
- const sock = openAndJoin();
349
- const progress: UploadProgress[] = [];
350
- const source = {
351
- size: 10 * KiB,
352
- read: async () => {
353
- const err = new Error('The requested file could not be read');
354
- err.name = 'NotReadableError';
355
- throw err;
356
- },
357
- };
358
- const p = client.uploadFileViaWebSocket('sid-1', 'gone.jpg', source, {onProgress: (pr) => progress.push(pr)});
359
- const settled = p.catch((e) => e);
360
- const {uploadId} = sock.lastSentOfType('upload_begin');
361
- sock.receive({type: 'upload_offset', uploadId, offset: 0});
362
- const err = await settled;
363
- expect(err.message).toMatch(/no longer readable, pick it again/);
364
- expect(err.code).toBe('not_readable');
365
- expect(progress.at(-1)).toMatchObject({
366
- state: 'failed',
367
- error: {message: expect.stringMatching(/pick it again/), code: 'not_readable'},
368
- });
369
- expect(get(client.uploadsStore)[uploadId].error?.code).toBe('not_readable');
370
- });
371
-
372
- it('a server error fails the chunked upload with its reason', async () => {
373
- const sock = openAndJoin();
374
- const {source} = makeSource(10 * KiB);
375
- const p = client.uploadFileViaWebSocket('sid-1', 'x.bin', source);
376
- const {uploadId} = sock.lastSentOfType('upload_begin');
377
- sock.receive({type: 'file_upload_error', uploadId, sessionId: 'sid-1', error: 'read-only'});
378
- await expect(p).rejects.toThrow('read-only');
379
- expect(get(client.uploadsStore)[uploadId]).toMatchObject({state: 'failed', error: {message: 'read-only'}});
380
- });
381
-
382
- it('an upload is held while the socket capabilities are unknown, never sent as legacy', async () => {
383
- const sock = ws.last();
384
- sock.open();
385
- const {source} = makeSource(10 * KiB);
386
- void client.uploadFileViaWebSocket('sid-1', 'h.bin', source).catch(() => {});
387
- await flush();
388
- expect(sock.lastSentOfType('file_upload')).toBeUndefined();
389
- expect(sock.lastSentOfType('upload_begin')).toBeUndefined();
390
- connected(sock, ['upload.v1']);
391
- expect(sock.lastSentOfType('upload_begin')).toMatchObject({filename: 'h.bin'});
392
- });
393
-
394
- it('a server without upload.v1 gets the legacy base64 frame', async () => {
395
- const sock = ws.last();
396
- sock.open();
397
- connected(sock);
398
- const {data, source} = makeSource(5 * KiB);
399
- void client.uploadFileViaWebSocket('sid-1', 'l.bin', source).catch(() => {});
400
- await flush();
401
- expect(sock.lastSentOfType('upload_begin')).toBeUndefined();
402
- const frame = sock.lastSentOfType('file_upload');
403
- expect(frame).toMatchObject({sessionId: 'sid-1', filename: 'l.bin'});
404
- expect(sameBytes(Buffer.from(frame.data, 'base64'), data)).toBe(true);
405
- });
406
-
407
- it('legacy base64 is encoded across reads with no padding mid-stream', async () => {
408
- const sock = ws.last();
409
- sock.open();
410
- connected(sock);
411
- // Larger than one encode chunk, and not a multiple of 3.
412
- const {data, source, reads} = makeSource(2 * MiB + 1);
413
- void client.uploadFileViaWebSocket('sid-1', 'l2.bin', source).catch(() => {});
414
- await flush();
415
- expect(reads.length).toBeGreaterThan(1);
416
- for (const [, len] of reads.slice(0, -1)) expect(len % 3).toBe(0);
417
- const frame = sock.lastSentOfType('file_upload');
418
- expect(frame.data.slice(0, -4).includes('=')).toBe(false);
419
- expect(sameBytes(Buffer.from(frame.data, 'base64'), data)).toBe(true);
420
- });
421
-
422
- it('a paused chunked upload that reconnects to a server without upload.v1 fails with the retry message', async () => {
423
- openAndJoin();
424
- const {source} = makeSource(1 * MiB);
425
- const p = client.uploadFileViaWebSocket('sid-1', 'g.bin', source);
426
- const first = ws.last();
427
- const {uploadId} = first.lastSentOfType('upload_begin');
428
- first.receive({type: 'upload_offset', uploadId, offset: 0});
429
- await flush();
430
- const settled = p.catch((e) => e);
431
- const second = dropAndReconnect(null);
432
- second.receive(SESSION);
433
- const err = await settled;
434
- expect(err.message).toMatch(/server no longer supports resumable uploads, retry/);
435
- expect(second.lastSentOfType('file_upload')).toBeUndefined();
436
- expect(second.lastSentOfType('upload_begin')).toBeUndefined();
437
- expect(get(client.uploadsStore)[uploadId]).toMatchObject({state: 'failed'});
438
- });
439
-
440
- it('a paused chunked upload fails with "session changed" when the rejoin lands on another session', async () => {
441
- openAndJoin();
442
- const {source} = makeSource(1 * MiB);
443
- const p = client.uploadFileViaWebSocket('sid-1', 's.bin', source);
444
- const first = ws.last();
445
- const {uploadId} = first.lastSentOfType('upload_begin');
446
- first.receive({type: 'upload_offset', uploadId, offset: 0});
447
- await flush();
448
- const settled = expect(p).rejects.toThrow(/session changed/);
449
- const second = dropAndReconnect();
450
- second.receive({...SESSION, sessionId: 'sid-other'});
451
- await settled;
452
- expect(second.lastSentOfType('upload_begin')).toBeUndefined();
453
- });
454
-
455
- it('a paused chunked upload keeps the 10 min waiting clock', async () => {
456
- openAndJoin();
457
- const {source} = makeSource(1 * MiB);
458
- const p = client.uploadFileViaWebSocket('sid-1', 't.bin', source);
459
- const first = ws.last();
460
- const {uploadId} = first.lastSentOfType('upload_begin');
461
- first.receive({type: 'upload_offset', uploadId, offset: 0});
462
- await flush();
463
- const settled = expect(p).rejects.toThrow(/timed out/);
464
- client.suspend();
465
- vi.advanceTimersByTime(10 * 60_000 + 1);
466
- await settled;
467
- });
468
-
469
- it('a chunked upload is not capped by the legacy send count', async () => {
470
- openAndJoin();
471
- const {source} = makeSource(1 * MiB);
472
- const p = client.uploadFileViaWebSocket('sid-1', 'n.bin', source);
473
- const {uploadId} = ws.last().lastSentOfType('upload_begin');
474
- let sock = ws.last();
475
- for (let i = 0; i < 4; i++) {
476
- sock = dropAndReconnect();
477
- sock.receive(SESSION);
478
- expect(sock.lastSentOfType('upload_begin')).toMatchObject({uploadId});
479
- }
480
- sock.receive({type: 'upload_offset', uploadId, offset: 1 * MiB, done: true, savedPath: '/up/n'});
481
- await expect(p).resolves.toMatchObject({savedPath: '/up/n'});
482
- });
483
- });
@@ -1,111 +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 type {WhereverState} from '../src/types.js';
5
- import {makeWSFactory} from './harness.js';
6
-
7
- // The per-turn CONVERSATION-MODE SIGNAL on the wire: a spoken conversation is
8
- // state that lives in the web app's knobs registry, and a dictated message is
9
- // byte-identical to a typed one, so the agent could never tell the mode was on
10
- // and never called `say`. The client now stamps an OPTIONAL `conversationMode`
11
- // boolean on the EXISTING `message` payload (no new message type, no new chat
12
- // role); when off the field is absent, so the frame is exactly what it was before
13
- // and an older server ignores it.
14
-
15
- function installLocalStorage() {
16
- const store = new Map<string, string>();
17
- (globalThis as any).localStorage = {
18
- getItem: (k: string) => (store.has(k) ? store.get(k)! : null),
19
- setItem: (k: string, v: string) => void store.set(k, v),
20
- removeItem: (k: string) => void store.delete(k),
21
- clear: () => store.clear(),
22
- };
23
- return store;
24
- }
25
-
26
- function createAndLoad(ws: ReturnType<typeof makeWSFactory>, client: WhereverClient) {
27
- client.joinSession('/tmp/project/session.jsonl');
28
- ws.last().receive({
29
- type: 'session_created',
30
- sessionId: 'sid-1',
31
- sessionFile: '/tmp/project/session.jsonl',
32
- cwd: '/tmp/project',
33
- model: 'fake:model',
34
- isStreaming: false,
35
- });
36
- ws.last().receive({
37
- type: 'message_history',
38
- sessionId: 'sid-1',
39
- messages: [],
40
- totalCount: 0,
41
- offset: 0,
42
- });
43
- }
44
-
45
- describe('conversation-mode flag on the message payload', () => {
46
- let ws: ReturnType<typeof makeWSFactory>;
47
- let client: WhereverClient;
48
- const state = () => get<WhereverState>(client.stateStore);
49
-
50
- beforeEach(() => {
51
- vi.useFakeTimers();
52
- installLocalStorage();
53
- (globalThis as any).localStorage.clear();
54
- ws = makeWSFactory();
55
- client = new WhereverClient({
56
- host: 'localhost',
57
- port: 1234,
58
- secure: false,
59
- WebSocketCtor: ws.ctor,
60
- });
61
- client.connect();
62
- ws.last().open();
63
- createAndLoad(ws, client);
64
- });
65
-
66
- afterEach(() => {
67
- client.disconnect(true);
68
- vi.useRealTimers();
69
- });
70
-
71
- it('omits the field entirely for a normal send (no options)', () => {
72
- expect(client.sendMessage('plain send')).toBe(true);
73
- const frame = ws.last().lastSentOfType('message');
74
- expect(frame.message).toBe('plain send');
75
- expect('conversationMode' in frame).toBe(false);
76
- });
77
-
78
- it('omits the field when the caller passes conversationMode: false', () => {
79
- client.sendMessage('mode off', {conversationMode: false});
80
- const frame = ws.last().lastSentOfType('message');
81
- expect('conversationMode' in frame).toBe(false);
82
- });
83
-
84
- it('stamps conversationMode: true when the caller asks for it', () => {
85
- client.sendMessage('speak this back', {conversationMode: true});
86
- const frame = ws.last().lastSentOfType('message');
87
- expect(frame.type).toBe('message');
88
- expect(frame.conversationMode).toBe(true);
89
- // The user text is untouched: the signal is a field, never a preamble.
90
- expect(frame.message).toBe('speak this back');
91
- });
92
-
93
- it('keeps the local echo free of the flag (nothing user-visible changes)', () => {
94
- client.sendMessage('echo check', {conversationMode: true});
95
- const echo = state().messages.find((m) => m.content === 'echo check')!;
96
- expect(echo.content).toBe('echo check');
97
- expect((echo as unknown as Record<string, unknown>).conversationMode).toBeUndefined();
98
- });
99
-
100
- it('re-stamps the flag on resend, from the caller (a resend is a fresh turn)', () => {
101
- client.sendMessage('retry me', {conversationMode: true});
102
- const id = state().messages.find((m) => m.content === 'retry me')!.id;
103
- // The user flipped the mode OFF before hitting Retry: no flag this time.
104
- expect(client.resendMessage(id)).toBe(true);
105
- expect('conversationMode' in ws.last().lastSentOfType('message')).toBe(false);
106
-
107
- // And with the mode back on, the resend carries it again.
108
- expect(client.resendMessage(id, {conversationMode: true})).toBe(true);
109
- expect(ws.last().lastSentOfType('message').conversationMode).toBe(true);
110
- });
111
- });
@@ -1,82 +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
- // Bug 1: creating a new session shows the full-screen "Creating session..."
7
- // spinner (state.creatingSession) forever when the server never replies with
8
- // session_created / session_error (a slow git init or remote-repo creation, an
9
- // error thrown before the reply is sent, or a half-open socket the liveness
10
- // watchdog has not reaped yet). Every OTHER load flag (loadingSession,
11
- // resyncing) is guarded by a watchdog; creatingSession was not. These tests pin
12
- // the contract: creatingSession must always resolve.
13
-
14
- describe('creating-session watchdog', () => {
15
- let ws: ReturnType<typeof makeWSFactory>;
16
- let client: WhereverClient;
17
-
18
- beforeEach(() => {
19
- vi.useFakeTimers();
20
- ws = makeWSFactory();
21
- client = new WhereverClient({
22
- host: 'localhost',
23
- port: 1234,
24
- secure: false,
25
- WebSocketCtor: ws.ctor,
26
- });
27
- client.connect();
28
- ws.last().open();
29
- });
30
-
31
- afterEach(() => {
32
- client.disconnect(true);
33
- vi.useRealTimers();
34
- });
35
-
36
- it('sets creatingSession true when a create is requested', () => {
37
- client.createSession('/tmp/project');
38
- expect(get(client.stateStore).creatingSession).toBe(true);
39
- });
40
-
41
- it('clears creatingSession and surfaces a recoverable error if no reply ever arrives', () => {
42
- client.createSession('/tmp/project');
43
- expect(get(client.stateStore).creatingSession).toBe(true);
44
-
45
- // No session_created / session_error comes back. Advance past the watchdog.
46
- vi.advanceTimersByTime(30_000);
47
-
48
- const s = get(client.stateStore);
49
- expect(s.creatingSession).toBe(false);
50
- expect(s.sessionError).toBeTruthy();
51
- });
52
-
53
- it('clears creatingSession normally on session_created (watchdog does not fire)', () => {
54
- client.createSession('/tmp/project');
55
- ws.last().receive({
56
- type: 'session_created',
57
- sessionId: 'sid-1',
58
- sessionFile: '/tmp/project/session.jsonl',
59
- cwd: '/tmp/project',
60
- model: 'fake:model',
61
- isStreaming: false,
62
- });
63
-
64
- expect(get(client.stateStore).creatingSession).toBe(false);
65
-
66
- // The watchdog must have been disarmed: advancing time must NOT resurrect
67
- // an error on the now-successfully-created session.
68
- vi.advanceTimersByTime(30_000);
69
- const s = get(client.stateStore);
70
- expect(s.creatingSession).toBe(false);
71
- expect(s.sessionError).toBeFalsy();
72
- expect(s.activeSessionFile).toBe('/tmp/project/session.jsonl');
73
- });
74
-
75
- it('clears creatingSession on session_error', () => {
76
- client.createSession('/tmp/project');
77
- ws.last().receive({type: 'session_error', error: 'boom'});
78
- const s = get(client.stateStore);
79
- expect(s.creatingSession).toBe(false);
80
- expect(s.sessionError).toBe('boom');
81
- });
82
- });