|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { existsSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { dirname, resolve } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { beforeAll, describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | +const cliEntry = resolve(__dirname, "../dist/cli.js"); |
| 10 | + |
| 11 | +const CV = `# Jane Doe |
| 12 | +
|
| 13 | +## Experience |
| 14 | +
|
| 15 | +### Senior Engineer — Acme (2021 – 2024) |
| 16 | +
|
| 17 | +- Shipped a payments service in Python on PostgreSQL handling 2M requests/day |
| 18 | +- Reduced p99 latency from 800ms to 120ms by rewriting the hot path in Rust |
| 19 | +- Led migration from Redis to Kafka, cutting infrastructure cost by 35% |
| 20 | +`; |
| 21 | + |
| 22 | +let cvPath: string; |
| 23 | +let jdPath: string; |
| 24 | + |
| 25 | +beforeAll(() => { |
| 26 | + if (!existsSync(cliEntry)) { |
| 27 | + throw new Error( |
| 28 | + `CLI not built at ${cliEntry}. Run \`pnpm --filter @cv-builder/cli build\` first.` |
| 29 | + ); |
| 30 | + } |
| 31 | + const dir = mkdtempSync(resolve(tmpdir(), "cv-builder-cli-test-")); |
| 32 | + cvPath = resolve(dir, "cv.md"); |
| 33 | + jdPath = resolve(dir, "jd.md"); |
| 34 | + writeFileSync(cvPath, CV, "utf-8"); |
| 35 | + writeFileSync( |
| 36 | + jdPath, |
| 37 | + "Looking for a Python engineer with PostgreSQL and Kafka experience.", |
| 38 | + "utf-8" |
| 39 | + ); |
| 40 | +}); |
| 41 | + |
| 42 | +function runCli(args: string[]) { |
| 43 | + return spawnSync("node", [cliEntry, ...args], { encoding: "utf-8" }); |
| 44 | +} |
| 45 | + |
| 46 | +describe("cv-builder evaluate --format json", () => { |
| 47 | + it("emits a valid EvaluationResult JSON object on stdout", () => { |
| 48 | + const result = runCli(["evaluate", cvPath, "--format", "json"]); |
| 49 | + |
| 50 | + expect(result.status).toBe(0); |
| 51 | + const parsed = JSON.parse(result.stdout); |
| 52 | + expect(typeof parsed.score).toBe("number"); |
| 53 | + expect(Array.isArray(parsed.dimensions)).toBe(true); |
| 54 | + expect(parsed.dimensions.length).toBeGreaterThan(0); |
| 55 | + expect(Array.isArray(parsed.strengths)).toBe(true); |
| 56 | + expect(Array.isArray(parsed.issues)).toBe(true); |
| 57 | + expect(Array.isArray(parsed.rewrites)).toBe(true); |
| 58 | + expect(parsed.archetype).toBeDefined(); |
| 59 | + expect(typeof parsed.atsCompatible).toBe("boolean"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("works alongside --jd", () => { |
| 63 | + const result = runCli(["evaluate", cvPath, "--jd", jdPath, "--format", "json"]); |
| 64 | + |
| 65 | + expect(result.status).toBe(0); |
| 66 | + const parsed = JSON.parse(result.stdout); |
| 67 | + expect(typeof parsed.score).toBe("number"); |
| 68 | + expect(Array.isArray(parsed.dimensions)).toBe(true); |
| 69 | + expect(parsed.dimensions.length).toBeGreaterThan(0); |
| 70 | + expect(Array.isArray(parsed.strengths)).toBe(true); |
| 71 | + expect(Array.isArray(parsed.issues)).toBe(true); |
| 72 | + expect(Array.isArray(parsed.rewrites)).toBe(true); |
| 73 | + expect(parsed.archetype).toBeDefined(); |
| 74 | + expect(typeof parsed.atsCompatible).toBe("boolean"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("preserves human-readable output by default", () => { |
| 78 | + const result = runCli(["evaluate", cvPath]); |
| 79 | + |
| 80 | + expect(result.status).toBe(0); |
| 81 | + expect(result.stdout).toContain("CV Score:"); |
| 82 | + expect(() => JSON.parse(result.stdout)).toThrow(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("rejects unknown --format values", () => { |
| 86 | + const result = runCli(["evaluate", cvPath, "--format", "yaml"]); |
| 87 | + |
| 88 | + expect(result.status).not.toBe(0); |
| 89 | + expect(result.stderr).toMatch(/format/i); |
| 90 | + }); |
| 91 | + |
| 92 | + it("rejects missing option values", () => { |
| 93 | + const missingFormat = runCli(["evaluate", cvPath, "--format"]); |
| 94 | + const missingJd = runCli(["evaluate", cvPath, "--jd", "--format", "json"]); |
| 95 | + |
| 96 | + expect(missingFormat.status).not.toBe(0); |
| 97 | + expect(missingFormat.stderr).toContain("Missing value for --format"); |
| 98 | + expect(missingJd.status).not.toBe(0); |
| 99 | + expect(missingJd.stderr).toContain("Missing value for --jd"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("rejects an option token in place of the CV file", () => { |
| 103 | + const result = runCli(["evaluate", "--format", "json"]); |
| 104 | + |
| 105 | + expect(result.status).not.toBe(0); |
| 106 | + expect(result.stderr).toContain("Usage: cv-builder evaluate <cv-file>"); |
| 107 | + }); |
| 108 | +}); |
0 commit comments