// Copyright (C) 2026 Kingsrook, LLC. // SPDX-License-Identifier: Apache-2.0 // Fixed trusted qualification corpus only. This is not a production compiler route. import { mkdir, writeFile } from "node:fs/promises"; import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; import { compile } from "../../worker/dist/compiler.js"; import { PROTOCOL, TOOLCHAIN, sourceHash } from "../../worker/dist/protocol.js"; if(process.versions.node !== "24.22.2") { throw new Error("QUALIFICATION_NODE_VERSION"); } const destination = new URL("../target/native-fixtures/", import.meta.url); await mkdir(destination, { recursive: true }); const sdk = 'import type { Context, Qqq } from "@qqq/sdk";\n'; const cases = [ { name: "state", files: [ { path: "main.ts", content: sdk + `import { bump, stamp, random } from './state.js'; await Promise.resolve(); export async function main(context: Context, qqq: Qqq) { const rows = await qqq.query('purchaseOrder', ['id'], [], 1); return { count: bump(), stamp, random, id: rows[0]?.id, actor: context.payload.actor }; }` }, { path: "state.ts", content: "let count = 0; export function bump() { return ++count; } export const stamp = Date.now(); export const random = Math.random();" }, ] }, { name: "unicode", files: [ { path: "main.ts", content: "import { a } from './a.js'; import { b } from './b.js'; import { c } from './c.js'; export async function main() { return { label: a + b + c }; }" }, ...["a", "b", "c"].map((name, index) => ({ path: `${name}.ts`, content: `/*${"é".repeat(180000)}*/\nexport const ${name} = ${JSON.stringify(["café ", "漢字 ", "Привет"][index])};` })), ] }, { name: "plain", files: [{ path: "main.ts", content: "export async function main() { return { label: 'plain' }; }" }] }, { name: "script-error", files: [{ path: "main.ts", content: "export async function main() { throw new Error('fixed trusted fixture'); }" }] }, ]; const hashes = []; for(const fixture of cases) { const request = { protocol: PROTOCOL, type: "compile", requestId: `trusted-${fixture.name}`, strict: true, toolchainId: TOOLCHAIN, entryPoint: "main.ts", files: fixture.files, sourceSha256: sourceHash(fixture.files) }; const result = compile(request); if(result.success !== true || typeof result.bundle !== "string") { throw new Error(`TRUSTED_COMPILE_REFUSED:${fixture.name}`); } const record = { name: fixture.name, entryPoint: "main.ts", sourceSha256: request.sourceSha256, toolchainId: TOOLCHAIN, bundle: result.bundle, sha256: createHash("sha256").update(result.bundle).digest("hex") }; if(fixture.name === "unicode" && Buffer.byteLength(record.bundle) <= 1024 * 1024) { throw new Error("UNICODE_BOUNDARY_NOT_EXERCISED"); } const serialized = JSON.stringify(record); await writeFile(new URL(`${fixture.name}.json`, destination), serialized, "utf8"); await writeFile(new URL(`${fixture.name}.sources.json`, destination), JSON.stringify(request), "utf8"); hashes.push({ name: fixture.name, sha256: createHash("sha256").update(serialized).digest("hex"), artifactSha256: record.sha256 }); } await writeFile(new URL("manifest.json", destination), JSON.stringify({ scope: "fixed trusted TypeScript compiler fixtures", node: process.versions.node, toolchain: TOOLCHAIN, files: hashes }, null, 2), "utf8"); process.stdout.write(`Compiled ${cases.length} fixed trusted fixtures into ${fileURLToPath(destination)}\n`);