-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.test.ts
More file actions
106 lines (95 loc) · 3.51 KB
/
Copy pathcli.test.ts
File metadata and controls
106 lines (95 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { describe, expect, test } from "bun:test";
import { join } from "node:path";
import { parseBootstrapArgs, validateIndexModeArgs } from "./cli";
import { CODEMAP_VERSION } from "./version";
describe("parseBootstrapArgs", () => {
test("passes --help through in rest after --root", () => {
const { root, rest } = parseBootstrapArgs(["--root", "/tmp/foo", "--help"]);
expect(root).toBe("/tmp/foo");
expect(rest).toEqual(["--help"]);
});
});
async function runCli(
args: string[],
): Promise<{ exitCode: number; out: string; err: string }> {
const indexTs = join(import.meta.dir, "index.ts");
const proc = Bun.spawn([Bun.which("bun")!, indexTs, ...args], {
cwd: join(import.meta.dir, ".."),
stdout: "pipe",
stderr: "pipe",
});
const exitCode = await proc.exited;
const out = await new Response(proc.stdout).text();
const err = await new Response(proc.stderr).text();
return { exitCode, out, err };
}
describe("CLI --help", () => {
test("exits 0 and prints usage without touching the database", async () => {
const { exitCode, out, err } = await runCli(["--help"]);
expect(exitCode).toBe(0);
expect(out).toContain("Usage:");
expect(out).toContain("codemap query");
expect(err).toBe("");
});
test("query --help exits 0 and documents --json + --summary + --group-by", async () => {
const { exitCode, out, err } = await runCli(["query", "--help"]);
expect(exitCode).toBe(0);
expect(out).toContain("--json");
expect(out).toContain("--summary");
expect(out).toContain("--changed-since");
expect(out).toContain("--group-by");
expect(out).toContain("--recipe");
expect(out).toContain("fan-out");
expect(out).toContain("codemap query");
expect(out).toContain("LIMIT");
expect(err).toBe("");
});
test("query with no SQL exits 1", async () => {
const { exitCode, err } = await runCli(["query"]);
expect(exitCode).toBe(1);
expect(err).toContain("missing SQL");
});
});
describe("CLI version", () => {
test.each(["version", "--version", "-V"])(
"%s prints version and exits 0",
async (flag) => {
const { exitCode, out, err } = await runCli([flag]);
expect(exitCode).toBe(0);
expect(out.trim()).toBe(CODEMAP_VERSION);
expect(err).toBe("");
},
);
});
describe("validateIndexModeArgs", () => {
test("allows empty, --full, --files paths, and combinations", () => {
expect(() => validateIndexModeArgs([])).not.toThrow();
expect(() => validateIndexModeArgs(["--full"])).not.toThrow();
expect(() =>
validateIndexModeArgs(["--files", "a.ts", "b.tsx"]),
).not.toThrow();
expect(() =>
validateIndexModeArgs(["--full", "--files", "src/x.ts"]),
).not.toThrow();
});
});
describe("CLI unknown / invalid args", () => {
test("typo --versiond exits 1 before DB (stderr)", async () => {
const { exitCode, out, err } = await runCli(["--versiond"]);
expect(exitCode).toBe(1);
expect(out).toBe("");
expect(err).toContain("unknown option");
expect(err).toContain("--versiond");
});
test("bare subcommand typo exits 1", async () => {
const { exitCode, err } = await runCli(["notacommand"]);
expect(exitCode).toBe(1);
expect(err).toContain("unexpected argument");
});
test("agents init rejects positional argument (use --interactive)", async () => {
const { exitCode, err } = await runCli(["agents", "init", "interactive"]);
expect(exitCode).toBe(1);
expect(err).toContain("unexpected argument");
expect(err).toContain("interactive");
});
});