Skip to content

Commit 9a123bc

Browse files
AddonoCopilot
andcommitted
test: add CLI help output snapshot tests
Add snapshot tests for CLI help output per Testing spec requirement: - Main help output snapshot - Upload, login, config, mcp command help snapshots - Version output format validation Also fixes: - Remove duplicate shebang from CLI source (tsup adds it via banner) - Fix package.json path resolution for bundled CLI Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 153c062 commit 9a123bc

3 files changed

Lines changed: 128 additions & 5 deletions

File tree

src/cli/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
#!/usr/bin/env node
2-
31
/**
42
* gh-attach CLI entry point.
53
*/
64

75
import { readFileSync } from "fs";
6+
import { dirname, resolve } from "path";
7+
import { fileURLToPath } from "url";
88
import { Command } from "commander";
99

10-
const pkg = JSON.parse(
11-
readFileSync(new URL("../../package.json", import.meta.url), "utf8"),
12-
);
10+
// Get package.json from the project root (works in both src and dist)
11+
const __dirname = dirname(fileURLToPath(import.meta.url));
12+
const pkgPath = resolve(__dirname, "..", "package.json");
13+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8")) as { version: string };
1314

1415
const program = new Command();
1516

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`CLI Help Output Snapshots > should match config command help output 1`] = `
4+
"Usage: gh-attach config [options] <action> [key] [value]
5+
6+
Manage gh-attach configuration
7+
8+
Arguments:
9+
action Action: list, set, get
10+
key Configuration key
11+
value Configuration value
12+
13+
Options:
14+
-h, --help display help for command"
15+
`;
16+
17+
exports[`CLI Help Output Snapshots > should match login command help output 1`] = `
18+
"Usage: gh-attach login [options]
19+
20+
Authenticate with GitHub via browser
21+
22+
Options:
23+
--state-path <path> Path to save session state
24+
--status Check current authentication status
25+
-h, --help display help for command"
26+
`;
27+
28+
exports[`CLI Help Output Snapshots > should match main help output 1`] = `
29+
"Usage: gh-attach [options] [command]
30+
31+
Upload images to GitHub issues, PRs, and comments
32+
33+
Options:
34+
-V, --version output the version number
35+
-h, --help display help for command
36+
37+
Commands:
38+
upload [options] <files...> Upload an image and get a markdown embed URL
39+
login [options] Authenticate with GitHub via browser
40+
config <action> [key] [value] Manage gh-attach configuration
41+
mcp [options] Start the MCP server
42+
help [command] display help for command"
43+
`;
44+
45+
exports[`CLI Help Output Snapshots > should match mcp command help output 1`] = `
46+
"Usage: gh-attach mcp [options]
47+
48+
Start the MCP server
49+
50+
Options:
51+
--transport <type> Transport: stdio, http (default: "stdio")
52+
--port <number> Port for HTTP transport (default: "3000")
53+
-h, --help display help for command"
54+
`;
55+
56+
exports[`CLI Help Output Snapshots > should match upload command help output 1`] = `
57+
"Usage: gh-attach upload [options] <files...>
58+
59+
Upload an image and get a markdown embed URL
60+
61+
Arguments:
62+
files Image file(s) to upload
63+
64+
Options:
65+
--target <ref> GitHub issue/PR reference (owner/repo#N, #N, or URL)
66+
--strategy <name> Upload strategy to use
67+
--format <type> Output format: markdown, url, json (default: "markdown")
68+
--stdin Read image from stdin
69+
--filename <name> Filename when using --stdin
70+
-h, --help display help for command"
71+
`;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { describe, it, expect } from "vitest";
2+
import { execSync } from "child_process";
3+
import { resolve } from "path";
4+
5+
const CLI_PATH = resolve(import.meta.dirname, "../../../dist/cli.js");
6+
7+
/**
8+
* Executes the CLI with given arguments and returns stdout.
9+
*/
10+
function runCli(args: string): string {
11+
const result = execSync(`node ${CLI_PATH} ${args}`, {
12+
encoding: "utf8",
13+
cwd: resolve(import.meta.dirname, "../../.."),
14+
});
15+
return result.trim();
16+
}
17+
18+
describe("CLI Help Output Snapshots", () => {
19+
it("should match main help output", () => {
20+
const output = runCli("--help");
21+
expect(output).toMatchSnapshot();
22+
});
23+
24+
it("should match upload command help output", () => {
25+
const output = runCli("upload --help");
26+
expect(output).toMatchSnapshot();
27+
});
28+
29+
it("should match login command help output", () => {
30+
const output = runCli("login --help");
31+
expect(output).toMatchSnapshot();
32+
});
33+
34+
it("should match config command help output", () => {
35+
const output = runCli("config --help");
36+
expect(output).toMatchSnapshot();
37+
});
38+
39+
it("should match mcp command help output", () => {
40+
const output = runCli("mcp --help");
41+
expect(output).toMatchSnapshot();
42+
});
43+
});
44+
45+
describe("CLI Version Output", () => {
46+
it("should output version in expected format", () => {
47+
const output = runCli("--version");
48+
// Version output should be a semver-ish pattern
49+
expect(output).toMatch(/^\d+\.\d+\.\d+(-\w+)?$/);
50+
});
51+
});

0 commit comments

Comments
 (0)