|
1 | 1 | import { spawn } from "node:child_process"; |
2 | | -import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs"; |
| 2 | +import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { dirname, join } from "node:path"; |
5 | 5 | import { fileURLToPath } from "node:url"; |
@@ -101,6 +101,18 @@ function expectQuiet(run: Run): void { |
101 | 101 | expect(output).not.toMatch(NAG); |
102 | 102 | } |
103 | 103 |
|
| 104 | +function declaresJson(command: CommandDef): boolean { |
| 105 | + const args = command.args; |
| 106 | + return typeof args === "object" && args !== null && "json" in args; |
| 107 | +} |
| 108 | + |
| 109 | +function expectJsonFailure(run: Run, code?: string): void { |
| 110 | + const parsed = JSON.parse(run.stdout) as { ok?: unknown; error?: { code?: unknown } }; |
| 111 | + expect(parsed.ok).toBe(false); |
| 112 | + expect(typeof parsed.error?.code).toBe("string"); |
| 113 | + if (code) expect(parsed.error?.code).toBe(code); |
| 114 | +} |
| 115 | + |
104 | 116 | const commands = await registeredCommands(); |
105 | 117 | // From source, `preview` refuses to load until the studio packages are built; the test job does |
106 | 118 | // not build them, so preview is only spawned where they are. |
@@ -130,6 +142,31 @@ describe("agent-mode contract", () => { |
130 | 142 | }, |
131 | 143 | ); |
132 | 144 |
|
| 145 | + it.each( |
| 146 | + spawnable |
| 147 | + .filter((c) => declaresJson(c.command)) |
| 148 | + .map((c) => [c.path.join(" "), c.path] as const), |
| 149 | + )("%s --json answers a usage error with the JSON envelope", async (_label, path) => { |
| 150 | + const run = await runAsAgent([...path, "--json", "--not-a-real-flag"]); |
| 151 | + expectQuiet(run); |
| 152 | + expect(run.code, run.stderr).toBe(2); |
| 153 | + expectJsonFailure(run); |
| 154 | + }); |
| 155 | + |
| 156 | + it.each( |
| 157 | + spawnable |
| 158 | + .filter((c) => !declaresJson(c.command)) |
| 159 | + .map((c) => [c.path.join(" "), c.path] as const), |
| 160 | + )( |
| 161 | + "%s has no JSON output, so --json is a usage error, answered as JSON", |
| 162 | + async (_label, path) => { |
| 163 | + const run = await runAsAgent([...path, "--json"]); |
| 164 | + expectQuiet(run); |
| 165 | + expect(run.code, run.stderr).toBe(2); |
| 166 | + expectJsonFailure(run); |
| 167 | + }, |
| 168 | + ); |
| 169 | + |
133 | 170 | it.each(spawnable.map((c) => [c.path.join(" "), c.path] as const))( |
134 | 171 | "%s rejects an unknown flag with exit 2", |
135 | 172 | async (_label, path) => { |
@@ -187,6 +224,67 @@ describe("agent-mode contract", () => { |
187 | 224 | }, |
188 | 225 | ); |
189 | 226 |
|
| 227 | + it.each([["lint"], ["check"], ["info"], ["validate"], ["layout"], ["inspect"]])( |
| 228 | + "%s --json in an empty directory names the real failure, and no notice", |
| 229 | + async (name) => { |
| 230 | + const run = await runAsAgent([name, "--json"]); |
| 231 | + const parsed = JSON.parse(run.stdout) as { ok: boolean; error: { message: string } }; |
| 232 | + expect(parsed.ok).toBe(false); |
| 233 | + expect(parsed.error.message).toContain("No composition found"); |
| 234 | + expect(parsed.error.message).not.toMatch(/Command failed|deprecated|anonymous usage/); |
| 235 | + }, |
| 236 | + ); |
| 237 | + |
| 238 | + it("a missing argument under --json is a usage_error with the real message", async () => { |
| 239 | + const run = await runAsAgent(["capture", "--json"]); |
| 240 | + expect(run.code).toBe(2); |
| 241 | + expectJsonFailure(run, "usage_error"); |
| 242 | + expect(JSON.parse(run.stdout).error.message).toContain("Missing URL"); |
| 243 | + }); |
| 244 | + |
| 245 | + it("a runtime failure under --json is an envelope that says why, not empty stdout", async () => { |
| 246 | + const run = await runAsAgent(["info", "--json"]); |
| 247 | + expectQuiet(run); |
| 248 | + expect(run.code).toBe(1); |
| 249 | + expectJsonFailure(run, "runtime_error"); |
| 250 | + expect(run.stdout).toContain("No composition found"); |
| 251 | + }); |
| 252 | + |
| 253 | + it("a render that fails under --json prints only the envelope on stdout", async () => { |
| 254 | + const project = join(home, "render-json"); |
| 255 | + mkdirSync(project, { recursive: true }); |
| 256 | + writeFileSync(join(project, "index.html"), "<!doctype html><html><body></body></html>"); |
| 257 | + const run = await runAsAgent(["render", project, "--variables", "{bad", "--json"]); |
| 258 | + expectQuiet(run); |
| 259 | + expect(run.code, run.stderr).toBe(1); |
| 260 | + expect(run.stdout.trim().split("\n")).toHaveLength(1); |
| 261 | + expectJsonFailure(run, "runtime_error"); |
| 262 | + expect(JSON.parse(run.stdout).error.message).toBe("Invalid JSON in --variables"); |
| 263 | + }); |
| 264 | + |
| 265 | + it("a --strict lint abort under --json names the lint errors, on stdout and stderr", async () => { |
| 266 | + const project = join(home, "render-strict"); |
| 267 | + mkdirSync(project, { recursive: true }); |
| 268 | + writeFileSync( |
| 269 | + join(project, "index.html"), |
| 270 | + '<!doctype html><html><body><div data-composition-id="main"></div></body></html>', |
| 271 | + ); |
| 272 | + const run = await runAsAgent(["render", project, "--strict", "--json"]); |
| 273 | + expectQuiet(run); |
| 274 | + expect(run.code, run.stderr).toBe(1); |
| 275 | + expectJsonFailure(run, "runtime_error"); |
| 276 | + const { message } = JSON.parse(run.stdout).error as { message: string }; |
| 277 | + expect(message).toMatch(/Aborting render due to lint issues in --strict mode: [1-9]\d* error/); |
| 278 | + expect(run.stderr).toContain(message); |
| 279 | + }); |
| 280 | + |
| 281 | + it("an unknown command under --json is an envelope with the suggestion", async () => { |
| 282 | + const run = await runAsAgent(["rendr", "--json"]); |
| 283 | + expect(run.code).toBe(2); |
| 284 | + expectJsonFailure(run, "unknown_command"); |
| 285 | + expect(JSON.parse(run.stdout).error.hint).toBe("Did you mean 'hyperframes render'?"); |
| 286 | + }); |
| 287 | + |
190 | 288 | it("an unknown flag suggests the flag it most likely means", async () => { |
191 | 289 | const run = await runAsAgent(["render", "--out", "x.mp4"]); |
192 | 290 | expect(run.code).toBe(2); |
|
0 commit comments