import { cpSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { basename, join, relative } from "node:path"; import { describe, expect, it } from "vitest"; import { createPiSession } from "../test/fixture/create-pi-session.ts"; import { CONFIG_PATH, expectedGenerated } from "./codegen.ts"; import { ROOT } from "./lib/common.ts"; const PI_PROVIDED = /^(node:|@earendil-works\/pi-[a-z-]+(\/|$)|typebox(\/|$))/; function importSpecifiers(source: string): string[] { return [ ...source.matchAll(/\bfrom\s+"([^"]+)"|\bimport\s*\(\s*"([^"]+)"\s*\)/g), ].map((match) => (match[1] ?? match[2])!); } describe("repository codegen", () => { const generated = expectedGenerated(ROOT); it("regenerates identical bytes and matches committed output", () => { const again = expectedGenerated(ROOT); expect(again.map((file) => file.output)).toEqual( generated.map((file) => file.output), ); for (const [index, file] of generated.entries()) { const label = relative(ROOT, file.output); expect(again[index]?.bytes.equals(file.bytes), label).toBe(true); expect(readFileSync(file.output).equals(file.bytes), label).toBe(true); } }); it("emits leaf helpers importing only Node built-ins and Pi-provided modules", () => { for (const file of generated) { const specifiers = importSpecifiers(file.bytes.toString("utf8")); for (const specifier of specifiers) expect(specifier, relative(ROOT, file.output)).toMatch(PI_PROVIDED); } }); const settings = ( JSON.parse(readFileSync(join(ROOT, CONFIG_PATH), "utf8")) as { targets: { name: string; extensions: string[] }[]; } ).targets.find((target) => target.name === "pi-ext-settings"); it.each(settings?.extensions ?? [])( "%s loads alone from a copy outside the checkout", async (slug) => { const root = mkdtempSync(join(tmpdir(), `pi-ext-relocated-${slug}-`)); const copy = join(root, slug); try { cpSync(join(ROOT, "extensions", slug), copy, { recursive: true, filter: (path) => !["__tests__", "__bench__", "__e2e__", "node_modules"].includes( basename(path), ), }); const manifest = JSON.parse( readFileSync(join(copy, "package.json"), "utf8"), ) as { dependencies?: Record }; // Provision declared runtime deps, never the checkout-wide node_modules. for (const dependency of Object.keys(manifest.dependencies ?? {})) { cpSync( join(ROOT, "node_modules", dependency), join(copy, "node_modules", dependency), { recursive: true }, ); } const t = await createPiSession({ cwd: root, extensionPaths: [join(copy, "index.ts")], }); try { expect(t.errors).toEqual([]); expect(t.extensions).toHaveLength(1); expect(t.extensions[0]?.path).toBe(join(copy, "index.ts")); } finally { await t.dispose(); } } finally { rmSync(root, { recursive: true, force: true }); } }, ); });