@wherever-dev/client 0.8.0 → 0.8.1

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 (38) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +63 -0
  3. package/dist/client.js +2 -0
  4. package/dist/client.js.map +1 -1
  5. package/dist/types.d.ts +1 -0
  6. package/dist/types.js.map +1 -1
  7. package/package.json +23 -3
  8. package/src/client.ts +2 -0
  9. package/src/types.ts +3 -0
  10. package/test/agent-pending.test.ts +0 -107
  11. package/test/chunked-upload.test.ts +0 -483
  12. package/test/conversation-mode-flag.test.ts +0 -111
  13. package/test/creating-session-watchdog.test.ts +0 -82
  14. package/test/folder-conflict-read-only.test.ts +0 -145
  15. package/test/folder-missing-read-only.test.ts +0 -114
  16. package/test/harness.ts +0 -133
  17. package/test/hydration-seam.test.ts +0 -400
  18. package/test/liveness-probe.test.ts +0 -183
  19. package/test/message-delivery-confirmation.test.ts +0 -320
  20. package/test/mid-stream-steer.test.ts +0 -91
  21. package/test/new-session-clone-progress.test.ts +0 -263
  22. package/test/optimistic-force-command.test.ts +0 -127
  23. package/test/queued-steer-restored.test.ts +0 -175
  24. package/test/reconnect-rejoins-session.test.ts +0 -136
  25. package/test/restore-panel.test.ts +0 -324
  26. package/test/resume-keeps-session.test.ts +0 -118
  27. package/test/session-load-by-id.test.ts +0 -100
  28. package/test/session-reload.test.ts +0 -82
  29. package/test/skills-autocomplete.test.ts +0 -113
  30. package/test/steer-queue-cancel.test.ts +0 -130
  31. package/test/superseded-connection-key.test.ts +0 -70
  32. package/test/superseded-session-load.test.ts +0 -180
  33. package/test/tool-aborted-render.test.ts +0 -170
  34. package/test/tool-duration-history.test.ts +0 -93
  35. package/test/tool-duration.test.ts +0 -86
  36. package/test/tool-interrupted-history.test.ts +0 -294
  37. package/test/upload-survives-reconnect.test.ts +0 -482
  38. package/tsconfig.json +0 -16
package/package.json CHANGED
@@ -1,19 +1,39 @@
1
1
  {
2
2
  "name": "@wherever-dev/client",
3
- "version": "0.8.0",
4
- "description": "Framework-agnostic isomorphic TypeScript client for Wherever standalone server",
3
+ "version": "0.8.1",
4
+ "description": "Framework-agnostic TypeScript client (browser and Node.js) for the Wherever server's pi coding agent WebSocket API",
5
+ "keywords": [
6
+ "wherever",
7
+ "pi",
8
+ "pi-coding-agent",
9
+ "coding-agent",
10
+ "client",
11
+ "websocket",
12
+ "isomorphic",
13
+ "typescript"
14
+ ],
15
+ "homepage": "https://github.com/wighawag/wherever/tree/main/client#readme",
16
+ "bugs": {
17
+ "url": "https://github.com/wighawag/wherever/issues"
18
+ },
5
19
  "repository": {
6
20
  "type": "git",
7
21
  "url": "git+https://github.com/wighawag/wherever.git",
8
22
  "directory": "client"
9
23
  },
24
+ "license": "AGPL-3.0-only",
25
+ "author": "wighawag <wighawag@gmail.com>",
10
26
  "publishConfig": {
11
27
  "access": "public"
12
28
  },
13
- "license": "AGPL-3.0-only",
14
29
  "type": "module",
15
30
  "main": "dist/index.js",
16
31
  "types": "dist/index.d.ts",
32
+ "files": [
33
+ "dist",
34
+ "src",
35
+ "CHANGELOG.md"
36
+ ],
17
37
  "dependencies": {
18
38
  "sveltore": "^0.0.2"
19
39
  },
package/src/client.ts CHANGED
@@ -251,6 +251,7 @@ const defaultState: WhereverState = {
251
251
  messages: [],
252
252
  clientId: null,
253
253
  serverVersion: null,
254
+ piVersion: null,
254
255
  folderConflict: null,
255
256
  folderMissing: null,
256
257
  restore: null,
@@ -1132,6 +1133,7 @@ export class WhereverClient {
1132
1133
  connecting: false,
1133
1134
  clientId: msg.clientId,
1134
1135
  serverVersion: msg.serverVersion ?? null,
1136
+ piVersion: msg.piVersion ?? null,
1135
1137
  error: null,
1136
1138
  }));
