@mycadet/cli 0.13.0-dev.202611012320.cea2f8a → 0.13.0-dev.202611022129.ce2988d

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 (116) hide show
  1. package/node_modules/@mycadet/assistant-api/node_modules/@mycadet/service-contracts/package.json +1 -1
  2. package/node_modules/@mycadet/avatar-manifest/package.json +1 -1
  3. package/node_modules/@mycadet/environments/package.json +1 -1
  4. package/node_modules/@mycadet/ipc-server-utils/package.json +1 -1
  5. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/avatar-manifest/package.json +1 -1
  6. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/environments/package.json +1 -1
  7. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/ipc-server-utils/package.json +1 -1
  8. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/service-contracts/package.json +1 -1
  9. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/shared/package.json +1 -1
  10. package/node_modules/@mycadet/local-mode/package.json +1 -1
  11. package/node_modules/@mycadet/local-mode/src/__tests__/guardian-token-refresh.test.ts +0 -1
  12. package/node_modules/@mycadet/local-mode/src/__tests__/guardian-token.test.ts +0 -1
  13. package/{src/__tests__/config-utils.test.ts → node_modules/@mycadet/local-mode/src/__tests__/hatch-config.test.ts} +1 -1
  14. package/{src/__tests__/local-hatch-lock.test.ts → node_modules/@mycadet/local-mode/src/__tests__/hatch-lock.test.ts} +1 -1
  15. package/{src/__tests__/random-name.test.ts → node_modules/@mycadet/local-mode/src/__tests__/naming.test.ts} +1 -4
  16. package/node_modules/@mycadet/local-mode/src/__tests__/operations-hatch.test.ts +60 -0
  17. package/node_modules/@mycadet/local-mode/src/__tests__/operations-resolution.test.ts +340 -0
  18. package/node_modules/@mycadet/local-mode/src/__tests__/operations-retire.test.ts +7 -7
  19. package/node_modules/@mycadet/local-mode/src/__tests__/operations-routes.test.ts +634 -0
  20. package/node_modules/@mycadet/local-mode/src/__tests__/operations-sleep.test.ts +12 -4
  21. package/node_modules/@mycadet/local-mode/src/__tests__/operations-wake.test.ts +9 -3
  22. package/node_modules/@mycadet/local-mode/src/__tests__/pair.test.ts +0 -2
  23. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-docker-pair.test.ts +3 -3
  24. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-local-pair.test.ts +2 -2
  25. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-local-retire.test.ts +0 -11
  26. package/node_modules/@mycadet/local-mode/src/__tests__/status.test.ts +1 -129
  27. package/node_modules/@mycadet/local-mode/src/cloud-registration.ts +614 -0
  28. package/node_modules/@mycadet/local-mode/src/daemon-readiness.ts +87 -11
  29. package/node_modules/@mycadet/local-mode/src/gateway-proxy.ts +0 -1
  30. package/node_modules/@mycadet/local-mode/src/guardian-token.ts +276 -6
  31. package/node_modules/@mycadet/local-mode/src/hatch.ts +0 -1
  32. package/node_modules/@mycadet/local-mode/src/index.ts +72 -18
  33. package/node_modules/@mycadet/local-mode/src/lockfile-contract.ts +5 -5
  34. package/node_modules/@mycadet/local-mode/src/lockfile.ts +1 -2
  35. package/node_modules/@mycadet/local-mode/src/operations/hatch.ts +50 -0
  36. package/node_modules/@mycadet/local-mode/src/operations/list.ts +33 -0
  37. package/node_modules/@mycadet/local-mode/src/operations/machine.ts +1 -1
  38. package/node_modules/@mycadet/local-mode/src/operations/resolve.ts +56 -0
  39. package/node_modules/@mycadet/local-mode/src/operations/routes.ts +487 -32
  40. package/node_modules/@mycadet/local-mode/src/pair.ts +0 -5
  41. package/node_modules/@mycadet/local-mode/src/process.ts +1 -1
  42. package/node_modules/@mycadet/local-mode/src/provider-credentials.ts +495 -0
  43. package/node_modules/@mycadet/local-mode/src/provider-env-vars.ts +22 -0
  44. package/node_modules/@mycadet/local-mode/src/remotes/apple-container/retire.ts +0 -5
  45. package/node_modules/@mycadet/local-mode/src/remotes/docker/common.ts +1 -3
  46. package/node_modules/@mycadet/local-mode/src/remotes/docker/retire.ts +2 -10
  47. package/node_modules/@mycadet/local-mode/src/remotes/docker/wake.ts +0 -1
  48. package/node_modules/@mycadet/local-mode/src/remotes/local/common.ts +0 -4
  49. package/{src/lib/config-utils.ts → node_modules/@mycadet/local-mode/src/remotes/local/hatch-config.ts} +3 -3
  50. package/{src/lib/local-hatch-lock.ts → node_modules/@mycadet/local-mode/src/remotes/local/hatch-lock.ts} +6 -6
  51. package/node_modules/@mycadet/local-mode/src/remotes/local/hatch.ts +124 -0
  52. package/node_modules/@mycadet/local-mode/src/remotes/local/retire.ts +1 -6
  53. package/node_modules/@mycadet/local-mode/src/status.ts +1 -366
  54. package/node_modules/@mycadet/local-mode/src/upgrade.ts +0 -1
  55. package/node_modules/@mycadet/service-contracts/package.json +1 -1
  56. package/node_modules/@mycadet/shared/package.json +1 -1
  57. package/package.json +1 -2
  58. package/src/__tests__/app-command.test.ts +30 -45
  59. package/src/__tests__/app-server-state.test.ts +195 -0
  60. package/src/__tests__/app-tunnel-status.test.ts +1 -1
  61. package/src/__tests__/assistant-client-refresh.test.ts +0 -2
  62. package/src/__tests__/backup.test.ts +1 -1
  63. package/src/__tests__/bun.test.ts +1649 -0
  64. package/src/__tests__/client-tui-refresh.test.ts +0 -1
  65. package/src/__tests__/debug.test.ts +1 -1
  66. package/src/__tests__/devices.test.ts +1 -1
  67. package/src/__tests__/guardian-token.test.ts +5 -7
  68. package/src/__tests__/hatch-provider-secrets.test.ts +4 -2
  69. package/src/__tests__/helpers/guardian-token.ts +1 -2
  70. package/src/__tests__/host-teleport.test.ts +46 -26
  71. package/src/__tests__/llm-provider-env-var-parity.test.ts +1 -1
  72. package/src/__tests__/multi-local.test.ts +1 -1
  73. package/src/__tests__/provider-secrets.test.ts +2 -3
  74. package/src/__tests__/ps-cloud-status.test.ts +7 -7
  75. package/src/__tests__/root-restore-command.test.ts +1 -1
  76. package/src/__tests__/setup.test.ts +0 -3
  77. package/src/__tests__/tui-midsession-refresh.test.ts +0 -1
  78. package/src/__tests__/tunnel.test.ts +15 -296
  79. package/src/__tests__/upgrade-local.test.ts +4 -5
  80. package/src/__tests__/wake.test.ts +6 -544
  81. package/src/__tests__/web-dist.test.ts +39 -5
  82. package/src/commands/app.ts +25 -28
  83. package/src/commands/debug.ts +2 -1
  84. package/src/commands/devices.ts +2 -1
  85. package/src/commands/hatch.ts +70 -2
  86. package/src/commands/login.ts +3 -6
  87. package/src/commands/ps.ts +14 -12
  88. package/src/commands/setup.ts +3 -3
  89. package/src/commands/tunnel.ts +6 -9
  90. package/src/commands/upgrade.ts +1 -4
  91. package/src/commands/wake.ts +0 -11
  92. package/src/lib/api-key-check.ts +1 -1
  93. package/src/lib/assistant-config.ts +1 -43
  94. package/src/lib/cloud-client.ts +0 -404
  95. package/src/lib/cloud-login.ts +7 -110
  96. package/src/lib/guardian-token.ts +4 -297
  97. package/src/lib/host-teleport.ts +82 -51
  98. package/src/lib/provider-secrets.ts +7 -494
  99. package/src/lib/remotes/docker.ts +8 -8
  100. package/src/lib/remotes/local.ts +36 -150
  101. package/src/lib/root-backup.ts +1 -5
  102. package/src/lib/servers/bun.ts +1471 -0
  103. package/src/lib/sync-cloud-assistants.ts +2 -1
  104. package/src/lib/tunnel.ts +4 -239
  105. package/src/lib/tunnels/cloudflare.ts +6 -6
  106. package/src/lib/tunnels/ngrok.ts +3 -5
  107. package/src/lib/tunnels/tailscale.ts +1 -1
  108. package/src/lib/upgrade-lifecycle.ts +0 -35
  109. package/src/lib/web-dist.ts +47 -20
  110. package/src/shared/provider-env-vars.ts +3 -20
  111. package/node_modules/@mycadet/local-mode/src/ingress.ts +0 -229
  112. package/src/__tests__/assistant-api-key-resolution.test.ts +0 -102
  113. package/src/__tests__/nginx.test.ts +0 -2447
  114. package/src/lib/assistant-api-key-resolution.ts +0 -89
  115. package/src/lib/servers/nginx.ts +0 -1241
  116. /package/{src/lib/random-name.ts → node_modules/@mycadet/local-mode/src/naming.ts} +0 -0
