@mycadet/cli 0.13.0-dev.202610271937.2f71260 → 0.13.0-dev.202610290143.526d7ca

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 (58) 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/service-contracts/package.json +1 -1
  8. package/node_modules/@mycadet/local-mode/node_modules/@mycadet/shared/package.json +1 -1
  9. package/node_modules/@mycadet/local-mode/package.json +1 -1
  10. package/node_modules/@mycadet/local-mode/src/__tests__/environment.test.ts +1 -1
  11. package/node_modules/@mycadet/local-mode/src/__tests__/gateway-proxy.test.ts +40 -30
  12. package/node_modules/@mycadet/local-mode/src/__tests__/guardian-token-refresh.test.ts +5 -25
  13. package/node_modules/@mycadet/local-mode/src/__tests__/guardian-token.test.ts +40 -70
  14. package/node_modules/@mycadet/local-mode/src/__tests__/loopback-auth.test.ts +21 -15
  15. package/node_modules/@mycadet/local-mode/src/__tests__/pair.test.ts +100 -310
  16. package/node_modules/@mycadet/local-mode/src/__tests__/status.test.ts +24 -24
  17. package/node_modules/@mycadet/local-mode/src/__tests__/unpair.test.ts +17 -22
  18. package/node_modules/@mycadet/local-mode/src/avatar.test.ts +7 -6
  19. package/node_modules/@mycadet/local-mode/src/avatar.ts +1 -2
  20. package/node_modules/@mycadet/local-mode/src/config.ts +6 -6
  21. package/node_modules/@mycadet/local-mode/src/gateway-proxy.ts +4 -4
  22. package/node_modules/@mycadet/local-mode/src/guardian-token.ts +4 -7
  23. package/node_modules/@mycadet/local-mode/src/index.ts +0 -5
  24. package/node_modules/@mycadet/local-mode/src/lockfile-contract.test.ts +0 -28
  25. package/node_modules/@mycadet/local-mode/src/lockfile-contract.ts +4 -31
  26. package/node_modules/@mycadet/local-mode/src/lockfile-lock.test.ts +30 -19
  27. package/node_modules/@mycadet/local-mode/src/lockfile-lock.ts +4 -6
  28. package/node_modules/@mycadet/local-mode/src/lockfile.test.ts +49 -140
  29. package/node_modules/@mycadet/local-mode/src/lockfile.ts +32 -104
  30. package/node_modules/@mycadet/local-mode/src/pair.ts +32 -167
  31. package/node_modules/@mycadet/local-mode/src/status.ts +2 -4
  32. package/node_modules/@mycadet/local-mode/src/unpair.ts +69 -79
  33. package/node_modules/@mycadet/service-contracts/package.json +1 -1
  34. package/node_modules/@mycadet/shared/package.json +1 -1
  35. package/package.json +1 -1
  36. package/src/__tests__/app-command.test.ts +0 -21
  37. package/src/__tests__/app-tunnel-status.test.ts +1 -0
  38. package/src/__tests__/connect-import.test.ts +0 -41
  39. package/src/__tests__/guardian-token.test.ts +7 -22
  40. package/src/__tests__/nginx-ingress.test.ts +36 -14
  41. package/src/__tests__/tailscale.test.ts +28 -133
  42. package/src/__tests__/tunnel-stop.test.ts +12 -18
  43. package/src/__tests__/tunnel.test.ts +116 -293
  44. package/src/__tests__/upgrade-local.test.ts +3 -1
  45. package/src/__tests__/wake.test.ts +17 -30
  46. package/src/commands/connect/import.ts +20 -59
  47. package/src/commands/retire.ts +1 -4
  48. package/src/commands/tunnel.ts +13 -52
  49. package/src/lib/assistant-config.ts +2 -4
  50. package/src/lib/guardian-token.ts +4 -4
  51. package/src/lib/ingress-config.ts +3 -85
  52. package/src/lib/nginx-ingress.ts +7 -14
  53. package/src/lib/tunnel.ts +29 -132
  54. package/src/lib/tunnels/cloudflare.ts +5 -26
  55. package/src/lib/tunnels/ngrok.ts +17 -64
  56. package/src/lib/tunnels/process-lifecycle.ts +0 -2
  57. package/src/lib/tunnels/tailscale.ts +13 -114
  58. package/src/__tests__/ingress-config.test.ts +0 -184
@@ -9,13 +9,10 @@ import { guardianTokenPath } from "../config";
9
9
  import { saveLockfileAssistant } from "../lockfile";
