@wherever-dev/client 0.5.4 → 0.7.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.
- package/CHANGELOG.md +33 -0
- package/dist/client.d.ts +40 -0
- package/dist/client.js +386 -9
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +48 -0
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +433 -9
- package/src/types.ts +118 -0
- package/test/folder-conflict-read-only.test.ts +145 -0
- package/test/folder-missing-read-only.test.ts +114 -0
- package/test/new-session-clone-progress.test.ts +263 -0
- package/test/queued-steer-restored.test.ts +175 -0
- package/test/restore-panel.test.ts +324 -0
- package/test/session-load-by-id.test.ts +100 -0
- package/test/session-reload.test.ts +82 -0
- package/test/skills-autocomplete.test.ts +113 -0
- package/test/superseded-connection-key.test.ts +70 -0
|
@@ -0,0 +1,175 @@
|
|
|
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: after a reload, a message queued mid-stream (a STEER) was invisible. The
|
|
7
|
+
// server now snapshots pi's queue on attach (queue_update), but the queued text
|
|
8
|
+
// is NOT in the session file yet, so message_history cannot contain it: with
|
|
9
|
+
// only `pendingSteering` set and no matching message, the UI had nothing to
|
|
10
|
+
// badge and the user's text simply vanished until pi injected it at the next
|
|
11
|
+
// step.
|
|
12
|
+
//
|
|
13
|
+
// Fix: queue_update re-materializes any queued text with no matching user
|
|
14
|
+
// message, so a reloaded client shows the queued messages (badged "Queued") and
|
|
15
|
+
// can still cancel them.
|
|
16
|
+
|
|
17
|
+
function newClient(ws: ReturnType<typeof makeWSFactory>) {
|
|
18
|
+
const 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
|
+
return client;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function attach(ws: ReturnType<typeof makeWSFactory>, client: WhereverClient) {
|
|
30
|
+
client.joinSession('/tmp/project/session.jsonl');
|
|
31
|
+
ws.last().receive({
|
|
32
|
+
type: 'session_created',
|
|
33
|
+
sessionId: 'sid-1',
|
|
34
|
+
sessionFile: '/tmp/project/session.jsonl',
|
|
35
|
+
cwd: '/tmp/project',
|
|
36
|
+
model: 'fake:model',
|
|
37
|
+
});
|
|
38
|
+
ws.last().receive({
|
|
39
|
+
type: 'message_history',
|
|
40
|
+
sessionId: 'sid-1',
|
|
41
|
+
messages: [{role: 'user', content: 'do the first thing', timestamp: 1}],
|
|
42
|
+
totalCount: 1,
|
|
43
|
+
offset: 0,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe('a queued steer is restored into the conversation on reload', () => {
|
|
48
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
49
|
+
let client: WhereverClient;
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
vi.useFakeTimers();
|
|
53
|
+
ws = makeWSFactory();
|
|
54
|
+
client = newClient(ws);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
client.disconnect(true);
|
|
59
|
+
vi.useRealTimers();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('renders a queued message the freshly-loaded history does not contain', () => {
|
|
63
|
+
attach(ws, client);
|
|
64
|
+
|
|
65
|
+
ws.last().receive({
|
|
66
|
+
type: 'queue_update',
|
|
67
|
+
sessionId: 'sid-1',
|
|
68
|
+
steering: ['actually do X instead'],
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const s = get(client.stateStore);
|
|
72
|
+
expect(s.pendingSteering).toEqual(['actually do X instead']);
|
|
73
|
+
const restored = s.messages.filter(
|
|
74
|
+
(m) => m.role === 'user' && m.content === 'actually do X instead',
|
|
75
|
+
);
|
|
76
|
+
expect(restored).toHaveLength(1);
|
|
77
|
+
// It is server-side queued, not an unconfirmed local send: no retry banner.
|
|
78
|
+
expect(restored[0].delivery).toBeUndefined();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('does not duplicate a queued message we sent ourselves this session', () => {
|
|
82
|
+
attach(ws, client);
|
|
83
|
+
client.sendMessage('actually do X instead');
|
|
84
|
+
|
|
85
|
+
ws.last().receive({
|
|
86
|
+
type: 'queue_update',
|
|
87
|
+
sessionId: 'sid-1',
|
|
88
|
+
steering: ['actually do X instead'],
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const s = get(client.stateStore);
|
|
92
|
+
expect(
|
|
93
|
+
s.messages.filter((m) => m.content === 'actually do X instead'),
|
|
94
|
+
).toHaveLength(1);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('does not duplicate when the queued message is finally delivered', () => {
|
|
98
|
+
attach(ws, client);
|
|
99
|
+
ws.last().receive({
|
|
100
|
+
type: 'queue_update',
|
|
101
|
+
sessionId: 'sid-1',
|
|
102
|
+
steering: ['actually do X instead'],
|
|
103
|
+
});
|
|
104
|
+
// pi injects it: the queue empties and the server echoes the user message.
|
|
105
|
+
ws.last().receive({type: 'queue_update', sessionId: 'sid-1', steering: []});
|
|
106
|
+
ws.last().receive({
|
|
107
|
+
type: 'message_end',
|
|
108
|
+
sessionId: 'sid-1',
|
|
109
|
+
role: 'user',
|
|
110
|
+
content: 'actually do X instead',
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const s = get(client.stateStore);
|
|
114
|
+
expect(s.pendingSteering).toEqual([]);
|
|
115
|
+
expect(
|
|
116
|
+
s.messages.filter((m) => m.content === 'actually do X instead'),
|
|
117
|
+
).toHaveLength(1);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('restores every queued message, including repeats of the same text', () => {
|
|
121
|
+
attach(ws, client);
|
|
122
|
+
ws.last().receive({
|
|
123
|
+
type: 'queue_update',
|
|
124
|
+
sessionId: 'sid-1',
|
|
125
|
+
steering: ['ping', 'ping'],
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const s = get(client.stateStore);
|
|
129
|
+
expect(s.messages.filter((m) => m.content === 'ping')).toHaveLength(2);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('does not duplicate when the OLDER of two queued messages is delivered', () => {
|
|
133
|
+
attach(ws, client);
|
|
134
|
+
ws.last().receive({
|
|
135
|
+
type: 'queue_update',
|
|
136
|
+
sessionId: 'sid-1',
|
|
137
|
+
steering: ['first queued', 'second queued'],
|
|
138
|
+
});
|
|
139
|
+
// pi injects the oldest: the queue drops it and the server echoes it. The
|
|
140
|
+
// echoed message is NOT the last user bubble ('second queued' is), so a
|
|
141
|
+
// naive last-message dedupe would append a duplicate.
|
|
142
|
+
ws.last().receive({
|
|
143
|
+
type: 'queue_update',
|
|
144
|
+
sessionId: 'sid-1',
|
|
145
|
+
steering: ['second queued'],
|
|
146
|
+
});
|
|
147
|
+
ws.last().receive({
|
|
148
|
+
type: 'message_end',
|
|
149
|
+
sessionId: 'sid-1',
|
|
150
|
+
role: 'user',
|
|
151
|
+
content: 'first queued',
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const s = get(client.stateStore);
|
|
155
|
+
expect(s.messages.filter((m) => m.content === 'first queued')).toHaveLength(1);
|
|
156
|
+
expect(s.messages.filter((m) => m.content === 'second queued')).toHaveLength(1);
|
|
157
|
+
expect(s.pendingSteering).toEqual(['second queued']);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('leaves an already-restored queue alone on a repeated snapshot', () => {
|
|
161
|
+
attach(ws, client);
|
|
162
|
+
const snapshot = {
|
|
163
|
+
type: 'queue_update',
|
|
164
|
+
sessionId: 'sid-1',
|
|
165
|
+
steering: ['actually do X instead'],
|
|
166
|
+
};
|
|
167
|
+
ws.last().receive(snapshot);
|
|
168
|
+
ws.last().receive(snapshot);
|
|
169
|
+
|
|
170
|
+
const s = get(client.stateStore);
|
|
171
|
+
expect(
|
|
172
|
+
s.messages.filter((m) => m.content === 'actually do X instead'),
|
|
173
|
+
).toHaveLength(1);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -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,82 @@
|
|
|
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 web `/reload` reducer contract: reloadResources() only ASKS (one
|
|
7
|
+
// session_reload frame for the active session); the server drives the state.
|
|
8
|
+
// session_reloading blocks the composer (agentPending) like a cold load, and the
|
|
9
|
+
// session_ready that ends the reload unblocks it and re-requests the skill list
|
|
10
|
+
// (a reload can change which skills exist). A frame for another session is ignored.
|
|
11
|
+
|
|
12
|
+
describe('session reload', () => {
|
|
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
|
+
client.joinSession('/tmp/p/session.jsonl');
|
|
28
|
+
ws.last().receive({
|
|
29
|
+
type: 'session_created',
|
|
30
|
+
sessionId: 'sid-1',
|
|
31
|
+
sessionFile: '/tmp/p/session.jsonl',
|
|
32
|
+
cwd: '/tmp/p',
|
|
33
|
+
model: 'fake:a',
|
|
34
|
+
isStreaming: false,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
client.disconnect(true);
|
|
40
|
+
vi.useRealTimers();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function sentOfType(type: string): any[] {
|
|
44
|
+
return ws.last().sent.filter((m: any) => m.type === type);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
it('reloadResources() sends one session_reload for the active session and changes nothing locally', () => {
|
|
48
|
+
const before = get(client.stateStore);
|
|
49
|
+
client.reloadResources();
|
|
50
|
+
expect(sentOfType('session_reload')).toEqual([{type: 'session_reload', sessionId: 'sid-1'}]);
|
|
51
|
+
expect(get(client.stateStore).agentPending).toBe(before.agentPending);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('session_reloading blocks the composer until session_ready, which re-requests skills', () => {
|
|
55
|
+
ws.last().receive({type: 'session_reloading', sessionId: 'sid-1'});
|
|
56
|
+
expect(get(client.stateStore).agentPending).toBe(true);
|
|
57
|
+
|
|
58
|
+
const skillRequestsBefore = sentOfType('skills_request').length;
|
|
59
|
+
ws.last().receive({
|
|
60
|
+
type: 'session_ready',
|
|
61
|
+
sessionId: 'sid-1',
|
|
62
|
+
sessionFile: '/tmp/p/session.jsonl',
|
|
63
|
+
model: 'fake:a',
|
|
64
|
+
isStreaming: false,
|
|
65
|
+
});
|
|
66
|
+
expect(get(client.stateStore).agentPending).toBe(false);
|
|
67
|
+
expect(sentOfType('skills_request').length).toBe(skillRequestsBefore + 1);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('a failed reload (session_error) unblocks the composer and surfaces the error', () => {
|
|
71
|
+
ws.last().receive({type: 'session_reloading', sessionId: 'sid-1'});
|
|
72
|
+
ws.last().receive({type: 'session_error', sessionId: 'sid-1', error: 'Reload failed: boom'});
|
|
73
|
+
const s = get(client.stateStore);
|
|
74
|
+
expect(s.agentPending).toBe(false);
|
|
75
|
+
expect(s.sessionError).toBe('Reload failed: boom');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('ignores session_reloading for a session we are not viewing', () => {
|
|
79
|
+
ws.last().receive({type: 'session_reloading', sessionId: 'other'});
|
|
80
|
+
expect(get(client.stateStore).agentPending).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
});
|