example-burrow.test.ts
This file
Path
packages/rules/test/example-burrow.test.ts
301 lines · 8.6 KiB · TypeScript
Last changed by
Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302// The config folder is found as the one at burrow's root holding a// `project.ts`: its name is the brand's, which `rules` never spells.import { describe, expect, it } from "@effect/vitest";import { CheckSpec, RatchetMeasure } from "@repo/protocol/domain/check";import { Command, SymbolName } from "@repo/protocol/domain/common";import { Policy } from "@repo/protocol/domain/policy";import { Effect, FileSystem, Path, Schema } from "effect";
import { EvaluationFacts, RulesEvaluator } from "../src/rules-evaluator.ts";import { at, bundleOf, bunEvaluator, configFolder, now } from "./world.ts";
interface File { readonly content: string; readonly path: string;}
const Manifest = Schema.Struct({ exports: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)), name: Schema.String, workspaces: Schema.optionalKey(Schema.Array(Schema.String)),});
const decodeManifest = Schema.decodeEffect(Schema.fromJsonString(Manifest));
const rootEntries = (root: string) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const folders: string[] = []; const files: File[] = [];
for (const name of (yield* fs.readDirectory(root)).toSorted()) { const full = path.join(root, name);
if ((yield* fs.stat(full)).type === "File") { files.push({ content: yield* fs.readFileString(full), path: name }); } else if ( name.startsWith(".") && (yield* fs.exists(path.join(full, "project.ts"))) ) { folders.push(name); } }
return { files, folders }; });
const sourcesIn = (folder: string) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const sources: Record<string, string> = {};
for (const file of (yield* fs.readDirectory(folder, { recursive: true, })).toSorted()) { const full = path.join(folder, file);
if ((yield* fs.stat(full)).type === "File" && file.endsWith(".ts")) { sources[file] = yield* fs.readFileString(full); } }
return sources; });
const workspacesAt = (root: string, globs: readonly string[]) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path;
const dirs = yield* Effect.forEach(globs, (glob) => Effect.map( fs.readDirectory(path.join(root, glob.replace(/\/\*$/u, ""))), (names) => names.toSorted().map((name) => `${glob.replace(/\*$/u, "")}${name}`) ) );
return yield* Effect.forEach(dirs.flat(), (dir) => Effect.map( Effect.flatMap( fs.readFileString(path.join(root, dir, "package.json")), decodeManifest ), ({ exports, name }) => ({ dir, exports: Object.values(exports ?? {}).map( (target) => `${dir}/${target.replace(/^\.\//u, "")}` ), name, }) ) ); });
const readBurrow = () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path;
const root = yield* path.fromFileUrl( new URL("../../../examples/burrow/", import.meta.url) );
const { files, folders } = yield* rootEntries(root);
expect(folders, "one config folder with a project.ts").toHaveLength(1);
const [folder = ""] = folders;
const manifest = yield* decodeManifest( yield* fs.readFileString(path.join(root, "package.json")) );
const facts = yield* Schema.decodeEffect(EvaluationFacts)({ ci: files, initializer: "okafor", now, project: "oscabriel/burrow", seed: 508, vouchFile: yield* fs.readFileString( path.join(root, folder, "vouched.td") ), workspaces: yield* workspacesAt(root, manifest.workspaces ?? []), });
return { facts, modules: bundleOf(yield* sourcesIn(path.join(root, folder))), }; });
const evaluated = () => Effect.gen(function* () { const { facts, modules } = yield* readBurrow(); const evaluator = yield* RulesEvaluator;
return yield* Schema.encodeEffect(Policy)( yield* evaluator.evaluate({ at, facts, modules }) ); });
const modules = [ { module: "@burrow/auth", paths: ["packages/auth/**"], publicFiles: ["packages/auth/src/index.ts"], }, { module: "@burrow/billing", paths: ["packages/billing/**"], publicFiles: ["packages/billing/src/index.ts"], }, { module: "@burrow/catalog", paths: ["packages/catalog/**"], publicFiles: ["packages/catalog/src/index.ts"], }, { module: "@burrow/kit", paths: ["packages/kit/**"], publicFiles: ["packages/kit/src/index.ts", "packages/kit/src/testing.ts"], }, { module: "@burrow/notify", paths: ["packages/notify/**"], publicFiles: ["packages/notify/src/index.ts"], }, { module: "@burrow/search", paths: ["packages/search/**"], publicFiles: ["packages/search/src/index.ts"], },];
const dropLegacyFetch = { askedBy: "okafor", blockedBy: [], brief: { context: [ "http.get hands back a failed request as a value, where legacyFetch threw, so each caller decides what a failure means", ], outOfScope: ["The module-level state the lint warns about: its ratchet"], want: "Replace every call to legacyFetch with http.get, then delete legacyFetch", }, check: CheckSpec.cases.Migrate.make({ count: Command.make("bun scripts/count-callers.ts legacyFetch --json"), from: SymbolName.make("legacyFetch"), to: SymbolName.make("http.get"), }), file: `${configFolder}/asks/drop-legacy-fetch.ask.ts`, id: "drop-legacy-fetch", kind: "migrate", openedAt: now, openedIn: at, project: "oscabriel/burrow", state: "open", territory: { module: ".", paths: ["packages/**"] },};
const migrateKind = { checkKind: "migrate", claimers: "vouched", finish: "never", name: "migrate", oneWay: false, race: 1, requires: ["want", "check", "territory"], strength: "assertion",};
describe("examples/burrow's config folder", () => { it.live( "evaluates to six modules, a ratchet on the rule's lint count, the migrate kind and one migrate ask", () => Effect.gen(function* () { const policy = yield* evaluated();
expect({ asks: policy.asks, migrate: policy.kinds.filter(({ name }) => name === "migrate"), modules: policy.modules, ratchets: policy.ratchets, source: policy.source, }).toStrictEqual({ asks: [dropLegacyFetch], migrate: [migrateKind], modules, ratchets: [ { measure: RatchetMeasure.cases.Count.make({ command: Command.make("bun run lint --format json"), rule: "burrow/no-module-level-mutable-state", }), name: "no-module-level-mutable-state", }, ], source: "config", }); }).pipe(Effect.provide(bunEvaluator())) );
it.live( "names its maintainers, the people who may claim, and the run block its hooks run", () => Effect.gen(function* () { const policy = yield* evaluated();
expect({ deciders: policy.deciders, run: policy.run, vouches: policy.vouches.map(({ denounced, handle }) => [ handle, denounced, ]), }).toStrictEqual({ deciders: [{ handles: ["okafor", "lin-h"], paths: "**" }], run: { checks: { lint: "bun run lint", types: "bun run typecheck", unit: "bun test", }, setup: "bun install --frozen-lockfile", test: "bun test {path}", }, vouches: [ ["okafor", false], ["lin-h", false], ["wren", false], ["tamsin", false], ["ivo", false], ["marisol", false], ["dagny", false], ["pell", false], ["quickclaim", true], ], }); }).pipe(Effect.provide(bunEvaluator())) );
it.live("spans every package with the migrate ask's territory", () => Effect.gen(function* () { const policy = yield* evaluated(); const globs = policy.asks.flatMap(({ territory }) => territory.paths);
const covered = policy.modules.filter(({ paths: [folder] }) => globs.some( (glob) => glob.endsWith("/**") && folder.startsWith(glob.slice(0, -"**".length)) ) );
expect(covered.map(({ module }) => module)).toStrictEqual( modules.map(({ module }) => module) ); }).pipe(Effect.provide(bunEvaluator())) );});