|
| 1 | +import { |
| 2 | + describe, |
| 3 | + it, |
| 4 | + expect, |
| 5 | + beforeEach, |
| 6 | + afterEach, |
| 7 | + setDefaultTimeout, |
| 8 | +} from "bun:test"; |
| 9 | +import fs from "fs"; |
| 10 | +import os from "os"; |
| 11 | +import path from "path"; |
| 12 | + |
| 13 | +const CLI = path.resolve(import.meta.dir, "../index.ts"); |
| 14 | +const SUBPROCESS_TIMEOUT = 30_000; |
| 15 | + |
| 16 | +setDefaultTimeout(SUBPROCESS_TIMEOUT); |
| 17 | + |
| 18 | +async function runCli( |
| 19 | + args: string[], |
| 20 | + opts?: { cwd?: string; env?: Record<string, string> } |
| 21 | +): Promise<{ stdout: string; stderr: string; exitCode: number }> { |
| 22 | + const proc = Bun.spawn(["bun", "run", CLI, ...args], { |
| 23 | + stdout: "pipe", |
| 24 | + stderr: "pipe", |
| 25 | + cwd: opts?.cwd ?? process.cwd(), |
| 26 | + env: { ...process.env, ...(opts?.env ?? {}) }, |
| 27 | + }); |
| 28 | + const [stdout, stderr] = await Promise.all([ |
| 29 | + new Response(proc.stdout).text(), |
| 30 | + new Response(proc.stderr).text(), |
| 31 | + ]); |
| 32 | + const exitCode = await proc.exited; |
| 33 | + return { stdout, stderr, exitCode }; |
| 34 | +} |
| 35 | + |
| 36 | +describe("--json output mode", () => { |
| 37 | + let workDir: string; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + workDir = fs.mkdtempSync(path.join(os.tmpdir(), "ntt-json-test-")); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + if (workDir && fs.existsSync(workDir)) { |
| 45 | + fs.rmSync(workDir, { recursive: true, force: true }); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + it("ntt init --json emits a single JSON envelope on stdout", async () => { |
| 50 | + const { stdout, exitCode } = await runCli(["init", "Testnet", "--json"], { |
| 51 | + cwd: workDir, |
| 52 | + }); |
| 53 | + expect(exitCode).toBe(0); |
| 54 | + // stdout should be exactly the JSON envelope (plus trailing newline). |
| 55 | + const lines = stdout.trim().split("\n").filter(Boolean); |
| 56 | + expect(lines.length).toBe(1); |
| 57 | + const parsed = JSON.parse(lines[0]!); |
| 58 | + expect(parsed.ok).toBe(true); |
| 59 | + expect(parsed.command).toBe("init"); |
| 60 | + expect(parsed.data.network).toBe("Testnet"); |
| 61 | + expect(parsed.data.path).toBe("deployment.json"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("WL_NTT_JSON=1 activates the same envelope without --json flag", async () => { |
| 65 | + const { stdout, exitCode } = await runCli(["init", "Mainnet"], { |
| 66 | + cwd: workDir, |
| 67 | + env: { WL_NTT_JSON: "1" }, |
| 68 | + }); |
| 69 | + expect(exitCode).toBe(0); |
| 70 | + const lines = stdout.trim().split("\n").filter(Boolean); |
| 71 | + expect(lines.length).toBe(1); |
| 72 | + const parsed = JSON.parse(lines[0]!); |
| 73 | + expect(parsed.ok).toBe(true); |
| 74 | + expect(parsed.command).toBe("init"); |
| 75 | + expect(parsed.data.network).toBe("Mainnet"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("human mode is unchanged (no JSON envelope on stdout)", async () => { |
| 79 | + const { stdout, stderr, exitCode } = await runCli(["init", "Testnet"], { |
| 80 | + cwd: workDir, |
| 81 | + }); |
| 82 | + expect(exitCode).toBe(0); |
| 83 | + // Human-readable messages on stdout, no JSON envelope. |
| 84 | + expect(stdout).toContain("deployment.json created"); |
| 85 | + expect(stdout).not.toContain('"ok":true'); |
| 86 | + // Should NOT have routed human output to stderr in human mode. |
| 87 | + expect(stderr).not.toContain("deployment.json created"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("--json routes human messages to stderr, keeping stdout clean", async () => { |
| 91 | + const { stdout, stderr, exitCode } = await runCli( |
| 92 | + ["init", "Testnet", "--json"], |
| 93 | + { cwd: workDir } |
| 94 | + ); |
| 95 | + expect(exitCode).toBe(0); |
| 96 | + expect(stderr).toContain("deployment.json created"); |
| 97 | + // stdout must be ONLY the JSON envelope. |
| 98 | + expect(stdout.trim()).toMatch(/^\{.*\}$/); |
| 99 | + }); |
| 100 | +}); |
0 commit comments