@indigoai-us/hq-cli 5.77.14 → 5.79.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (309) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/assets/scaffold/core/scripts/archive-old-threads.sh +86 -0
  3. package/assets/scaffold/core/scripts/backfill-company-skill-mirrors.sh +73 -0
  4. package/assets/scaffold/core/scripts/backfill-workspace-mirror.sh +51 -0
  5. package/assets/scaffold/core/scripts/codex-skill-bridge.sh +550 -0
  6. package/assets/scaffold/core/scripts/detect-stale-core-policy-mirror.sh +185 -0
  7. package/assets/scaffold/core/scripts/hq-status-summary.sh +187 -0
  8. package/assets/scaffold/core/scripts/ontology-readme-drift.sh +264 -0
  9. package/assets/scaffold/core/scripts/qmd-reindex-after-sync.sh +93 -0
  10. package/assets/scaffold/core/scripts/rebuild-all-indexes.sh +69 -0
  11. package/assets/scaffold/core/scripts/rebuild-companies-index.sh +206 -0
  12. package/assets/scaffold/core/scripts/rebuild-company-knowledge-index.sh +127 -0
  13. package/assets/scaffold/core/scripts/rebuild-orchestrator-index.sh +86 -0
  14. package/assets/scaffold/core/scripts/rebuild-projects-index.sh +105 -0
  15. package/assets/scaffold/core/scripts/rebuild-public-knowledge-index.sh +83 -0
  16. package/assets/scaffold/core/scripts/rebuild-reports-index.sh +114 -0
  17. package/assets/scaffold/core/scripts/rebuild-social-drafts-index.sh +135 -0
  18. package/assets/scaffold/core/scripts/rebuild-threads-index.sh +82 -0
  19. package/assets/scaffold/core/scripts/rebuild-workers-index.sh +97 -0
  20. package/assets/scaffold/core/scripts/resize-screenshot.sh +47 -0
  21. package/assets/scaffold/core/scripts/token-usage-report.sh +254 -0
  22. package/assets/scaffold/core/scripts/worktree.sh +136 -0
  23. package/dist/commands/agents.js +28 -12
  24. package/dist/commands/api-keys.js +100 -20
  25. package/dist/commands/core.d.ts +90 -0
  26. package/dist/commands/core.js +279 -0
  27. package/dist/commands/files-browse.d.ts +21 -0
  28. package/dist/commands/files-browse.js +145 -24
  29. package/dist/commands/files.js +33 -7
  30. package/dist/commands/meetings.js +16 -9
  31. package/dist/commands/members.js +16 -5
  32. package/dist/commands/pack-install.d.ts +14 -0
  33. package/dist/commands/pack-install.js +42 -2
  34. package/dist/commands/pkg-install.js +39 -2
  35. package/dist/commands/run.js +6 -2
  36. package/dist/commands/secrets.js +120 -65
  37. package/dist/main.js +6 -0
  38. package/dist/utils/api-key-command-gate.d.ts +45 -0
  39. package/dist/utils/api-key-command-gate.js +51 -0
  40. package/dist/utils/hq-roots.d.ts +91 -0
  41. package/dist/utils/hq-roots.js +212 -0
  42. package/dist/utils/resolve-vault-credential.d.ts +63 -3
  43. package/dist/utils/resolve-vault-credential.js +152 -6
  44. package/dist/utils/run-bundled-script.d.ts +66 -0
  45. package/dist/utils/run-bundled-script.js +80 -0
  46. package/dist/utils/vault-api.d.ts +21 -0
  47. package/dist/utils/vault-api.js +74 -3
  48. package/package.json +7 -1
  49. package/.claude/policies/hq-cli-interactive-reads-use-sts-vend.md +0 -49
  50. package/.github/workflows/ci.yml +0 -46
  51. package/.github/workflows/publish.yml +0 -36
  52. package/.github/workflows/scripts/smoke-test-pkg.sh +0 -97
  53. package/e2e/cli.test.ts +0 -128
  54. package/eslint.config.js +0 -37
  55. package/pnpm-workspace.yaml +0 -4
  56. package/scripts/chmod-bins.mjs +0 -49
  57. package/scripts/generate-dsn.mjs +0 -26
  58. package/scripts/generate-scan-packages-table.mjs +0 -113
  59. package/scripts/smoke-sources-signals.sh +0 -103
  60. package/src/bin/hq-auth-refresh.ts +0 -43
  61. package/src/cli-version.ts +0 -13
  62. package/src/commands/__fixtures__/make-tar.ts +0 -126
  63. package/src/commands/_patterns.ts +0 -10
  64. package/src/commands/add.ts +0 -75
  65. package/src/commands/agents.test.ts +0 -527
  66. package/src/commands/agents.ts +0 -923
  67. package/src/commands/api-keys.test.ts +0 -291
  68. package/src/commands/api-keys.ts +0 -382
  69. package/src/commands/artifact-verify.test.ts +0 -177
  70. package/src/commands/auth.test.ts +0 -82
  71. package/src/commands/auth.ts +0 -213
  72. package/src/commands/billing.test.ts +0 -158
  73. package/src/commands/billing.ts +0 -146
  74. package/src/commands/channels.test.ts +0 -74
  75. package/src/commands/channels.ts +0 -91
  76. package/src/commands/cloud-demote.test.ts +0 -401
  77. package/src/commands/cloud-demote.ts +0 -278
  78. package/src/commands/cloud-provision.test.ts +0 -1117
  79. package/src/commands/cloud-provision.ts +0 -942
  80. package/src/commands/cloud.pull-all.test.ts +0 -711
  81. package/src/commands/cloud.pull-per-company.test.ts +0 -188
  82. package/src/commands/cloud.push-all.test.ts +0 -485
  83. package/src/commands/cloud.scope-excluded-warning.test.ts +0 -22
  84. package/src/commands/cloud.selectors.test.ts +0 -208
  85. package/src/commands/cloud.status.test.ts +0 -160
  86. package/src/commands/cloud.test.ts +0 -41
  87. package/src/commands/cloud.ts +0 -2130
  88. package/src/commands/company.test.ts +0 -202
  89. package/src/commands/company.ts +0 -148
  90. package/src/commands/creators.test.ts +0 -60
  91. package/src/commands/creators.ts +0 -117
  92. package/src/commands/crm.test.ts +0 -179
  93. package/src/commands/crm.ts +0 -236
  94. package/src/commands/db-migrate.ts +0 -55
  95. package/src/commands/db-provision.ts +0 -114
  96. package/src/commands/db-sql.ts +0 -124
  97. package/src/commands/db-status.ts +0 -108
  98. package/src/commands/db.ts +0 -26
  99. package/src/commands/dm.test.ts +0 -748
  100. package/src/commands/dm.ts +0 -1226
  101. package/src/commands/feedback.test.ts +0 -413
  102. package/src/commands/feedback.ts +0 -169
  103. package/src/commands/files-browse.test.ts +0 -1249
  104. package/src/commands/files-browse.ts +0 -1506
  105. package/src/commands/files-delete.test.ts +0 -484
  106. package/src/commands/files-recovery.test.ts +0 -370
  107. package/src/commands/files.test.ts +0 -722
  108. package/src/commands/files.ts +0 -1446
  109. package/src/commands/group-grants.test.ts +0 -330
  110. package/src/commands/group-grants.ts +0 -455
  111. package/src/commands/groups.test.ts +0 -44
  112. package/src/commands/groups.ts +0 -368
  113. package/src/commands/integrations.test.ts +0 -633
  114. package/src/commands/integrations.ts +0 -507
  115. package/src/commands/list.ts +0 -85
  116. package/src/commands/login.ts +0 -52
  117. package/src/commands/logout.ts +0 -30
  118. package/src/commands/marketplace-install.test.ts +0 -543
  119. package/src/commands/marketplace-security.test.ts +0 -646
  120. package/src/commands/mcp-registration.test.ts +0 -2787
  121. package/src/commands/mcp-registration.ts +0 -2612
  122. package/src/commands/mcp-status.test.ts +0 -483
  123. package/src/commands/mcp-status.ts +0 -575
  124. package/src/commands/mcp-status.us011.test.ts +0 -243
  125. package/src/commands/meetings.test.ts +0 -522
  126. package/src/commands/meetings.ts +0 -789
  127. package/src/commands/members.test.ts +0 -1474
  128. package/src/commands/members.ts +0 -1181
  129. package/src/commands/onboard-identity-guard.test.ts +0 -99
  130. package/src/commands/onboard-identity-guard.ts +0 -103
  131. package/src/commands/onboard-join.test.ts +0 -96
  132. package/src/commands/onboard-join.ts +0 -101
  133. package/src/commands/onboard-warning.test.ts +0 -26
  134. package/src/commands/onboard-warning.ts +0 -12
  135. package/src/commands/onboard.ts +0 -267
  136. package/src/commands/outposts-heartbeat.test.ts +0 -299
  137. package/src/commands/outposts-heartbeat.ts +0 -310
  138. package/src/commands/outposts-replica-sync.test.ts +0 -272
  139. package/src/commands/outposts-self-deploy.test.ts +0 -243
  140. package/src/commands/outposts.test.ts +0 -1039
  141. package/src/commands/outposts.ts +0 -1935
  142. package/src/commands/pack-install-secret-authorization.test.ts +0 -115
  143. package/src/commands/pack-install.test.ts +0 -1469
  144. package/src/commands/pack-install.ts +0 -2256
  145. package/src/commands/pack-update-cache.test.ts +0 -149
  146. package/src/commands/packs-update-api-key.test.ts +0 -105
  147. package/src/commands/packs.test.ts +0 -88
  148. package/src/commands/packs.ts +0 -622
  149. package/src/commands/people.test.ts +0 -807
  150. package/src/commands/people.ts +0 -422
  151. package/src/commands/pkg-install.dispatch.test.ts +0 -103
  152. package/src/commands/pkg-install.ts +0 -221
  153. package/src/commands/pkg-list.test.ts +0 -79
  154. package/src/commands/pkg-list.ts +0 -13
  155. package/src/commands/pkg-remove.ts +0 -71
  156. package/src/commands/pkg-update.ts +0 -189
  157. package/src/commands/publish.test.ts +0 -650
  158. package/src/commands/publish.ts +0 -580
  159. package/src/commands/reindex.test.ts +0 -259
  160. package/src/commands/reindex.ts +0 -274
  161. package/src/commands/rescue.test.ts +0 -39
  162. package/src/commands/rescue.ts +0 -210
  163. package/src/commands/run.env-local.test.ts +0 -88
  164. package/src/commands/run.test.ts +0 -45
  165. package/src/commands/run.ts +0 -190
  166. package/src/commands/safe-extract.test.ts +0 -459
  167. package/src/commands/safe-extract.ts +0 -444
  168. package/src/commands/secrets-scope.test.ts +0 -56
  169. package/src/commands/secrets-scope.ts +0 -32
  170. package/src/commands/secrets.parse-destination.test.ts +0 -38
  171. package/src/commands/secrets.test.ts +0 -2050
  172. package/src/commands/secrets.ts +0 -2249
  173. package/src/commands/signals.ts +0 -351
  174. package/src/commands/skill.test.ts +0 -770
  175. package/src/commands/skill.ts +0 -796
  176. package/src/commands/sources.ts +0 -362
  177. package/src/commands/sync-mode.test.ts +0 -366
  178. package/src/commands/sync-mode.ts +0 -387
  179. package/src/commands/sync-narrow.test.ts +0 -633
  180. package/src/commands/sync-narrow.ts +0 -555
  181. package/src/commands/sync.ts +0 -165
  182. package/src/commands/team-sync.ts +0 -628
  183. package/src/commands/update.ts +0 -71
  184. package/src/commands/whoami.ts +0 -83
  185. package/src/commands/workers.test.ts +0 -158
  186. package/src/commands/workers.ts +0 -301
  187. package/src/index.test.ts +0 -32
  188. package/src/index.ts +0 -20
  189. package/src/lib/db/control-plane.test.ts +0 -59
  190. package/src/lib/db/control-plane.ts +0 -113
  191. package/src/lib/db/local.test.ts +0 -81
  192. package/src/lib/db/local.ts +0 -148
  193. package/src/lib/db/migrate.test.ts +0 -133
  194. package/src/lib/db/migrate.ts +0 -137
  195. package/src/lib/db/paths.test.ts +0 -112
  196. package/src/lib/db/paths.ts +0 -128
  197. package/src/lib/db/remote-engine.test.ts +0 -44
  198. package/src/lib/db/remote-engine.ts +0 -148
  199. package/src/lib/db/remote-sql.test.ts +0 -32
  200. package/src/lib/db/remote-sql.ts +0 -62
  201. package/src/lib/db/sql.test.ts +0 -106
  202. package/src/lib/db/sql.ts +0 -192
  203. package/src/lib/hq-cloud-dep.smoke.test.ts +0 -75
  204. package/src/lib/local-tree-diff.test.ts +0 -301
  205. package/src/lib/local-tree-diff.ts +0 -362
  206. package/src/lib/narrow-hint-banner.test.ts +0 -235
  207. package/src/lib/narrow-hint-banner.ts +0 -212
  208. package/src/lib/plan-limit-nag.test.ts +0 -317
  209. package/src/lib/plan-limit-nag.ts +0 -264
  210. package/src/main.ts +0 -371
  211. package/src/node-preflight.test.ts +0 -60
  212. package/src/node-preflight.ts +0 -67
  213. package/src/outpost/session-heartbeat-bounds.test.ts +0 -195
  214. package/src/outpost/session-heartbeat-guard.test.ts +0 -105
  215. package/src/outpost/session-heartbeat-publisher.test.ts +0 -178
  216. package/src/outpost/session-heartbeat-publisher.ts +0 -186
  217. package/src/outpost/session-heartbeat-retain-guard.test.ts +0 -126
  218. package/src/outpost/session-heartbeat.test.ts +0 -459
  219. package/src/outpost/session-heartbeat.ts +0 -877
  220. package/src/packaging.test.ts +0 -109
  221. package/src/run/__fixtures__/discover-schemas/example.env.schema +0 -4
  222. package/src/run/discover-schemas.test.ts +0 -153
  223. package/src/run/discover-schemas.ts +0 -79
  224. package/src/run/hq-plugin.test.ts +0 -371
  225. package/src/run/hq-plugin.ts +0 -277
  226. package/src/run/varlock-shape.test.ts +0 -57
  227. package/src/schemas/hq-package.schema.json +0 -155
  228. package/src/sentry-before-send.test.ts +0 -359
  229. package/src/sentry-before-send.ts +0 -221
  230. package/src/sentry-epipe.test.ts +0 -37
  231. package/src/sentry-release.test.ts +0 -54
  232. package/src/sentry.test.ts +0 -24
  233. package/src/sentry.ts +0 -54
  234. package/src/strategies/link.ts +0 -62
  235. package/src/strategies/merge.ts +0 -142
  236. package/src/types.ts +0 -146
  237. package/src/utils/__fixtures__/scan-packages.generated-block.sh +0 -23
  238. package/src/utils/auth-error.test.ts +0 -40
  239. package/src/utils/auth-error.ts +0 -42
  240. package/src/utils/billing-gate.test.ts +0 -95
  241. package/src/utils/billing-gate.ts +0 -228
  242. package/src/utils/breadcrumb-buffer.ts +0 -18
  243. package/src/utils/cli-telemetry.test.ts +0 -153
  244. package/src/utils/cli-telemetry.ts +0 -61
  245. package/src/utils/cognito-session.machine.test.ts +0 -109
  246. package/src/utils/cognito-session.refresh.test.ts +0 -84
  247. package/src/utils/cognito-session.test.ts +0 -328
  248. package/src/utils/cognito-session.ts +0 -448
  249. package/src/utils/company-selection-error.test.ts +0 -42
  250. package/src/utils/company-selection-error.ts +0 -45
  251. package/src/utils/contribution-table.ts +0 -83
  252. package/src/utils/environmental-error.test.ts +0 -45
  253. package/src/utils/environmental-error.ts +0 -39
  254. package/src/utils/epipe.test.ts +0 -28
  255. package/src/utils/epipe.ts +0 -29
  256. package/src/utils/expected-cli-error.test.ts +0 -28
  257. package/src/utils/expected-cli-error.ts +0 -39
  258. package/src/utils/feedback-diagnostics.test.ts +0 -183
  259. package/src/utils/feedback-diagnostics.ts +0 -123
  260. package/src/utils/feedback-screenshots.test.ts +0 -134
  261. package/src/utils/feedback-screenshots.ts +0 -124
  262. package/src/utils/feedback-versions.test.ts +0 -98
  263. package/src/utils/feedback-versions.ts +0 -68
  264. package/src/utils/git.ts +0 -74
  265. package/src/utils/integrity.ts +0 -59
  266. package/src/utils/intercepted-process-exit.test.ts +0 -37
  267. package/src/utils/intercepted-process-exit.ts +0 -36
  268. package/src/utils/login-provider.test.ts +0 -42
  269. package/src/utils/login-provider.ts +0 -30
  270. package/src/utils/manifest.test.ts +0 -167
  271. package/src/utils/manifest.ts +0 -131
  272. package/src/utils/pack-contributions.test.ts +0 -603
  273. package/src/utils/pack-contributions.ts +0 -450
  274. package/src/utils/people.ts +0 -215
  275. package/src/utils/registry-client.ts +0 -204
  276. package/src/utils/registry.ts +0 -90
  277. package/src/utils/resolve-vault-credential.test.ts +0 -69
  278. package/src/utils/resolve-vault-credential.ts +0 -60
  279. package/src/utils/sandbox-runner-client.test.ts +0 -279
  280. package/src/utils/sandbox-runner-client.ts +0 -260
  281. package/src/utils/secrets-cache.ts +0 -183
  282. package/src/utils/sentry-identity.ts +0 -45
  283. package/src/utils/settle-with-timeout.test.ts +0 -21
  284. package/src/utils/settle-with-timeout.ts +0 -22
  285. package/src/utils/user-yaml-error.test.ts +0 -24
  286. package/src/utils/user-yaml-error.ts +0 -30
  287. package/src/utils/vault-api.test.ts +0 -536
  288. package/src/utils/vault-api.ts +0 -382
  289. package/src/utils/version-check.test.ts +0 -176
  290. package/src/utils/version-check.ts +0 -155
  291. package/src/utils/version-gate.test.ts +0 -1024
  292. package/src/utils/version-gate.ts +0 -702
  293. package/test/commands/db-tenant-isolation.test.ts +0 -94
  294. package/test/commands/db.test.ts +0 -85
  295. package/test/commands/signals.test.ts +0 -207
  296. package/test/commands/sources.test.ts +0 -231
  297. package/test/e2e/smoke-install-mcp.sh +0 -113
  298. package/test/fixtures/hq-pack-smoke-mcp/mcp/smoke-http.json +0 -1
  299. package/test/fixtures/hq-pack-smoke-mcp/package.yaml +0 -11
  300. package/test/fixtures/signals/action_item/sample.md +0 -16
  301. package/test/fixtures/signals/summary/sample.md +0 -12
  302. package/test/fixtures/sources/meetings/sample.md +0 -25
  303. package/test/helpers/cli-runner.ts +0 -150
  304. package/test/helpers/s3-list-mock.ts +0 -79
  305. package/test/helpers/vault-service-mock.ts +0 -295
  306. package/test/sources-signals/smoke.test.ts +0 -228
  307. package/tsconfig.json +0 -20
  308. package/vitest.config.ts +0 -11
  309. package/vitest.e2e.config.ts +0 -7
