project-file.test.ts
This file
Path
apps/cli/test/local/project-file.test.ts
218 lines · 6.6 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–218why, one step back1import { NodeServices } from "@effect/platform-node";2import { describe, expect, it } from "@effect/vitest";3import { Brand } from "@repo/brand";4import { Effect, FileSystem, Path, Stream } from "effect";5import { ChildProcess, ChildProcessSpawner } from "effect/process";67import { projectFile } from "../../src/local/project-file.ts";89const found = {10 run: {11 checks: Object.fromEntries(12 new Map([13 ["unit", "bun run test"],14 ["lint", "bun run lint"],15 ["types", "bun run typecheck"],16 ])17 ),18 setup: "bun install --frozen-lockfile",19 test: "vitest run {path}",20 },21 sources: [22 "setup from .github/workflows/ci.yml",23 "test from package.json",24 "checks.unit from package.json",25 "checks.lint from package.json",26 "checks.types from package.json",27 ],28} as const;2930const noTest = {31 run: {32 checks: { lint: "npm run lint" },33 setup: "npm install",34 },35 sources: [36 "setup inferred from package.json",37 "checks.lint from package.json",38 "skipped .github/workflows/broken.yml: not YAML",39 ],40} as const;4142describe("the proposed project file", () => {43 it("writes each command the finder found, with where it was found", () => {44 expect(projectFile(found.run, found.sources)).toBe(45 [46 `import { project } from "${Brand.cliPackage}/rules";`,47 "",48 `// The run block \`${Brand.cli} init\` found in this repo's own files, each`,49 "// command with where it was found. Every other setting takes its default.",50 "export default project({",51 " run: {",52 " // setup from .github/workflows/ci.yml",53 ` setup: "bun install --frozen-lockfile",`,54 " // test from package.json",55 ` test: "vitest run {path}",`,56 " checks: {",57 " // checks.unit from package.json",58 ` unit: "bun run test",`,59 " // checks.lint from package.json",60 ` lint: "bun run lint",`,61 " // checks.types from package.json",62 ` types: "bun run typecheck",`,63 " },",64 " },",65 "});",66 "",67 ].join("\n")68 );69 });7071 it("is the same text for the same commands, in whatever order the keys come", () => {72 // `checks` moves from first to last.73 const { checks, ...rest } = found.run;74 const reordered = { ...rest, checks };7576 expect(projectFile(reordered, found.sources)).toBe(77 projectFile(found.run, found.sources)78 );79 });8081 it("falls back to the run block found from CI when no command runs one test file, listing what it found", () => {82 expect(projectFile(noTest.run, noTest.sources)).toBe(83 [84 `import { project, run } from "${Brand.cliPackage}/rules";`,85 "",86 `// \`${Brand.cli} init\` found no command that runs one test file ({path}), so`,87 "// the platform reads the run block from CI at each landing. It found:",88 `// setup: "npm install" (setup inferred from package.json)`,89 `// checks.lint: "npm run lint" (checks.lint from package.json)`,90 "// skipped .github/workflows/broken.yml: not YAML",91 "export default project({",92 " run: run.fromCI,",93 "});",94 "",95 ].join("\n")96 );97 });9899 it("quotes a lane name that is no identifier, and a command's quotes", () => {100 const text = projectFile(101 {102 checks: { "test-e2e": `bun run e2e -- --grep "slow"` },103 test: "bun test {path}",104 },105 []106 );107108 expect(text).toContain(109 ` "test-e2e": "bun run e2e -- --grep \\"slow\\"",`110 );111 });112});113114const text = <E>(bytes: Stream.Stream<Uint8Array, E>) =>115 bytes.pipe(Stream.decodeText(), Stream.mkString);116117const typeCheck = (files: Readonly<Record<string, string>>) =>118 Effect.scoped(119 Effect.gen(function* () {120 const fs = yield* FileSystem.FileSystem;121 const path = yield* Path.Path;122 const dir = yield* fs.makeTempDirectoryScoped();123124 const sdk = yield* path.fromFileUrl(125 new URL(import.meta.resolve("@repo/sdk"))126 );127128 const tsc = path.join(129 path.dirname(130 yield* path.fromFileUrl(131 new URL(import.meta.resolve("typescript/package.json"))132 )133 ),134 "bin",135 "tsc"136 );137138 yield* fs.writeFileString(139 path.join(dir, "package.json"),140 JSON.stringify({ type: "module" })141 );142143 yield* fs.writeFileString(144 path.join(dir, "tsconfig.json"),145 JSON.stringify({146 compilerOptions: {147 allowImportingTsExtensions: true,148 exactOptionalPropertyTypes: true,149 lib: ["ES2023"],150 module: "NodeNext",151 moduleResolution: "NodeNext",152 noEmit: true,153 noUncheckedIndexedAccess: true,154 paths: { [`${Brand.cliPackage}/rules`]: [sdk] },155 skipLibCheck: true,156 strict: true,157 target: "ES2023",158 types: [],159 },160 files: Object.keys(files),161 })162 );163164 for (const [name, content] of Object.entries(files)) {165 yield* fs.writeFileString(path.join(dir, name), content);166 }167168 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;169170 const child = yield* spawner.spawn(171 ChildProcess.make(tsc, ["--noEmit", "-p", "tsconfig.json"], {172 cwd: dir,173 stdin: "ignore",174 })175 );176177 const [stdout, exitCode] = yield* Effect.all(178 [text(child.stdout), child.exitCode],179 { concurrency: "unbounded" }180 );181182 return { exitCode, stdout };183 })184 ).pipe(Effect.provide(NodeServices.layer));185186describe("the proposed project file, compiled", () => {187 it.live(188 "type-checks against the SDK's project signature, explicit or from CI; a test without {path} does not",189 () =>190 Effect.gen(function* () {191 const proposed = yield* typeCheck({192 "explicit.ts": projectFile(found.run, found.sources),193 "from-ci.ts": projectFile(noTest.run, noTest.sources),194 "quoted.ts": projectFile(195 {196 checks: { "test-e2e": `bun run e2e -- --grep "slow"` },197 test: "bun test {path}",198 },199 []200 ),201 });202203 expect(proposed).toStrictEqual({ exitCode: 0, stdout: "" });204205 // The compile is a real check: a run block whose test names no file fails it.206 const broken = yield* typeCheck({207 "broken.ts": projectFile(found.run, found.sources).replace(208 "vitest run {path}",209 "vitest run"210 ),211 });212213 expect(broken.exitCode).not.toBe(0);214 expect(broken.stdout).toContain("broken.ts");215 }),216 { timeout: 60_000 }217 );218});