|
| 1 | +import { |
| 2 | + afterAll, |
| 3 | + afterEach, |
| 4 | + beforeAll, |
| 5 | + beforeEach, |
| 6 | + expect, |
| 7 | + test, |
| 8 | +} from "bun:test"; |
| 9 | +import { mkdir, mkdtemp, rm, stat, writeFile } from "node:fs/promises"; |
| 10 | +import { tmpdir } from "node:os"; |
| 11 | +import { join, resolve } from "node:path"; |
| 12 | + |
| 13 | +import { ensureHackDirGitignore } from "../src/lib/project-env-config.ts"; |
| 14 | +import { registerScopedModuleMock } from "./helpers/scoped-module-mock.ts"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Regression coverage for the field report: `hack doctor` (and `--fix`) must |
| 18 | + * not create the tickets extension's local git cache (`.hack/tickets/`) |
| 19 | + * unless the extension is actually enabled for the project — it previously |
| 20 | + * checked the unrelated `tickets.git.enabled` flag (default `true`) instead |
| 21 | + * of `controlPlane.extensions["dance.hack.tickets"].enabled` (default |
| 22 | + * `false`), so every `hack doctor` run silently created the directory. |
| 23 | + * |
| 24 | + * Also covers the companion fix: once the committed `.hack/.gitignore` is |
| 25 | + * generated, `tickets/` is covered so an enabled project's cache never shows |
| 26 | + * up as untracked. |
| 27 | + * |
| 28 | + * Docker/shell/OS are mocked (as in tests/doctor-fix-noninteractive.test.ts) |
| 29 | + * so `--fix` never touches this machine's real Docker/global infra state. |
| 30 | + */ |
| 31 | + |
| 32 | +async function dirExists(path: string): Promise<boolean> { |
| 33 | + try { |
| 34 | + const info = await stat(path); |
| 35 | + return info.isDirectory(); |
| 36 | + } catch { |
| 37 | + return false; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +const clackMock = await registerScopedModuleMock({ |
| 42 | + importerPath: import.meta.path, |
| 43 | + specifier: "@clack/prompts", |
| 44 | + overrides: { |
| 45 | + confirm: async () => { |
| 46 | + throw new Error( |
| 47 | + "confirm() must not be called under HACK_NO_INTERACTIVE=1" |
| 48 | + ); |
| 49 | + }, |
| 50 | + isCancel: () => false, |
| 51 | + note: () => {}, |
| 52 | + spinner: () => ({ |
| 53 | + start: () => {}, |
| 54 | + stop: () => {}, |
| 55 | + }), |
| 56 | + }, |
| 57 | +}); |
| 58 | + |
| 59 | +const shellMock = await registerScopedModuleMock({ |
| 60 | + importerPath: import.meta.path, |
| 61 | + specifier: "../src/lib/shell.ts", |
| 62 | + overrides: { |
| 63 | + exec: async (cmd: readonly string[]) => { |
| 64 | + if (cmd[0] === "docker" && cmd[1] === "info") { |
| 65 | + return { exitCode: 0, stdout: "", stderr: "" }; |
| 66 | + } |
| 67 | + if (cmd[0] === "docker" && cmd[1] === "network" && cmd[2] === "inspect") { |
| 68 | + return { exitCode: 0, stdout: "[]", stderr: "" }; |
| 69 | + } |
| 70 | + return { exitCode: 1, stdout: "", stderr: "" }; |
| 71 | + }, |
| 72 | + execOrThrow: async () => ({ exitCode: 0, stdout: "", stderr: "" }), |
| 73 | + run: async () => 0, |
| 74 | + findExecutableInPath: (name?: string) => { |
| 75 | + if (name === "hack" || name === "bun" || name === "docker") { |
| 76 | + return `/usr/local/bin/${name}`; |
| 77 | + } |
| 78 | + return null; |
| 79 | + }, |
| 80 | + CommandError: class CommandError extends Error {}, |
| 81 | + }, |
| 82 | +}); |
| 83 | + |
| 84 | +const osMock = await registerScopedModuleMock({ |
| 85 | + importerPath: import.meta.path, |
| 86 | + specifier: "../src/lib/os.ts", |
| 87 | + overrides: { |
| 88 | + isMac: () => true, |
| 89 | + isLinux: () => false, |
| 90 | + openUrl: async () => 0, |
| 91 | + }, |
| 92 | +}); |
| 93 | + |
| 94 | +let tempHome: string | null = null; |
| 95 | +let originalHome: string | undefined; |
| 96 | +let originalMutagenPath: string | undefined; |
| 97 | + |
| 98 | +beforeAll(() => { |
| 99 | + clackMock.activate(); |
| 100 | + shellMock.activate(); |
| 101 | + osMock.activate(); |
| 102 | +}); |
| 103 | + |
| 104 | +beforeEach(async () => { |
| 105 | + originalHome = process.env.HOME; |
| 106 | + originalMutagenPath = process.env.HACK_MUTAGEN_PATH; |
| 107 | + tempHome = await mkdtemp(join(tmpdir(), "hack-doctor-tickets-home-")); |
| 108 | + process.env.HOME = tempHome; |
| 109 | + // Short-circuit mutagen path resolution so --fix never attempts a real |
| 110 | + // network install. |
| 111 | + process.env.HACK_MUTAGEN_PATH = "/usr/local/bin/mutagen"; |
| 112 | +}); |
| 113 | + |
| 114 | +afterEach(async () => { |
| 115 | + if (tempHome) { |
| 116 | + await rm(tempHome, { recursive: true, force: true }); |
| 117 | + tempHome = null; |
| 118 | + } |
| 119 | + process.env.HOME = originalHome; |
| 120 | + if (originalMutagenPath === undefined) { |
| 121 | + Reflect.deleteProperty(process.env, "HACK_MUTAGEN_PATH"); |
| 122 | + } else { |
| 123 | + process.env.HACK_MUTAGEN_PATH = originalMutagenPath; |
| 124 | + } |
| 125 | +}); |
| 126 | + |
| 127 | +afterAll(() => { |
| 128 | + clackMock.deactivate(); |
| 129 | + shellMock.deactivate(); |
| 130 | + osMock.deactivate(); |
| 131 | +}); |
| 132 | + |
| 133 | +const tempDirs = new Set<string>(); |
| 134 | + |
| 135 | +afterEach(async () => { |
| 136 | + for (const dir of tempDirs) { |
| 137 | + await rm(dir, { recursive: true, force: true }); |
| 138 | + } |
| 139 | + tempDirs.clear(); |
| 140 | +}); |
| 141 | + |
| 142 | +async function createTempDir(): Promise<string> { |
| 143 | + const dir = await mkdtemp(join(tmpdir(), "hack-doctor-tickets-repo-")); |
| 144 | + tempDirs.add(dir); |
| 145 | + return dir; |
| 146 | +} |
| 147 | + |
| 148 | +async function runGit(args: readonly string[], cwd: string): Promise<string> { |
| 149 | + const proc = Bun.spawn({ |
| 150 | + cmd: ["git", ...args], |
| 151 | + cwd, |
| 152 | + stdout: "pipe", |
| 153 | + stderr: "pipe", |
| 154 | + }); |
| 155 | + const [stdout, stderr, exitCode] = await Promise.all([ |
| 156 | + new Response(proc.stdout).text(), |
| 157 | + new Response(proc.stderr).text(), |
| 158 | + proc.exited, |
| 159 | + ]); |
| 160 | + if (exitCode !== 0) { |
| 161 | + throw new Error(stderr || stdout || `git ${args.join(" ")} failed`); |
| 162 | + } |
| 163 | + return stdout.trim(); |
| 164 | +} |
| 165 | + |
| 166 | +async function gitCheckIgnore(opts: { |
| 167 | + readonly repoRoot: string; |
| 168 | + readonly path: string; |
| 169 | +}): Promise<boolean> { |
| 170 | + const proc = Bun.spawn({ |
| 171 | + cmd: ["git", "check-ignore", "-q", "--", opts.path], |
| 172 | + cwd: opts.repoRoot, |
| 173 | + stdout: "ignore", |
| 174 | + stderr: "ignore", |
| 175 | + }); |
| 176 | + return (await proc.exited) === 0; |
| 177 | +} |
| 178 | + |
| 179 | +async function createFixtureRepo(opts: { |
| 180 | + readonly ticketsEnabled: boolean; |
| 181 | +}): Promise<string> { |
| 182 | + const dir = await createTempDir(); |
| 183 | + const repoRoot = resolve(dir, "repo"); |
| 184 | + await mkdir(resolve(repoRoot, ".hack"), { recursive: true }); |
| 185 | + await runGit(["init", "-b", "main"], repoRoot); |
| 186 | + await runGit(["config", "user.name", "Hack Test"], repoRoot); |
| 187 | + await runGit(["config", "user.email", "hack@example.com"], repoRoot); |
| 188 | + |
| 189 | + await writeFile( |
| 190 | + resolve(repoRoot, ".hack", "docker-compose.yml"), |
| 191 | + "services:\n api:\n image: alpine:3.19\n" |
| 192 | + ); |
| 193 | + await writeFile( |
| 194 | + resolve(repoRoot, ".hack", "hack.config.json"), |
| 195 | + `${JSON.stringify( |
| 196 | + { |
| 197 | + name: "fixture", |
| 198 | + controlPlane: { |
| 199 | + extensions: { |
| 200 | + "dance.hack.tickets": { enabled: opts.ticketsEnabled }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + }, |
| 204 | + null, |
| 205 | + 2 |
| 206 | + )}\n` |
| 207 | + ); |
| 208 | + await ensureHackDirGitignore({ projectDir: resolve(repoRoot, ".hack") }); |
| 209 | + await runGit(["add", "."], repoRoot); |
| 210 | + await runGit(["commit", "-m", "init"], repoRoot); |
| 211 | + return repoRoot; |
| 212 | +} |
| 213 | + |
| 214 | +async function runDoctor(opts: { |
| 215 | + readonly repoRoot: string; |
| 216 | + readonly extraArgs?: readonly string[]; |
| 217 | +}): Promise<number> { |
| 218 | + const { runCli } = await import("../src/cli/run.ts"); |
| 219 | + return await runCli([ |
| 220 | + "doctor", |
| 221 | + "--path", |
| 222 | + opts.repoRoot, |
| 223 | + "--no-interactive", |
| 224 | + ...(opts.extraArgs ?? []), |
| 225 | + ]); |
| 226 | +} |
| 227 | + |
| 228 | +test("hack doctor does not create .hack/tickets/ when the extension is disabled", async () => { |
| 229 | + const repoRoot = await createFixtureRepo({ ticketsEnabled: false }); |
| 230 | + |
| 231 | + await runDoctor({ repoRoot }); |
| 232 | + |
| 233 | + expect(await dirExists(resolve(repoRoot, ".hack", "tickets"))).toBe(false); |
| 234 | +}); |
| 235 | + |
| 236 | +test("hack doctor --fix does not create .hack/tickets/ when the extension is disabled", async () => { |
| 237 | + const repoRoot = await createFixtureRepo({ ticketsEnabled: false }); |
| 238 | + |
| 239 | + await runDoctor({ repoRoot, extraArgs: ["--fix"] }); |
| 240 | + |
| 241 | + expect(await dirExists(resolve(repoRoot, ".hack", "tickets"))).toBe(false); |
| 242 | +}); |
| 243 | + |
| 244 | +test("hack doctor creates .hack/tickets/ when the extension is enabled, and it is gitignored", async () => { |
| 245 | + const repoRoot = await createFixtureRepo({ ticketsEnabled: true }); |
| 246 | + |
| 247 | + await runDoctor({ repoRoot }); |
| 248 | + |
| 249 | + expect(await dirExists(resolve(repoRoot, ".hack", "tickets"))).toBe(true); |
| 250 | + expect( |
| 251 | + await gitCheckIgnore({ repoRoot, path: ".hack/tickets/git/bare.git" }) |
| 252 | + ).toBe(true); |
| 253 | + |
| 254 | + const status = await runGit(["status", "--porcelain"], repoRoot); |
| 255 | + expect(status).toBe(""); |
| 256 | +}); |
0 commit comments