1137
1139
  // Uploads held for unknown capabilities can now choose their protocol
package/src/types.ts CHANGED
@@ -227,6 +227,9 @@ export interface WhereverState {
227
227
  // version), as reported in its `connected` message. null until connected, or
228
228
  // when talking to a server old enough not to report it.
229
229
  serverVersion: string | null;
230
+ // Version of the pi SDK that server runs its sessions on. null until
231
+ // connected, or when the server is too old to report it.
232
+ piVersion: string | null;
230
233
  folderConflict: FolderConflictInfo | null;
231
234
  // The active session's working folder is gone from this machine (see
232
235
  // FolderMissingInfo). Non-null implies `readOnly`, and no `session_ready` is
@@ -1,107 +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
- // Fast-first load reducer contract: a `pending` session_created paints history
7
- // but keeps the composer blocked (agentPending) until session_ready; a
8
- // non-pending create is immediately sendable; a session_error while pending
9
- // degrades to readable-but-not-sendable.
10
-
11
- describe('agentPending (fast-first load)', () => {
12
- let ws: ReturnType<typeof makeWSFactory>;
13
- let client: WhereverClient;
14
-
15
- beforeEach(() => {
16
- vi.useFakeTimers();
17
- ws = makeWSFactory();
18
- client = new WhereverClient({
19
- host: 'localhost',
20
- port: 1234,
21
- secure: false,
22
- WebSocketCtor: ws.ctor,
23
- });
24
- client.connect();
25
- ws.last().open();
26
- });
27
-
28
- afterEach(() => {
29
- client.disconnect(true);
30
- vi.useRealTimers();
31
- });
32
-
33
- function loadPending() {
34
- client.joinSession('/tmp/p/session.jsonl');
35
- ws.last().receive({
36
- type: 'session_created',
37
- sessionId: 'sid-1',
38
- sessionFile: '/tmp/p/session.jsonl',
39
- cwd: '/tmp/p',
40
- model: '',
41
- isStreaming: false,
42
- pending: true,
43
- });
44
- ws.last().receive({
45
- type: 'message_history',
46
- sessionId: 'sid-1',
47
- messages: [
48
- {role: 'user', content: 'hi', timestamp: 1},
49
- {role: 'assistant', content: 'hello', timestamp: 2},
50
- ],
51
- totalCount: 2,
52
- offset: 0,
53
- });
54
- }
55
-
56
- it('paints history but stays agentPending until session_ready', () => {
57
- loadPending();
58
- let s = get(client.stateStore);
59
- // History is readable now.
60
- expect(s.messages.length).toBe(2);
61
- expect(s.loadingSession).toBe(false);
62
- // But sending is blocked until the agent is ready.
63
- expect(s.agentPending).toBe(true);
64
- expect(client.sendMessage('nope')).toBe(false);
65
-
66
- ws.last().receive({
67
- type: 'session_ready',
68
- sessionId: 'sid-1',
69
- sessionFile: '/tmp/p/session.jsonl',
70
- model: 'fake:model',
71
- isStreaming: false,
72
- });
73
- s = get(client.stateStore);
74
- expect(s.agentPending).toBe(false);
75
- expect(s.activeModel).toBe('fake:model');
76
- // Now a send goes out over the open socket.
77
- expect(client.sendMessage('now it works')).toBe(true);
78
- expect(ws.last().lastSentOfType('message')?.message).toBe('now it works');
79
- });
80
-
81
- it('a non-pending create is immediately sendable', () => {
82
- client.createSession('/tmp/new');
83
- ws.last().receive({
84
- type: 'session_created',
85
- sessionId: 'sid-2',
86
- sessionFile: '/tmp/new/session.jsonl',
87
- cwd: '/tmp/new',
88
- model: 'fake:model',
89
- isStreaming: false,
90
- // no pending flag
91
- });
92
- const s = get(client.stateStore);
93
- expect(s.agentPending).toBe(false);
94
- expect(client.sendMessage('hi')).toBe(true);
95
- });
96
-
97
- it('session_error while pending degrades to readable-but-not-sendable', () => {
98
- loadPending();
99
- expect(get(client.stateStore).agentPending).toBe(true);
100
- ws.last().receive({type: 'session_error', sessionId: 'sid-1', error: 'agent build failed'});
101
- const s = get(client.stateStore);
102
- expect(s.agentPending).toBe(false);
103
- expect(s.sessionError).toBe('agent build failed');
104
- // History is still there to read.
105
- expect(s.messages.length).toBe(2);
106
- });
107
- });
@@ -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
- });