|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { |
| 6 | + checkStudioWorkspaceBuild, |
| 7 | + SKIP_STALE_CHECK_ENV, |
| 8 | + STUDIO_WORKSPACE_BUILD_ORDER, |
| 9 | +} from "./workspaceBuildCheck.js"; |
| 10 | + |
| 11 | +let dirs: string[] = []; |
| 12 | + |
| 13 | +function tmpRepoRoot(): string { |
| 14 | + const dir = mkdtempSync(join(tmpdir(), "hf-workspace-build-check-")); |
| 15 | + dirs.push(dir); |
| 16 | + return dir; |
| 17 | +} |
| 18 | + |
| 19 | +/** A fully built package: dist/index.js exists, no exports gap, dist newer than src. */ |
| 20 | +function writeBuiltPackage( |
| 21 | + repoRoot: string, |
| 22 | + pkg: string, |
| 23 | + exportsMap: Record<string, unknown> = { |
| 24 | + ".": { node: "./dist/index.js", import: "./src/index.ts" }, |
| 25 | + }, |
| 26 | +): void { |
| 27 | + const pkgDir = join(repoRoot, "packages", pkg); |
| 28 | + mkdirSync(join(pkgDir, "src"), { recursive: true }); |
| 29 | + mkdirSync(join(pkgDir, "dist"), { recursive: true }); |
| 30 | + writeFileSync(join(pkgDir, "package.json"), JSON.stringify({ name: pkg, exports: exportsMap })); |
| 31 | + writeFileSync(join(pkgDir, "src", "index.ts"), "export {};"); |
| 32 | + writeFileSync(join(pkgDir, "dist", "index.js"), "export {};"); |
| 33 | + const srcFiles = [join(pkgDir, "src", "index.ts")]; |
| 34 | + if (pkg === "core") { |
| 35 | + mkdirSync(join(pkgDir, "src", "generated"), { recursive: true }); |
| 36 | + writeFileSync(join(pkgDir, "src", "generated", "runtime-inline.ts"), "export {};"); |
| 37 | + writeFileSync(join(pkgDir, "src", "generated", "audio-fx-runtime-inline.ts"), "export {};"); |
| 38 | + srcFiles.push( |
| 39 | + join(pkgDir, "src", "generated", "runtime-inline.ts"), |
| 40 | + join(pkgDir, "src", "generated", "audio-fx-runtime-inline.ts"), |
| 41 | + ); |
| 42 | + } |
| 43 | + // dist strictly newer than every src file so a fresh build never reads as |
| 44 | + // stale — explicit timestamps, not write order, since two writeFileSync |
| 45 | + // calls can land within the same JS Date.now() tick. |
| 46 | + const past = new Date(Date.now() - 10_000); |
| 47 | + const now = new Date(); |
| 48 | + for (const file of srcFiles) utimesSync(file, past, past); |
| 49 | + utimesSync(join(pkgDir, "dist", "index.js"), now, now); |
| 50 | +} |
| 51 | + |
| 52 | +function writeAllBuilt(repoRoot: string): void { |
| 53 | + for (const pkg of STUDIO_WORKSPACE_BUILD_ORDER) writeBuiltPackage(repoRoot, pkg); |
| 54 | +} |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + delete process.env[SKIP_STALE_CHECK_ENV]; |
| 58 | + for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); |
| 59 | + dirs = []; |
| 60 | +}); |
| 61 | + |
| 62 | +describe("checkStudioWorkspaceBuild", () => { |
| 63 | + it("reports every package as missing on a fresh clone with nothing built", () => { |
| 64 | + const repoRoot = tmpRepoRoot(); |
| 65 | + mkdirSync(join(repoRoot, "packages"), { recursive: true }); |
| 66 | + |
| 67 | + const problems = checkStudioWorkspaceBuild(repoRoot); |
| 68 | + |
| 69 | + expect(problems.map((p) => p.package)).toEqual([...STUDIO_WORKSPACE_BUILD_ORDER]); |
| 70 | + expect(problems.every((p) => p.kind === "missing")).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it("reports only the one package whose src changed after its last build", () => { |
| 74 | + const repoRoot = tmpRepoRoot(); |
| 75 | + writeAllBuilt(repoRoot); |
| 76 | + const parsersSrc = join(repoRoot, "packages", "parsers", "src", "index.ts"); |
| 77 | + utimesSync(parsersSrc, new Date(), new Date()); |
| 78 | + |
| 79 | + const problems = checkStudioWorkspaceBuild(repoRoot); |
| 80 | + |
| 81 | + expect(problems).toEqual([expect.objectContaining({ package: "parsers", kind: "stale" })]); |
| 82 | + }); |
| 83 | + |
| 84 | + it("skips the stale-src check under the escape hatch env var", () => { |
| 85 | + const repoRoot = tmpRepoRoot(); |
| 86 | + writeAllBuilt(repoRoot); |
| 87 | + const parsersSrc = join(repoRoot, "packages", "parsers", "src", "index.ts"); |
| 88 | + utimesSync(parsersSrc, new Date(), new Date()); |
| 89 | + process.env[SKIP_STALE_CHECK_ENV] = "1"; |
| 90 | + |
| 91 | + expect(checkStudioWorkspaceBuild(repoRoot)).toEqual([]); |
| 92 | + }); |
| 93 | + |
| 94 | + it("reports a new export subpath missing from an otherwise fresh dist", () => { |
| 95 | + const repoRoot = tmpRepoRoot(); |
| 96 | + writeAllBuilt(repoRoot); |
| 97 | + // Simulates a merge that added a subpath to package.json and its src file, |
| 98 | + // without anyone rebuilding this already-checked-out worktree's dist. |
| 99 | + writeBuiltPackage(repoRoot, "core", { |
| 100 | + ".": { node: "./dist/index.js", import: "./src/index.ts" }, |
| 101 | + "./new-thing": { node: "./dist/newThing.js", import: "./src/newThing.ts" }, |
| 102 | + }); |
| 103 | + |
| 104 | + const problems = checkStudioWorkspaceBuild(repoRoot); |
| 105 | + |
| 106 | + expect(problems).toEqual([expect.objectContaining({ package: "core", kind: "missing" })]); |
| 107 | + expect(problems[0]?.detail).toContain("newThing.js"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("reports nothing for a fully built, fresh workspace", () => { |
| 111 | + const repoRoot = tmpRepoRoot(); |
| 112 | + writeAllBuilt(repoRoot); |
| 113 | + |
| 114 | + expect(checkStudioWorkspaceBuild(repoRoot)).toEqual([]); |
| 115 | + }); |
| 116 | + |
| 117 | + it("reports a broken package.json instead of throwing", () => { |
| 118 | + const repoRoot = tmpRepoRoot(); |
| 119 | + writeAllBuilt(repoRoot); |
| 120 | + writeFileSync(join(repoRoot, "packages", "parsers", "package.json"), "{not json"); |
| 121 | + |
| 122 | + const problems = checkStudioWorkspaceBuild(repoRoot); |
| 123 | + |
| 124 | + expect(problems).toEqual([expect.objectContaining({ package: "parsers", kind: "missing" })]); |
| 125 | + expect(problems[0]?.detail).toContain("package.json is invalid"); |
| 126 | + }); |
| 127 | +}); |
0 commit comments