|
| 1 | +import { execFileSync } from "child_process"; |
| 2 | +import { mkdtemp, mkdir, rm, writeFile } from "fs/promises"; |
| 3 | +import { tmpdir } from "os"; |
| 4 | +import { join } from "path"; |
| 5 | + |
| 6 | +import { afterEach, describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +import { runCli } from "./helpers.js"; |
| 9 | + |
| 10 | +async function createRepo( |
| 11 | + baseDir: string, |
| 12 | + files: Record<string, string> |
| 13 | +): Promise<string> { |
| 14 | + const repoDir = join(baseDir, "fixture-health-repo"); |
| 15 | + await mkdir(repoDir, { recursive: true }); |
| 16 | + |
| 17 | + for (const [relativePath, content] of Object.entries(files)) { |
| 18 | + const fullPath = join(repoDir, relativePath); |
| 19 | + await mkdir(join(fullPath, ".."), { recursive: true }); |
| 20 | + await writeFile(fullPath, content, "utf-8"); |
| 21 | + } |
| 22 | + |
| 23 | + execFileSync("git", ["init", "-b", "main"], { cwd: repoDir, stdio: "ignore" }); |
| 24 | + execFileSync("git", ["config", "user.email", "test@example.com"], { cwd: repoDir, stdio: "ignore" }); |
| 25 | + execFileSync("git", ["config", "user.name", "Test User"], { cwd: repoDir, stdio: "ignore" }); |
| 26 | + execFileSync("git", ["add", "-A"], { cwd: repoDir, stdio: "ignore" }); |
| 27 | + execFileSync("git", ["commit", "-m", "init", "--no-gpg-sign"], { cwd: repoDir, stdio: "ignore" }); |
| 28 | + |
| 29 | + return repoDir; |
| 30 | +} |
| 31 | + |
| 32 | +const HEALTHY_FILES: Record<string, string> = { |
| 33 | + "package.json": JSON.stringify({ name: "fixture-health-repo", version: "1.0.0" }, null, 2), |
| 34 | + "README.md": `# Fixture Health Repo\n\n${"Detailed onboarding documentation. ".repeat(60)}`, |
| 35 | + LICENSE: "MIT License\n", |
| 36 | + "CONTRIBUTING.md": "# Contributing\n\nRun the tests before opening a PR.\n", |
| 37 | + "CHANGELOG.md": "# Changelog\n", |
| 38 | + "CODE_OF_CONDUCT.md": "# Code of Conduct\n", |
| 39 | + "SECURITY.md": "# Security Policy\n", |
| 40 | + ".github/ISSUE_TEMPLATE/bug.yml": "name: Bug\n", |
| 41 | + ".github/PULL_REQUEST_TEMPLATE.md": "## Description\n", |
| 42 | + ".github/CODEOWNERS": "* @owner\n", |
| 43 | + ".github/workflows/ci.yml": "name: CI\non: [push]\njobs:\n t:\n runs-on: ubuntu-latest\n steps:\n - run: npm test\n", |
| 44 | + ".github/dependabot.yml": "version: 2\n", |
| 45 | + ".eslintrc.json": "{}\n", |
| 46 | + ".prettierrc": "{}\n", |
| 47 | + ".editorconfig": "root = true\n", |
| 48 | + ".gitignore": "node_modules\n", |
| 49 | + ".husky/pre-commit": "npm test\n", |
| 50 | + "src/index.ts": "export const x = 1;\n", |
| 51 | + "test/index.test.ts": "import '../src/index';\n", |
| 52 | +}; |
| 53 | + |
| 54 | +const BARE_FILES: Record<string, string> = { |
| 55 | + "src/index.ts": "export const x = 1;\n", |
| 56 | + "src/app.ts": "export const y = 2;\n", |
| 57 | +}; |
| 58 | + |
| 59 | +describe("health command", () => { |
| 60 | + const tempDirs: string[] = []; |
| 61 | + |
| 62 | + afterEach(async () => { |
| 63 | + await Promise.all(tempDirs.map((dir) => rm(dir, { recursive: true, force: true }))); |
| 64 | + tempDirs.length = 0; |
| 65 | + }); |
| 66 | + |
| 67 | + it("prints a human-readable health report for a local repo", async () => { |
| 68 | + const tempDir = await mkdtemp(join(tmpdir(), "bootcamp-health-e2e-")); |
| 69 | + tempDirs.push(tempDir); |
| 70 | + const repoPath = await createRepo(tempDir, HEALTHY_FILES); |
| 71 | + |
| 72 | + const result = await runCli(["health", repoPath]); |
| 73 | + expect(result.exitCode).toBe(0); |
| 74 | + expect(result.stdout).toContain("Repo Health"); |
| 75 | + expect(result.stdout).toContain("/100"); |
| 76 | + expect(result.stdout).toContain("Documentation"); |
| 77 | + expect(result.stdout).toContain("Automation"); |
| 78 | + }, 60_000); |
| 79 | + |
| 80 | + it("emits machine-readable JSON with --json", async () => { |
| 81 | + const tempDir = await mkdtemp(join(tmpdir(), "bootcamp-health-e2e-")); |
| 82 | + tempDirs.push(tempDir); |
| 83 | + const repoPath = await createRepo(tempDir, HEALTHY_FILES); |
| 84 | + |
| 85 | + const result = await runCli(["health", repoPath, "--json"]); |
| 86 | + expect(result.exitCode).toBe(0); |
| 87 | + |
| 88 | + const parsed = JSON.parse(result.stdout); |
| 89 | + expect(typeof parsed.score).toBe("number"); |
| 90 | + expect(parsed.score).toBeGreaterThanOrEqual(90); |
| 91 | + expect(parsed.grade).toBe("A"); |
| 92 | + expect(Array.isArray(parsed.checks)).toBe(true); |
| 93 | + expect(parsed.checks.length).toBeGreaterThan(0); |
| 94 | + }, 60_000); |
| 95 | + |
| 96 | + it("fails the --check gate for a bare repo and passes it with a low minimum", async () => { |
| 97 | + const tempDir = await mkdtemp(join(tmpdir(), "bootcamp-health-e2e-")); |
| 98 | + tempDirs.push(tempDir); |
| 99 | + const repoPath = await createRepo(tempDir, BARE_FILES); |
| 100 | + |
| 101 | + const failing = await runCli(["health", repoPath, "--check", "--min-score", "70"]); |
| 102 | + expect(failing.exitCode).toBe(1); |
| 103 | + expect(`${failing.stdout}\n${failing.stderr}`).toContain("below the required minimum"); |
| 104 | + |
| 105 | + const passing = await runCli(["health", repoPath, "--check", "--min-score", "0"]); |
| 106 | + expect(passing.exitCode).toBe(0); |
| 107 | + }, 60_000); |
| 108 | +}); |
0 commit comments