@@ -0,0 +1,634 @@
1
+ import {
2
+ afterAll,
3
+ afterEach,
4
+ beforeEach,
5
+ describe,
6
+ expect,
7
+ mock,
8
+ test,
9
+ } from "bun:test";
10
+ import fs from "node:fs";
11
+ import os from "node:os";
12
+ import path from "node:path";
13
+
14
+ // Snapshot the real modules before mocking so the module-scoped `afterAll`
15
+ // below can restore them — bun runs every test file in one process with a
16
+ // shared loader, and `mock.restore()` does not undo `mock.module()`.
17
+ const realHatch = { ...(await import("../hatch")) };
18
+ const realDevices = { ...(await import("../devices")) };
19
+ const realUpgrade = { ...(await import("../upgrade")) };
20
+ const realAvatar = { ...(await import("../avatar")) };
21
+ const realUnpair = { ...(await import("../unpair")) };
22
+ const realPair = { ...(await import("../pair")) };
23
+
24
+ const runHatchMock = mock(
25
+ async (_options: {
26
+ remote?: string;
27
+ }): Promise<
28
+ { ok: true; assistantId: string } | { ok: false; error: string }
29
+ > => ({ ok: true, assistantId: "hatched-1" }),
30
+ );
31
+ const runDevicesListMock = mock(async (_assistantId: string) => ({
32
+ ok: true as const,
33
+ devices: [],
34
+ }));
35
+ const runDevicesRevokeMock = mock(
36
+ async (_assistantId: string, _hashedDeviceId: string) => ({ ok: true }),
37
+ );
38
+ const runUpgradeMock = mock(
39
+ async (_assistantId: string, _options?: unknown) => ({
40
+ ok: true as const,
41
+ version: "v9.9.9",
42
+ }),
43
+ );
44
+ const readLockfileAssistantAvatarMock = mock((_assistantId: string) => ({
45
+ ok: true as const,
46
+ avatar: { kind: "character" as const, traits: {} },
47
+ }));
48
+ type UnpairAnswer =
49
+ | { ok: true; lockfile: { assistants: never[]; activeAssistant: null } }
50
+ | { ok: false; status: number; error: string };
51
+ const unpairAssistantMock = mock(
52
+ (_assistantId: string): UnpairAnswer => ({
53
+ ok: true,
54
+ lockfile: { assistants: [], activeAssistant: null },
55
+ }),
56
+ );
57
+ type PairingStartAnswer =
58
+ | {
59
+ ok: true;
60
+ handle: string;
61
+ userCode: string;
62
+ expiresAt: string;
63
+ intervalSeconds: number;
64
+ }
65
+ | {
66
+ ok: false;
67
+ status: number;
68
+ reason: "invalid-address";
69
+ error: string;
70
+ rejection: string;
71
+ };
72
+ const pairingStartMock = mock(
73
+ async (_address?: unknown): Promise<PairingStartAnswer> => ({
74
+ ok: true,
75
+ handle: "handle-1",
76
+ userCode: "ABCD-EFGH",
77
+ expiresAt: "2099-01-01T00:00:00.000Z",
78
+ intervalSeconds: 5,
79
+ }),
80
+ );
81
+ type PairingPollAnswer =
82
+ | { ok: true; status: "pending"; expiresAt: string; intervalSeconds: number }
83
+ | {
84
+ ok: true;
85
+ status: "imported";
86
+ assistantId: string;
87
+ accessOnly: boolean;
88
+ }
89
+ | { ok: false; status: number; reason: string; error: string };
90
+ const pairingPollMock = mock(
91
+ async (_options?: unknown): Promise<PairingPollAnswer> => ({
92
+ ok: true,
93
+ status: "pending",
94
+ expiresAt: "2099-01-01T00:00:00.000Z",
95
+ intervalSeconds: 5,
96
+ }),
97
+ );
98
+ const pairingCancelMock = mock((_handle?: unknown): boolean => true);
99
+
100
+ mock.module("../hatch", () => ({
101
+ ...realHatch,
102
+ runHatch: runHatchMock,
103
+ }));
104
+ mock.module("../devices", () => ({
105
+ ...realDevices,
106
+ runDevicesList: runDevicesListMock,
107
+ runDevicesRevoke: runDevicesRevokeMock,
108
+ }));
109
+ mock.module("../upgrade", () => ({
110
+ ...realUpgrade,
111
+ runUpgrade: runUpgradeMock,
112
+ }));
113
+ mock.module("../avatar", () => ({
114
+ ...realAvatar,
115
+ readLockfileAssistantAvatar: readLockfileAssistantAvatarMock,
116
+ }));
117
+ mock.module("../unpair", () => ({
118
+ ...realUnpair,
119
+ unpairAssistant: unpairAssistantMock,
120
+ }));
121
+ mock.module("../pair", () => ({
122
+ ...realPair,
123
+ pairingStart: pairingStartMock,
124
+ pairingPoll: pairingPollMock,
125
+ pairingCancel: pairingCancelMock,
126
+ }));
127
+
128
+ // Restore the real modules once this file finishes so the mocks above do not
129
+ // leak into other test files in the same `bun test` run.
130
+ afterAll(() => {
131
+ mock.module("../hatch", () => realHatch);
132
+ mock.module("../devices", () => realDevices);
133
+ mock.module("../upgrade", () => realUpgrade);
134
+ mock.module("../avatar", () => realAvatar);
135
+ mock.module("../unpair", () => realUnpair);
136
+ mock.module("../pair", () => realPair);
137
+ });
138
+
139
+ import { readLockfile } from "../lockfile";
140
+ import { stripSensitiveFields } from "../util";
141
+ import { matchOperatorRequest } from "../operations/routes";
142
+
143
+ let tmpDir: string;
144
+ let lockfilePath: string;
145
+
146
+ beforeEach(() => {
147
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "cadet-routes-op-"));
148
+ lockfilePath = path.join(tmpDir, "cadet", "lockfile.json");
149
+ process.env.XDG_CONFIG_HOME = tmpDir;
150
+ for (const m of [
151
+ runHatchMock,
152
+ runDevicesListMock,
153
+ runDevicesRevokeMock,
154
+ runUpgradeMock,
155
+ readLockfileAssistantAvatarMock,
156
+ unpairAssistantMock,
157
+ pairingStartMock,
158
+ pairingPollMock,
159
+ pairingCancelMock,
160
+ ]) {
161
+ m.mockReset();
162
+ }
163
+ runHatchMock.mockImplementation(async (_options: { remote?: string }) => ({
164
+ ok: true as const,
165
+ assistantId: "hatched-1",
166
+ }));
167
+ runDevicesListMock.mockImplementation(async () => ({
168
+ ok: true as const,
169
+ devices: [],
170
+ }));
171
+ runDevicesRevokeMock.mockImplementation(async () => ({ ok: true }));
172
+ runUpgradeMock.mockImplementation(async () => ({
173
+ ok: true as const,
174
+ version: "v9.9.9",
175
+ }));
176
+ readLockfileAssistantAvatarMock.mockImplementation(() => ({
177
+ ok: true as const,
178
+ avatar: { kind: "character" as const, traits: {} },
179
+ }));
180
+ unpairAssistantMock.mockImplementation(() => ({
181
+ ok: true as const,
182
+ lockfile: { assistants: [], activeAssistant: null },
183
+ }));
184
+ pairingStartMock.mockImplementation(async () => ({
185
+ ok: true as const,
186
+ handle: "handle-1",
187
+ userCode: "ABCD-EFGH",
188
+ expiresAt: "2099-01-01T00:00:00.000Z",
189
+ intervalSeconds: 5,
190
+ }));
191
+ pairingPollMock.mockImplementation(async () => ({
192
+ ok: true as const,
193
+ status: "pending" as const,
194
+ expiresAt: "2099-01-01T00:00:00.000Z",
195
+ intervalSeconds: 5,
196
+ }));
197
+ pairingCancelMock.mockImplementation(() => true);
198
+ });
199
+
200
+ afterEach(() => {
201
+ delete process.env.XDG_CONFIG_HOME;
202
+ fs.rmSync(tmpDir, { recursive: true, force: true });
203
+ });
204
+
205
+ const writeLockfile = (data: Record<string, unknown>): void => {
206
+ fs.mkdirSync(path.dirname(lockfilePath), { recursive: true });
207
+ fs.writeFileSync(lockfilePath, JSON.stringify(data));
208
+ };
209
+
210
+ const seedEntry = (entry: Record<string, unknown>) => {
211
+ writeLockfile({ assistants: [entry], activeAssistant: null });
212
+ return readLockfile().assistants[0]!;
213
+ };
214
+
215
+ const dispatch = (
216
+ pathname: string,
217
+ method: string,
218
+ body?: unknown,
219
+ ): Promise<Response | null> => {
220
+ const match = matchOperatorRequest(pathname, method);
221
+ if (!match) {
222
+ return Promise.reject(new Error(`no route for ${method} ${pathname}`));
223
+ }
224
+ const request = new Request(`http://host.test${pathname}`, {
225
+ method,
226
+ headers:
227
+ body === undefined ? undefined : { "content-type": "application/json" },
228
+ body:
229
+ body === undefined || method === "GET" ? undefined : JSON.stringify(body),
230
+ });
231
+ return match.route.run({
232
+ assistantId: match.assistantId,
233
+ body: request.body,
234
+ });
235
+ };
236
+
237
+ /** dispatch for a row that must answer — narrows the pass-through null. */
238
+ const dispatchAnswered = async (
239
+ pathname: string,
240
+ method: string,
241
+ body?: unknown,
242
+ ): Promise<Response> => {
243
+ const response = await dispatch(pathname, method, body);
244
+ if (!response) {
245
+ throw new Error(`${method} ${pathname} passed through; expected an answer`);
246
+ }
247
+ return response;
248
+ };
249
+
250
+ describe("the list row — GET /v1/assistants/ is the list contract", () => {
251
+ test("a populated lockfile answers a selection of credential-free rows", async () => {
252
+ seedEntry({
253
+ assistantId: "asst-1",
254
+ name: "Workbench",
255
+ cloud: "local",
256
+ port: 7830,
257
+ bearerToken: "secret-token",
258
+ });
259
+
260
+ const response = await dispatchAnswered("/v1/assistants/", "GET");
261
+
262
+ expect(response.status).toBe(200);
263
+ expect(await response.json()).toEqual({
264
+ status: "selection",
265
+ assistants: [{ id: "asst-1", name: "Workbench", remote: "local" }],
266
+ });
267
+ });
268
+
269
+ test("an empty lockfile answers the empty arm", async () => {
270
+ const response = await dispatchAnswered("/v1/assistants/", "GET");
271
+ expect(await response.json()).toEqual({ status: "empty" });
272
+ });
273
+
274
+ test("the union never reaches disk: the stored file keeps its secrets", async () => {
275
+ seedEntry({ assistantId: "asst-1", cloud: "local", port: 7830 });
276
+ await dispatch("/v1/assistants/", "GET");
277
+ expect(stripSensitiveFields).toBeDefined();
278
+ expect(JSON.parse(fs.readFileSync(lockfilePath, "utf-8"))).toBeDefined();
279
+ });
280
+ });
281
+
282
+ describe("the hatch row — POST /v1/assistants/hatch", () => {
283
+ test("spawns the CLI harness and returns the assistant id", async () => {
284
+ const response = await dispatchAnswered("/v1/assistants/hatch", "POST", {
285
+ remote: "docker",
286
+ });
287
+
288
+ expect(response.status).toBe(200);
289
+ expect(await response.json()).toEqual({
290
+ ok: true,
291
+ assistantId: "hatched-1",
292
+ });
293
+ expect(runHatchMock).toHaveBeenCalledWith({ remote: "docker" });
294
+ });
295
+
296
+ test("a non-object body degrades to a plain hatch", async () => {
297
+ const response = await dispatchAnswered("/v1/assistants/hatch", "POST", {});
298
+ expect(runHatchMock).toHaveBeenCalledWith({ remote: undefined });
299
+ expect((await response.json()).ok).toBe(true);
300
+ });
301
+
302
+ test("a failed hatch resolves {ok:false, error} — never a throw", async () => {
303
+ runHatchMock.mockImplementation(async () => ({
304
+ ok: false as const,
305
+ error: "docker daemon unreachable",
306
+ }));
307
+
308
+ const response = await dispatchAnswered("/v1/assistants/hatch", "POST", {});
309
+
310
+ expect(response.status).toBe(200);
311
+ expect(await response.json()).toEqual({
312
+ ok: false,
313
+ error: "docker daemon unreachable",
314
+ });
315
+ });
316
+ });
317
+
318
+ describe("the unpair row — POST /v1/assistants/{id}/unpair", () => {
319
+ test("an on-machine entry unpairs and returns the written lockfile", async () => {
320
+ seedEntry({ assistantId: "asst-1", cloud: "local", port: 7830 });
321
+
322
+ const response = await dispatchAnswered(
323
+ "/v1/assistants/asst-1/unpair",
324
+ "POST",
325
+ {},
326
+ );
327
+
328
+ expect(response.status).toBe(200);
329
+ expect(await response.json()).toEqual({
330
+ ok: true,
331
+ lockfile: { assistants: [], activeAssistant: null },
332
+ });
333
+ expect(unpairAssistantMock).toHaveBeenCalledWith("asst-1");
334
+ });
335
+
336
+ test("an id that is not an entry on this machine passes through", async () => {
337
+ const response = await dispatch("/v1/assistants/ghost/unpair", "POST", {});
338
+
339
+ expect(response).toBeNull();
340
+ expect(unpairAssistantMock).not.toHaveBeenCalled();
341
+ });
342
+
343
+ test("a refused unpair carries the operation's status", async () => {
344
+ seedEntry({ assistantId: "asst-1", cloud: "local", port: 7830 });
345
+ unpairAssistantMock.mockImplementation(() => ({
346
+ ok: false as const,
347
+ status: 409,
348
+ error: "the assistant is running",
349
+ }));
350
+
351
+ const response = await dispatchAnswered(
352
+ "/v1/assistants/asst-1/unpair",
353
+ "POST",
354
+ {},
355
+ );
356
+
357
+ expect(response?.status).toBe(409);
358
+ expect(await response?.json()).toEqual({
359
+ ok: false,
360
+ error: "the assistant is running",
361
+ });
362
+ });
363
+ });
364
+
365
+ describe("the pairing rows — POST /v1/remote-web/pairing-*", () => {
366
+ test("pairing-start maps the started shape", async () => {
367
+ const response = await dispatchAnswered(
368
+ "/v1/remote-web/pairing-start",
369
+ "POST",
370
+ { address: "https://gw.example.com" },
371
+ );
372
+
373
+ expect(response.status).toBe(200);
374
+ expect(await response.json()).toEqual({
375
+ ok: true,
376
+ handle: "handle-1",
377
+ userCode: "ABCD-EFGH",
378
+ expiresAt: "2099-01-01T00:00:00.000Z",
379
+ intervalSeconds: 5,
380
+ });
381
+ expect(pairingStartMock).toHaveBeenCalledWith("https://gw.example.com");
382
+ });
383
+
384
+ test("pairing-start carries the refusal's reason and rejection", async () => {
385
+ pairingStartMock.mockImplementation(async () => ({
386
+ ok: false as const,
387
+ status: 400,
388
+ reason: "invalid-address" as const,
389
+ error: "That address points back at this machine.",
390
+ rejection: "loopback",
391
+ }));
392
+
393
+ const response = await dispatch("/v1/remote-web/pairing-start", "POST", {
394
+ address: "https://localhost:7830",
395
+ });
396
+
397
+ expect(response?.status).toBe(400);
398
+ expect(await response?.json()).toMatchObject({
399
+ ok: false,
400
+ reason: "invalid-address",
401
+ rejection: "loopback",
402
+ });
403
+ });
404
+
405
+ test("pairing-poll distinguishes pending from imported", async () => {
406
+ const pending = await dispatchAnswered(
407
+ "/v1/remote-web/pairing-poll",
408
+ "POST",
409
+ { handle: "handle-1" },
410
+ );
411
+ expect(await pending.json()).toMatchObject({ status: "pending" });
412
+
413
+ pairingPollMock.mockImplementation(async () => ({
414
+ ok: true as const,
415
+ status: "imported" as const,
416
+ assistantId: "desk",
417
+ accessOnly: true,
418
+ }));
419
+ const imported = await dispatchAnswered(
420
+ "/v1/remote-web/pairing-poll",
421
+ "POST",
422
+ { handle: "handle-1", name: "desk" },
423
+ );
424
+ expect(await imported.json()).toEqual({
425
+ ok: true,
426
+ status: "imported",
427
+ assistantId: "desk",
428
+ accessOnly: true,
429
+ });
430
+ expect(pairingPollMock).toHaveBeenLastCalledWith({
431
+ handle: "handle-1",
432
+ name: "desk",
433
+ });
434
+ });
435
+
436
+ test("pairing-cancel answers with the op's boolean", async () => {
437
+ pairingCancelMock.mockImplementation(() => false);
438
+
439
+ const response = await dispatchAnswered(
440
+ "/v1/remote-web/pairing-cancel",
441
+ "POST",
442
+ { handle: "handle-1" },
443
+ );
444
+
445
+ expect(await response.json()).toEqual({ ok: false });
446
+ expect(pairingCancelMock).toHaveBeenCalledWith("handle-1");
447
+ });
448
+
449
+ test("malformed bodies answer 400 with the shared invalid-JSON shape", async () => {
450
+ for (const pathname of [
451
+ "/v1/remote-web/pairing-start",
452
+ "/v1/remote-web/pairing-poll",
453
+ "/v1/remote-web/pairing-cancel",
454
+ "/v1/devices",
455
+ "/v1/devices/revoke",
456
+ ]) {
457
+ const match = matchOperatorRequest(pathname, "POST")!;
458
+ const request = new Request(`http://host.test${pathname}`, {
459
+ method: "POST",
460
+ headers: { "content-type": "application/json" },
461
+ body: "{not json",
462
+ });
463
+ const response = await match.route.run({
464
+ assistantId: match.assistantId,
465
+ body: request.body,
466
+ });
467
+ expect(response?.status).toBe(400);
468
+ expect(await response?.json()).toEqual({
469
+ ok: false,
470
+ error: "Invalid JSON body",
471
+ });
472
+ }
473
+ });
474
+ });
475
+
476
+ describe("the devices rows — POST /v1/devices[/revoke]", () => {
477
+ test("the list row passes the assistant id to the run helper", async () => {
478
+ const response = await dispatchAnswered("/v1/devices", "POST", {
479
+ assistantId: "asst-1",
480
+ });
481
+
482
+ expect(response.status).toBe(200);
483
+ expect(await response.json()).toEqual({ ok: true, devices: [] });
484
+ expect(runDevicesListMock).toHaveBeenCalledWith("asst-1");
485
+ });
486
+
487
+ test("the revoke row requires the hashed device id", async () => {
488
+ const missing = await dispatchAnswered("/v1/devices/revoke", "POST", {
489
+ assistantId: "asst-1",
490
+ });
491
+ expect(missing.status).toBe(400);
492
+ expect(await missing.json()).toEqual({
493
+ ok: false,
494
+ error: "Missing hashedDeviceId",
495
+ });
496
+
497
+ const response = await dispatchAnswered("/v1/devices/revoke", "POST", {
498
+ assistantId: "asst-1",
499
+ hashedDeviceId: "hash-1",
500
+ });
501
+ expect(await response.json()).toEqual({ ok: true });
502
+ expect(runDevicesRevokeMock).toHaveBeenCalledWith("asst-1", "hash-1");
503
+ });
504
+
505
+ test("a missing assistantId answers 400 before any spawn", async () => {
506
+ const response = await dispatchAnswered("/v1/devices", "POST", {});
507
+ expect(response.status).toBe(400);
508
+ expect(await response.json()).toEqual({
509
+ ok: false,
510
+ error: "Missing assistantId",
511
+ });
512
+ expect(runDevicesListMock).not.toHaveBeenCalled();
513
+ });
514
+
515
+ test("run-helper failures resolve {ok:false, error} — never a throw", async () => {
516
+ runDevicesListMock.mockImplementation(async () => {
517
+ throw new Error("cli spawn failed");
518
+ });
519
+
520
+ const response = await dispatchAnswered("/v1/devices", "POST", {
521
+ assistantId: "asst-1",
522
+ });
523
+
524
+ expect(response.status).toBe(200);
525
+ expect(await response.json()).toEqual({
526
+ ok: false,
527
+ error: "cli spawn failed",
528
+ });
529
+ });
530
+ });
531
+
532
+ describe("the upgrade row — POST /v1/assistants/{id}/upgrade", () => {
533
+ test("the active local assistant upgrades through the CLI", async () => {
534
+ seedEntry({ assistantId: "asst-1", cloud: "local", port: 7830 });
535
+ writeLockfile({
536
+ assistants: [{ assistantId: "asst-1", cloud: "local", port: 7830 }],
537
+ activeAssistant: "asst-1",
538
+ });
539
+
540
+ const response = await dispatchAnswered(
541
+ "/v1/assistants/asst-1/upgrade",
542
+ "POST",
543
+ { latest: true },
544
+ );
545
+
546
+ expect(response.status).toBe(200);
547
+ expect(await response.json()).toEqual({ ok: true, version: "v9.9.9" });
548
+ expect(runUpgradeMock).toHaveBeenCalledWith("asst-1", { latest: true });
549
+ });
550
+
551
+ test("a non-active local entry is refused", async () => {
552
+ // A single entry IS the active one (the active-or-single doctrine) —
553
+ // refusal needs a second entry holding the active seat.
554
+ writeLockfile({
555
+ assistants: [
556
+ { assistantId: "asst-1", cloud: "local", port: 7830 },
557
+ { assistantId: "asst-2", cloud: "local", port: 7831 },
558
+ ],
559
+ activeAssistant: "asst-2",
560
+ });
561
+
562
+ const response = await dispatch(
563
+ "/v1/assistants/asst-1/upgrade",
564
+ "POST",
565
+ {},
566
+ );
567
+
568
+ expect(response?.status).toBe(200);
569
+ expect(await response?.json()).toEqual({
570
+ ok: false,
571
+ error: "Can only upgrade the active local assistant",
572
+ });
573
+ expect(runUpgradeMock).not.toHaveBeenCalled();
574
+ });
575
+
576
+ test("an id that is not an entry on this machine passes through", async () => {
577
+ const response = await dispatch(
578
+ "/v1/assistants/cloud-id/upgrade",
579
+ "POST",
580
+ {},
581
+ );
582
+
583
+ expect(response).toBeNull();
584
+ expect(runUpgradeMock).not.toHaveBeenCalled();
585
+ });
586
+ });
587
+
588
+ describe("the avatar row — GET /v1/assistants/{id}/avatar/get", () => {
589
+ test("an on-machine entry reads the avatar off its workspace", async () => {
590
+ seedEntry({ assistantId: "asst-1", cloud: "local", port: 7830 });
591
+
592
+ const response = await dispatchAnswered(
593
+ "/v1/assistants/asst-1/avatar/get",
594
+ "GET",
595
+ );
596
+
597
+ expect(response.status).toBe(200);
598
+ expect(await response.json()).toEqual({
599
+ ok: true,
600
+ avatar: { kind: "character", traits: {} },
601
+ });
602
+ expect(readLockfileAssistantAvatarMock).toHaveBeenCalledWith("asst-1");
603
+ });
604
+
605
+ test("an id that is not an entry on this machine passes through", async () => {
606
+ const response = await dispatch("/v1/assistants/ghost/avatar/get", "GET");
607
+ expect(response).toBeNull();
608
+ expect(readLockfileAssistantAvatarMock).not.toHaveBeenCalled();
609
+ });
610
+ });
611
+
612
+ describe("the route table's method scoping", () => {
613
+ test("a method-mismatched request to a known path matches no row", () => {
614
+ expect(matchOperatorRequest("/v1/devices", "GET")).toBeNull();
615
+ expect(
616
+ matchOperatorRequest("/v1/remote-web/pairing-start", "DELETE"),
617
+ ).toBeNull();
618
+ });
619
+
620
+ test("a GET on a path-shaped id rides the resolution row's pattern", () => {
621
+ // `/v1/assistants/hatch` is a legal resolution probe for an id literally
622
+ // named "hatch" — the id-scoped pattern owns it, the entry lookup
623
+ // pass-through decides.
624
+ const match = matchOperatorRequest("/v1/assistants/hatch", "GET");
625
+ expect(match?.assistantId).toBe("hatch");
626
+ });
627
+
628
+ test("no-capture patterns resolve the empty id", () => {
629
+ // Every body-only route's pattern has no capture group; the rows that
630
+ // read an id never match on those patterns.
631
+ const match = matchOperatorRequest("/v1/devices", "POST");
632
+ expect(match?.assistantId).toBe("");
633
+ });
634
+ });
@@ -411,7 +411,9 @@ describe("the operator route table — the scoped surface's sleep arm", () => {
411
411
  "POST",
412
412
  );
413
413
  expect(match).not.toBeNull();
414
- const response = await match!.route.run(match!.assistantId);
414
+ const response = await match!.route.run({
415
+ assistantId: match!.assistantId,
416
+ });
415
417
  expect(response?.status).toBe(204);
416
418
  expect(sleepDockerMock).toHaveBeenCalledWith(
417
419
  expect.objectContaining({ assistantId: "asst-docker" }),
@@ -426,7 +428,9 @@ describe("the operator route table — the scoped surface's sleep arm", () => {
426
428
  "POST",
427
429
  );
428
430
  expect(match).not.toBeNull();
429
- const response = await match!.route.run(match!.assistantId);
431
+ const response = await match!.route.run({
432
+ assistantId: match!.assistantId,
433
+ });
430
434
  expect(response?.status).toBe(400);
431
435
  const body = (await response!.json()) as { detail: string };
432
436
  expect(body.detail).toBe(
@@ -441,7 +445,9 @@ describe("the operator route table — the scoped surface's sleep arm", () => {
441
445
  "POST",
442
446
  );
443
447
  expect(match).not.toBeNull();
444
- expect(await match!.route.run(match!.assistantId)).toBeNull();
448
+ expect(
449
+ await match!.route.run({ assistantId: match!.assistantId }),
450
+ ).toBeNull();
445
451
  expect(sleepDockerMock).not.toHaveBeenCalled();
446
452
  });
447
453
 
@@ -460,7 +466,9 @@ describe("the operator route table — the scoped surface's sleep arm", () => {
460
466
  "/v1/assistants/asst-local/sleep/",
461
467
  "POST",
462
468
  );
463
- const response = await match!.route.run(match!.assistantId);
469
+ const response = await match!.route.run({
470
+ assistantId: match!.assistantId,
471
+ });
464
472
  expect(response?.status).toBe(409);
465
473
  const body = (await response!.json()) as { detail: string };
466
474
  expect(body.detail).toContain("active phone calls");