import assert from "node:assert/strict" import { readdirSync, readFileSync } from "node:fs" import { join, sep } from "node:path" import { test } from "node:test" /** * No published export may name a condition that a build tool matches by itself. * * An `exports` block can list several addresses for one subpath and pick * between them by name. Node picks with `--conditions`. A build tool picks with * a list of its own, and Vite's list holds `development` while its dev server * runs, and `production` while it builds. * * This package once listed its TypeScript source under `development`. That is * useful here, where a script asks for it by name and skips the compile step. * It was harmful everywhere else: Vite matched the name without being asked, * loaded raw TypeScript out of `node_modules`, and pnpm stores a package behind * a symbolic link, from which the package cannot reach its own dependencies. * The dev server then failed on every request, and neither message it printed * named this package. The condition is now called `rulekit-source`, which no * build tool matches unless somebody asks for it. * * A `types` condition is safe, because TypeScript matches it on purpose and * reads a declaration file rather than source. */ const MANIFEST = JSON.parse(readFileSync(join(import.meta.dirname, "../package.json"), "utf8")) as { exports: Record> dependencies: Record peerDependenciesMeta: Record } /** Names a build tool matches with no instruction from anybody. */ const TOOL_MATCHED = ["development", "production", "browser", "node", "import", "require", "default"] test("required dependencies cannot also be optional peers", () => { // pnpm omits an optional peer even when dependencies also requires it. A // clean rulekit install then fails before the free CLI can read a corpus. for (const name of Object.keys(MANIFEST.dependencies)) { assert.notEqual( MANIFEST.peerDependenciesMeta[name]?.optional, true, `${name} is required at runtime; declaring it as an optional peer prevents a clean install.`, ) } }) test("no export condition is one a build tool matches by itself", () => { for (const [subpath, entry] of Object.entries(MANIFEST.exports)) { if (typeof entry === "string") continue for (const condition of Object.keys(entry)) { if (condition === "types" || condition === "default") continue assert.ok( !TOOL_MATCHED.includes(condition), `${subpath} lists the "${condition}" condition, which a build tool matches by itself. ` + `Use "rulekit-source" for source, or "default" for the compiled output.`, ) } } }) test("the root export explains that every part has its own subpath", async () => { // Without this module the resolver refuses the import, and its refusal shows // no subpath and no example. A reader meets it before they meet the table in // the README that explains the layout. await assert.rejects( () => import("./index.ts"), (error: Error) => { assert.match(error.message, /no root export/) assert.match(error.message, /@rulekitai\/rulekit\/server\/handler/) return true }, ) }) test("every export a build tool reaches is compiled output", () => { for (const [subpath, entry] of Object.entries(MANIFEST.exports)) { if (typeof entry === "string") continue assert.ok( entry.default?.startsWith("./dist/"), `${subpath} must fall back to ./dist/, so an install from npm never loads TypeScript.`, ) } }) test("every module under src has a subpath, so nothing ships unreachable", () => { // A module with no entry in `exports` compiles, ships, and cannot be // imported. Nothing else notices: the build succeeds and the tests pass, // because the tests reach it by relative path and a reader cannot. const sourced = new Set( Object.values(MANIFEST.exports) .map((entry) => (typeof entry === "string" ? entry : entry["rulekit-source"])) .filter(Boolean), ) /** Reached some other way, or on purpose internal. Each one states which. */ const INTERNAL: Record = { "./src/cli/bin.ts": "the `bin` field publishes it as the rulekit command", "./src/cli/eval.ts": "reached through the command, not imported", "./src/cli/grade.ts": "reached through the command, not imported", "./src/agent/prose.ts": "generated; read through agent/instructions and agent/skills", "./src/corpus/bm25.ts": "the ranking index the JSON store reads. No caller builds one", } const modules = readdirSync(join(import.meta.dirname), { recursive: true, encoding: "utf8" }) .filter((file) => file.endsWith(".ts") && !file.endsWith(".test.ts")) .map((file) => `./src/${file.split(sep).join("/")}`) .filter((file) => !INTERNAL[file]) const missing = modules.filter((file) => !sourced.has(file)) assert.deepEqual(missing, [], `these modules ship with no way to import them: ${missing.join(", ")}`) })