@mycadet/cli 0.13.0-dev.202611012320.cea2f8a → 0.13.0-dev.202611031057.420f196

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 (138) 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/{src → node_modules/@mycadet/local-mode/src}/__tests__/device-id.test.ts +3 -6
  12. package/node_modules/@mycadet/local-mode/src/__tests__/guardian-token-refresh.test.ts +0 -1
  13. package/node_modules/@mycadet/local-mode/src/__tests__/guardian-token.test.ts +0 -1
  14. package/{src/__tests__/config-utils.test.ts → node_modules/@mycadet/local-mode/src/__tests__/hatch-config.test.ts} +1 -1
  15. package/{src/__tests__/local-hatch-lock.test.ts → node_modules/@mycadet/local-mode/src/__tests__/hatch-lock.test.ts} +1 -1
  16. package/node_modules/@mycadet/local-mode/src/__tests__/helpers/env.ts +19 -0
  17. package/node_modules/@mycadet/local-mode/src/__tests__/helpers/os-mock.ts +27 -0
  18. package/{src/__tests__/random-name.test.ts → node_modules/@mycadet/local-mode/src/__tests__/naming.test.ts} +1 -4
  19. package/node_modules/@mycadet/local-mode/src/__tests__/operations-hatch.test.ts +60 -0
  20. package/node_modules/@mycadet/local-mode/src/__tests__/operations-resolution.test.ts +340 -0
  21. package/node_modules/@mycadet/local-mode/src/__tests__/operations-retire.test.ts +7 -7
  22. package/node_modules/@mycadet/local-mode/src/__tests__/operations-routes.test.ts +634 -0
  23. package/node_modules/@mycadet/local-mode/src/__tests__/operations-sleep.test.ts +12 -4
  24. package/node_modules/@mycadet/local-mode/src/__tests__/operations-wake.test.ts +41 -22
  25. package/node_modules/@mycadet/local-mode/src/__tests__/pair.test.ts +0 -2
  26. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-docker-pair.test.ts +3 -3
  27. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-docker-recreate.test.ts +72 -0
  28. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-docker-statefulset.test.ts +307 -0
  29. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-docker-wake.test.ts +176 -0
  30. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-local-pair.test.ts +2 -2
  31. package/node_modules/@mycadet/local-mode/src/__tests__/remotes-local-retire.test.ts +0 -11
  32. package/node_modules/@mycadet/local-mode/src/__tests__/status.test.ts +1 -129
  33. package/node_modules/@mycadet/local-mode/src/cloud-registration.ts +614 -0
  34. package/node_modules/@mycadet/local-mode/src/daemon-readiness.ts +87 -11
  35. package/{src/lib → node_modules/@mycadet/local-mode/src}/device-id.ts +4 -4
  36. package/node_modules/@mycadet/local-mode/src/gateway-proxy.ts +0 -1
  37. package/node_modules/@mycadet/local-mode/src/guardian-token.ts +276 -6
  38. package/node_modules/@mycadet/local-mode/src/hatch.ts +0 -1
  39. package/node_modules/@mycadet/local-mode/src/index.ts +92 -20
  40. package/node_modules/@mycadet/local-mode/src/lockfile-contract.ts +5 -5
  41. package/node_modules/@mycadet/local-mode/src/lockfile.ts +1 -2
  42. package/node_modules/@mycadet/local-mode/src/operations/hatch.ts +50 -0
  43. package/node_modules/@mycadet/local-mode/src/operations/list.ts +33 -0
  44. package/node_modules/@mycadet/local-mode/src/operations/machine.ts +1 -1
  45. package/node_modules/@mycadet/local-mode/src/operations/resolve.ts +56 -0
  46. package/node_modules/@mycadet/local-mode/src/operations/routes.ts +487 -32
  47. package/node_modules/@mycadet/local-mode/src/operations/wake.ts +9 -2
  48. package/node_modules/@mycadet/local-mode/src/pair.ts +0 -5
  49. package/node_modules/@mycadet/local-mode/src/process.ts +1 -1
  50. package/node_modules/@mycadet/local-mode/src/provider-credentials.ts +495 -0
  51. package/{src/shared → node_modules/@mycadet/local-mode/src}/provider-env-vars.ts +3 -20
  52. package/node_modules/@mycadet/local-mode/src/remotes/apple-container/retire.ts +0 -5
  53. package/node_modules/@mycadet/local-mode/src/remotes/docker/common.ts +1 -3
  54. package/node_modules/@mycadet/local-mode/src/remotes/docker/recreate.ts +193 -0
  55. package/node_modules/@mycadet/local-mode/src/remotes/docker/retire.ts +2 -10
  56. package/{src/lib → node_modules/@mycadet/local-mode/src/remotes/docker}/statefulset.ts +22 -7
  57. package/node_modules/@mycadet/local-mode/src/remotes/docker/wake.ts +35 -23
  58. package/node_modules/@mycadet/local-mode/src/remotes/local/common.ts +26 -53
  59. package/{src/lib/config-utils.ts → node_modules/@mycadet/local-mode/src/remotes/local/hatch-config.ts} +3 -3
  60. package/{src/lib/local-hatch-lock.ts → node_modules/@mycadet/local-mode/src/remotes/local/hatch-lock.ts} +6 -6
  61. package/node_modules/@mycadet/local-mode/src/remotes/local/hatch.ts +124 -0
  62. package/node_modules/@mycadet/local-mode/src/remotes/local/retire.ts +1 -6
  63. package/node_modules/@mycadet/local-mode/src/remotes/local/wake.ts +3 -13
  64. package/node_modules/@mycadet/local-mode/src/status.ts +1 -366
  65. package/node_modules/@mycadet/local-mode/src/upgrade.ts +0 -1
  66. package/node_modules/@mycadet/service-contracts/package.json +1 -1
  67. package/node_modules/@mycadet/shared/package.json +1 -1
  68. package/package.json +1 -2
  69. package/src/__tests__/app-command.test.ts +30 -45
  70. package/src/__tests__/app-tunnel-status.test.ts +1 -1
  71. package/src/__tests__/assistant-client-refresh.test.ts +0 -2
  72. package/src/__tests__/backup.test.ts +1 -1
  73. package/src/__tests__/client-tui-refresh.test.ts +0 -1
  74. package/src/__tests__/debug.test.ts +1 -1
  75. package/src/__tests__/devices.test.ts +1 -1
  76. package/src/__tests__/guardian-token.test.ts +5 -7
  77. package/src/__tests__/hatch-provider-secrets.test.ts +4 -2
  78. package/src/__tests__/helpers/guardian-token.ts +1 -2
  79. package/src/__tests__/host-teleport-docker.test.ts +115 -42
  80. package/src/__tests__/host-teleport.test.ts +46 -26
  81. package/src/__tests__/llm-provider-env-var-parity.test.ts +1 -1
  82. package/src/__tests__/multi-local.test.ts +1 -1
  83. package/src/__tests__/provider-secrets.test.ts +2 -3
  84. package/src/__tests__/ps-cloud-status.test.ts +7 -7
  85. package/src/__tests__/root-backup-docker.test.ts +1 -1
  86. package/src/__tests__/root-backup.test.ts +6 -3
  87. package/src/__tests__/root-restore-command.test.ts +1 -1
  88. package/src/__tests__/search-provider-env-var-parity.test.ts +2 -2
  89. package/src/__tests__/servers/app-server-state.test.ts +195 -0
  90. package/src/__tests__/servers/bun.test.ts +1647 -0
  91. package/src/__tests__/setup.test.ts +0 -3
  92. package/src/__tests__/tui-midsession-refresh.test.ts +0 -1
  93. package/src/__tests__/tunnel.test.ts +15 -296
  94. package/src/__tests__/upgrade-local.test.ts +4 -5
  95. package/src/__tests__/upgrade-replay-env.test.ts +4 -2
  96. package/src/__tests__/upgrade-root-recovery.test.ts +2 -2
  97. package/src/__tests__/wake.test.ts +37 -650
  98. package/src/__tests__/web-dist.test.ts +39 -5
  99. package/src/commands/app.ts +26 -28
  100. package/src/commands/debug.ts +2 -1
  101. package/src/commands/devices.ts +2 -1
  102. package/src/commands/exec.ts +1 -1
  103. package/src/commands/hatch.ts +70 -2
  104. package/src/commands/login.ts +3 -6
  105. package/src/commands/ps.ts +17 -12
  106. package/src/commands/setup.ts +3 -3
  107. package/src/commands/tunnel.ts +6 -9
  108. package/src/commands/upgrade.ts +1 -4
  109. package/src/commands/wake.ts +8 -114
  110. package/src/lib/__tests__/docker.test.ts +0 -228
  111. package/src/lib/api-key-check.ts +1 -1
  112. package/src/lib/assistant-config.ts +1 -43
  113. package/src/lib/cloud-client.ts +0 -404
  114. package/src/lib/cloud-login.ts +7 -110
  115. package/src/lib/cloud-releases.ts +1 -1
  116. package/src/lib/guardian-token.ts +4 -297
  117. package/src/lib/host-teleport.ts +83 -227
  118. package/src/lib/provider-secrets.ts +7 -494
  119. package/src/lib/remotes/baseten/index.ts +2 -4
  120. package/src/lib/remotes/docker.ts +132 -213
  121. package/src/lib/remotes/local.ts +36 -150
  122. package/src/lib/root-backup.ts +88 -14
  123. package/src/lib/servers/bun.ts +548 -0
  124. package/src/lib/servers/common.ts +946 -0
  125. package/src/lib/sync-cloud-assistants.ts +2 -1
  126. package/src/lib/tunnel.ts +4 -239
  127. package/src/lib/tunnels/cloudflare.ts +6 -6
  128. package/src/lib/tunnels/ngrok.ts +3 -5
  129. package/src/lib/tunnels/tailscale.ts +1 -1
  130. package/src/lib/upgrade-lifecycle.ts +6 -39
  131. package/src/lib/web-dist.ts +47 -20
  132. package/node_modules/@mycadet/local-mode/src/ingress.ts +0 -229
  133. package/src/__tests__/assistant-api-key-resolution.test.ts +0 -102
  134. package/src/__tests__/nginx.test.ts +0 -2447
  135. package/src/__tests__/statefulset.test.ts +0 -152
  136. package/src/lib/assistant-api-key-resolution.ts +0 -89
  137. package/src/lib/servers/nginx.ts +0 -1241
  138. /package/{src/lib/random-name.ts → node_modules/@mycadet/local-mode/src/naming.ts} +0 -0
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/service-contracts",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/avatar-manifest",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/environments",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/ipc-server-utils",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/avatar-manifest",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/environments",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/ipc-server-utils",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/service-contracts",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/shared",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "exports": {
6
6
  ".": "./github-repository.ts",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/local-mode",