@@ -1,1039 +0,0 @@
1
- /**
2
- * Unit tests for `hq outposts` (outposts.ts).
3
- *
4
- * Mirrors company.test.ts: mock ensureCognitoToken, spy on global fetch, drive
5
- * through a Commander program, assert the /outpost/* request shape (method +
6
- * path + query) plus the destroy guard and the teardown-incomplete retry path.
7
- * Outposts are caller-scoped, so there is no company resolution to mock.
8
- */
9
-
10
- import { Command } from "commander";
11
- import {
12
- afterEach,
13
- beforeEach,
14
- describe,
15
- expect,
16
- it,
17
- vi,
18
- type MockInstance,
19
- } from "vitest";
20
-
21
- vi.mock("../utils/cognito-session.js", async (importOriginal) => {
22
- const original =
23
- await importOriginal<typeof import("../utils/cognito-session.js")>();
24
- return {
25
- ...original,
26
- ensureCognitoToken: vi.fn(async () => "test-token"),
27
- };
28
- });
29
-
30
- // Outpost provision reads the cached refresh token to send to the box. Stub the
31
- // hq-cloud token store so tests don't need a real session on disk.
32
- vi.mock("@indigoai-us/hq-cloud", async (importOriginal) => {
33
- const original =
34
- await importOriginal<typeof import("@indigoai-us/hq-cloud")>();
35
- return {
36
- ...original,
37
- loadCachedTokens: vi.fn(() => ({
38
- refreshToken: "rt_test",
39
- idToken: "id_test",
40
- accessToken: "at_test",
41
- })),
42
- };
43
- });
44
-
45
- // The SSH fallback shells out to `ssh` via spawnSync — stub it so tests don't
46
- // touch a real box or need the ssh binary.
47
- vi.mock("node:child_process", async (importOriginal) => {
48
- const original = await importOriginal<typeof import("node:child_process")>();
49
- return { ...original, spawnSync: vi.fn() };
50
- });
51
-
52
- import { execFileSync, spawnSync } from "node:child_process";
53
- import * as fs from "node:fs";
54
- import * as path from "node:path";
55
- import { ensureCognitoToken } from "../utils/cognito-session.js";
56
- import {
57
- fetchExecResult,
58
- joinCommandParts,
59
- registerOutpostsCommand,
60
- stageExecInput,
61
- submitExec,
62
- withRemoteHqDir,
63
- } from "./outposts.js";
64
-
65
- const mockSpawnSync = vi.mocked(spawnSync);
66
-
67
- function jsonResponse(status: number, body: unknown): Response {
68
- return new Response(JSON.stringify(body), {
69
- status,
70
- headers: { "Content-Type": "application/json" },
71
- });
72
- }
73
-
74
- let fetchSpy: MockInstance<typeof fetch>;
75
- let exitSpy: MockInstance<typeof process.exit>;
76
- let logSpy: MockInstance<typeof console.log>;
77
- const mockEnsureCognitoToken = vi.mocked(ensureCognitoToken);
78
-
79
- beforeEach(() => {
80
- vi.clearAllMocks();
81
- fetchSpy = vi.spyOn(globalThis, "fetch");
82
- mockEnsureCognitoToken.mockResolvedValue("test-token");
83
- exitSpy = vi.spyOn(process, "exit").mockImplementation((code?: number) => {
84
- throw new Error(`process.exit(${code})`);
85
- }) as unknown as MockInstance<typeof process.exit>;
86
- logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
87
- vi.spyOn(console, "error").mockImplementation(() => {});
88
- });
89
-
90
- afterEach(() => {
91
- vi.restoreAllMocks();
92
- });
93
-
94
- function buildProgram(): Command {
95
- const program = new Command();
96
- program.name("hq").exitOverride();
97
- registerOutpostsCommand(program);
98
- return program;
99
- }
100
-
101
- async function run(args: string[]): Promise<void> {
102
- await buildProgram().parseAsync(["node", "hq", ...args]);
103
- }
104
-
105
- describe("hq outposts list", () => {
106
- it("GETs /outpost/list", async () => {
107
- fetchSpy.mockResolvedValueOnce(
108
- jsonResponse(200, {
109
- outposts: [
110
- {
111
- outpostId: "primary",
112
- state: "running",
113
- instanceName: "outpost-abc",
114
- region: "us-east-1",
115
- agentRuntime: "claude",
116
- platform: "ec2",
117
- createdAt: "2026-01-01T00:00:00Z",
118
- },
119
- ],
120
- }),
121
- );
122
-
123
- await run(["outposts", "list"]);
124
-
125
- const [url, init] = fetchSpy.mock.calls[0];
126
- expect(String(url)).toContain("/outpost/list");
127
- expect(init?.method ?? "GET").toBe("GET");
128
- });
129
-
130
- it("emits raw JSON with --json", async () => {
131
- const stdoutSpy = vi
132
- .spyOn(process.stdout, "write")
133
- .mockImplementation(() => true);
134
- fetchSpy.mockResolvedValueOnce(jsonResponse(200, { outposts: [] }));
135
- await run(["outposts", "list", "--json"]);
136
- const printed = stdoutSpy.mock.calls.map((c) => String(c[0])).join("\n");
137
- expect(printed).toContain("[]");
138
- });
139
- });
140
-
141
- describe("hq outposts status", () => {
142
- it("GETs /outpost/status with the --id query param", async () => {
143
- fetchSpy.mockResolvedValueOnce(jsonResponse(200, { state: "running" }));
144
- await run(["outposts", "status", "--id", "2"]);
145
- const url = String(fetchSpy.mock.calls[0][0]);
146
- expect(url).toContain("/outpost/status");
147
- expect(url).toContain("outpostId=2");
148
- });
149
-
150
- it("omits outpostId when --id is not given", async () => {
151
- fetchSpy.mockResolvedValueOnce(jsonResponse(200, { state: "running" }));
152
- await run(["outposts", "status"]);
153
- expect(String(fetchSpy.mock.calls[0][0])).not.toContain("outpostId=");
154
- });
155
- });
156
-
157
- describe("hq outposts codex-enable / login", () => {
158
- it("POSTs /outpost/codex/enable", async () => {
159
- fetchSpy.mockResolvedValueOnce(jsonResponse(200, { ok: true }));
160
- await run(["outposts", "codex-enable"]);
161
- const [url, init] = fetchSpy.mock.calls[0];
162
- expect(String(url)).toContain("/outpost/codex/enable");
163
- expect(init?.method).toBe("POST");
164
- });
165
-
166
- it("POSTs /outpost/regenerate-login-url", async () => {
167
- fetchSpy.mockResolvedValueOnce(jsonResponse(200, { ok: true }));
168
- await run(["outposts", "login"]);
169
- const [url, init] = fetchSpy.mock.calls[0];
170
- expect(String(url)).toContain("/outpost/regenerate-login-url");
171
- expect(init?.method).toBe("POST");
172
- });
173
-
174
- it("login-code POSTs /outpost/login-code with the code in the body", async () => {
175
- fetchSpy.mockResolvedValueOnce(
176
- jsonResponse(200, { ok: true, userId: "u1" }),
177
- );
178
- await run(["outposts", "login-code", "ABC-123"]);
179
- const [url, init] = fetchSpy.mock.calls[0];
180
- expect(String(url)).toContain("/outpost/login-code");
181
- expect(init?.method).toBe("POST");
182
- expect(JSON.parse(String(init?.body))).toEqual({ code: "ABC-123" });
183
- });
184
-
185
- it("login-code targets a specific box via --id (outpostId query)", async () => {
186
- fetchSpy.mockResolvedValueOnce(
187
- jsonResponse(200, { ok: true, userId: "u1" }),
188
- );
189
- await run(["outposts", "login-code", "CODE", "--id", "3"]);
190
- const [url] = fetchSpy.mock.calls[0];
191
- expect(String(url)).toContain("outpostId=3");
192
- });
193
-
194
- it("login-code trims whitespace and refuses an empty code without calling the API", async () => {
195
- await expect(run(["outposts", "login-code", " "])).rejects.toThrow(
196
- "process.exit(1)",
197
- );
198
- expect(fetchSpy).not.toHaveBeenCalled();
199
- });
200
-
201
- it("login-code surfaces an awaiting-login not-found error", async () => {
202
- fetchSpy.mockResolvedValueOnce(
203
- jsonResponse(404, {
204
- error: true,
205
- step: "not-found",
206
- message: "no outpost",
207
- }),
208
- );
209
- await expect(run(["outposts", "login-code", "CODE"])).rejects.toThrow(
210
- "process.exit(1)",
211
- );
212
- });
213
- });
214
-
215
- describe("hq outposts destroy", () => {
216
- it("refuses without --yes and does not call the API", async () => {
217
- await expect(run(["outposts", "destroy"])).rejects.toThrow(
218
- "process.exit(1)",
219
- );
220
- expect(fetchSpy).not.toHaveBeenCalled();
221
- });
222
-
223
- it("POSTs /outpost/destroy with --yes", async () => {
224
- fetchSpy.mockResolvedValueOnce(
225
- jsonResponse(200, { ok: true, userId: "u1" }),
226
- );
227
- await run(["outposts", "destroy", "--yes"]);
228
- const [url, init] = fetchSpy.mock.calls[0];
229
- expect(String(url)).toContain("/outpost/destroy");
230
- expect(init?.method).toBe("POST");
231
- });
232
-
233
- it("treats a 409 teardown-incomplete as retryable (no exit)", async () => {
234
- fetchSpy.mockResolvedValueOnce(
235
- jsonResponse(409, { error: true, step: "teardown-incomplete" }),
236
- );
237
- await run(["outposts", "destroy", "--id", "2", "--yes"]);
238
- const printed = logSpy.mock.calls.map((c) => String(c[0])).join("\n");
239
- expect(printed).toMatch(/still in progress/i);
240
- expect(exitSpy).not.toHaveBeenCalled();
241
- });
242
-
243
- it("exits 1 on a genuine failure", async () => {
244
- fetchSpy.mockResolvedValueOnce(
245
- jsonResponse(500, { error: "boom", step: "teardown" }),
246
- );
247
- await expect(run(["outposts", "destroy", "--yes"])).rejects.toThrow(
248
- "process.exit(1)",
249
- );
250
- });
251
- });
252
-
253
- describe("hq outposts provision (billing gate)", () => {
254
- it("refuses without --yes, prints the cost, and makes NO API call", async () => {
255
- const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
256
- await expect(run(["outposts", "provision"])).rejects.toThrow(
257
- "process.exit(1)",
258
- );
259
- expect(fetchSpy).not.toHaveBeenCalled();
260
- const printed = errSpy.mock.calls
261
- .map((c) => c.map(String).join(" "))
262
- .join("\n");
263
- expect(printed).toMatch(/\$80\.00\/month/);
264
- });
265
-
266
- it("POSTs /outpost/provision with the refresh token when --yes", async () => {
267
- fetchSpy.mockResolvedValueOnce(jsonResponse(200, { ok: true }));
268
- await run(["outposts", "provision", "--runtime", "codex", "--yes"]);
269
- const [url, init] = fetchSpy.mock.calls[0];
270
- expect(String(url)).toContain("/outpost/provision");
271
- expect(init?.method).toBe("POST");
272
- const sent = JSON.parse(init?.body as string);
273
- expect(sent.refreshToken).toBe("rt_test");
274
- expect(sent.agentRuntime).toBe("codex");
275
- });
276
-
277
- it("rejects an unknown --runtime before the charge gate and makes NO API call", async () => {
278
- const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
279
- await expect(
280
- run(["outposts", "provision", "--runtime", "codx", "--yes"]),
281
- ).rejects.toThrow("process.exit(1)");
282
- expect(fetchSpy).not.toHaveBeenCalled();
283
- const printed = errSpy.mock.calls
284
- .map((c) => c.map(String).join(" "))
285
- .join("\n");
286
- // A typo'd runtime must not silently resolve to claude — codex is the whole
287
- // point of the flag, so a near-miss has to name itself.
288
- expect(printed).toContain("codx");
289
- expect(printed).toMatch(/claude/);
290
- expect(printed).toMatch(/codex/);
291
- });
292
-
293
- it("accepts an explicit --runtime claude", async () => {
294
- fetchSpy.mockResolvedValueOnce(jsonResponse(200, { ok: true }));
295
- await run(["outposts", "provision", "--runtime", "claude", "--yes"]);
296
- const sent = JSON.parse(fetchSpy.mock.calls[0][1]?.body as string);
297
- expect(sent.agentRuntime).toBe("claude");
298
- });
299
-
300
- it("explains the per-person cap on a 409 rather than printing a bare 'Conflict'", async () => {
301
- const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
302
- // hq-pro's cap body carries `capped`/`limit`/`outposts` and NO
303
- // `message`/`error` field, so the generic decoder falls through to
304
- // res.statusText — the operator sees only "Conflict".
305
- fetchSpy.mockResolvedValueOnce(
306
- jsonResponse(409, {
307
- capped: true,
308
- userId: "user_test_123",
309
- limit: 1,
310
- outposts: [
311
- {
312
- outpostId: "primary",
313
- state: "ready",
314
- instanceName: "outpost-user_test_123",
315
- region: "us-east-1",
316
- agentRuntime: "claude",
317
- platform: "ec2",
318
- createdAt: "2026-07-01T00:00:00.000Z",
319
- },
320
- ],
321
- }),
322
- );
323
-
324
- await expect(run(["outposts", "provision", "--yes"])).rejects.toThrow(
325
- "process.exit(1)",
326
- );
327
-
328
- const printed = errSpy.mock.calls
329
- .map((c) => c.map(String).join(" "))
330
- .join("\n");
331
- expect(printed).not.toMatch(/^Conflict$/m);
332
- expect(printed).toMatch(/limit/i);
333
- // Name the box they already own, and reassure on the charge they were
334
- // just warned about — the cap is checked BEFORE activation billing.
335
- expect(printed).toContain("primary");
336
- expect(printed).toMatch(/not been charged/i);
337
- });
338
-
339
- it("mints a card-capture link on 402 billing_required and exits 1", async () => {
340
- fetchSpy
341
- .mockResolvedValueOnce(
342
- jsonResponse(402, {
343
- error: "payment required",
344
- billing: {
345
- status: "billing_required",
346
- setup: {
347
- payerType: "person",
348
- path: "/v1/billing/checkout/person",
349
- method: "POST",
350
- },
351
- },
352
- }),
353
- )
354
- .mockResolvedValueOnce(
355
- jsonResponse(200, { url: "https://checkout.stripe.com/outpost" }),
356
- );
357
-
358
- await expect(run(["outposts", "provision", "--yes"])).rejects.toThrow(
359
- "process.exit(1)",
360
- );
361
-
362
- expect(String(fetchSpy.mock.calls[1][0])).toContain(
363
- "/v1/billing/checkout/person",
364
- );
365
- const printed = logSpy.mock.calls.map((c) => String(c[0])).join("\n");
366
- expect(printed).toContain("https://checkout.stripe.com/outpost");
367
- });
368
-
369
- it("surfaces the DECLINE reason on 402 payment_failed — never the 'No card on file' copy", async () => {
370
- const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
371
- // hq-pro's payment_failed envelope: a card EXISTS and was declined. The
372
- // server's `message` carries the friendly decline copy; telling this user
373
- // "No card on file" sends them down the wrong remediation path entirely.
374
- fetchSpy
375
- .mockResolvedValueOnce(
376
- jsonResponse(402, {
377
- error: "payment required",
378
- message: "Your card was declined. Try a different card or contact your bank.",
379
- billing: {
380
- status: "payment_failed",
381
- setup: {
382
- payerType: "person",
383
- path: "/v1/billing/checkout/person",
384
- method: "POST",
385
- },
386
- },
387
- }),
388
- )
389
- .mockResolvedValueOnce(
390
- jsonResponse(200, { url: "https://checkout.stripe.com/update-card" }),
391
- );
392
-
393
- await expect(run(["outposts", "provision", "--yes"])).rejects.toThrow(
394
- "process.exit(1)",
395
- );
396
-
397
- const printed = [...errSpy.mock.calls, ...logSpy.mock.calls]
398
- .map((c) => c.map(String).join(" "))
399
- .join("\n");
400
- expect(printed).toContain("declined");
401
- expect(printed).not.toContain("No card on file");
402
- // The card-capture link still surfaces — as the way to UPDATE the card.
403
- expect(printed).toContain("https://checkout.stripe.com/update-card");
404
- });
405
- });
406
-
407
- describe("hq outposts exec", () => {
408
- afterEach(() => {
409
- process.exitCode = undefined;
410
- });
411
-
412
- it("wraps the command to run from the box's HQ folder, then runs it", () => {
413
- const wrapped = withRemoteHqDir("pwd");
414
- // SSM may invoke the command as root with HOME unset. Seed a real home so
415
- // tools such as gh do not write machine state into the HQ checkout.
416
- expect(wrapped).toContain('export HOME="${HOME:-/root}"');
417
- // cd into $HOME/hq (SSH/login user) …
418
- expect(wrapped).toContain('cd "$HOME/hq"');
419
- // … falling back to ec2-user's home for the SSM/root path (no $HOME) …
420
- expect(wrapped).toContain("cd ~ec2-user/hq");
421
- // … never failing merely because the box has no HQ folder …
422
- expect(wrapped).toContain("|| true");
423
- // … and the caller's command still runs after the cd.
424
- expect(wrapped.endsWith("; pwd")).toBe(true);
425
- });
426
-
427
- it("POSTs /outpost/exec with the joined command and streams stdout", async () => {
428
- const stdoutSpy = vi
429
- .spyOn(process.stdout, "write")
430
- .mockImplementation(() => true);
431
- fetchSpy.mockResolvedValueOnce(
432
- jsonResponse(200, {
433
- ok: true,
434
- outpostId: "primary",
435
- instanceId: "i-abc",
436
- commandId: "cmd-1",
437
- status: "Success",
438
- exitCode: 0,
439
- stdout: "hello world\n",
440
- stderr: "",
441
- truncated: false,
442
- }),
443
- );
444
-
445
- await run(["outposts", "exec", "echo", "hello", "world"]);
446
-
447
- const [url, init] = fetchSpy.mock.calls[0];
448
- expect(String(url)).toContain("/outpost/exec");
449
- expect(init?.method).toBe("POST");
450
- // The command is wrapped so it runs from the box's HQ folder.
451
- expect(JSON.parse(init?.body as string)).toEqual({
452
- command: withRemoteHqDir("'echo' 'hello' 'world'"),
453
- });
454
- const printed = stdoutSpy.mock.calls.map((c) => String(c[0])).join("");
455
- expect(printed).toContain("hello world\n");
456
- expect(process.exitCode).toBe(0);
457
- });
458
-
459
- it("propagates a non-zero remote exit code", async () => {
460
- vi.spyOn(process.stdout, "write").mockImplementation(() => true);
461
- vi.spyOn(process.stderr, "write").mockImplementation(() => true);
462
- fetchSpy.mockResolvedValueOnce(
463
- jsonResponse(200, {
464
- ok: true,
465
- outpostId: "primary",
466
- instanceId: "i-abc",
467
- commandId: "cmd-2",
468
- status: "Failed",
469
- exitCode: 2,
470
- stdout: "",
471
- stderr: "nope\n",
472
- truncated: false,
473
- }),
474
- );
475
- await run(["outposts", "exec", "exit", "2"]);
476
- expect(process.exitCode).toBe(2);
477
- });
478
-
479
- it("passes --id through as the outpostId query param", async () => {
480
- vi.spyOn(process.stdout, "write").mockImplementation(() => true);
481
- fetchSpy.mockResolvedValueOnce(
482
- jsonResponse(200, {
483
- ok: true,
484
- outpostId: "2",
485
- instanceId: "i-2",
486
- commandId: "c",
487
- status: "Success",
488
- exitCode: 0,
489
- stdout: "",
490
- stderr: "",
491
- truncated: false,
492
- }),
493
- );
494
- await run(["outposts", "exec", "--id", "2", "uptime"]);
495
- expect(String(fetchSpy.mock.calls[0][0])).toContain("outpostId=2");
496
- });
497
-
498
- it("exits 1 and surfaces the server message on a non-2xx (e.g. not-ready)", async () => {
499
- fetchSpy.mockResolvedValueOnce(
500
- jsonResponse(409, {
501
- error: true,
502
- step: "not-ready",
503
- message:
504
- 'outpost is "provisioning", not ready — it must be running to exec',
505
- }),
506
- );
507
- await expect(run(["outposts", "exec", "echo", "hi"])).rejects.toThrow(
508
- "process.exit(1)",
509
- );
510
- });
511
-
512
- it("emits raw JSON with --json", async () => {
513
- const stdoutSpy = vi
514
- .spyOn(process.stdout, "write")
515
- .mockImplementation(() => true);
516
- fetchSpy.mockResolvedValueOnce(
517
- jsonResponse(200, {
518
- ok: true,
519
- outpostId: "primary",
520
- instanceId: "i-abc",
521
- commandId: "cmd-3",
522
- status: "Success",
523
- exitCode: 0,
524
- stdout: "x\n",
525
- stderr: "",
526
- truncated: false,
527
- }),
528
- );
529
- await run(["outposts", "exec", "--json", "echo", "x"]);
530
- const printed = stdoutSpy.mock.calls.map((c) => String(c[0])).join("");
531
- expect(printed).toContain('"commandId": "cmd-3"');
532
- });
533
- });
534
-
535
- describe("hq outposts exec — Lightsail SSH fallback", () => {
536
- afterEach(() => {
537
- process.exitCode = undefined;
538
- });
539
-
540
- function platformUnsupported(): Response {
541
- return jsonResponse(409, {
542
- error: true,
543
- step: "platform-unsupported",
544
- message: "remote exec requires the EC2 platform",
545
- });
546
- }
547
-
548
- function sshAccess(): Response {
549
- return jsonResponse(200, {
550
- ok: true,
551
- outpostId: "primary",
552
- platform: "lightsail",
553
- host: "52.2.2.2",
554
- port: 22,
555
- username: "ec2-user",
556
- privateKey: "-----BEGIN KEY-----\nx\n-----END KEY-----\n",
557
- });
558
- }
559
-
560
- it("falls back to SSH when the box is Lightsail, streams stdout, propagates exit", async () => {
561
- const stdoutSpy = vi
562
- .spyOn(process.stdout, "write")
563
- .mockImplementation(() => true);
564
- fetchSpy
565
- .mockResolvedValueOnce(platformUnsupported()) // /outpost/exec (SSM) → 409
566
- .mockResolvedValueOnce(sshAccess()); // /outpost/ssh-access → 200
567
- mockSpawnSync.mockReturnValue({
568
- status: 0,
569
- stdout: "lightsail-out\n",
570
- stderr: "",
571
- signal: null,
572
- output: [],
573
- pid: 1,
574
- } as unknown as ReturnType<typeof spawnSync>);
575
-
576
- // Flags meant for the remote command go after `--` (same as the SSM path).
577
- await run(["outposts", "exec", "--", "uname", "-a"]);
578
-
579
- // Second HTTP call is the ssh-access mint.
580
- expect(String(fetchSpy.mock.calls[1][0])).toContain("/outpost/ssh-access");
581
- // ssh was invoked with the box coordinates and the command as the last arg.
582
- const args = mockSpawnSync.mock.calls[0][1] as string[];
583
- expect(mockSpawnSync.mock.calls[0][0]).toBe("ssh");
584
- expect(args).toContain("ec2-user@52.2.2.2");
585
- expect(args[args.length - 1]).toBe(withRemoteHqDir("'uname' '-a'"));
586
- expect(args).toContain("BatchMode=yes");
587
- const printed = stdoutSpy.mock.calls.map((c) => String(c[0])).join("");
588
- expect(printed).toContain("lightsail-out\n");
589
- expect(process.exitCode).toBe(0);
590
- });
591
-
592
- it("propagates a non-zero SSH exit code", async () => {
593
- vi.spyOn(process.stdout, "write").mockImplementation(() => true);
594
- vi.spyOn(process.stderr, "write").mockImplementation(() => true);
595
- fetchSpy
596
- .mockResolvedValueOnce(platformUnsupported())
597
- .mockResolvedValueOnce(sshAccess());
598
- mockSpawnSync.mockReturnValue({
599
- status: 7,
600
- stdout: "",
601
- stderr: "boom\n",
602
- signal: null,
603
- output: [],
604
- pid: 1,
605
- } as unknown as ReturnType<typeof spawnSync>);
606
- await run(["outposts", "exec", "false"]);
607
- expect(process.exitCode).toBe(7);
608
- });
609
-
610
- it("exits 1 with a clear message when the ssh client is missing", async () => {
611
- fetchSpy
612
- .mockResolvedValueOnce(platformUnsupported())
613
- .mockResolvedValueOnce(sshAccess());
614
- const enoent = Object.assign(new Error("spawnSync ssh ENOENT"), {
615
- code: "ENOENT",
616
- });
617
- mockSpawnSync.mockReturnValue({
618
- error: enoent,
619
- status: null,
620
- stdout: "",
621
- stderr: "",
622
- signal: null,
623
- output: [],
624
- pid: 0,
625
- } as unknown as ReturnType<typeof spawnSync>);
626
- await expect(run(["outposts", "exec", "echo", "hi"])).rejects.toThrow(
627
- "process.exit(1)",
628
- );
629
- });
630
-
631
- it("does NOT fall back for a non-platform error (e.g. not-ready)", async () => {
632
- fetchSpy.mockResolvedValueOnce(
633
- jsonResponse(409, {
634
- error: true,
635
- step: "not-ready",
636
- message: "not ready",
637
- }),
638
- );
639
- await expect(run(["outposts", "exec", "echo", "hi"])).rejects.toThrow(
640
- "process.exit(1)",
641
- );
642
- // Only the SSM call happened; no ssh-access mint, no ssh.
643
- expect(fetchSpy).toHaveBeenCalledTimes(1);
644
- expect(mockSpawnSync).not.toHaveBeenCalled();
645
- });
646
- });
647
-
648
-
649
- describe("hq outposts exec --async / --detach", () => {
650
- afterEach(() => {
651
- process.exitCode = undefined;
652
- });
653
-
654
- it("--detach submits with HQ-dir wrapper and prints commandId without waiting", async () => {
655
- const stdoutSpy = vi
656
- .spyOn(process.stdout, "write")
657
- .mockImplementation(() => true);
658
- fetchSpy.mockResolvedValueOnce(
659
- jsonResponse(200, {
660
- ok: true,
661
- userId: "u",
662
- outpostId: "3",
663
- instanceId: "i-3",
664
- commandId: "cmd-detach",
665
- outputPrefix: "outpost-exec/u/3/n/out",
666
- executionTimeoutSeconds: 172800,
667
- }),
668
- );
669
-
670
- await run(["outposts", "exec", "--detach", "--id", "3", "--json", "sleep", "999"]);
671
-
672
- expect(fetchSpy).toHaveBeenCalledTimes(1);
673
- const [url, init] = fetchSpy.mock.calls[0];
674
- expect(String(url)).toContain("/outpost/exec");
675
- expect(String(url)).toContain("outpostId=3");
676
- expect(JSON.parse(init?.body as string)).toEqual({
677
- mode: "submit",
678
- command: withRemoteHqDir("'sleep' '999'"),
679
- });
680
- const printed = stdoutSpy.mock.calls.map((c) => String(c[0])).join("");
681
- expect(printed).toContain('"commandId":"cmd-detach"');
682
- // No second poll.
683
- expect(fetchSpy).toHaveBeenCalledTimes(1);
684
- });
685
-
686
- it("--async submits then polls until done and streams stdout", async () => {
687
- const stdoutSpy = vi
688
- .spyOn(process.stdout, "write")
689
- .mockImplementation(() => true);
690
- fetchSpy
691
- .mockResolvedValueOnce(
692
- jsonResponse(200, {
693
- ok: true,
694
- userId: "u",
695
- outpostId: "primary",
696
- instanceId: "i",
697
- commandId: "cmd-async",
698
- outputPrefix: "p",
699
- executionTimeoutSeconds: 7200,
700
- }),
701
- )
702
- .mockResolvedValueOnce(
703
- jsonResponse(200, {
704
- ok: true,
705
- userId: "u",
706
- outpostId: "primary",
707
- status: "Success",
708
- done: true,
709
- exitCode: 0,
710
- stdout: "finished\n",
711
- stderr: "",
712
- }),
713
- );
714
-
715
- await run([
716
- "outposts",
717
- "exec",
718
- "--async",
719
- "--timeout-seconds",
720
- "7200",
721
- "echo",
722
- "finished",
723
- ]);
724
-
725
- // submit + one result poll (terminal on first poll)
726
- expect(fetchSpy).toHaveBeenCalledTimes(2);
727
- expect(JSON.parse(fetchSpy.mock.calls[0][1]?.body as string)).toEqual({
728
- mode: "submit",
729
- command: withRemoteHqDir("'echo' 'finished'"),
730
- timeoutSeconds: 7200,
731
- });
732
- expect(JSON.parse(fetchSpy.mock.calls[1][1]?.body as string)).toEqual({
733
- mode: "result",
734
- commandId: "cmd-async",
735
- });
736
- const printed = stdoutSpy.mock.calls.map((c) => String(c[0])).join("");
737
- expect(printed).toContain("finished\n");
738
- expect(process.exitCode).toBe(0);
739
- });
740
-
741
- it("refuses --async and --detach together", async () => {
742
- await expect(
743
- run(["outposts", "exec", "--async", "--detach", "true"]),
744
- ).rejects.toThrow("process.exit(1)");
745
- expect(fetchSpy).not.toHaveBeenCalled();
746
- });
747
-
748
- it("exec-submit forwards --timeout-seconds", async () => {
749
- const stdoutSpy = vi
750
- .spyOn(process.stdout, "write")
751
- .mockImplementation(() => true);
752
- fetchSpy.mockResolvedValueOnce(
753
- jsonResponse(200, {
754
- ok: true,
755
- commandId: "cmd-sub",
756
- executionTimeoutSeconds: 3600,
757
- userId: "u",
758
- outpostId: "primary",
759
- instanceId: "i",
760
- outputPrefix: "p",
761
- }),
762
- );
763
- await run([
764
- "outposts",
765
- "exec-submit",
766
- "--timeout-seconds",
767
- "3600",
768
- "--json",
769
- "sleep",
770
- "10",
771
- ]);
772
- expect(JSON.parse(fetchSpy.mock.calls[0][1]?.body as string)).toEqual({
773
- mode: "submit",
774
- command: "'sleep' '10'",
775
- timeoutSeconds: 3600,
776
- });
777
- const printed = stdoutSpy.mock.calls.map((c) => String(c[0])).join("");
778
- expect(printed).toContain("cmd-sub");
779
- });
780
- });
781
-
782
- describe("hq outposts asynchronous exec", () => {
783
- it("sends the stage, submit, and result mode request bodies", async () => {
784
- fetchSpy
785
- .mockResolvedValueOnce(
786
- jsonResponse(200, {
787
- ok: true,
788
- userId: "u1",
789
- outpostId: "2",
790
- key: "input-key",
791
- putUrl: "https://upload.invalid/put",
792
- getUrl: "https://download.invalid/get",
793
- expiresInSeconds: 300,
794
- }),
795
- )
796
- .mockResolvedValueOnce(
797
- jsonResponse(200, {
798
- ok: true,
799
- userId: "u1",
800
- outpostId: "2",
801
- instanceId: "i-2",
802
- commandId: "cmd-1",
803
- outputPrefix: "output-prefix",
804
- }),
805
- )
806
- .mockResolvedValueOnce(
807
- jsonResponse(200, {
808
- ok: true,
809
- userId: "u1",
810
- outpostId: "2",
811
- status: "InProgress",
812
- done: false,
813
- }),
814
- );
815
-
816
- await stageExecInput("test-token", "2");
817
- await submitExec("test-token", "cd /tmp && run-worker", "2");
818
- await fetchExecResult("test-token", "cmd-1", "2");
819
-
820
- expect(fetchSpy.mock.calls.map(([url]) => String(url))).toEqual([
821
- expect.stringContaining("/outpost/exec?outpostId=2"),
822
- expect.stringContaining("/outpost/exec?outpostId=2"),
823
- expect.stringContaining("/outpost/exec?outpostId=2"),
824
- ]);
825
- expect(
826
- fetchSpy.mock.calls.map(([, init]) => JSON.parse(init?.body as string)),
827
- ).toEqual([
828
- { mode: "stage" },
829
- { mode: "submit", command: "cd /tmp && run-worker" },
830
- { mode: "result", commandId: "cmd-1" },
831
- ]);
832
- });
833
-
834
- it("exec-stage PUTs the exact file bytes and prints only key + getUrl JSON", async () => {
835
- const stdoutSpy = vi
836
- .spyOn(process.stdout, "write")
837
- .mockImplementation(() => true);
838
- const file = path.join(process.cwd(), "package.json");
839
- const expectedBytes = fs.readFileSync(file);
840
- fetchSpy
841
- .mockResolvedValueOnce(
842
- jsonResponse(200, {
843
- ok: true,
844
- userId: "u1",
845
- outpostId: "2",
846
- key: "input-key",
847
- putUrl: "https://upload.invalid/put",
848
- getUrl: "https://download.invalid/get",
849
- expiresInSeconds: 300,
850
- }),
851
- )
852
- .mockResolvedValueOnce(new Response(null, { status: 200 }));
853
-
854
- await run([
855
- "outposts",
856
- "exec-stage",
857
- "--id",
858
- "2",
859
- "--file",
860
- file,
861
- "--json",
862
- ]);
863
-
864
- expect(fetchSpy).toHaveBeenCalledTimes(2);
865
- const [uploadUrl, uploadInit] = fetchSpy.mock.calls[1];
866
- expect(String(uploadUrl)).toBe("https://upload.invalid/put");
867
- expect(uploadInit?.method).toBe("PUT");
868
- expect(uploadInit?.headers).toEqual({
869
- "Content-Length": String(expectedBytes.byteLength),
870
- });
871
- expect(Buffer.from(uploadInit?.body as Uint8Array)).toEqual(expectedBytes);
872
- const printed = stdoutSpy.mock.calls
873
- .map((call) => String(call[0]))
874
- .join("");
875
- expect(JSON.parse(printed)).toEqual({
876
- key: "input-key",
877
- getUrl: "https://download.invalid/get",
878
- });
879
- });
880
-
881
- it("exec-stage exits non-zero when the presigned upload fails", async () => {
882
- fetchSpy
883
- .mockResolvedValueOnce(
884
- jsonResponse(200, {
885
- ok: true,
886
- userId: "u1",
887
- outpostId: "primary",
888
- key: "input-key",
889
- putUrl: "https://upload.invalid/put",
890
- getUrl: "https://download.invalid/get",
891
- expiresInSeconds: 300,
892
- }),
893
- )
894
- .mockResolvedValueOnce(new Response(null, { status: 503 }));
895
-
896
- await expect(
897
- run([
898
- "outposts",
899
- "exec-stage",
900
- "--file",
901
- path.join(process.cwd(), "package.json"),
902
- "--json",
903
- ]),
904
- ).rejects.toThrow("process.exit(1)");
905
- });
906
-
907
- it("exec-submit shell-quotes multiple argv parts without adding the HQ-directory wrapper", async () => {
908
- const stdoutSpy = vi
909
- .spyOn(process.stdout, "write")
910
- .mockImplementation(() => true);
911
- const script = 'printf "%s\\n" "$0" "$1" "$2"';
912
- fetchSpy.mockResolvedValueOnce(
913
- jsonResponse(200, {
914
- ok: true,
915
- userId: "u1",
916
- outpostId: "primary",
917
- instanceId: "i-1",
918
- commandId: "cmd-submit",
919
- outputPrefix: "output-prefix",
920
- }),
921
- );
922
-
923
- await run([
924
- "outposts",
925
- "exec-submit",
926
- "--json",
927
- "--",
928
- "bash",
929
- "-c",
930
- script,
931
- "marker",
932
- "a b",
933
- "quote's",
934
- ]);
935
-
936
- const body = JSON.parse(fetchSpy.mock.calls[0][1]?.body as string);
937
- expect(body).toEqual({
938
- mode: "submit",
939
- command: joinCommandParts([
940
- "bash",
941
- "-c",
942
- script,
943
- "marker",
944
- "a b",
945
- "quote's",
946
- ]),
947
- });
948
- expect(body.command).not.toContain("cd ~ec2-user/hq");
949
- expect(JSON.parse(String(stdoutSpy.mock.calls[0][0]))).toEqual({
950
- commandId: "cmd-submit",
951
- });
952
- });
953
-
954
- // Spawns a real `bash` (execFileSync) to prove the round-trip end-to-end.
955
- // Under the saturated full-suite parallel pool that spawn can exceed vitest's
956
- // 5s default purely on scheduling contention; the extra budget covers that,
957
- // not slow logic (isolated, this runs in milliseconds).
958
- it("preserves one command part verbatim and round-trips quoted bash argv", () => {
959
- const single = " printf 'already formed' ";
960
- expect(joinCommandParts([single])).toBe(single);
961
-
962
- const script = 'printf "%s\\n" "$0" "$1" "$2"';
963
- const command = joinCommandParts([
964
- "bash",
965
- "-c",
966
- script,
967
- "marker",
968
- "a b",
969
- "quote's",
970
- ]);
971
- expect(execFileSync("bash", ["-c", command], { encoding: "utf8" })).toBe(
972
- "marker\na b\nquote's\n",
973
- );
974
- }, 30_000);
975
-
976
- it("exec-result --wait retries a 429, polls until done, and prints the result contract", async () => {
977
- vi.useFakeTimers();
978
- try {
979
- const stdoutSpy = vi
980
- .spyOn(process.stdout, "write")
981
- .mockImplementation(() => true);
982
- fetchSpy
983
- .mockResolvedValueOnce(
984
- jsonResponse(429, {
985
- error: true,
986
- step: "rate",
987
- message: "slow down",
988
- }),
989
- )
990
- .mockResolvedValueOnce(
991
- jsonResponse(200, {
992
- ok: true,
993
- userId: "u1",
994
- outpostId: "2",
995
- status: "InProgress",
996
- done: false,
997
- }),
998
- )
999
- .mockResolvedValueOnce(
1000
- jsonResponse(200, {
1001
- ok: true,
1002
- userId: "u1",
1003
- outpostId: "2",
1004
- status: "Success",
1005
- done: true,
1006
- exitCode: 0,
1007
- stdout: "finished\n",
1008
- stderr: "",
1009
- truncated: false,
1010
- }),
1011
- );
1012
-
1013
- const pending = run([
1014
- "outposts",
1015
- "exec-result",
1016
- "--id",
1017
- "2",
1018
- "--command-id",
1019
- "cmd-1",
1020
- "--wait",
1021
- "--json",
1022
- ]);
1023
- await vi.advanceTimersByTimeAsync(500);
1024
- await vi.advanceTimersByTimeAsync(1_000);
1025
- await pending;
1026
-
1027
- expect(fetchSpy).toHaveBeenCalledTimes(3);
1028
- expect(JSON.parse(String(stdoutSpy.mock.calls[0][0]))).toEqual({
1029
- done: true,
1030
- status: "Success",
1031
- exitCode: 0,
1032
- stdout: "finished\n",
1033
- stderr: "",
1034
- });
1035
- } finally {
1036
- vi.useRealTimers();
1037
- }
1038
- });
1039
- });