@mycadet/cli 0.13.0-dev.202610181631.3760abb → 0.13.0-dev.202610192200.a3394a1

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.
@@ -26,8 +26,8 @@ function portBlock(base: number): PortMap {
26
26
 
27
27
  /**
28
28
  * Base URL of the cloud-hosted assistant SPA (the "hub") for a deployment
29
- * environment: always `${SEEDS[env].webUrl}/assistant`. The Capacitor mobile
30
- * shell bakes this into native builds as `server.url`, and the CLI's
29
+ * environment: always `${SEEDS[env].webUrl}/assistant`. The Capacitor boot
30
+ * bundle stamps this into baked mobile builds as `handoffUrl`, and the CLI's
31
31
  * remote-web edge stamps it into the served config as `hubUrl`, so the
32
32
  * environment-to-hub mapping lives here exactly once. Unknown environment
33
33
  * names fall back to the dev SPA defensively.
@@ -26,8 +26,8 @@ function portBlock(base: number): PortMap {
26
26
 
27
27
  /**
28
28
  * Base URL of the cloud-hosted assistant SPA (the "hub") for a deployment
29
- * environment: always `${SEEDS[env].webUrl}/assistant`. The Capacitor mobile
30
- * shell bakes this into native builds as `server.url`, and the CLI's
29
+ * environment: always `${SEEDS[env].webUrl}/assistant`. The Capacitor boot
30
+ * bundle stamps this into baked mobile builds as `handoffUrl`, and the CLI's
31
31
  * remote-web edge stamps it into the served config as `hubUrl`, so the
32
32
  * environment-to-hub mapping lives here exactly once. Unknown environment
33
33
  * names fall back to the dev SPA defensively.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/cli",
3
- "version": "0.13.0-dev.202610181631.3760abb",
3
+ "version": "0.13.0-dev.202610192200.a3394a1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/vargasjr-dev/mycadet-temp.git"
@@ -24,4 +24,14 @@ describe("cadet client web __config", () => {
24
24
  // assistant this origin serves would read a false `foreign`.
25
25
  expect(config).not.toHaveProperty("assistantId");
26
26
  });
27
+
28
+ test("carries the serving version when the caller stamps one", () => {
29
+ const config = buildWebInterfaceConfig({
30
+ ...BASE,
31
+ version: "0.13.0-local.202610191920.58bcf38",
32
+ });
33
+
34
+ expect(config["version"]).toBe("0.13.0-local.202610191920.58bcf38");
35
+ expect(config).not.toHaveProperty("assistantId");
36
+ });
27
37
  });
@@ -0,0 +1,45 @@
1
+ import { describe, expect, test } from "bun:test";
2
+
3
+ import { displayVersion } from "../lib/local-version.js";
4
+ import { environmentFromVersion } from "../lib/environments/resolve.js";
5
+ import { isLocalVersion, parseVersion } from "../lib/version-compat.js";
6
+
7
+ describe("displayVersion", () => {
8
+ test("stamps source checkouts with a -local. postfix in the dev-release format", () => {
9
+ // In the repo the CLI always runs as Bun-executed TypeScript with git
10
+ // history, so the stamp applies. Format mirrors dev-release.yaml:
11
+ // `<base>-local.<yyyymmddhhmm>.<shortsha>`.
12
+ expect(displayVersion("0.13.0")).toMatch(
13
+ /^0\.13\.0-local\.\d{12}\.[0-9a-f]{7,}$/,
14
+ );
15
+ });
16
+
17
+ test("stamped version parses as semver and denotes a local build", () => {
18
+ const version = displayVersion("0.13.0");
19
+ expect(parseVersion(version)?.pre).toMatch(/^local\.\d{12}\.[0-9a-f]{7,}$/);
20
+ expect(isLocalVersion(version)).toBe(true);
21
+ });
22
+
23
+ test("stamped version resolves the local environment via the shared mapping", () => {
24
+ // `cadet-local` pins CADET_ENVIRONMENT anyway, but the version stamp and
25
+ // the environment mapping must agree so a source checkout is legible as
26
+ // a local build no matter which signal is consulted.
27
+ expect(environmentFromVersion(displayVersion("0.13.0"))).toBe("local");
28
+ });
29
+
30
+ test("non-source entries keep the plain package version", () => {
31
+ // Compiled installs run from a non-.ts entry: no stamp, so the release
32
+ // pipeline's version stays authoritative there.
33
+ expect(displayVersion("0.13.0", new Date(), "/usr/local/bin/mycadet")).toBe(
34
+ "0.13.0",
35
+ );
36
+ });
37
+
38
+ test("source trees without git history stay unstamped", () => {
39
+ // The sha is what makes the stamp meaningful; a tarball checkout can't
40
+ // produce one, so it keeps the plain version rather than a half-stamp.
41
+ expect(
42
+ displayVersion("0.13.0", new Date(), "/opt/src/cli/src/index.ts"),
43
+ ).toBe("0.13.0");
44
+ });
45
+ });
@@ -1,4 +1,5 @@
1
1
  import * as childProcess from "node:child_process";
2
+ import type { SpawnOptions } from "node:child_process";
2
3
  import { createHash } from "node:crypto";
3
4
  import * as fsModule from "node:fs";
4
5
  import {
@@ -349,10 +350,11 @@ describe("buildIngressNginxConfig", () => {
349
350
  assistantName: "Homelab",
350
351
  assistantId: "assistant-1",
351
352
  hubUrl: "https://mycadet.ai/assistant",
353
+ version: "1.2.3",
352
354
  },
353
355
  });
354
356
  expect(named).toContain(
355
- 'return 200 "{\\"remote\\":\\"custom\\",\\"apiBaseUrl\\":\\"/v1\\",\\"assistantName\\":\\"Homelab\\",\\"assistantId\\":\\"assistant-1\\",\\"hubUrl\\":\\"https://mycadet.ai/assistant\\"}";',
357
+ 'return 200 "{\\"remote\\":\\"custom\\",\\"apiBaseUrl\\":\\"/v1\\",\\"assistantName\\":\\"Homelab\\",\\"assistantId\\":\\"assistant-1\\",\\"hubUrl\\":\\"https://mycadet.ai/assistant\\",\\"version\\":\\"1.2.3\\"}";',
356
358
  );
357
359
  });
358
360
 
@@ -731,11 +733,21 @@ function mockNginxSpawnExitsAfterProbe(): void {
731
733
  }) as unknown as typeof childProcess.spawn);
732
734
  }
733
735
 
734
- /** Force findWebDistDir() to resolve nothing, regardless of checkout state. */
736
+ /**
737
+ * Force the dist lookup to resolve nothing, regardless of checkout state:
738
+ * no built bundle anywhere, and no clients/{web,app} source checkout for
739
+ * the auto-build to run from.
740
+ */
735
741
  function mockWebDistMissing(): void {
736
- existsSyncMock.mockImplementation((path) =>
737
- String(path).endsWith("index.html") ? false : realFs.existsSync(path),
738
- );
742
+ existsSyncMock.mockImplementation((path) => {
743
+ const p = String(path);
744
+ // No built bundle anywhere...
745
+ if (p.endsWith("index.html")) return false;
746
+ // ...and no source package for the auto-build to run.
747
+ if (p.includes("clients/web/package.json")) return false;
748
+ if (p.includes("clients/app/package.json")) return false;
749
+ return realFs.existsSync(path);
750
+ });
739
751
  }
740
752
 
741
753
  /** Force findWebDistDir() to resolve a dist dir, regardless of checkout state. */
@@ -856,6 +868,7 @@ function spaConfigHash(
856
868
  ...(opts.assistantName ? { assistantName: opts.assistantName } : {}),
857
869
  ...(opts.assistantId ? { assistantId: opts.assistantId } : {}),
858
870
  hubUrl: PRODUCTION_HUB_URL,
871
+ version: "1.2.3",
859
872
  },
860
873
  webDistDir: opts.webDistDir ?? findWebDistDir(),
861
874
  version: "1.2.3",
@@ -1035,6 +1048,7 @@ describe("startRemoteWebIngress", () => {
1035
1048
  webDistDir,
1036
1049
  indexHtmlPath: join(ws, "data", "ingress", "assistant-index.html"),
1037
1050
  hubUrl: PRODUCTION_HUB_URL,
1051
+ version: "1.2.3",
1038
1052
  },
1039
1053
  }),