3
- "version": "0.13.0-dev.202611012320.cea2f8a",
3
+ "version": "0.13.0-dev.202611031057.420f196",
4
4
  "private": true,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,12 +16,9 @@ import { join } from "path";
16
16
  let fakeHome: string | undefined;
17
17
  await mockOsHomedir((realHomedir) => () => fakeHome ?? realHomedir());
18
18
 
19
- import {
20
- getOrCreateHostDeviceId,
21
- resetHostDeviceIdCache,
22
- } from "../lib/device-id.js";
23
- import { snapshotEnv } from "./helpers/env.js";
24
- import { mockOsHomedir } from "./helpers/os-mock.js";
19
+ import { getOrCreateHostDeviceId, resetHostDeviceIdCache } from "../device-id";
20
+ import { snapshotEnv } from "./helpers/env";
21
+ import { mockOsHomedir } from "./helpers/os-mock";
25
22
 
26
23
  const UUID_RE =
27
24
  /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
@@ -52,7 +52,6 @@ function makeTokenData(over: Partial<GuardianTokenData>): GuardianTokenData {
52
52
  refreshToken: "refresh",
53
53
  refreshTokenExpiresAt: FUTURE,
54
54
  refreshAfter: FUTURE,
55
- isNew: false,
56
55
  deviceId: "device",
57
56
  leasedAt: new Date().toISOString(),
58
57
  ...over,
@@ -32,7 +32,6 @@ function makeTokenData(over: Partial<GuardianTokenData>): GuardianTokenData {
32
32
  refreshToken: "refresh",
33
33
  refreshTokenExpiresAt: FUTURE,
34
34
  refreshAfter: FUTURE,
35
- isNew: false,
36
35
  deviceId: "device",
37
36
  leasedAt: new Date().toISOString(),
38
37
  ...over,
@@ -5,7 +5,7 @@ import {
5
5
  buildHatchConfigValues,
6
6
  buildNestedConfig,
7
7
  writeInitialConfig,
8
- } from "../lib/config-utils.js";
8
+ } from "../remotes/local/hatch-config";
9
9
 
10
10
  function readInitialConfig(
11
11
  configValues: Record<string, string>,
@@ -6,7 +6,7 @@ import { join } from "path";
6
6
  import {
7
7
  resolveLocalHatchLockPath,
8
8
  withLocalHatchLock,
9
- } from "../lib/local-hatch-lock.js";
9
+ } from "../remotes/local/hatch-lock";
10
10
 
11
11
  const tempDirs: string[] = [];
12
12
 
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Snapshot the given env vars now; returns a restore function suitable for
3
+ * `afterEach` that resets each var to its captured value (or deletes it).
4
+ */
5
+ export function snapshotEnv(keys: readonly string[]): () => void {
6
+ const saved: Record<string, string | undefined> = {};
7
+ for (const key of keys) {
8
+ saved[key] = process.env[key];
9
+ }
10
+ return () => {
11
+ for (const key of keys) {
12
+ if (saved[key] === undefined) {
13
+ delete process.env[key];
14
+ } else {
15
+ process.env[key] = saved[key];
16
+ }
17
+ }
18
+ };
19
+ }
@@ -0,0 +1,27 @@
1
+ import { mock } from "bun:test";
2
+
3
+ /**
4
+ * Mock `os.homedir()` (both the `"os"` and `"node:os"` specifiers) while
5
+ * keeping every other os export intact.
6
+ *
7
+ * The factory passed to `mock.module()` must only close over plain snapshots
8
+ * captured before the mock is installed. When another test file has already
9
+ * loaded "os", `mock.module()` patches the live namespace in place — a
10
+ * factory that spreads or calls back through that namespace resolves to the
11
+ * mock itself and recurses forever, a synchronous spin that froze the whole
12
+ * suite (and CI) at whichever file loaded next. This helper owns that
13
+ * invariant so test files don't have to.
14
+ *
15
+ * @param makeHomedir Receives the real (pre-mock) `homedir` and returns the
16
+ * replacement implementation.
17
+ */
18
+ export async function mockOsHomedir(
19
+ makeHomedir: (realHomedir: () => string) => () => string,
20
+ ): Promise<void> {
21
+ const realOs = await import("node:os");
22
+ const realOsSnapshot = { ...realOs };
23
+ const homedir = makeHomedir(realOs.homedir);
24
+ const factory = () => ({ ...realOsSnapshot, homedir });
25
+ mock.module("node:os", factory);
26
+ mock.module("os", factory);
27
+ }
@@ -1,8 +1,5 @@
1
1
  import { describe, test, expect } from "bun:test";
2
- import {
3
- generateRandomSuffix,
4
- generateInstanceName,
5
- } from "../lib/random-name.js";
2
+ import { generateRandomSuffix, generateInstanceName } from "../naming";
6
3
 
7
4
  describe("generateRandomSuffix", () => {
8
5
  test("returns a string in adjective-noun-nanoid format", () => {
@@ -0,0 +1,60 @@
1
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, rmSync } from "node:fs";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+
6
+ import { hatchAssistant } from "../operations/hatch";
7
+ import { readLockfile, saveLockfileAssistant } from "../lockfile";
8
+ import { findAssistantByName } from "../operations/lookup";
9
+
10
+ let home: string;
11
+ const ENV_KEYS = ["XDG_CONFIG_HOME", "XDG_DATA_HOME"] as const;
12
+ const saved = new Map<string, string | undefined>();
13
+
14
+ beforeEach(() => {
15
+ home = mkdtempSync(path.join(os.tmpdir(), "hatch-op-"));
16
+ for (const key of ENV_KEYS) {
17
+ saved.set(key, process.env[key]);
18
+ process.env[key] = path.join(home, key.toLowerCase());
19
+ }
20
+ });
21
+
22
+ afterEach(() => {
23
+ for (const [key, value] of saved) {
24
+ if (value === undefined) Reflect.deleteProperty(process.env, key);
25
+ else process.env[key] = value;
26
+ }
27
+ rmSync(home, { recursive: true, force: true });
28
+ });
29
+
30
+ describe("hatchAssistant", () => {
31
+ test("the router refuses the remotes it does not cover yet", async () => {
32
+ for (const remote of ["docker", "baseten", "cloud", "custom"]) {
33
+ const result = await hatchAssistant({ remote });
34
+ expect(result.ok).toBe(false);
35
+ if (result.ok) continue;
36
+ expect(result.status).toBe(400);
37
+ expect(result.error).toContain(remote);
38
+ }
39
+ expect(findAssistantByName("docker")).toBeNull();
40
+ });
41
+
42
+ test("a duplicate local name answers 409 before anything starts", async () => {
43
+ saveLockfileAssistant({
44
+ assistantId: "hatch-op-dup",
45
+ cloud: "local",
46
+ instanceDir: path.join(home, "assistants", "hatch-op-dup"),
47
+ port: 7830,
48
+ daemonPort: 7821,
49
+ cesPort: 7832,
50
+ });
51
+ const result = await hatchAssistant({ remote: "local", name: "hatch-op-dup" });
52
+ expect(result.ok).toBe(false);
53
+ if (result.ok) return;
54
+ expect(result.status).toBe(409);
55
+ expect(result.error).toContain("already hatched");
56
+ expect(result.assistantId).toBe("hatch-op-dup");
57
+ // The pre-existing entry is untouched — no second allocation ran.
58
+ expect(readLockfile().assistants).toHaveLength(1);
59
+ });
60
+ });
@@ -0,0 +1,340 @@
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 realLoopbackFetch = { ...(await import("../loopback-fetch")) };
18
+ const realDaemonReadiness = { ...(await import("../daemon-readiness")) };
19
+
20
+ const loopbackSafeFetchMock = mock(
21
+ async (_input: string | URL, _init?: RequestInit): Promise<Response> =>
22
+ new Response(null, { status: 204 }),
23
+ );
24
+ const getGatewayHealthMock = mock(
25
+ async (
26
+ _entry: unknown,
27
+ _opts?: { signal?: AbortSignal; headers?: unknown },
28
+ ): Promise<{
29
+ version?: string;
30
+ authenticated: boolean;
31
+ }> => ({ version: undefined, authenticated: true }),
32
+ );
33
+
34
+ mock.module("../loopback-fetch", () => ({
35
+ ...realLoopbackFetch,
36
+ loopbackSafeFetch: loopbackSafeFetchMock,
37
+ }));
38
+ mock.module("../daemon-readiness", () => ({
39
+ ...realDaemonReadiness,
40
+ getGatewayHealth: getGatewayHealthMock,
41
+ }));
42
+
43
+ // Restore the real modules once this file finishes so the mocks above do not
44
+ // leak into other test files in the same `bun test` run.
45
+ afterAll(() => {
46
+ mock.module("../loopback-fetch", () => realLoopbackFetch);
47
+ mock.module("../daemon-readiness", () => realDaemonReadiness);
48
+ });
49
+
50
+ import { readLockfile } from "../lockfile";
51
+ import { OPERATOR_ROUTES, matchOperatorRequest } from "../operations/routes";
52
+
53
+ let tmpDir: string;
54
+ let lockfilePath: string;
55
+
56
+ beforeEach(() => {
57
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "cadet-resolution-op-"));
58
+ lockfilePath = path.join(tmpDir, "cadet", "lockfile.json");
59
+ process.env.XDG_CONFIG_HOME = tmpDir;
60
+ delete process.env.CADET_PLATFORM_TOKEN;
61
+ delete process.env.CADET_CLOUD_TOKEN;
62
+ for (const m of [loopbackSafeFetchMock, getGatewayHealthMock]) {
63
+ m.mockReset();
64
+ }
65
+ loopbackSafeFetchMock.mockResolvedValue(new Response(null, { status: 204 }));
66
+ getGatewayHealthMock.mockResolvedValue({
67
+ version: undefined,
68
+ authenticated: true,
69
+ });
70
+ });
71
+
72
+ afterEach(() => {
73
+ fs.rmSync(tmpDir, { recursive: true, force: true });
74
+ });
75
+
76
+ const writeLockfile = (data: Record<string, unknown>): void => {
77
+ fs.mkdirSync(path.dirname(lockfilePath), { recursive: true });
78
+ fs.writeFileSync(lockfilePath, JSON.stringify(data));
79
+ };
80
+
81
+ const sentHeaders = (): Headers => {
82
+ const forwarded = getGatewayHealthMock.mock.calls[0]?.[1]?.headers;
83
+ if (!(forwarded instanceof Headers)) {
84
+ throw new Error("the resolution did not forward a Headers instance");
85
+ }
86
+ return forwarded;
87
+ };
88
+
89
+ let callerHeaders: Headers;
90
+
91
+ const runResolution = async (
92
+ headers: Record<string, string> = {},
93
+ requestPath = "/v1/assistant",
94
+ ): Promise<Response> => {
95
+ const match = matchOperatorRequest(requestPath, "GET");
96
+ if (!match) throw new Error("the resolution row did not match");
97
+ callerHeaders = new Headers(headers);
98
+ return (await match.route.run({
99
+ assistantId: match.assistantId,
100
+ headers: callerHeaders,
101
+ }))!;
102
+ };
103
+
104
+ const resolutionBody = async (response: Response): Promise<unknown> =>
105
+ (await response.json()) as unknown;
106
+
107
+ describe("matchOperatorRequest — the four prefixes", () => {
108
+ test("the resolution answers every assistant-reference spelling", () => {
109
+ for (const requestPath of [
110
+ "/v1/assistant",
111
+ "/v1/assistant/",
112
+ "/assistant",
113
+ "/assistant/",
114
+ "/v1/assistants/a1",
115
+ "/v1/assistants/a1/",
116
+ "/assistants/a1",
117
+ "/assistants/a1/",
118
+ ]) {
119
+ expect(matchOperatorRequest(requestPath, "GET")).not.toBeNull();
120
+ }
121
+ });
122
+
123
+ test("a scoped match carries the decoded id; an unscoped one names the active entry", () => {
124
+ writeLockfile({
125
+ assistants: [
126
+ { assistantId: "a1", cloud: "local", port: 7830 },
127
+ { assistantId: "a2", cloud: "local", port: 7831 },
128
+ ],
129
+ activeAssistant: "a2",
130
+ });
131
+ expect(matchOperatorRequest("/v1/assistants/a1/", "GET")?.assistantId).toBe(
132
+ "a1",
133
+ );
134
+ // Unscoped: the operator backend's active assistant, from the lockfile.
135
+ expect(matchOperatorRequest("/v1/assistant", "GET")?.assistantId).toBe(
136
+ "a2",
137
+ );
138
+ });
139
+
140
+ test("does not match other methods or foreign tails", () => {
141
+ expect(matchOperatorRequest("/v1/assistant", "POST")).toBeNull();
142
+ expect(matchOperatorRequest("/v1/assistant", "DELETE")).toBeNull();
143
+ expect(matchOperatorRequest("/v1/assistant/meta", "GET")).toBeNull();
144
+ expect(matchOperatorRequest("/v1/assistant/meta", "DELETE")).toBeNull();
145
+ });
146
+
147
+ test("the derivation helpers give every row both spellings", () => {
148
+ for (const route of OPERATOR_ROUTES) {
149
+ const derived = route.pattern.source;
150
+ if (derived.includes("(?<id>")) {
151
+ // An assistant row: helper 2's id capture plus helper 1's /v1 arm.
152
+ expect(derived).toContain("(?:\\/v1)?");
153
+ expect(derived).toContain("|\\/assistant");
154
+ } else {
155
+ // An absolute row: helper 1's /v1 arm, no scoping.
156
+ expect(derived).toContain("(?:\\/v1)?");
157
+ expect(derived).not.toContain("(?<id>");
158
+ }
159
+ }
160
+ });
161
+ });
162
+
163
+ describe("runResolveRoute — the lockfile arms", () => {
164
+ test("an unpaired machine answers empty", async () => {
165
+ const response = await runResolution();
166
+ expect(response.status).toBe(200);
167
+ expect(await resolutionBody(response)).toEqual({ status: "empty" });
168
+ });
169
+
170
+ test("the single entry resolves ready through the gateway health read", async () => {
171
+ writeLockfile({
172
+ assistants: [{ assistantId: "a1", cloud: "local", port: 7830 }],
173
+ activeAssistant: null,
174
+ });
175
+ getGatewayHealthMock.mockResolvedValue({
176
+ version: "0.13.0",
177
+ authenticated: true,
178
+ });
179
+
180
+ const response = await runResolution({
181
+ cookie: "cadet_web_access=tok",
182
+ });
183
+
184
+ expect(response.status).toBe(200);
185
+ expect(await resolutionBody(response)).toEqual({
186
+ status: "ready",
187
+ assistant: { id: "a1", version: "0.13.0" },
188
+ });
189
+ // The health read rides the ENTRY (the gateway url is resolved inside),
190
+ // and the caller's credential headers are forwarded so the verdict is
191
+ // the caller's.
192
+ // The caller's headers ride VERBATIM — the instance the caller sent,
193
+ // no flattening in between.
194
+ expect(sentHeaders()).toBe(callerHeaders);
195
+ expect(callerHeaders.get("cookie")).toBe("cadet_web_access=tok");
196
+ });
197
+
198
+ test("an explicit id resolves that entry even when another is active", async () => {
199
+ writeLockfile({
200
+ assistants: [
201
+ { assistantId: "a1", cloud: "local", port: 7830 },
202
+ { assistantId: "a2", cloud: "local", port: 7831 },
203
+ ],
204
+ activeAssistant: "a2",
205
+ });
206
+ getGatewayHealthMock.mockResolvedValue({
207
+ version: "1.4.0",
208
+ authenticated: true,
209
+ });
210
+
211
+ const response = await runResolution({}, "/v1/assistants/a1/");
212
+
213
+ expect(await resolutionBody(response)).toEqual({
214
+ status: "ready",
215
+ assistant: { id: "a1", version: "1.4.0" },
216
+ });
217
+ });
218
+
219
+ test("several entries and no active pointer hand off to the chooser", async () => {
220
+ writeLockfile({
221
+ assistants: [
222
+ { assistantId: "a1", cloud: "local", port: 7830, name: "One" },
223
+ { assistantId: "a2", cloud: "docker", port: 7831 },
224
+ ],
225
+ activeAssistant: null,
226
+ });
227
+
228
+ const response = await runResolution();
229
+
230
+ expect(response.status).toBe(200);
231
+ expect(await resolutionBody(response)).toEqual({
232
+ status: "selection",
233
+ assistants: [
234
+ { id: "a1", name: "One", remote: "local" },
235
+ { id: "a2", remote: "docker" },
236
+ ],
237
+ });
238
+ expect(getGatewayHealthMock).not.toHaveBeenCalled();
239
+ });
240
+
241
+ test("a health read with no verdict and no version answers notReady", async () => {
242
+ writeLockfile({
243
+ assistants: [{ assistantId: "a1", cloud: "local", port: 7830 }],
244
+ activeAssistant: null,
245
+ });
246
+ getGatewayHealthMock.mockResolvedValue({
247
+ version: undefined,
248
+ authenticated: true,
249
+ });
250
+
251
+ const response = await runResolution();
252
+
253
+ expect(await resolutionBody(response)).toEqual({ status: "notReady" });
254
+ });
255
+
256
+ test("a rejected verdict answers unauthenticated", async () => {
257
+ writeLockfile({
258
+ assistants: [{ assistantId: "a1", cloud: "local", port: 7830 }],
259
+ activeAssistant: null,
260
+ });
261
+ getGatewayHealthMock.mockResolvedValue({
262
+ version: "0.13.0",
263
+ authenticated: false,
264
+ });
265
+
266
+ const response = await runResolution();
267
+
268
+ expect(await resolutionBody(response)).toEqual({
269
+ status: "unauthenticated",
270
+ });
271
+ });
272
+
273
+ test("requests without credentials forward no credential headers", async () => {
274
+ writeLockfile({
275
+ assistants: [{ assistantId: "a1", cloud: "local", port: 7830 }],
276
+ activeAssistant: null,
277
+ });
278
+
279
+ await runResolution();
280
+
281
+ expect(sentHeaders()).toBe(callerHeaders);
282
+ expect([...callerHeaders]).toEqual([]);
283
+ });
284
+
285
+ test("an id the lockfile does not know passes (null)", async () => {
286
+ writeLockfile({
287
+ assistants: [{ assistantId: "a1", cloud: "local", port: 7830 }],
288
+ activeAssistant: null,
289
+ });
290
+
291
+ const response = await runResolution({}, "/v1/assistants/ghost/");
292
+
293
+ expect(response).toBeNull();
294
+ });
295
+ });
296
+
297
+ describe("runResolveRoute — the cloud leg", () => {
298
+ test("a cloud entry resolves through the same remote-agnostic health read", async () => {
299
+ writeLockfile({
300
+ assistants: [{ assistantId: "c1", cloud: "cloud" }],
301
+ activeAssistant: null,
302
+ });
303
+ getGatewayHealthMock.mockResolvedValue({
304
+ version: "9.9.9",
305
+ authenticated: true,
306
+ });
307
+
308
+ const response = await runResolution({
309
+ cookie: "session=abc",
310
+ authorization: "Bearer tok",
311
+ });
312
+
313
+ expect(response.status).toBe(200);
314
+ expect(await resolutionBody(response)).toEqual({
315
+ status: "ready",
316
+ assistant: { id: "c1", version: "9.9.9" },
317
+ });
318
+ // The health read rides the ENTRY — the platform's own gateway address
319
+ // is resolved inside, the caller's credential headers forwarded so the
320
+ // verdict is the caller's.
321
+ // The caller's headers ride VERBATIM — the instance the caller sent,
322
+ // no flattening in between.
323
+ expect(sentHeaders()).toBe(callerHeaders);
324
+ expect(callerHeaders.get("cookie")).toBe("session=abc");
325
+ expect(callerHeaders.get("authorization")).toBe("Bearer tok");
326
+ });
327
+ });
328
+
329
+ describe("the lockfile is untouched by resolution", () => {
330
+ test("the read-only operation leaves the registry as it found it", async () => {
331
+ writeLockfile({
332
+ assistants: [{ assistantId: "a1", cloud: "local", port: 7830 }],
333
+ activeAssistant: null,
334
+ });
335
+
336
+ await runResolution();
337
+
338
+ expect(readLockfile().assistants).toHaveLength(1);
339
+ });
340
+ });
@@ -154,7 +154,7 @@ const seedEntry = (entry: Record<string, unknown>) => {
154
154
  };
155
155
 
156
156
  describe("retireAssistant — the headless dispatch", () => {
157
- test("docker retire stops the containers then the app-server edge", async () => {
157
+ test("docker retire stops the containers", async () => {
158
158
  const entry = seedEntry({
159
159
  assistantId: "asst-docker",
160
160
  cloud: "docker",
@@ -168,7 +168,7 @@ describe("retireAssistant — the headless dispatch", () => {
168
168
  expect(readLockfile().assistants).toEqual([]);
169
169
  });
170
170
 
171
- test("apple-container retire sends the socket command then stops the edge", async () => {
171
+ test("apple-container retire sends the socket command", async () => {
172
172
  const entry = seedEntry({
173
173
  assistantId: "asst-apple",
174
174
  cloud: "apple-container",
@@ -265,7 +265,7 @@ describe("the operator route table — the scoped surface's retire arm", () => {
265
265
  const response = await matchOperatorRequest(
266
266
  "/v1/assistants/asst-local/retire/",
267
267
  "DELETE",
268
- )!.route.run("asst-local");
268
+ )!.route.run({ assistantId: "asst-local" });
269
269
 
270
270
  expect(response?.status).toBe(204);
271
271
  expect(retireLocalMock).toHaveBeenCalledWith(
@@ -281,7 +281,7 @@ describe("the operator route table — the scoped surface's retire arm", () => {
281
281
  const response = await matchOperatorRequest(
282
282
  "/v1/assistants/asst-docker/retire/",
283
283
  "DELETE",
284
- )!.route.run("asst-docker");
284
+ )!.route.run({ assistantId: "asst-docker" });
285
285
 
286
286
  expect(response?.status).toBe(500);
287
287
  expect(await response?.json()).toEqual({ detail: "no such assistant" });
@@ -298,7 +298,7 @@ describe("the operator route table — the scoped surface's retire arm", () => {
298
298
  await matchOperatorRequest(
299
299
  "/v1/assistants/asst-cloud/retire/",
300
300
  "DELETE",
301
- )!.route.run("asst-cloud"),
301
+ )!.route.run({ assistantId: "asst-cloud" }),
302
302
  ).toBeNull();
303
303
  expect(retireLocalMock).not.toHaveBeenCalled();
304
304
  expect(retireDockerMock).not.toHaveBeenCalled();
@@ -310,7 +310,7 @@ describe("the operator route table — the scoped surface's retire arm", () => {
310
310
  const response = await matchOperatorRequest(
311
311
  "/v1/assistants/asst-paired/retire/",
312
312
  "DELETE",
313
- )!.route.run("asst-paired");
313
+ )!.route.run({ assistantId: "asst-paired" });
314
314
 
315
315
  expect(response?.status).toBe(204);
316
316
  expect(execMock.mock.calls[0]![0]).toBe("ssh");
@@ -324,7 +324,7 @@ describe("the operator route table — the scoped surface's retire arm", () => {
324
324
  await matchOperatorRequest(
325
325
  "/v1/assistants/ghost/retire/",
326
326
  "DELETE",
327
- )!.route.run("ghost"),
327
+ )!.route.run({ assistantId: "ghost" }),
328
328
  ).toBeNull();
329
329
  expect(retireLocalMock).not.toHaveBeenCalled();
330
330
  });