// The ./testing entry's built declarations. `effected.toml` suppresses `ae-forgotten-export` for // `entry point testing.d.ts` because the second API Extractor pass reports every root type `Git` and `GitShape` // reach through the self-referencing `@effected/git` import (GitFixture.recorder). That suppression would also hide // a type local to the testing entry left unexported; this test is what makes it safe, by reading the built // declarations the root vitest setup refreshes. import { readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { assert, describe, it } from "@effect/vitest"; const BUILT = join(dirname(fileURLToPath(import.meta.url)), "..", "dist", "prod", "npm", "pkg"); /** A top-level declaration in a rolled-up `.d.ts`: whether the line exports it, and its name. */ const TOP_LEVEL_DECLARATION = /^(export\s+)?(?:declare\s+)?(?:abstract\s+)?(?:class|interface|type|const|let|var|function|enum|namespace)\s+([A-Za-z_$][\w$]*)/gm; /** Names exported through `export { … }` / `export type { … }` blocks with no `from`. */ const exportBlockNames = (dts: string): ReadonlyArray => [...dts.matchAll(/^export\s+(?:type\s+)?\{([^}]*)\}\s*;?\s*$/gm)].flatMap((match) => (match[1] ?? "") .split(",") .map((name) => name.trim().replace(/^type\s+/, "")) .filter((name) => name.length > 0), ); /** The entry's declarations: each name and whether it is exported, sorted by name. */ const declarationsOf = (dts: string): ReadonlyArray => { const blockExports = new Set(exportBlockNames(dts)); return [...dts.matchAll(TOP_LEVEL_DECLARATION)] .map((match): readonly [string, boolean] => { const name = match[2] ?? ""; return [name, match[1] !== undefined || blockExports.has(name)]; }) .sort(([a], [b]) => a.localeCompare(b)); }; describe("the built ./testing declarations", () => { const dts = readFileSync(join(BUILT, "testing.d.ts"), "utf8"); it("declare only the exported testing surface, plus the error class's synthesized base", () => { assert.deepStrictEqual(declarationsOf(dts), [ ["GitCall", true], ["GitFaultHandlers", true], ["GitFaults", true], ["GitFixture", true], ["GitFixtureError", true], // The class factory's base, a module-local `declare const` the `_base` suppression covers. ["GitFixtureError_base", false], ["GitFixtureOptions", true], ["GitRecorder", true], ["GitSpawn", true], ["GitSpawnConfig", true], ["LocalRemote", true], ["LocalRemotePushOptions", true], ["MakeRemoteOptions", true], ["RecordSpawnsOptions", true], ]); }); it("name Git and GitShape through the root rather than carrying a copy", () => { assert.match(dts, /import \{[^}]*\bGitShape\b[^}]*\} from "@effected\/git";/); assert.notMatch(dts, /^(?:export\s+)?declare\s+class\s+Git\b/m); assert.notMatch(dts, /^(?:export\s+)?interface\s+GitShape\b/m); }); it("mutation control: an unexported local declaration is reported", () => { const mutated = `${dts}\ninterface Leaked {\n}\n`; assert.deepInclude(declarationsOf(mutated), ["Leaked", false]); }); });