@wherever-dev/client 0.5.3 → 0.6.0

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,324 @@
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
+ // The RESTORE half of the folder-missing state: the panel that replaces the
7
+ // composer clones the repository back into the missing path.
8
+ //
9
+ // The job is server-owned and keyed by that PATH, so this client is never the
10
+ // owner of the operation: it asks, and then paints whatever the server says --
11
+ // including a job it did not start (a second device, or its own previous socket
12
+ // before the phone dropped off). Frames are addressed by path, so one for a
13
+ // folder this client is no longer looking at must be ignored rather than
14
+ // repainting the panel of a different session.
15
+
16
+ const TARGET = '/home/u/dev/github/owner/repo';
17
+ const SESSION_FILE = `${TARGET}/session.jsonl`;
18
+
19
+ function runningJob(overrides: Record<string, unknown> = {}) {
20
+ return {
21
+ id: 7,
22
+ kind: 'clone',
23
+ targetPath: TARGET,
24
+ url: 'git@github.com:owner/repo.git',
25
+ state: 'running',
26
+ progress: {
27
+ phase: 'receiving',
28
+ scope: 'repository',
29
+ percent: 42,
30
+ indeterminate: false,
31
+ text: 'Receiving objects: 42% (42/100)',
32
+ at: 1,
33
+ },
34
+ startedAt: 1,
35
+ ...overrides,
36
+ };
37
+ }
38
+
39
+ describe('restore panel state', () => {
40
+ let ws: ReturnType<typeof makeWSFactory>;
41
+ let client: WhereverClient;
42
+
43
+ function loadMissingFolderSession(job?: Record<string, unknown>) {
44
+ client.joinSession(SESSION_FILE);
45
+ ws.last().receive({
46
+ type: 'session_created',
47
+ sessionId: 'sid-1',
48
+ sessionFile: SESSION_FILE,
49
+ cwd: TARGET,
50
+ model: 'fake:model',
51
+ readOnly: true,
52
+ folderMissing: true,
53
+ pending: true,
54
+ });
55
+ ws.last().receive({
56
+ type: 'folder_missing',
57
+ sessionId: 'sid-1',
58
+ cwd: TARGET,
59
+ ...(job ? {job} : {}),
60
+ });
61
+ }
62
+
63
+ beforeEach(() => {
64
+ vi.useFakeTimers();
65
+ ws = makeWSFactory();
66
+ client = new WhereverClient({
67
+ host: 'localhost',
68
+ port: 1234,
69
+ secure: false,
70
+ WebSocketCtor: ws.ctor,
71
+ });
72
+ client.connect();
73
+ ws.last().open();
74
+ });
75
+
76
+ afterEach(() => {
77
+ client.disconnect(true);
78
+ vi.useRealTimers();
79
+ });
80
+
81
+ it('offers a restore for the missing path when nothing is running', () => {
82
+ loadMissingFolderSession();
83
+
84
+ const s = get(client.stateStore);
85
+ expect(s.restore).toEqual({
86
+ targetPath: TARGET,
87
+ job: null,
88
+ joined: false,
89
+ rejection: null,
90
+ });
91
+ });
92
+
93
+ it('repaints a job the server was ALREADY running for that path', () => {
94
+ // The reconnect / second-device case: the frame carries the running job,
95
+ // so the panel shows live progress instead of offering a second clone.
96
+ loadMissingFolderSession(runningJob());
97
+
98
+ const s = get(client.stateStore);
99
+ expect(s.restore?.job?.id).toBe(7);
100
+ expect(s.restore?.job?.state).toBe('running');
101
+ expect(s.restore?.job?.progress?.percent).toBe(42);
102
+ });
103
+
104
+ it('sends the target path the server named, and follows the job to completion', () => {
105
+ loadMissingFolderSession();
106
+
107
+ client.startRestore('clone', ' git@github.com:owner/repo.git ');
108
+ const sent = ws.last().lastSentOfType('restore_start');
109
+ expect(sent).toEqual({
110
+ type: 'restore_start',
111
+ targetPath: TARGET,
112
+ action: 'clone',
113
+ url: 'git@github.com:owner/repo.git',
114
+ });
115
+
116
+ ws.last().receive({
117
+ type: 'restore_started',
118
+ targetPath: TARGET,
119
+ outcome: 'started',
120
+ job: runningJob(),
121
+ });
122
+ expect(get(client.stateStore).restore?.joined).toBe(false);
123
+
124
+ ws.last().receive({
125
+ type: 'restore_progress',
126
+ targetPath: TARGET,
127
+ job: runningJob({
128
+ progress: {
129
+ phase: 'starting',
130
+ scope: 'sub',
131
+ percent: null,
132
+ indeterminate: true,
133
+ text: "Cloning into '/home/u/dev/github/owner/repo/sub'...",
134
+ at: 2,
135
+ },
136
+ }),
137
+ });
138
+ // Honest progress survives the round trip: an indeterminate phase is
139
+ // carried AS indeterminate, and the submodule keeps its own scope.
140
+ const mid = get(client.stateStore).restore?.job?.progress;
141
+ expect(mid?.indeterminate).toBe(true);
142
+ expect(mid?.percent).toBeNull();
143
+ expect(mid?.scope).toBe('sub');
144
+
145
+ ws.last().receive({
146
+ type: 'restore_complete',
147
+ targetPath: TARGET,
148
+ job: runningJob({state: 'done', endedAt: 3}),
149
+ });
150
+ expect(get(client.stateStore).restore?.job?.state).toBe('done');
151
+ });
152
+
153
+ it('sends the CREATE remedy with the git-init choice, in both states', () => {
154
+ // The second remedy: a folder that was never a clone. Same frame family,
155
+ // same path, no url -- only the checkbox differs, and it must travel as an
156
+ // EXPLICIT boolean either way. Omitting it when off would let the server's
157
+ // own default decide, which is exactly the surprise repository a user who
158
+ // turned `gitInitDefault` off is entitled not to get.
159
+ loadMissingFolderSession();
160
+
161
+ client.startRestore('create', undefined, true);
162
+ expect(ws.last().lastSentOfType('restore_start')).toEqual({
163
+ type: 'restore_start',
164
+ targetPath: TARGET,
165
+ action: 'create',
166
+ gitInit: true,
167
+ });
168
+
169
+ client.startRestore('create', undefined, false);
170
+ expect(ws.last().lastSentOfType('restore_start')).toEqual({
171
+ type: 'restore_start',
172
+ targetPath: TARGET,
173
+ action: 'create',
174
+ gitInit: false,
175
+ });
176
+
177
+ // It drives the ONE state machine: the create job is followed exactly as a
178
+ // clone is, to the same terminal state the panel's ready-and-reload rests on.
179
+ ws.last().receive({
180
+ type: 'restore_started',
181
+ targetPath: TARGET,
182
+ outcome: 'started',
183
+ job: runningJob({kind: 'create', url: undefined, gitInit: false}),
184
+ });
185
+ ws.last().receive({
186
+ type: 'restore_complete',
187
+ targetPath: TARGET,
188
+ job: runningJob({
189
+ kind: 'create',
190
+ url: undefined,
191
+ gitInit: false,
192
+ state: 'done',
193
+ endedAt: 3,
194
+ }),
195
+ });
196
+ const job = get(client.stateStore).restore?.job;
197
+ expect(job?.kind).toBe('create');
198
+ expect(job?.state).toBe('done');
199
+ expect(job?.gitInit).toBe(false);
200
+ });
201
+
202
+ it('says when a second tap JOINED a job cloning a DIFFERENT url', () => {
203
+ loadMissingFolderSession(runningJob());
204
+
205
+ client.startRestore('clone', 'git@github.com:someone/else.git');
206
+ expect(get(client.stateStore).restore?.requestedUrl).toBe(
207
+ 'git@github.com:someone/else.git',
208
+ );
209
+
210
+ ws.last().receive({
211
+ type: 'restore_started',
212
+ targetPath: TARGET,
213
+ outcome: 'joined',
214
+ job: runningJob(),
215
+ });
216
+
217
+ const s = get(client.stateStore);
218
+ expect(s.restore?.joined).toBe(true);
219
+ // The url in flight, NOT the one this client typed: the panel can only
220
+ // say so honestly because both are kept.
221
+ expect(s.restore?.job?.url).toBe('git@github.com:owner/repo.git');
222
+ expect(s.restore?.requestedUrl).toBe('git@github.com:someone/else.git');
223
+ });
224
+
225
+ it('surfaces a refusal without inventing a job', () => {
226
+ loadMissingFolderSession();
227
+
228
+ client.startRestore('clone', 'https://github.com/owner/repo.git');
229
+ ws.last().receive({
230
+ type: 'restore_rejected',
231
+ targetPath: TARGET,
232
+ reason: 'invalid-url',
233
+ message: 'Refusing to clone "https://github.com/owner/repo.git": not a recognised SSH repository URL',
234
+ });
235
+
236
+ const s = get(client.stateStore);
237
+ expect(s.restore?.job).toBeNull();
238
+ expect(s.restore?.rejection?.reason).toBe('invalid-url');
239
+ // A corrected retry clears the refusal so the panel is not stuck on it.
240
+ client.startRestore('clone', 'git@github.com:owner/repo.git');
241
+ expect(get(client.stateStore).restore?.rejection).toBeNull();
242
+ });
243
+
244
+ it('carries a failure with its mapped cause and git raw output', () => {
245
+ loadMissingFolderSession();
246
+ ws.last().receive({
247
+ type: 'restore_complete',
248
+ targetPath: TARGET,
249
+ job: runningJob({
250
+ state: 'failed',
251
+ failure: {
252
+ cause: 'no-key',
253
+ message: 'No SSH key on this machine is accepted by github.com.',
254
+ stderr: 'git@github.com: Permission denied (publickey).\nfatal: Could not read from remote repository.\n',
255
+ },
256
+ }),
257
+ });
258
+
259
+ const failure = get(client.stateStore).restore?.job?.failure;
260
+ expect(failure?.cause).toBe('no-key');
261
+ expect(failure?.stderr).toContain('Permission denied (publickey)');
262
+
263
+ // Retrying drops the FINISHED job it is retrying, so the panel shows the
264
+ // request in flight rather than the previous failure until the server
265
+ // answers. (A RUNNING job would be kept: that is the one being joined.)
266
+ client.startRestore('clone', 'git@github.com:owner/repo.git');
267
+ expect(get(client.stateStore).restore?.job).toBeNull();
268
+ });
269
+
270
+ it('cancels the job for its own folder, and nothing when there is none', () => {
271
+ loadMissingFolderSession(runningJob());
272
+ expect(client.cancelRestore()).toBe(true);
273
+ expect(ws.last().lastSentOfType('restore_cancel')).toEqual({
274
+ type: 'restore_cancel',
275
+ targetPath: TARGET,
276
+ });
277
+
278
+ ws.last().receive({
279
+ type: 'restore_complete',
280
+ targetPath: TARGET,
281
+ job: runningJob({state: 'cancelled', endedAt: 4}),
282
+ });
283
+ expect(get(client.stateStore).restore?.job?.state).toBe('cancelled');
284
+ });
285
+
286
+ it('ignores restore frames for a folder it is not looking at', () => {
287
+ loadMissingFolderSession(runningJob());
288
+
289
+ ws.last().receive({
290
+ type: 'restore_progress',
291
+ targetPath: '/home/u/dev/github/owner/other',
292
+ job: {...runningJob(), id: 99, targetPath: '/home/u/dev/github/owner/other'},
293
+ });
294
+
295
+ expect(get(client.stateStore).restore?.job?.id).toBe(7);
296
+ });
297
+
298
+ it('reloads the session to go live, and clears the restore with it', () => {
299
+ loadMissingFolderSession();
300
+ ws.last().receive({
301
+ type: 'restore_complete',
302
+ targetPath: TARGET,
303
+ job: runningJob({state: 'done', endedAt: 5}),
304
+ });
305
+
306
+ // A live agent is a LOAD-TIME decision, so going live is a re-load of the
307
+ // same session file, not a local unlock.
308
+ expect(client.reloadSession()).toBe(true);
309
+ expect(ws.last().lastSentOfType('session_load')).toMatchObject({
310
+ type: 'session_load',
311
+ sessionFile: SESSION_FILE,
312
+ });
313
+ const s = get(client.stateStore);
314
+ expect(s.restore).toBeNull();
315
+ expect(s.folderMissing).toBeNull();
316
+ });
317
+
318
+ it('clears with the session it belonged to', () => {
319
+ loadMissingFolderSession(runningJob());
320
+
321
+ client.switchSession('/home/u/dev/other/session.jsonl');
322
+ expect(get(client.stateStore).restore).toBeNull();
323
+ });
324
+ });
@@ -0,0 +1,100 @@
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: opening the app on a deep link like `https://host/#<sessionId>` hung
7
+ // forever on "Loading session...", while clicking the SAME session in the
8
+ // sidebar loaded instantly.
9
+ //
10
+ // Cause: the sidebar joins by session FILE path, but the URL hash carries the
11
+ // session ID. `session_load` accepts either (the server resolves an ID to its
12
+ // file), yet the reply always carries the resolved FILE. The superseded-load
13
+ // guard compared `msg.sessionFile === pendingLoadFile`, so an ID-issued load
14
+ // never matched its own reply: session_created was dropped as "stale", no
15
+ // session ever became active, and the hash-driven spinner never cleared.
16
+ //
17
+ // Fix: the pending load target matches its reply by sessionFile OR sessionId.
18
+
19
+ function newClient(ws: ReturnType<typeof makeWSFactory>) {
20
+ const client = new WhereverClient({
21
+ host: 'localhost',
22
+ port: 1234,
23
+ secure: false,
24
+ WebSocketCtor: ws.ctor,
25
+ });
26
+ client.connect();
27
+ ws.last().open();
28
+ return client;
29
+ }
30
+
31
+ const SESSION_ID = '019fa1e9-0e62-795f-bee9-f2e1fcdc39d5';
32
+ const SESSION_FILE = '/tmp/project/019fa1e9-0e62-795f-bee9-f2e1fcdc39d5.jsonl';
33
+
34
+ describe('loading a session by ID (URL hash deep link)', () => {
35
+ let ws: ReturnType<typeof makeWSFactory>;
36
+ let client: WhereverClient;
37
+
38
+ beforeEach(() => {
39
+ vi.useFakeTimers();
40
+ ws = makeWSFactory();
41
+ client = newClient(ws);
42
+ });
43
+
44
+ afterEach(() => {
45
+ client.disconnect(true);
46
+ vi.useRealTimers();
47
+ });
48
+
49
+ it('accepts the reply whose sessionFile is the resolved path of the requested ID', () => {
50
+ client.joinSession(SESSION_ID);
51
+ expect(get(client.stateStore).loadingSession).toBe(true);
52
+
53
+ ws.last().receive({
54
+ type: 'session_created',
55
+ sessionId: SESSION_ID,
56
+ sessionFile: SESSION_FILE,
57
+ cwd: '/tmp/project',
58
+ model: 'fake:model',
59
+ pending: true,
60
+ });
61
+ ws.last().receive({
62
+ type: 'message_history',
63
+ sessionId: SESSION_ID,
64
+ messages: [{role: 'user', content: 'hello', timestamp: 1}],
65
+ totalCount: 1,
66
+ offset: 0,
67
+ });
68
+
69
+ const s = get(client.stateStore);
70
+ expect(s.sessionId).toBe(SESSION_ID);
71
+ expect(s.activeSessionFile).toBe(SESSION_FILE);
72
+ expect(s.loadingSession).toBe(false);
73
+ expect(s.messages.map((m) => m.content)).toContain('hello');
74
+ });
75
+
76
+ it('still drops a late reply for a different session after switching by ID', () => {
77
+ client.joinSession('other-session-id');
78
+ client.switchSession(SESSION_ID);
79
+
80
+ ws.last().receive({
81
+ type: 'session_created',
82
+ sessionId: SESSION_ID,
83
+ sessionFile: SESSION_FILE,
84
+ cwd: '/tmp/project',
85
+ model: 'fake:model',
86
+ });
87
+ // The abandoned load finally answers; it must not take over.
88
+ ws.last().receive({
89
+ type: 'session_created',
90
+ sessionId: 'other-session-id',
91
+ sessionFile: '/tmp/project/other.jsonl',
92
+ cwd: '/tmp/project',
93
+ model: 'fake:model',
94
+ });
95
+
96
+ const s = get(client.stateStore);
97
+ expect(s.sessionId).toBe(SESSION_ID);
98
+ expect(s.activeSessionFile).toBe(SESSION_FILE);
99
+ });
100
+ });
@@ -0,0 +1,113 @@
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
+ // `/skill:` composer autocomplete contract. The skill list is only resolvable
7
+ // once the session's live agent exists, so the client asks for it as soon as
8
+ // the agent is live. A brand-new session (session_new) is live IMMEDIATELY and
9
+ // never emits session_ready, so requesting only on session_ready left a fresh
10
+ // session with an empty skill menu (the regression this covers).
11
+
12
+ describe('skill autocomplete list', () => {
13
+ let ws: ReturnType<typeof makeWSFactory>;
14
+ let client: WhereverClient;
15
+
16
+ beforeEach(() => {
17
+ vi.useFakeTimers();
18
+ ws = makeWSFactory();
19
+ client = new WhereverClient({
20
+ host: 'localhost',
21
+ port: 1234,
22
+ secure: false,
23
+ WebSocketCtor: ws.ctor,
24
+ });
25
+ client.connect();
26
+ ws.last().open();
27
+ });
28
+
29
+ afterEach(() => {
30
+ client.disconnect(true);
31
+ vi.useRealTimers();
32
+ });
33
+
34
+ it('requests skills for a freshly created session (no session_ready)', () => {
35
+ client.createSession('/tmp/new');
36
+ ws.last().receive({
37
+ type: 'session_created',
38
+ sessionId: 'sid-new',
39
+ sessionFile: '/tmp/new/session.jsonl',
40
+ cwd: '/tmp/new',
41
+ model: 'fake:model',
42
+ isStreaming: false,
43
+ // no pending flag: the agent is already live
44
+ });
45
+
46
+ const req = ws.last().lastSentOfType('skills_request');
47
+ expect(req).toBeDefined();
48
+ expect(req.sessionId).toBe('sid-new');
49
+
50
+ ws.last().receive({
51
+ type: 'skills_list',
52
+ sessionId: 'sid-new',
53
+ skills: [{name: 'skill:setup', description: 'Set things up'}],
54
+ });
55
+ expect(get(client.stateStore).skills).toEqual([
56
+ {name: 'skill:setup', description: 'Set things up'},
57
+ ]);
58
+ });
59
+
60
+ it('defers the request to session_ready for a cold (pending) load', () => {
61
+ client.joinSession('/tmp/p/session.jsonl');
62
+ ws.last().receive({
63
+ type: 'session_created',
64
+ sessionId: 'sid-cold',
65
+ sessionFile: '/tmp/p/session.jsonl',
66
+ cwd: '/tmp/p',
67
+ model: '',
68
+ isStreaming: false,
69
+ pending: true,
70
+ });
71
+ // The agent is still building: nothing to ask yet.
72
+ expect(ws.last().lastSentOfType('skills_request')).toBeUndefined();
73
+
74
+ ws.last().receive({
75
+ type: 'session_ready',
76
+ sessionId: 'sid-cold',
77
+ sessionFile: '/tmp/p/session.jsonl',
78
+ model: 'fake:model',
79
+ isStreaming: false,
80
+ });
81
+ expect(ws.last().lastSentOfType('skills_request')?.sessionId).toBe('sid-cold');
82
+ });
83
+
84
+ it('clears the previous session skills when attaching to another session', () => {
85
+ client.createSession('/tmp/a');
86
+ ws.last().receive({
87
+ type: 'session_created',
88
+ sessionId: 'sid-a',
89
+ sessionFile: '/tmp/a/session.jsonl',
90
+ cwd: '/tmp/a',
91
+ model: 'fake:model',
92
+ isStreaming: false,
93
+ });
94
+ ws.last().receive({
95
+ type: 'skills_list',
96
+ sessionId: 'sid-a',
97
+ skills: [{name: 'skill:a', description: 'A'}],
98
+ });
99
+ expect(get(client.stateStore).skills.length).toBe(1);
100
+
101
+ client.joinSession('/tmp/b/session.jsonl');
102
+ ws.last().receive({
103
+ type: 'session_created',
104
+ sessionId: 'sid-b',
105
+ sessionFile: '/tmp/b/session.jsonl',
106
+ cwd: '/tmp/b',
107
+ model: 'fake:model',
108
+ isStreaming: false,
109
+ pending: true,
110
+ });
111
+ expect(get(client.stateStore).skills).toEqual([]);
112
+ });
113
+ });
@@ -0,0 +1,70 @@
1
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
+ import {WhereverClient} from '../src/client.js';
3
+ import {makeWSFactory} from './harness.js';
4
+
5
+ // The clientKey lets the server retire this viewer's own superseded (silently
6
+ // dropped) connection instead of counting it as a second viewer. But the web
7
+ // client scopes the key per tab via sessionStorage, and "Duplicate tab" CLONES
8
+ // sessionStorage: two LIVE tabs can end up sharing one key and evicting each
9
+ // other forever, one reconnect apart.
10
+ //
11
+ // The server therefore tells a connection it is being retired. Receiving that
12
+ // proves this client is alive, so it takes a fresh key before the reconnect --
13
+ // one eviction, then both tabs are distinct viewers again.
14
+
15
+ describe('superseded connection', () => {
16
+ let ws: ReturnType<typeof makeWSFactory>;
17
+ let client: WhereverClient;
18
+ let persisted: string[];
19
+
20
+ function keyOf(url: string): string | null {
21
+ return new URL(url).searchParams.get('clientKey');
22
+ }
23
+
24
+ beforeEach(() => {
25
+ vi.useFakeTimers();
26
+ ws = makeWSFactory();
27
+ persisted = [];
28
+ client = new WhereverClient({
29
+ host: 'localhost',
30
+ port: 1234,
31
+ secure: false,
32
+ WebSocketCtor: ws.ctor,
33
+ clientKey: 'shared-key',
34
+ onClientKeyChange: (k) => persisted.push(k),
35
+ });
36
+ client.connect();
37
+ ws.last().open();
38
+ });
39
+
40
+ afterEach(() => {
41
+ client.disconnect(true);
42
+ vi.useRealTimers();
43
+ });
44
+
45
+ it('sends the configured key on connect', () => {
46
+ expect(keyOf(ws.last().url)).toBe('shared-key');
47
+ });
48
+
49
+ it('takes (and reports) a new key when the server says it was superseded', () => {
50
+ ws.last().receive({type: 'connection_superseded'});
51
+
52
+ expect(persisted).toHaveLength(1);
53
+ expect(persisted[0]).not.toBe('shared-key');
54
+
55
+ // The server closes right after; the reconnect must carry the NEW key, so
56
+ // the two tabs stop evicting each other.
57
+ ws.last().close();
58
+ vi.advanceTimersByTime(5000);
59
+ const reconnected = ws.last();
60
+ expect(keyOf(reconnected.url)).toBe(persisted[0]);
61
+ });
62
+
63
+ it('keeps its key across an ordinary reconnect', () => {
64
+ ws.last().close();
65
+ vi.advanceTimersByTime(5000);
66
+
67
+ expect(keyOf(ws.last().url)).toBe('shared-key');
68
+ expect(persisted).toHaveLength(0);
69
+ });
70
+ });