10
10
  import {
11
11
  checkPairedAssistantName,
12
- connectImport,
13
12
  pairAssistant,
14
13
  pairingCancel,
15
14
  pairingPoll,
16
15
  pairingStart,
17
- type ConnectImportOptions,
18
- type ConnectImportResult,
19
16
  type PairedAssistantCredentials,
20
17
  type PairOptions,
21
18
  type PairResult,
@@ -89,12 +86,9 @@ const readLockfileFromDisk = (): Record<string, unknown> =>
89
86
 
90
87
  const readGuardianToken = (assistantId: string): Record<string, unknown> =>
91
88
  JSON.parse(
92
- fs.readFileSync(guardianTokenPath(configDir, assistantId), "utf-8"),
89
+ fs.readFileSync(guardianTokenPath(assistantId), "utf-8"),
93
90
  ) as Record<string, unknown>;
94
91
 
95
- const encodeBundle = (obj: Record<string, unknown>): string =>
96
- Buffer.from(JSON.stringify(obj)).toString("base64");
97
-
98
92
  const credentials = (
99
93
  overrides: Partial<PairedAssistantCredentials> = {},
100
94
  ): PairedAssistantCredentials => ({
@@ -203,49 +197,52 @@ const sleep = (ms: number): Promise<void> =>
203
197
  new Promise((resolve) => setTimeout(resolve, ms));
204
198
 
205
199
  /**
206
- * The caller side of a pairing: register the exchanged entry through the
207
- * owning layer's save, exactly as the cli import command and the hosts do.
208
- * Pairing itself never touches the lockfile; the pre-check refused a
209
- * colliding id before the exchange spent its one-time code.
200
+ * The import side of a pairing, mirrored from the cli's connect-import
201
+ * command: the IMPORT defines the entry from the exchange's identity facts
202
+ * (the canonical custom encoding every reader derives from) and saves it
203
+ * through the owning layer. Pairing itself never defines or saves an entry.
210
204
  */
205
+ function registerImportedPairing(result: {
206
+ assistantId: string;
207
+ name: string;
208
+ gatewayUrl: string;
209
+ }): void {
210
+ const gateway = new URL(result.gatewayUrl);
211
+ saveLockfileAssistant({
212
+ assistantId: result.assistantId,
213
+ name: result.name,
214
+ cloud: "custom",
215
+ ipAddress: gateway.hostname,
216
+ port: Number(gateway.port) || (gateway.protocol === "https:" ? 443 : 80),
217
+ });
218
+ }
219
+
220
+ /** The import side of a pairing exchange: pair, then register. */
211
221
  function pairAndRegister(configDir: string, opts: PairOptions): PairResult {
212
- const result = pairAssistant(configDir, opts);
222
+ const result = pairAssistant(opts);
213
223
  if (!result.ok) {
214
224
  return result;
215
225
  }
216
- saveLockfileAssistant(result.entry);
226
+ registerImportedPairing(result);
217
227
  return result;
218
228
  }
219
229
 
220
- /** The caller side of a poll: register an imported pairing. */
230
+ /** The import side of a poll: pair on import, then register. */
221
231
  async function pollAndRegister(
222
232
  configDir: string,
223
233
  opts: PairingPollOptions,
224
234
  ): Promise<PairingPollResult> {
225
- const result = await pairingPoll(configDir, opts);
235
+ const result = await pairingPoll(opts);
226
236
  if (!result.ok || result.status !== "imported") {
227
237
  return result;
228
238
  }
229
- saveLockfileAssistant(result.entry);
230
- return result;
231
- }
232
-
233
- /** The caller side of a bundle import: register the exchanged entry. */
234
- function importAndRegister(
235
- configDir: string,
236
- opts: ConnectImportOptions,
237
- ): ConnectImportResult {
238
- const result = connectImport(configDir, opts);
239
- if (!result.ok) {
240
- return result;
241
- }
242
- saveLockfileAssistant(result.entry);
239
+ registerImportedPairing(result);
243
240
  return result;
244
241
  }
245
242
 
246
243
  describe("pairAssistant", () => {
247
- test("writes the guardian token and returns the entry to register, with the exact CLI shapes and modes", () => {
248
- const result = pairAssistant(configDir, {
244
+ test("writes the guardian token and returns the pairing's identity facts, defining no entry", () => {
245
+ const result = pairAssistant({
249
246
  credentials: credentials({ token: jwtWithExp, deviceId: "dev-aaa" }),
250
247
  });
251
248
 
@@ -255,27 +252,15 @@ describe("pairAssistant", () => {
255
252
  }
256
253
  expect(result.assistantId).toBe(DEFAULT_PAIRED_ID);
257
254
  expect(result.accessOnly).toBe(true);
258
- // The entry comes back for the caller to register create-only; pairing
255
+ // The identity facts the importing caller defines the entry from; pairing
259
256
  // itself never touches the lockfile.
260
- expect(result.entry).toEqual({
261
- assistantId: DEFAULT_PAIRED_ID,
262
- name: "paired (10.0.0.5:7830)",
263
- cloud: "custom",
264
- ipAddress: "10.0.0.5",
265
- port: 7830,
266
- });
267
- expect(Object.keys(result.entry)).toEqual([
268
- "assistantId",
269
- "name",
270
- "cloud",
271
- "ipAddress",
272
- "port",
273
- ]);
257
+ expect(result.name).toBe("paired (10.0.0.5:7830)");
258
+ expect(result.gatewayUrl).toBe("http://10.0.0.5:7830");
274
259
  expect(fs.existsSync(lockfilePath)).toBe(false);
275
260
 
276
261
  // The guardian token matches what `cadet connect import` writes, field
277
262
  // for field and in the same key order.
278
- const tokenPath = guardianTokenPath(configDir, DEFAULT_PAIRED_ID);
263
+ const tokenPath = guardianTokenPath(DEFAULT_PAIRED_ID);
279
264
  const raw = fs.readFileSync(tokenPath, "utf-8");
280
265
  expect(raw.endsWith("\n")).toBe(true);
281
266
  const token = JSON.parse(raw) as Record<string, unknown>;
@@ -311,7 +296,7 @@ describe("pairAssistant", () => {
311
296
 
312
297
  test("falls back to now+24h expiry for a non-JWT access token", () => {
313
298
  const before = Date.now();
314
- const result = pairAssistant(configDir, {
299
+ const result = pairAssistant({
315
300
  credentials: credentials({ token: "opaque-token", deviceId: "dev-exp" }),
316
301
  });
317
302
  expect(result.ok).toBe(true);
@@ -327,7 +312,7 @@ describe("pairAssistant", () => {
327
312
  });
328
313
 
329
314
  test("persists the refresh credential and reports accessOnly: false", () => {
330
- const result = pairAssistant(configDir, {
315
+ const result = pairAssistant({
331
316
  credentials: credentials({
332
317
  gatewayUrl: "https://gw.example.com",
333
318
  deviceId: "dev-refresh",
@@ -349,7 +334,7 @@ describe("pairAssistant", () => {
349
334
  });
350
335
 
351
336
  test("preserves a numeric (epoch-ms) refreshTokenExpiresAt", () => {
352
- const result = pairAssistant(configDir, {
337
+ const result = pairAssistant({
353
338
  credentials: credentials({
354
339
  gatewayUrl: "https://gw.example.com",
355
340
  deviceId: "dev-num",
@@ -380,7 +365,7 @@ describe("pairAssistant", () => {
380
365
  "http://[0:0:0:0:0:ffff:127.0.0.1]:7830",
381
366
  "http://[::ffff:0.0.0.0]:7830",
382
367
  ]) {
383
- const result = pairAssistant(configDir, {
368
+ const result = pairAssistant({
384
369
  credentials: credentials({ gatewayUrl, deviceId: "dev-loop" }),
385
370
  });
386
371
 
@@ -400,7 +385,7 @@ describe("pairAssistant", () => {
400
385
  // Private-network literals are refused before a pairing is ever minted, so
401
386
  // advice to retry with a LAN address would send the user into a guaranteed
402
387
  // second failure.
403
- const result = pairAssistant(configDir, {
388
+ const result = pairAssistant({
404
389
  credentials: credentials({ gatewayUrl: "http://localhost:7830" }),
405
390
  });
406
391
 
@@ -416,7 +401,7 @@ describe("pairAssistant", () => {
416
401
  // refreshGuardianToken refuses to send the refresh token over plaintext
417
402
  // LAN URLs, so the pairing can never renew; report it access-only so the
418
403
  // expiry warning shows.
419
- const result = pairAssistant(configDir, {
404
+ const result = pairAssistant({
420
405
  credentials: credentials({
421
406
  gatewayUrl: "http://10.0.0.5:7830",
422
407
  deviceId: "dev-lan",
@@ -433,7 +418,7 @@ describe("pairAssistant", () => {
433
418
  });
434
419
 
435
420
  test("a name is slugified into the id while the entry keeps the raw name", () => {
436
- const result = pairAssistant(configDir, {
421
+ const result = pairAssistant({
437
422
  credentials: credentials(),
438
423
  name: "Desk Box",
439
424
  });
@@ -443,11 +428,11 @@ describe("pairAssistant", () => {
443
428
  return;
444
429
  }
445
430
  expect(result.assistantId).toBe("desk-box");
446
- expect(result.entry.name).toBe("Desk Box");
431
+ expect(result.name).toBe("Desk Box");
447
432
  });
448
433
 
449
434
  test("a name with no alphanumerics is refused with a 400", () => {
450
- const result = pairAssistant(configDir, {
435
+ const result = pairAssistant({
451
436
  credentials: credentials(),
452
437
  name: "!!!",
453
438
  });
@@ -461,7 +446,7 @@ describe("pairAssistant", () => {
461
446
  });
462
447
 
463
448
  test("the derived id is path-safe whatever the deviceId carries", () => {
464
- const result = pairAssistant(configDir, {
449
+ const result = pairAssistant({
465
450
  credentials: credentials({ deviceId: "-/../../tmp/x" }),
466
451
  });
467
452
 
@@ -472,13 +457,11 @@ describe("pairAssistant", () => {
472
457
  // The id keys on the gateway host, so an untrusted deviceId never reaches
473
458
  // the token path at all.
474
459
  expect(result.assistantId).toBe(DEFAULT_PAIRED_ID);
475
- expect(
476
- fs.existsSync(guardianTokenPath(configDir, result.assistantId)),
477
- ).toBe(true);
460
+ expect(fs.existsSync(guardianTokenPath(result.assistantId))).toBe(true);
478
461
  });
479
462
 
480
463
  test("a gatewayUrl with no host falls back to a random path-safe id", () => {
481
- const result = pairAssistant(configDir, {
464
+ const result = pairAssistant({
482
465
  credentials: credentials({ gatewayUrl: "mailto:ops@example.com" }),
483
466
  });
484
467
 
@@ -582,7 +565,7 @@ describe("pairAssistant", () => {
582
565
  expect(slashed.assistantId).toBe(bare.assistantId);
583
566
  }),
584
567
  test("a --name still wins over the gateway-derived default", () => {
585
- const result = pairAssistant(configDir, {
568
+ const result = pairAssistant({
586
569
  credentials: credentials(),
587
570
  name: "Desk",
588
571
  });
@@ -607,7 +590,7 @@ describe("pairAssistant", () => {
607
590
  activeAssistant: "desk",
608
591
  });
609
592
 
610
- const result = pairAssistant(configDir, {
593
+ const result = pairAssistant({
611
594
  credentials: credentials(),
612
595
  name: "desk",
613
596
  });
@@ -627,7 +610,7 @@ describe("pairAssistant", () => {
627
610
  port: 7830,
628
611
  },
629
612
  ]);
630
- expect(fs.existsSync(guardianTokenPath(configDir, "desk"))).toBe(false);
613
+ expect(fs.existsSync(guardianTokenPath("desk"))).toBe(false);
631
614
  });
632
615
 
633
616
  (test("re-importing under the same name is refused with the old address intact", () => {
@@ -660,15 +643,15 @@ describe("pairAssistant", () => {
660
643
  expect(readGuardianToken("desk").accessToken).toBe("t1");
661
644
  }),
662
645
  test("a guardian-token write failure surfaces as a 500 with nothing left behind", () => {
663
- // A config dir whose parent is a regular file makes the token write
664
- // fail deterministically (unlike chmod, which root ignores). The token
665
- // is the only thing pairing writes.
646
+ // A config home that is a regular file makes the ambient token-path
647
+ // resolution fail deterministically (unlike chmod, which root ignores).
648
+ // The token is the only thing pairing writes.
666
649
  const notADir = path.join(tmpDir, "not-a-dir");
667
650
  fs.writeFileSync(notADir, "");
668
- const unwritable = path.join(notADir, "config");
669
- const tokenPath = guardianTokenPath(unwritable, DEFAULT_PAIRED_ID);
651
+ process.env.XDG_CONFIG_HOME = notADir;
652
+ const tokenPath = guardianTokenPath(DEFAULT_PAIRED_ID);
670
653
 
671
- const result = pairAssistant(unwritable, {
654
+ const result = pairAssistant({
672
655
  credentials: credentials(),
673
656
  });
674
657
 
@@ -683,7 +666,7 @@ describe("pairAssistant", () => {
683
666
  }));
684
667
 
685
668
  test("an unparseable gatewayUrl is refused, not thrown", () => {
686
- const result = pairAssistant(configDir, {
669
+ const result = pairAssistant({
687
670
  credentials: credentials({ gatewayUrl: "not a url" }),
688
671
  });
689
672
 
@@ -715,7 +698,7 @@ describe("checkPairedAssistantName", () => {
715
698
  expect(refusal!.status).toBe(409);
716
699
  expect(refusal!.assistantId).toBe("desk");
717
700
 
718
- const real = pairAssistant(configDir, {
701
+ const real = pairAssistant({
719
702
  credentials: credentials(),
720
703
  name: "Desk",
721
704
  });
@@ -1023,7 +1006,7 @@ describe("loopback refusals agree across the pairing path", () => {
1023
1006
  expect(started.reason).toBe("invalid-address");
1024
1007
  expect(started.rejection).toBe("loopback");
1025
1008
 
1026
- const imported = pairAssistant(configDir, {
1009
+ const imported = pairAssistant({
1027
1010
  credentials: credentials({ gatewayUrl: address }),
1028
1011
  });
1029
1012
  expect(imported.ok).toBe(false);
@@ -1056,14 +1039,9 @@ describe("pairingPoll", () => {
1056
1039
  ok: true,
1057
1040
  status: "imported",
1058
1041
  assistantId: expect.stringMatching(/^paired-/) as unknown as string,
1042
+ name: "paired (gw.example.com)",
1059
1043
  accessOnly: false,
1060
- entry: {
1061
- assistantId: expect.stringMatching(/^paired-/) as unknown as string,
1062
- name: "paired (gw.example.com)",
1063
- cloud: "custom",
1064
- ipAddress: "gw.example.com",
1065
- port: 443,
1066
- },
1044
+ gatewayUrl: "https://gw.example.com",
1067
1045
  });
1068
1046
  expect(fetchCalls).toHaveLength(1);
1069
1047
  expect(fetchCalls[0]!.url).toBe(
@@ -1096,7 +1074,7 @@ describe("pairingPoll", () => {
1096
1074
  });
1097
1075
  const handle = await startFromLink();
1098
1076
 
1099
- const result = await pairingPoll(configDir, {
1077
+ const result = await pairingPoll({
1100
1078
  handle,
1101
1079
  name: "Desk Box",
1102
1080
  });
@@ -1105,14 +1083,9 @@ describe("pairingPoll", () => {
1105
1083
  ok: true,
1106
1084
  status: "imported",
1107
1085
  assistantId: "desk-box",
1086
+ name: "Desk Box",
1108
1087
  accessOnly: true,
1109
- entry: {
1110
- assistantId: "desk-box",
1111
- name: "Desk Box",
1112
- cloud: "custom",
1113
- ipAddress: "gw.example.com",
1114
- port: 443,
1115
- },
1088
+ gatewayUrl: "https://gw.example.com",
1116
1089
  });
1117
1090
  expect(readGuardianToken("desk-box").refreshToken).toBe("");
1118
1091
  });
@@ -1121,7 +1094,7 @@ describe("pairingPoll", () => {
1121
1094
  respond = () => json(202, pendingBody());
1122
1095
  const handle = await startFromLink();
1123
1096
 
1124
- const pending = await pairingPoll(configDir, { handle });
1097
+ const pending = await pairingPoll({ handle });
1125
1098
  expect(pending).toEqual({
1126
1099
  ok: true,
1127
1100
  status: "pending",
@@ -1131,7 +1104,7 @@ describe("pairingPoll", () => {
1131
1104
  expect(fs.existsSync(lockfilePath)).toBe(false);
1132
1105
 
1133
1106
  respond = () => json(200, approvedBody());
1134
- const imported = await pairingPoll(configDir, { handle });
1107
+ const imported = await pairingPoll({ handle });
1135
1108
  expect(imported.ok).toBe(true);
1136
1109
  if (!imported.ok) {
1137
1110
  return;
@@ -1158,7 +1131,7 @@ describe("pairingPoll", () => {
1158
1131
  expiresAt: new Date(Date.now() + 365 * 86_400_000).toISOString(),
1159
1132
  }),
1160
1133
  );
1161
- const extended = await pairingPoll(configDir, {
1134
+ const extended = await pairingPoll({
1162
1135
  handle: started.handle,
1163
1136
  });
1164
1137
  expect(extended.ok).toBe(true);
@@ -1170,7 +1143,7 @@ describe("pairingPoll", () => {
1170
1143
  // Shortening is the gateway's to do: the code may be revoked early.
1171
1144
  const sooner = new Date(Date.now() + 30_000).toISOString();
1172
1145
  respond = () => json(202, pendingBody({ expiresAt: sooner }));
1173
- const shortened = await pairingPoll(configDir, {
1146
+ const shortened = await pairingPoll({
1174
1147
  handle: started.handle,
1175
1148
  });
1176
1149
  expect(shortened.ok).toBe(true);
@@ -1197,7 +1170,7 @@ describe("pairingPoll", () => {
1197
1170
  );
1198
1171
  const handle = await startFromLink();
1199
1172
 
1200
- const pending = await pairingPoll(configDir, { handle });
1173
+ const pending = await pairingPoll({ handle });
1201
1174
 
1202
1175
  expect(pending.ok).toBe(true);
1203
1176
  if (!pending.ok || pending.status !== "pending") {
@@ -1222,7 +1195,7 @@ describe("pairingPoll", () => {
1222
1195
  respond = () => json(200, approvedBody());
1223
1196
  const handle = await startFromLink();
1224
1197
 
1225
- const refused = await pairingPoll(configDir, {
1198
+ const refused = await pairingPoll({
1226
1199
  handle,
1227
1200
  name: "desk",
1228
1201
  });
@@ -1241,7 +1214,7 @@ describe("pairingPoll", () => {
1241
1214
  expect(fetchCalls).toHaveLength(0);
1242
1215
 
1243
1216
  // The very same session completes once the caller picks a free name.
1244
- const imported = await pairingPoll(configDir, {
1217
+ const imported = await pairingPoll({
1245
1218
  handle,
1246
1219
  name: "spare",
1247
1220
  });
@@ -1281,14 +1254,14 @@ describe("pairingPoll", () => {
1281
1254
  respond = () => json(202, pendingBody());
1282
1255
 
1283
1256
  const cliHandle = await startFromLink();
1284
- await pairingPoll(configDir, {
1257
+ await pairingPoll({
1285
1258
  handle: cliHandle,
1286
1259
  platform: "cli",
1287
1260
  });
1288
1261
  expect(fetchCalls[0]!.body.platform).toBe("cli");
1289
1262
 
1290
1263
  const unknownHandle = await startFromLink();
1291
- await pairingPoll(configDir, {
1264
+ await pairingPoll({
1292
1265
  handle: unknownHandle,
1293
1266
  platform: "toaster",
1294
1267
  });
@@ -1309,7 +1282,7 @@ describe("pairingPoll", () => {
1309
1282
  return;
1310
1283
  }
1311
1284
 
1312
- const result = await pairingPoll(configDir, {
1285
+ const result = await pairingPoll({
1313
1286
  handle: started.handle,
1314
1287
  });
1315
1288
 
@@ -1328,7 +1301,7 @@ describe("pairingPoll", () => {
1328
1301
  json(401, { error: { code: "INVALID_OR_EXPIRED_DEVICE_CODE" } });
1329
1302
  const handle = await startFromLink();
1330
1303
 
1331
- const denied = await pairingPoll(configDir, { handle });
1304
+ const denied = await pairingPoll({ handle });
1332
1305
  expect(denied.ok).toBe(false);
1333
1306
  if (denied.ok) {
1334
1307
  return;
@@ -1336,7 +1309,7 @@ describe("pairingPoll", () => {
1336
1309
  expect(denied.reason).toBe("expired");
1337
1310
 
1338
1311
  // The session is gone, so a further poll cannot replay the spent code.
1339
- const again = await pairingPoll(configDir, { handle });
1312
+ const again = await pairingPoll({ handle });
1340
1313
  expect(again.ok).toBe(false);
1341
1314
  if (again.ok) {
1342
1315
  return;
@@ -1351,7 +1324,7 @@ describe("pairingPoll", () => {
1351
1324
  };
1352
1325
  const handle = await startFromLink();
1353
1326
 
1354
- const failed = await pairingPoll(configDir, { handle });
1327
+ const failed = await pairingPoll({ handle });
1355
1328
  expect(failed.ok).toBe(false);
1356
1329
  if (failed.ok) {
1357
1330
  return;
@@ -1359,7 +1332,7 @@ describe("pairingPoll", () => {
1359
1332
  expect(failed.reason).toBe("unreachable");
1360
1333
 
1361
1334
  respond = () => json(200, approvedBody());
1362
- const imported = await pairingPoll(configDir, { handle });
1335
+ const imported = await pairingPoll({ handle });
1363
1336
  expect(imported.ok).toBe(true);
1364
1337
  });
1365
1338
 
@@ -1367,7 +1340,7 @@ describe("pairingPoll", () => {
1367
1340
  respond = () => json(503, { error: { code: "GUARDIAN_REPAIR_REQUIRED" } });
1368
1341
  const handle = await startFromLink();
1369
1342
 
1370
- const refused = await pairingPoll(configDir, { handle });
1343
+ const refused = await pairingPoll({ handle });
1371
1344
 
1372
1345
  expect(refused.ok).toBe(false);
1373
1346
  if (refused.ok) {
@@ -1383,7 +1356,7 @@ describe("pairingPoll", () => {
1383
1356
  // The same session exchanges the still-live code once the gateway is
1384
1357
  // repaired; the user never has to mint and approve another one.
1385
1358
  respond = () => json(200, approvedBody());
1386
- const imported = await pairingPoll(configDir, { handle });
1359
+ const imported = await pairingPoll({ handle });
1387
1360
  expect(imported.ok).toBe(true);
1388
1361
  if (!imported.ok) {
1389
1362
  return;
@@ -1396,7 +1369,7 @@ describe("pairingPoll", () => {
1396
1369
  respond = () => json(200, { status: "approved" });
1397
1370
  const handle = await startFromLink();
1398
1371
 
1399
- const result = await pairingPoll(configDir, { handle });
1372
+ const result = await pairingPoll({ handle });
1400
1373
 
1401
1374
  expect(result.ok).toBe(false);
1402
1375
  if (result.ok) {
@@ -1428,7 +1401,7 @@ describe("pairingPoll", () => {
1428
1401
  respond = () => json(200, approvedBody(overrides));
1429
1402
  const handle = await startFromLink();
1430
1403
 
1431
- const result = await pairingPoll(configDir, { handle });
1404
+ const result = await pairingPoll({ handle });
1432
1405
 
1433
1406
  expect(result.ok).toBe(false);
1434
1407
  if (result.ok) {
@@ -1455,7 +1428,7 @@ describe("pairingPoll", () => {
1455
1428
  });
1456
1429
  const handle = await startFromLink();
1457
1430
 
1458
- const result = await pairingPoll(configDir, {
1431
+ const result = await pairingPoll({
1459
1432
  handle,
1460
1433
  name: "Old Gateway",
1461
1434
  });
@@ -1464,14 +1437,9 @@ describe("pairingPoll", () => {
1464
1437
  ok: true,
1465
1438
  status: "imported",
1466
1439
  assistantId: "old-gateway",
1440
+ name: "Old Gateway",
1467
1441
  accessOnly: true,
1468
- entry: {
1469
- assistantId: "old-gateway",
1470
- name: "Old Gateway",
1471
- cloud: "custom",
1472
- ipAddress: "gw.example.com",
1473
- port: 443,
1474
- },
1442
+ gatewayUrl: "https://gw.example.com",
1475
1443
  });
1476
1444
  const token = readGuardianToken("old-gateway");
1477
1445
  expect(token.accessToken).toBe("acc-tok");
@@ -1486,7 +1454,7 @@ describe("pairingPoll", () => {
1486
1454
  json(200, approvedBody({ refreshTokenExpiresAt: expiresAtMs }));
1487
1455
  const handle = await startFromLink();
1488
1456
 
1489
- const result = await pairingPoll(configDir, {
1457
+ const result = await pairingPoll({
1490
1458
  handle,
1491
1459
  name: "Numeric Expiry",
1492
1460
  });
@@ -1498,18 +1466,17 @@ describe("pairingPoll", () => {
1498
1466
  });
1499
1467
 
1500
1468
  test("a local write that fails after the exchange surfaces as an import failure", async () => {
1501
- // A config dir whose parent is a regular file makes the token write fail
1502
- // after the code has already been spent. That is the `import` reason:
1503
- // the session is gone and a retry needs a fresh code, which is exactly
1504
- // what the pre-check reason does NOT mean.
1469
+ // A config home that is a regular file makes the ambient token write
1470
+ // fail after the code has already been spent. That is the `import`
1471
+ // reason: the session is gone and a retry needs a fresh code, which is
1472
+ // exactly what the pre-check reason does NOT mean.
1505
1473
  const notADir = path.join(tmpDir, "not-a-dir");
1506
1474
  fs.writeFileSync(notADir, "");
1475
+ process.env.XDG_CONFIG_HOME = notADir;
1507
1476
  respond = () => json(200, approvedBody());
1508
1477
  const handle = await startFromLink();
1509
1478
 
1510
- const result = await pairingPoll(path.join(notADir, "config"), {
1511
- handle,
1512
- });
1479
+ const result = await pairingPoll({ handle });
1513
1480
 
1514
1481
  expect(result.ok).toBe(false);
1515
1482
  if (result.ok) {
@@ -1519,7 +1486,7 @@ describe("pairingPoll", () => {
1519
1486
  expect(pairingSessionSurvives(result.reason)).toBe(false);
1520
1487
  // The exchange did go out, so the code is spent and the session is gone.
1521
1488
  expect(fetchCalls).toHaveLength(1);
1522
- const again = await pairingPoll(configDir, { handle });
1489
+ const again = await pairingPoll({ handle });
1523
1490
  expect(again.ok).toBe(false);
1524
1491
  if (again.ok) {
1525
1492
  return;
@@ -1531,7 +1498,7 @@ describe("pairingPoll", () => {
1531
1498
  respond = () => redirectTo("http://169.254.169.254/latest/meta-data/");
1532
1499
  const handle = await startFromLink();
1533
1500
 
1534
- const redirected = await pairingPoll(configDir, { handle });
1501
+ const redirected = await pairingPoll({ handle });
1535
1502
 
1536
1503
  expect(redirected.ok).toBe(false);
1537
1504
  if (redirected.ok) {
@@ -1545,7 +1512,7 @@ describe("pairingPoll", () => {
1545
1512
  expect(fetchCalls[0]!.redirect).toBe("error");
1546
1513
 
1547
1514
  respond = () => json(200, approvedBody());
1548
- const retried = await pairingPoll(configDir, { handle });
1515
+ const retried = await pairingPoll({ handle });
1549
1516
  expect(retried.ok).toBe(true);
1550
1517
  });
1551
1518
 
@@ -1557,7 +1524,7 @@ describe("pairingPoll", () => {
1557
1524
  });
1558
1525
  const handle = await startFromLink();
1559
1526
 
1560
- const result = await pairingPoll(configDir, { handle });
1527
+ const result = await pairingPoll({ handle });
1561
1528
 
1562
1529
  expect(result.ok).toBe(false);
1563
1530
  if (result.ok) {
@@ -1572,7 +1539,7 @@ describe("pairingPoll", () => {
1572
1539
 
1573
1540
  test("an unknown handle is refused without a request", async () => {
1574
1541
  for (const handle of ["nope", "", undefined, 42]) {
1575
- const result = await pairingPoll(configDir, { handle });
1542
+ const result = await pairingPoll({ handle });
1576
1543
  expect(result.ok).toBe(false);
1577
1544
  if (result.ok) {
1578
1545
  continue;
@@ -1598,7 +1565,7 @@ describe("pairingCancel", () => {
1598
1565
  // A second cancel is a no-op, and the handle is dead.
1599
1566
  expect(pairingCancel(started.handle)).toBe(false);
1600
1567
 
1601
- const result = await pairingPoll(configDir, {
1568
+ const result = await pairingPoll({
1602
1569
  handle: started.handle,
1603
1570
  });
1604
1571
  expect(result.ok).toBe(false);
@@ -1638,7 +1605,7 @@ describe("pairingCancel", () => {
1638
1605
  test("a cancel while the exchange is in flight persists nothing", async () => {
1639
1606
  const approve = heldApproval();
1640
1607
  const handle = await startLinkSession();
1641
- const polled = pairingPoll(configDir, { handle });
1608
+ const polled = pairingPoll({ handle });
1642
1609
  await requestsInFlight(1);
1643
1610
 
1644
1611
  expect(pairingCancel(handle)).toBe(true);
@@ -1665,7 +1632,7 @@ describe("pairingCancel", () => {
1665
1632
  });
1666
1633
  });
1667
1634
  const handle = await startLinkSession();
1668
- const polled = pairingPoll(configDir, { handle });
1635
+ const polled = pairingPoll({ handle });
1669
1636
  await requestsInFlight(1);
1670
1637
 
1671
1638
  expect(pairingCancel(handle)).toBe(true);
@@ -1686,7 +1653,7 @@ describe("pairingCancel", () => {
1686
1653
  test("an exchange nobody cancels still imports", async () => {
1687
1654
  const approve = heldApproval();
1688
1655
  const handle = await startLinkSession();
1689
- const polled = pairingPoll(configDir, { handle });
1656
+ const polled = pairingPoll({ handle });
1690
1657
  await requestsInFlight(1);
1691
1658
 
1692
1659
  approve();
@@ -1696,7 +1663,7 @@ describe("pairingCancel", () => {
1696
1663
  if (!result.ok || result.status !== "imported") {
1697
1664
  return;
1698
1665
  }
1699
- saveLockfileAssistant(result.entry);
1666
+ registerImportedPairing(result);
1700
1667
  const entry = (
1701
1668
  readLockfileFromDisk().assistants as Array<Record<string, unknown>>
1702
1669
  )[0]!;
@@ -1738,7 +1705,7 @@ describe("pairingCancel", () => {
1738
1705
  return;
1739
1706
  }
1740
1707
 
1741
- const polled = pairingPoll(configDir, {
1708
+ const polled = pairingPoll({
1742
1709
  handle: started.handle,
1743
1710
  });
1744
1711
  await requestsInFlight(2);
@@ -1761,180 +1728,3 @@ describe("pairingCancel", () => {
1761
1728
  expect(fs.existsSync(path.join(configDir, "assistants"))).toBe(false);
1762
1729
  });
1763
1730
  });
1764
-
1765
- describe("connectImport", () => {
1766
- test("registers a valid bundle and returns the wire-shaped success", () => {
1767
- const result = importAndRegister(configDir, {
1768
- bundle: ` ${encodeBundle({
1769
- gatewayUrl: "https://gw.example.com",
1770
- token: "tok",
1771
- deviceId: "dev-ci",
1772
- })} `,
1773
- name: "Desk Box",
1774
- });
1775
-
1776
- expect(result).toEqual({
1777
- ok: true,
1778
- assistantId: "desk-box",
1779
- accessOnly: true,
1780
- entry: {
1781
- assistantId: "desk-box",
1782
- name: "Desk Box",
1783
- cloud: "custom",
1784
- ipAddress: "gw.example.com",
1785
- port: 443,
1786
- },
1787
- });
1788
- expect(fs.existsSync(guardianTokenPath(configDir, "desk-box"))).toBe(true);
1789
- const entry = (
1790
- readLockfileFromDisk().assistants as Array<Record<string, unknown>>
1791
- )[0]!;
1792
- expect(entry).toMatchObject({
1793
- assistantId: "desk-box",
1794
- cloud: "custom",
1795
- ipAddress: "gw.example.com",
1796
- port: 443,
1797
- });
1798
- });
1799
-
1800
- test("a legacy id naming another gateway is not adopted", () => {
1801
- // The device id is untrusted bundle input, so an entry that does not name
1802
- // the same gateway is left alone rather than repointed at another host.
1803
- writeLockfile({
1804
- assistants: [
1805
- {
1806
- assistantId: "paired-dev-legacy",
1807
- cloud: "custom",
1808
- ipAddress: "other.example.com",
1809
- port: 443,
1810
- },
1811
- ],
1812
- });
1813
-
1814
- const result = importAndRegister(configDir, {
1815
- bundle: encodeBundle({
1816
- gatewayUrl: "https://gw.example.com",
1817
- token: "tok-1",
1818
- deviceId: "dev-legacy",
1819
- }),
1820
- });
1821
-
1822
- expect(result).toMatchObject({ ok: true });
1823
- const entries = readLockfileFromDisk().assistants as Array<
1824
- Record<string, unknown>
1825
- >;
1826
- expect(entries).toHaveLength(2);
1827
- expect(
1828
- entries.find((e) => e.assistantId === "paired-dev-legacy"),
1829
- ).toMatchObject({ ipAddress: "other.example.com" });
1830
- });
1831
-
1832
- test("a missing, empty, or non-string bundle is a 400", () => {
1833
- for (const value of [undefined, "", 42]) {
1834
- expect(connectImport(configDir, { bundle: value })).toEqual({
1835
- ok: false,
1836
- status: 400,
1837
- error: "Missing pairing bundle",
1838
- });
1839
- }
1840
- expect(fs.existsSync(lockfilePath)).toBe(false);
1841
- });
1842
-
1843
- test("an oversized bundle is refused before decoding", () => {
1844
- expect(
1845
- connectImport(configDir, {
1846
- bundle: "a".repeat(64 * 1024 + 1),
1847
- }),
1848
- ).toEqual({
1849
- ok: false,
1850
- status: 400,
1851
- error: "Pairing bundle is too large",
1852
- });
1853
- expect(fs.existsSync(lockfilePath)).toBe(false);
1854
- });
1855
-
1856
- test("a loopback gatewayUrl keeps pairAssistant's 422 and refusal copy", () => {
1857
- const result = connectImport(configDir, {
1858
- bundle: encodeBundle({
1859
- gatewayUrl: "http://127.0.0.1:5432",
1860
- token: "tok",
1861
- deviceId: "dev-loop",
1862
- }),
1863
- });
1864
-
1865
- expect(result.ok).toBe(false);
1866
- if (result.ok) {
1867
- return;
1868
- }
1869
- expect(result.status).toBe(422);
1870
- expect(result.error).toContain("non-loopback");
1871
- expect(fs.existsSync(lockfilePath)).toBe(false);
1872
- });
1873
-
1874
- test("a malformed bundle maps the decode error to a 400", () => {
1875
- const result = connectImport(configDir, {
1876
- bundle: "not-base64-json!!!",
1877
- });
1878
-
1879
- expect(result.ok).toBe(false);
1880
- if (result.ok) {
1881
- return;
1882
- }
1883
- expect(result.status).toBe(400);
1884
- expect(result.error).toContain("base64");
1885
- expect(fs.existsSync(lockfilePath)).toBe(false);
1886
- });
1887
-
1888
- test("an overwrite refusal keeps pairAssistant's 409 and error string", () => {
1889
- writeLockfile({
1890
- assistants: [
1891
- {
1892
- assistantId: "desk",
1893
- cloud: "local",
1894
- ipAddress: "127.0.0.1",
1895
- port: 7830,
1896
- },
1897
- ],
1898
- activeAssistant: "desk",
1899
- });
1900
-
1901
- const result = connectImport(configDir, {
1902
- bundle: encodeBundle({
1903
- gatewayUrl: "https://gw.example.com",
1904
- token: "tok",
1905
- }),
1906
- name: "desk",
1907
- });
1908
-
1909
- expect(result).toEqual({
1910
- ok: false,
1911
- status: 409,
1912
- error: "An assistant named 'desk' already exists",
1913
- });
1914
- expect(fs.existsSync(guardianTokenPath(configDir, "desk"))).toBe(false);
1915
- });
1916
-
1917
- test("a non-string name is ignored rather than slugified", () => {
1918
- const result = connectImport(configDir, {
1919
- bundle: encodeBundle({
1920
- gatewayUrl: "https://gw.example.com",
1921
- token: "tok",
1922
- deviceId: "dev-nn",
1923
- }),
1924
- name: 7,
1925
- });
1926
-
1927
- expect(result).toEqual({
1928
- ok: true,
1929
- assistantId: "paired-gw-example-com",
1930
- accessOnly: true,
1931
- entry: {
1932
- assistantId: "paired-gw-example-com",
1933
- name: "paired (gw.example.com)",
1934
- cloud: "custom",
1935
- ipAddress: "gw.example.com",
1936
- port: 443,
1937
- },
1938
- });
1939
- });
1940
- });