example-datekit.test.ts
This file
Path
packages/config/test/example-datekit.test.ts
330 lines · 10.4 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331// The example is a Bun project outside the workspaces, so each command runs// as a child process under `sh -c`, as the Checker runs a project's commands,// in a copy of its public tree (every tracked file but `layers/`).import { NodeServices } from "@effect/platform-node";import { describe, expect, it } from "@effect/vitest";import { Effect, FileSystem, Path, Stream } from "effect";import { ChildProcess, ChildProcessSpawner } from "effect/process";
import { Brand } from "../../brand/src/index.ts";import { repoRoot } from "./root.ts";
const example = "examples/datekit";
interface BunRun { readonly exitCode: number; readonly output: string;}
interface FileReport { readonly failed: readonly string[]; readonly file: string; readonly text: string;}
interface BunReport { readonly errors: number; readonly failed: number; readonly files: readonly FileReport[]; readonly passed: number; readonly ran: { readonly files: number; readonly tests: number };}
const ANSI = new RegExp(`${String.fromCodePoint(27)}\\[[0-9;]*m`, "gu");
// Bun opens a file's part with its path and a colon, when the file has// anything to report (a failure or an error); a passing file prints nothing.const FILE_HEADER = /^(?<file>\S+\.test\.[cm]?[jt]sx?):$/u;
// A failed test: `(fail) describe > test [0.24ms]`, or `✗ …` when bun// prints in color.const FAILED_TEST = /^(?:\(fail\)|✗) (?<name>.+?)(?: \[[\d.]+m?s\])?$/u;
// Where the files' parts end: the recap of failed tests some runs print// (`3 tests failed:`), or else the closing counts (` 34 pass`).const CLOSING = /^(?:\d+ tests? failed:|\d+ pass)$/u;
// The line bun prints under a failed `expect(…)` (the platform's classifier// reads the same line as an assertion).const ASSERTION = /^error: expect\(received\)/mu;
// What bun prints when a file never reached its tests.const LOAD_ERRORS = [ /Cannot find (?:module|package)/u, /\bSyntaxError\b/u, /Unhandled error between tests/u, /\bReferenceError\b/u,];
// An ask file names its check file as the `path` its kind takes// (`kinds.fix.ask({ path: "test/asks/<id>.test.ts", … })`), which the kind// makes the ask's `RedThenGreen.path`.const CHECK_PATH = /\bpath:\s*"(?<path>[^"]+)"/u;
// The run block as project.ts writes it (the rules test holds it to the// evaluated policy): the command that runs one check file, with `{path}`// in it, and the lanes every answer must pass, `checks: { unit: "…" }`.const TEST_COMMAND = /\btest:\s*"(?<command>[^"]*\{path\}[^"]*)"/u;
const LANES = /\bchecks:\s*\{(?<lanes>[^}]*)\}/u;
const LANE = /(?<lane>\w+):\s*"(?<command>[^"]+)"/gu;
const countOf = (output: string, word: string) => Number( new RegExp(`^\\s*(?<count>\\d+) ${word}$`, "mu").exec(output)?.groups ?.count ?? "0" );
const failedIn = (line: string) => { const name = FAILED_TEST.exec(line)?.groups?.name;
return name === undefined ? [] : [name];};
const readReport = (output: string): BunReport => { const lines = output.split("\n").map((line) => line.trim());
const starts = lines.flatMap((line, at) => FILE_HEADER.test(line) ? [at] : [] );
const closing = lines.findIndex((line) => CLOSING.test(line)); const end = closing === -1 ? lines.length : closing;
const files = starts.map((start, index) => { const part = lines.slice( start + 1, Math.min(starts[index + 1] ?? end, end) );
return { failed: part.flatMap(failedIn), file: FILE_HEADER.exec(lines[start] ?? "")?.groups?.file ?? "", text: part.join("\n"), }; });
const ran = /^Ran (?<tests>\d+) tests? across (?<files>\d+) files?\./mu.exec( output )?.groups;
return { errors: countOf(output, "errors?"), failed: countOf(output, "fail"), files, passed: countOf(output, "pass"), ran: { files: Number(ran?.files), tests: Number(ran?.tests) }, };};
const text = <E>(bytes: Stream.Stream<Uint8Array, E>) => bytes.pipe(Stream.decodeText(), Stream.mkString);
const publicTree = Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const root = yield* repoRoot; const dir = yield* fs.makeTempDirectoryScoped({ prefix: "datekit-main-" });
const tracked = yield* spawner.string( ChildProcess.make("git", ["ls-files", "-z", "--", example], { cwd: root }) );
const files = tracked .split("\0") .filter((file) => file.startsWith(`${example}/`)) .map((file) => file.slice(example.length + 1)) .filter((file) => !file.startsWith("layers/"));
for (const file of files) { yield* fs.makeDirectory(path.dirname(path.join(dir, file)), { recursive: true, }); yield* fs.copyFile(path.join(root, example, file), path.join(dir, file)); }
return dir;});
const inTree = (dir: string, command: string) => Effect.scoped( Effect.gen(function* () { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const child = yield* spawner.spawn( ChildProcess.make("sh", ["-c", command], { cwd: dir }) );
const [stdout, stderr, exitCode] = yield* Effect.all( [text(child.stdout), text(child.stderr), child.exitCode], { concurrency: "unbounded" } );
return { exitCode, output: `${stdout}\n${stderr}`.replaceAll(ANSI, ""), } satisfies BunRun; }) );
const runBlock = Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path;
const source = yield* fs.readFileString( path.join(yield* repoRoot, example, Brand.configDir, "project.ts") );
const lanes = Array.from( (LANES.exec(source)?.groups?.lanes ?? "").matchAll(LANE), ({ groups }) => ({ command: groups?.command ?? "", lane: groups?.lane ?? "", }) );
return { lanes, test: TEST_COMMAND.exec(source)?.groups?.command ?? "" };});
const askChecks = Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const asks = path.join(yield* repoRoot, example, Brand.configDir, "asks");
if (!(yield* fs.exists(asks))) { return []; }
const files = (yield* fs.readDirectory(asks)) .filter((file) => file.endsWith(".ask.ts")) .toSorted();
return yield* Effect.forEach(files, (file) => Effect.map(fs.readFileString(path.join(asks, file)), (source) => ({ check: CHECK_PATH.exec(source)?.groups?.path ?? "", file, })) );});
const howFailed = ({ failed, file, text: part }: FileReport) => ({ assertion: ASSERTION.test(part), file, loaded: !LOAD_ERRORS.some((pattern) => pattern.test(part)), tests: failed.length,});
const byFile = (a: { readonly file: string }, b: { readonly file: string }) => a.file.localeCompare(b.file);
describe("the datekit example", () => { it.live( "is red on main for exactly its three asks' checks, each by an assertion, and green elsewhere", () => Effect.gen(function* () { const run = yield* inTree(yield* publicTree, "bun test"); const report = readReport(run.output); const checks = yield* askChecks;
expect(checks.map(({ file }) => file)).toStrictEqual([ "add-months-clamp.ask.ts", "format-ordinal.ask.ts", "parse-iso-week.ask.ts", ]);
expect({ errors: report.errors, exitedNonZero: run.exitCode !== 0, failed: report.failed, failing: report.files .filter(({ failed }) => failed.length > 0) .map(howFailed) .toSorted(byFile), }).toStrictEqual({ errors: 0, exitedNonZero: true, failed: 3, failing: checks .map(({ check }) => ({ assertion: true, file: check, loaded: true, tests: 1, })) .toSorted(byFile), });
expect({ files: report.ran.files, passedOrFailed: report.passed + report.failed, }).toStrictEqual({ files: 7, passedOrFailed: report.ran.tests }); expect(report.passed).toBeGreaterThan(0); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), // One `bun test` over a small library: well under a second alone. The // limit catches a hang while turbo runs every package's tests at once. 30_000 );
it.live( "holds its run block on main: every lane green, and each ask's check red alone by an assertion", () => Effect.gen(function* () { const { lanes, test } = yield* runBlock; const checks = yield* askChecks; const tree = yield* publicTree;
// A lane red on main refuses every answer (`VerifierBlocked`), so the // suite every answer keeps green leaves the open asks' checks out and // runs the four the library keeps green. const laneRuns = yield* Effect.forEach(lanes, ({ command, lane }) => Effect.map(inTree(tree, command), (run) => { const report = readReport(run.output);
return { errors: report.errors, exitCode: run.exitCode, failed: report.failed, files: report.ran.files, lane, }; }) );
expect(laneRuns).toStrictEqual([ { errors: 0, exitCode: 0, failed: 0, files: 4, lane: "unit" }, ]);
// The platform opens an ask only when its check is red first // (`NotRedFirst` otherwise): the test command, with the check's path // for `{path}`, fails its one test by an assertion. const checkRuns = yield* Effect.forEach(checks, ({ check }) => Effect.map(inTree(tree, test.replaceAll("{path}", check)), (run) => { const report = readReport(run.output);
return { errors: report.errors, exitedNonZero: run.exitCode !== 0, failing: report.files .filter(({ failed }) => failed.length > 0) .map(howFailed), }; }) );
expect(checkRuns).toStrictEqual( [ "test/asks/add-months-clamp.test.ts", "test/asks/format-ordinal.test.ts", "test/asks/parse-iso-week.test.ts", ].map((file) => ({ errors: 0, exitedNonZero: true, failing: [{ assertion: true, file, loaded: true, tests: 1 }], })) ); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), 30_000 );});