import { describe, expect, test } from "bun:test"; import { mkdtemp, readFile, rm, stat } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createGeneratedOutputExtension, GENERATED_ARTIFACT_FILES, GENERATED_EXTENSION_VERSIONS, invalidateWatchDependencies, writeGeneratedArtifacts, createWatchDependencyIndex, type GeneratedOutputs, } from "../../packages/cli/src/compiler/index.ts"; const outputs: GeneratedOutputs = { graph: '{"contractVersion":1}\n', manifest: 'export const manifestGraphHash = "sha256:test";\n', runtimeActivation: '{"graphHash":"sha256:test"}\n', runtimeIntegrations: '{"graphHash":"sha256:test","integrations":[],"version":1}\n', runtimeIntegrationImports: "export const runtimeIntegrationModules = [] as const;\n", localServices: '{"graphHash":"sha256:test","services":[],"version":1}\n', diagnostics: "[]\n", openapi: "", client: "", contract: "export const contract = {};\n", clientContract: '{"protocol":"relkit.client-contract"}\n', clientRegistry: 'declare module "@relkit/client/react" {}\n', clientManifest: '{"protocol":"relkit.client-manifest"}\n', }; function descriptor(id: string, file: string, value: Record = {}) { return { kind: "function", invocationMode: "callable", id, source: { file, line: 1, column: 1 }, exportName: id, exportKind: "named" as const, value: { kind: "function", id, ...value }, }; } describe("compiler generated artifacts", () => { test("writes core bytes once and preserves unchanged modification state", async () => { const directory = await mkdtemp(join(tmpdir(), "relkit-artifacts-")); try { const first = await writeGeneratedArtifacts(outputs, { directory }); expect(first.changed).toBe(true); expect(first.writes.map(({ fileName }) => fileName)).toEqual([ GENERATED_ARTIFACT_FILES.graph, GENERATED_ARTIFACT_FILES.clientContract, GENERATED_ARTIFACT_FILES.clientManifest, GENERATED_ARTIFACT_FILES.clientRegistry, GENERATED_ARTIFACT_FILES.contract, GENERATED_ARTIFACT_FILES.diagnostics, GENERATED_ARTIFACT_FILES.localServices, GENERATED_ARTIFACT_FILES.runtimeActivation, GENERATED_ARTIFACT_FILES.runtimeIntegrations, GENERATED_ARTIFACT_FILES.runtimeIntegrationImports, GENERATED_ARTIFACT_FILES.manifest, ]); const before = await stat(join(directory, GENERATED_ARTIFACT_FILES.graph)); const second = await writeGeneratedArtifacts(outputs, { directory }); const after = await stat(join(directory, GENERATED_ARTIFACT_FILES.graph)); expect(second.writes.every(({ changed }) => !changed)).toBe(true); expect(after.mtimeNs).toBe(before.mtimeNs); expect(await readFile(join(directory, GENERATED_ARTIFACT_FILES.graph), "utf8")).toBe( outputs.graph, ); } finally { await rm(directory, { recursive: true, force: true }); } }); test("accepts only versioned future artifact extensions", async () => { const directory = await mkdtemp(join(tmpdir(), "relkit-artifacts-")); try { const report = await writeGeneratedArtifacts(outputs, { directory, extensions: [ createGeneratedOutputExtension("openapi", '{"openapi":"3.1.0"}\n'), createGeneratedOutputExtension("client", "export const client = true;\n"), createGeneratedOutputExtension("deploymentPlan", '{"graphHash":"sha256:test"}\n'), ], }); expect(report.writes.map(({ fileName }) => fileName)).toEqual([ "application.graph.json", "client-contract.json", "client-manifest.json", "client-registry.d.ts", "client.ts", "contract.ts", "deployment.plan.json", "diagnostics.json", "local-services.plan.json", "openapi.json", "runtime-activation.json", "runtime-integrations.plan.json", "runtime-integrations.ts", "runtime.manifest.ts", ]); expect(GENERATED_EXTENSION_VERSIONS.deploymentPlan.version).toBe(1); await expect( writeGeneratedArtifacts(outputs, { directory, extensions: [{ kind: "client", version: 2, content: "bad" }], }), ).rejects.toThrow("unsupported"); } finally { await rm(directory, { recursive: true, force: true }); } }); test("invalidates changed descriptors and transitive dependants deterministically", () => { const index = createWatchDependencyIndex([ descriptor("orders.route", "src/routes.ts", { target: { ref: { kind: "function", id: "orders.get" } }, }), descriptor("orders.page", "src/page.ts", { route: { ref: { kind: "route", id: "orders.route" } }, }), descriptor("orders.get", "src/functions.ts"), descriptor("orders.job", "src/jobs.ts", { target: { ref: { kind: "function", id: "orders.get" } }, }), ]); const result = invalidateWatchDependencies(index, [".\\src\\functions.ts"]); expect(result.changedDescriptorIds).toEqual(["orders.get"]); expect(result.affectedDescriptorIds).toEqual([ "orders.get", "orders.job", "orders.page", "orders.route", ]); expect(result.affectedFiles).toEqual([ "src/functions.ts", "src/jobs.ts", "src/page.ts", "src/routes.ts", ]); expect(result.invalidatedArtifacts).toHaveLength(8); expect(result.invalidatedArtifacts).toContain(GENERATED_ARTIFACT_FILES.jobsManifest); expect(invalidateWatchDependencies(index, ["src/unchanged.ts"]).affectedDescriptorIds).toEqual( [], ); }); });