|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import { spawnSync } from "child_process"; |
| 3 | +import { writeFileSync, mkdirSync, rmSync } from "fs"; |
| 4 | +import { tmpdir } from "os"; |
| 5 | +import { join, resolve } from "path"; |
| 6 | + |
| 7 | +const CLI_PATH = resolve(import.meta.dirname, "../../../dist/cli.js"); |
| 8 | + |
| 9 | +/** |
| 10 | + * Spawns the CLI with given args, returning exit code and stderr. |
| 11 | + */ |
| 12 | +function runCli( |
| 13 | + args: string[], |
| 14 | + env?: Record<string, string>, |
| 15 | +): { status: number; stderr: string; stdout: string } { |
| 16 | + const result = spawnSync("node", [CLI_PATH, ...args], { |
| 17 | + encoding: "utf8", |
| 18 | + cwd: resolve(import.meta.dirname, "../../.."), |
| 19 | + env: { |
| 20 | + ...process.env, |
| 21 | + ...env, |
| 22 | + // Ensure no stale auth leaks in |
| 23 | + GITHUB_TOKEN: undefined, |
| 24 | + GH_TOKEN: undefined, |
| 25 | + GH_ATTACH_COOKIES: undefined, |
| 26 | + ...env, |
| 27 | + }, |
| 28 | + }); |
| 29 | + return { |
| 30 | + status: result.status ?? 1, |
| 31 | + stderr: result.stderr ?? "", |
| 32 | + stdout: result.stdout ?? "", |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +describe("CLI exit code integration", () => { |
| 37 | + let testDir: string; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + testDir = join(tmpdir(), `gh-attach-exitcode-${Date.now()}`); |
| 41 | + mkdirSync(testDir, { recursive: true }); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + try { |
| 46 | + rmSync(testDir, { recursive: true, force: true }); |
| 47 | + } catch { |
| 48 | + // ignore |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + it("exits 0 on --help", () => { |
| 53 | + const { status } = runCli(["--help"]); |
| 54 | + expect(status).toBe(0); |
| 55 | + }); |
| 56 | + |
| 57 | + it("exits 0 on --version", () => { |
| 58 | + const { status, stdout } = runCli(["--version"]); |
| 59 | + expect(status).toBe(0); |
| 60 | + expect(stdout.trim()).toMatch(/^\d+\.\d+\.\d+/); |
| 61 | + }); |
| 62 | + |
| 63 | + it("exits 0 on upload --help", () => { |
| 64 | + const { status } = runCli(["upload", "--help"]); |
| 65 | + expect(status).toBe(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it("exits 3 (validation) when no files and no --stdin", () => { |
| 69 | + const { status, stderr } = runCli( |
| 70 | + ["upload", "--target", "owner/repo#42"], |
| 71 | + { GITHUB_TOKEN: "test-token" }, |
| 72 | + ); |
| 73 | + expect(status).toBe(3); |
| 74 | + expect(stderr).toContain("At least one file is required"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("exits 3 (validation) for unsupported file format", () => { |
| 78 | + const txtFile = join(testDir, "test.txt"); |
| 79 | + writeFileSync(txtFile, "not an image"); |
| 80 | + |
| 81 | + const { status, stderr } = runCli( |
| 82 | + ["upload", txtFile, "--target", "owner/repo#42"], |
| 83 | + { GITHUB_TOKEN: "test-token" }, |
| 84 | + ); |
| 85 | + expect(status).toBe(3); |
| 86 | + expect(stderr).toContain("Unsupported file format"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("exits 3 (validation) for non-existent file", () => { |
| 90 | + const { status, stderr } = runCli( |
| 91 | + ["upload", "/tmp/does-not-exist-abc.png", "--target", "owner/repo#42"], |
| 92 | + { GITHUB_TOKEN: "test-token" }, |
| 93 | + ); |
| 94 | + expect(status).toBe(3); |
| 95 | + expect(stderr).toContain("File not found"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("exits 3 (validation) when --stdin used without --filename", () => { |
| 99 | + const { status, stderr } = runCli( |
| 100 | + ["upload", "--stdin", "--target", "owner/repo#42"], |
| 101 | + { GITHUB_TOKEN: "test-token" }, |
| 102 | + ); |
| 103 | + expect(status).toBe(3); |
| 104 | + expect(stderr).toContain("--filename is required"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("exits 3 (validation) for invalid target", () => { |
| 108 | + const pngFile = join(testDir, "test.png"); |
| 109 | + writeFileSync( |
| 110 | + pngFile, |
| 111 | + Buffer.from([ |
| 112 | + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, |
| 113 | + 0x0d, 0x49, 0x48, 0x44, 0x52, |
| 114 | + ]), |
| 115 | + ); |
| 116 | + |
| 117 | + const { status, stderr } = runCli( |
| 118 | + ["upload", pngFile, "--target", "invalid"], |
| 119 | + { GITHUB_TOKEN: "test-token" }, |
| 120 | + ); |
| 121 | + expect(status).toBe(3); |
| 122 | + expect(stderr).toContain("Invalid target"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("exits 1 (general) when no strategy is available without auth", () => { |
| 126 | + const pngFile = join(testDir, "test.png"); |
| 127 | + writeFileSync( |
| 128 | + pngFile, |
| 129 | + Buffer.from([ |
| 130 | + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, |
| 131 | + 0x0d, 0x49, 0x48, 0x44, 0x52, |
| 132 | + ]), |
| 133 | + ); |
| 134 | + |
| 135 | + const { status, stderr } = runCli( |
| 136 | + ["upload", pngFile, "--target", "owner/repo#42"], |
| 137 | + { |
| 138 | + GH_ATTACH_STATE_PATH: join(testDir, "no-session.json"), |
| 139 | + }, |
| 140 | + ); |
| 141 | + expect(status).toBe(1); |
| 142 | + expect(stderr).toContain("No upload strategy available"); |
| 143 | + }); |
| 144 | +}); |
0 commit comments