1040
1054
  );
@@ -1140,6 +1154,84 @@ describe("startRemoteWebIngress", () => {
1140
1154
  expect(realFs.existsSync(ingressConfPath(ws))).toBe(false);
1141
1155
  });
1142
1156
 
1157
+ test("builds the missing web bundle from the source checkout and starts", async () => {
1158
+ const ws = makeWorkspace();
1159
+ mockNginxInstalled();
1160
+ // No built bundle anywhere, but the source checkout IS present, so the
1161
+ // edge auto-builds it through the spawn boundary (mocked here).
1162
+ existsSyncMock.mockImplementation((path) => {
1163
+ const p = String(path);
1164
+ if (p.endsWith(WEB_INDEX_SUFFIX)) return false;
1165
+ if (p.includes("clients/web/package.json")) return true;
1166
+ return realFs.existsSync(path);
1167
+ });
1168
+ mockNginxSpawn();
1169
+ const nginxSpawn = spawnMock.getMockImplementation();
1170
+ spawnMock.mockImplementation(((
1171
+ command: string,
1172
+ args: readonly string[],
1173
+ options?: SpawnOptions,
1174
+ ) => {
1175
+ if (command === "bun") {
1176
+ // The auto-build "lands" the bundle and exits cleanly.
1177
+ mockWebDistPresent();
1178
+ const child = new childProcess.ChildProcess();
1179
+ setTimeout(() => child.emit("exit", 0), 10);
1180
+ return child;
1181
+ }
1182
+ return nginxSpawn?.(command, args, options ?? {});
1183
+ }) as unknown as typeof childProcess.spawn);
1184
+
1185
+ const result = await startRemoteWebIngress({
1186
+ workspaceDir: ws,
1187
+ gatewayPort: 7830,
1188
+ listenPort: 7845,
1189
+ });
1190
+
1191
+ expect(result).toEqual({
1192
+ status: "started",
1193
+ listenPort: 7845,
1194
+ webDistDir: expect.any(String),
1195
+ version: NGINX_VERSION,
1196
+ });
1197
+ const buildCalls = spawnMock.mock.calls.filter(
1198
+ ([command]) => command === "bun",
1199
+ );
1200
+ expect(buildCalls).toHaveLength(1);
1201
+ const [, buildArgs, buildOptions] = buildCalls[0] ?? [];
1202
+ expect(buildArgs).toEqual(["run", "build"]);
1203
+ expect(buildOptions?.env?.VITE_CLOUD_MODE).toBe("false");
1204
+ expect(String(buildOptions?.cwd).endsWith(join("clients", "web"))).toBe(
1205
+ true,
1206
+ );
1207
+ });
1208
+
1209
+ test("reports a failed source build with the manual recipe", async () => {
1210
+ const ws = makeWorkspace();
1211
+ mockNginxInstalled();
1212
+ // Bundle missing, source checkout present: the edge auto-builds, and
1213
+ // the build spawn fails.
1214
+ existsSyncMock.mockImplementation((path) => {
1215
+ const p = String(path);
1216
+ if (p.endsWith(WEB_INDEX_SUFFIX)) return false;
1217
+ if (p.includes("clients/web/package.json")) return true;
1218
+ return realFs.existsSync(path);
1219
+ });
1220
+ spawnMock.mockImplementation((() => {
1221
+ const child = new childProcess.ChildProcess();
1222
+ setTimeout(() => child.emit("exit", 1), 10);
1223
+ return child;
1224
+ }) as unknown as typeof childProcess.spawn);
1225
+
1226
+ await expect(
1227
+ startRemoteWebIngress({
1228
+ workspaceDir: ws,
1229
+ gatewayPort: 7830,
1230
+ listenPort: 7845,
1231
+ }),
1232
+ ).rejects.toThrow("cd clients/web && VITE_CLOUD_MODE=false bun run build");
1233
+ });
1234
+
1143
1235
  test("short-circuits when the running edge already serves the requested mode", async () => {
1144
1236
  const ws = makeWorkspace();
1145
1237
  mockNginxInstalled();
@@ -2477,6 +2569,7 @@ function appConfigHash(
2477
2569
  ...(opts.assistantName ? { assistantName: opts.assistantName } : {}),
2478
2570
  ...(opts.assistantId ? { assistantId: opts.assistantId } : {}),
2479
2571
  hubUrl: PRODUCTION_HUB_URL,
2572
+ version: "1.2.3",
2480
2573
  },
2481
2574
  webDistDir: findWebDistDir(),
2482
2575
  version: "1.2.3",
@@ -2612,6 +2705,7 @@ describe("app shell mode", () => {
2612
2705
  );
2613
2706
  expect(appIndex).toContain("__CADET_CONFIG__");
2614
2707
  expect(appIndex).toContain(`"assistantId":"assistant-1"`);
2708
+ expect(appIndex).toContain(`"version":"1.2.3"`);
2615
2709
  const conf = realFs.readFileSync(ingressConfPath(ws), "utf-8");
2616
2710
  expect(conf).toContain("location = /__cadet-app-index.html {");
2617
2711
  expect(conf).toContain('\\"url\\":\\"web/assistant/\\"');
@@ -2632,13 +2726,13 @@ describe("app shell mode", () => {
2632
2726
  // path needs the lookup to resolve nothing, regardless of checkout state.
2633
2727
  // Web dist stays present so the app-dist check is the one that fires.
2634
2728
  const previousExists = existsSyncMock.getMockImplementation();
2635
- existsSyncMock.mockImplementation((path) =>
2636
- String(path).endsWith(APP_INDEX_SUFFIX)
2637
- ? false
2638
- : previousExists
2639
- ? previousExists(path)
2640
- : realFs.existsSync(path),
2641
- );
2729
+ existsSyncMock.mockImplementation((path) => {
2730
+ const p = String(path);
2731
+ if (p.endsWith(APP_INDEX_SUFFIX)) return false;
2732
+ // The app auto-build must not run from the real checkout either.
2733
+ if (p.includes("clients/app/package.json")) return false;
2734
+ return previousExists ? previousExists(path) : realFs.existsSync(path);
2735
+ });
2642
2736
 
2643
2737
  const result = await startRemoteWebIngress({
2644
2738
  workspaceDir: ws,
@@ -1,6 +1,16 @@
1
1
  import { describe, expect, test } from "bun:test";
2
2
 
3
- import { buildNgrokSpawnArgs, isNgrokAuthError } from "../lib/tunnels/ngrok.js";
3
+ import {
4
+ buildNgrokSpawnArgs,
5
+ confirmStopAnswer,
6
+ isNgrokAuthError,
7
+ isNgrokEndpointOnlineError,
8
+ isNgrokSessionLimitError,
9
+ ngrokAgentConfigPath,
10
+ ngrokOnlineEndpoint,
11
+ staleTrackedNgrokAgent,
12
+ untrackedNgrokHolderPid,
13
+ } from "../lib/tunnels/ngrok.js";
4
14
 
5
15
  describe("buildNgrokSpawnArgs", () => {
6
16
  test("spawns a plain http agent against the loopback edge", () => {
@@ -24,6 +34,36 @@ describe("buildNgrokSpawnArgs", () => {
24
34
  "--domain=my-assistant.ngrok.app",
25
35
  ]);
26
36
  });
37
+
38
+ test("a second agent layers the user's default config under ours", () => {
39
+ // Order is load-bearing: the default config carries the authtoken, ours
40
+ // wins for web_addr. A bare --config would replace the default and drop
41
+ // the token.
42
+ const args = buildNgrokSpawnArgs(7840, "me.ngrok.app", {
43
+ webAddr: "127.0.0.1:4041",
44
+ defaultConfigPath: "/users/me/.config/ngrok/ngrok.yml",
45
+ });
46
+ expect(args).toEqual([
47
+ "http",
48
+ "127.0.0.1:7840",
49
+ "--log=stdout",
50
+ "--domain=me.ngrok.app",
51
+ "--config=/users/me/.config/ngrok/ngrok.yml",
52
+ `--config=${ngrokAgentConfigPath()}`,
53
+ ]);
54
+ });
55
+
56
+ test("a second agent without a resolvable default config still sets its inspection address", () => {
57
+ const args = buildNgrokSpawnArgs(7840, undefined, {
58
+ webAddr: "127.0.0.1:4041",
59
+ });
60
+ expect(args).toEqual([
61
+ "http",
62
+ "127.0.0.1:7840",
63
+ "--log=stdout",
64
+ `--config=${ngrokAgentConfigPath()}`,
65
+ ]);
66
+ });
27
67
  });
28
68
 
29
69
  describe("isNgrokAuthError", () => {
@@ -46,4 +86,172 @@ describe("isNgrokAuthError", () => {
46
86
  ),
47
87
  ).toBe(false);
48
88
  });
89
+
90
+ test("recognizes the concurrent-agent session limit", () => {
91
+ expect(
92
+ isNgrokSessionLimitError(
93
+ "authentication failed: Your account is limited to 3 simultaneous ngrok agent sessions. ERR_NGROK_108",
94
+ ),
95
+ ).toBe(true);
96
+ expect(isNgrokSessionLimitError("connection refused")).toBe(false);
97
+ });
98
+ });
99
+
100
+ describe("isNgrokEndpointOnlineError", () => {
101
+ test("recognizes the free-plan domain collision", () => {
102
+ expect(
103
+ isNgrokEndpointOnlineError(
104
+ "ERROR: failed to start tunnel: The endpoint 'https://camping-trustful-fog.ngrok-free.dev' is already online. ERR_NGROK_334",
105
+ ),
106
+ ).toBe(true);
107
+ });
108
+
109
+ test("does not claim other startup failures are endpoint collisions", () => {
110
+ expect(
111
+ isNgrokEndpointOnlineError(
112
+ "creating tunnel session failed: dial tcp 3.219.x.x:443: connection refused",
113
+ ),
114
+ ).toBe(false);
115
+ });
116
+ });
117
+
118
+ describe("ngrokOnlineEndpoint", () => {
119
+ test("extracts the endpoint the incident refused", () => {
120
+ expect(
121
+ ngrokOnlineEndpoint(
122
+ "failed to start tunnel: The endpoint 'https://camping-trustful-fog.ngrok-free.dev' is already online.",
123
+ ),
124
+ ).toBe("https://camping-trustful-fog.ngrok-free.dev");
125
+ });
126
+
127
+ test("no endpoint named, no extraction", () => {
128
+ expect(
129
+ ngrokOnlineEndpoint("creating tunnel session failed: connection refused"),
130
+ ).toBeNull();
131
+ });
132
+ });
133
+
134
+ describe("confirmStopAnswer", () => {
135
+ test("Enter and y/yes confirm the stop", () => {
136
+ expect(confirmStopAnswer("")).toBe(true);
137
+ expect(confirmStopAnswer("y")).toBe(true);
138
+ expect(confirmStopAnswer(" YES\n")).toBe(true);
139
+ });
140
+
141
+ test("anything else declines", () => {
142
+ expect(confirmStopAnswer("n")).toBe(false);
143
+ expect(confirmStopAnswer("no")).toBe(false);
144
+ expect(confirmStopAnswer("wait")).toBe(false);
145
+ });
146
+ });
147
+
148
+ describe("staleTrackedNgrokAgent", () => {
149
+ const record = {
150
+ pid: 4242,
151
+ provider: "ngrok",
152
+ startedAt: new Date().toISOString(),
153
+ };
154
+
155
+ test("returns the record for a tracked, live ngrok agent", () => {
156
+ expect(staleTrackedNgrokAgent(record, () => true)).toEqual(record);
157
+ });
158
+
159
+ test("leaves agents recorded by other tunnel providers alone", () => {
160
+ expect(
161
+ staleTrackedNgrokAgent({ ...record, provider: "tailscale" }, () => true),
162
+ ).toBeNull();
163
+ });
164
+
165
+ test("leaves a pid that no longer verifies as ngrok alone (reuse guard)", () => {
166
+ expect(staleTrackedNgrokAgent(record, () => false)).toBeNull();
167
+ });
168
+
169
+ test("nothing recorded, nothing to stop", () => {
170
+ expect(staleTrackedNgrokAgent(undefined, () => true)).toBeNull();
171
+ });
172
+ });
173
+
174
+ describe("untrackedNgrokHolderPid", () => {
175
+ // Mirrors Vargas's Saturday incident: an agent with no lockfile record
176
+ // holding the free plan's single domain, answering its inspection API on
177
+ // the default port.
178
+ const holderAgent = {
179
+ apiAddr: "127.0.0.1:4040",
180
+ tunnels: [
181
+ {
182
+ public_url: "https://camping-trustful-fog.ngrok-free.dev",
183
+ config: { addr: "http://127.0.0.1:7840" },
184
+ },
185
+ ],
186
+ };
187
+
188
+ test("returns the verified pid of the agent serving the domain", () => {
189
+ expect(
190
+ untrackedNgrokHolderPid(
191
+ [holderAgent],
192
+ "camping-trustful-fog.ngrok-free.dev",
193
+ () => true,
194
+ () => 4242,
195
+ ),
196
+ ).toBe(4242);
197
+ });
198
+
199
+ test("refuses a port pid that does not verify as ngrok (reuse guard)", () => {
200
+ expect(
201
+ untrackedNgrokHolderPid(
202
+ [holderAgent],
203
+ "camping-trustful-fog.ngrok-free.dev",
204
+ () => false,
205
+ () => 4242,
206
+ ),
207
+ ).toBeNull();
208
+ });
209
+
210
+ test("only probes agents that actually serve the domain", () => {
211
+ const probes: number[] = [];
212
+ const pid = untrackedNgrokHolderPid(
213
+ [
214
+ {
215
+ apiAddr: "127.0.0.1:4041",
216
+ tunnels: [
217
+ {
218
+ public_url: "https://other-tunnel.ngrok-free.dev",
219
+ config: { addr: "http://127.0.0.1:7840" },
220
+ },
221
+ ],
222
+ },
223
+ holderAgent,
224
+ ],
225
+ "camping-trustful-fog.ngrok-free.dev",
226
+ () => true,
227
+ (port) => {
228
+ probes.push(port);
229
+ return 4242;
230
+ },
231
+ );
232
+ expect(pid).toBe(4242);
233
+ expect(probes).toEqual([4040]);
234
+ });
235
+
236
+ test("returns null when the inspection port has no listener pid", () => {
237
+ expect(
238
+ untrackedNgrokHolderPid(
239
+ [holderAgent],
240
+ "camping-trustful-fog.ngrok-free.dev",
241
+ () => true,
242
+ () => null,
243
+ ),
244
+ ).toBeNull();
245
+ });
246
+
247
+ test("matches the domain case-insensitively", () => {
248
+ expect(
249
+ untrackedNgrokHolderPid(
250
+ [holderAgent],
251
+ "Camping-Trustful-Fog.ngrok-free.dev",
252
+ () => true,
253
+ () => 4242,
254
+ ),
255
+ ).toBe(4242);
256
+ });
49
257
  });
@@ -265,7 +265,7 @@ describe("cadet ps — platform status line", () => {
265
265
  expect(output).toContain("nginx");
266
266
  expect(output).toContain("port ");
267
267
  // …the type column distinguishes the three process kinds…
268
- expect(output).toContain("🧑‍🚀 Assistant");
268
+ expect(output).toContain("🤖 Assistant");
269
269
  expect(output).toContain("🚇 Tunnel");
270
270
  expect(output).toContain("🌐 App");
271
271
  // …and the second table is gone: one header row only.