|
| 1 | +/** |
| 2 | + * Integration tests for `mux run` CLI command. |
| 3 | + * |
| 4 | + * These tests verify the CLI interface without actually running agent sessions. |
| 5 | + * They test argument parsing, help output, and error handling. |
| 6 | + */ |
| 7 | +import { describe, test, expect, beforeAll } from "bun:test"; |
| 8 | +import { spawn } from "child_process"; |
| 9 | +import * as path from "path"; |
| 10 | + |
| 11 | +const CLI_PATH = path.resolve(__dirname, "index.ts"); |
| 12 | +const RUN_PATH = path.resolve(__dirname, "run.ts"); |
| 13 | + |
| 14 | +interface ExecResult { |
| 15 | + stdout: string; |
| 16 | + stderr: string; |
| 17 | + output: string; // combined stdout + stderr |
| 18 | + exitCode: number; |
| 19 | +} |
| 20 | + |
| 21 | +async function runCli(args: string[], timeoutMs = 5000): Promise<ExecResult> { |
| 22 | + return new Promise((resolve) => { |
| 23 | + const proc = spawn("bun", [CLI_PATH, ...args], { |
| 24 | + timeout: timeoutMs, |
| 25 | + env: { ...process.env, NO_COLOR: "1" }, |
| 26 | + }); |
| 27 | + |
| 28 | + let stdout = ""; |
| 29 | + let stderr = ""; |
| 30 | + |
| 31 | + proc.stdout?.on("data", (data) => { |
| 32 | + stdout += data.toString(); |
| 33 | + }); |
| 34 | + |
| 35 | + proc.stderr?.on("data", (data) => { |
| 36 | + stderr += data.toString(); |
| 37 | + }); |
| 38 | + |
| 39 | + proc.on("close", (code) => { |
| 40 | + resolve({ stdout, stderr, output: stdout + stderr, exitCode: code ?? 1 }); |
| 41 | + }); |
| 42 | + |
| 43 | + proc.on("error", () => { |
| 44 | + resolve({ stdout, stderr, output: stdout + stderr, exitCode: 1 }); |
| 45 | + }); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Run run.ts directly with stdin closed to avoid hanging. |
| 51 | + * Passes empty stdin to simulate non-TTY invocation without input. |
| 52 | + */ |
| 53 | +async function runRunDirect(args: string[], timeoutMs = 5000): Promise<ExecResult> { |
| 54 | + return new Promise((resolve) => { |
| 55 | + const proc = spawn("bun", [RUN_PATH, ...args], { |
| 56 | + timeout: timeoutMs, |
| 57 | + env: { ...process.env, NO_COLOR: "1" }, |
| 58 | + stdio: ["pipe", "pipe", "pipe"], // stdin, stdout, stderr |
| 59 | + }); |
| 60 | + |
| 61 | + let stdout = ""; |
| 62 | + let stderr = ""; |
| 63 | + |
| 64 | + proc.stdout?.on("data", (data) => { |
| 65 | + stdout += data.toString(); |
| 66 | + }); |
| 67 | + |
| 68 | + proc.stderr?.on("data", (data) => { |
| 69 | + stderr += data.toString(); |
| 70 | + }); |
| 71 | + |
| 72 | + // Close stdin immediately to prevent hanging on stdin.read() |
| 73 | + proc.stdin?.end(); |
| 74 | + |
| 75 | + proc.on("close", (code) => { |
| 76 | + resolve({ stdout, stderr, output: stdout + stderr, exitCode: code ?? 1 }); |
| 77 | + }); |
| 78 | + |
| 79 | + proc.on("error", () => { |
| 80 | + resolve({ stdout, stderr, output: stdout + stderr, exitCode: 1 }); |
| 81 | + }); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +describe("mux CLI", () => { |
| 86 | + beforeAll(() => { |
| 87 | + // Verify CLI files exist |
| 88 | + expect(Bun.file(CLI_PATH).size).toBeGreaterThan(0); |
| 89 | + expect(Bun.file(RUN_PATH).size).toBeGreaterThan(0); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("top-level", () => { |
| 93 | + test("--help shows usage", async () => { |
| 94 | + const result = await runCli(["--help"]); |
| 95 | + expect(result.exitCode).toBe(0); |
| 96 | + expect(result.stdout).toContain("Usage: mux"); |
| 97 | + expect(result.stdout).toContain("Mux - AI agent orchestration"); |
| 98 | + expect(result.stdout).toContain("run"); |
| 99 | + expect(result.stdout).toContain("server"); |
| 100 | + }); |
| 101 | + |
| 102 | + test("--version shows version info", async () => { |
| 103 | + const result = await runCli(["--version"]); |
| 104 | + expect(result.exitCode).toBe(0); |
| 105 | + // Version format: vX.Y.Z-N-gHASH (HASH) |
| 106 | + expect(result.stdout).toMatch(/v\d+\.\d+\.\d+/); |
| 107 | + }); |
| 108 | + |
| 109 | + test("unknown command shows error", async () => { |
| 110 | + const result = await runCli(["nonexistent"]); |
| 111 | + expect(result.exitCode).toBe(1); |
| 112 | + expect(result.stderr).toContain("unknown command"); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe("mux run", () => { |
| 117 | + test("--help shows all options", async () => { |
| 118 | + const result = await runCli(["run", "--help"]); |
| 119 | + expect(result.exitCode).toBe(0); |
| 120 | + expect(result.stdout).toContain("Usage: mux run"); |
| 121 | + expect(result.stdout).toContain("--dir"); |
| 122 | + expect(result.stdout).toContain("--model"); |
| 123 | + expect(result.stdout).toContain("--runtime"); |
| 124 | + expect(result.stdout).toContain("--mode"); |
| 125 | + expect(result.stdout).toContain("--thinking"); |
| 126 | + expect(result.stdout).toContain("--timeout"); |
| 127 | + expect(result.stdout).toContain("--json"); |
| 128 | + expect(result.stdout).toContain("--quiet"); |
| 129 | + expect(result.stdout).toContain("--workspace-id"); |
| 130 | + expect(result.stdout).toContain("--config-root"); |
| 131 | + }); |
| 132 | + |
| 133 | + test("shows default model as opus", async () => { |
| 134 | + const result = await runCli(["run", "--help"]); |
| 135 | + expect(result.exitCode).toBe(0); |
| 136 | + expect(result.stdout).toContain("anthropic:claude-opus-4-5"); |
| 137 | + }); |
| 138 | + |
| 139 | + test("no message shows error", async () => { |
| 140 | + const result = await runRunDirect([]); |
| 141 | + expect(result.exitCode).toBe(1); |
| 142 | + expect(result.output).toContain("No message provided"); |
| 143 | + }); |
| 144 | + |
| 145 | + test("invalid thinking level shows error", async () => { |
| 146 | + const result = await runRunDirect(["--thinking", "extreme", "test message"]); |
| 147 | + expect(result.exitCode).toBe(1); |
| 148 | + expect(result.output).toContain("Invalid thinking level"); |
| 149 | + }); |
| 150 | + |
| 151 | + test("invalid mode shows error", async () => { |
| 152 | + const result = await runRunDirect(["--mode", "chaos", "test message"]); |
| 153 | + expect(result.exitCode).toBe(1); |
| 154 | + expect(result.output).toContain("Invalid mode"); |
| 155 | + }); |
| 156 | + |
| 157 | + test("invalid timeout shows error", async () => { |
| 158 | + const result = await runRunDirect(["--timeout", "abc", "test message"]); |
| 159 | + expect(result.exitCode).toBe(1); |
| 160 | + expect(result.output).toContain("Invalid timeout"); |
| 161 | + }); |
| 162 | + |
| 163 | + test("nonexistent directory shows error", async () => { |
| 164 | + const result = await runRunDirect([ |
| 165 | + "--dir", |
| 166 | + "/nonexistent/path/that/does/not/exist", |
| 167 | + "test message", |
| 168 | + ]); |
| 169 | + expect(result.exitCode).toBe(1); |
| 170 | + expect(result.output.length).toBeGreaterThan(0); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe("mux server", () => { |
| 175 | + test("--help shows all options", async () => { |
| 176 | + const result = await runCli(["server", "--help"]); |
| 177 | + expect(result.exitCode).toBe(0); |
| 178 | + expect(result.stdout).toContain("Usage: mux server"); |
| 179 | + expect(result.stdout).toContain("--host"); |
| 180 | + expect(result.stdout).toContain("--port"); |
| 181 | + expect(result.stdout).toContain("--auth-token"); |
| 182 | + expect(result.stdout).toContain("--add-project"); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments