import assert from "node:assert/strict"; import { test } from "node:test"; import { mkdtemp, mkdir, writeFile, symlink, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { createHash } from "node:crypto"; import { compileExecutionPlan, artifactFileReader, loadArtifactWorld, BrowserArtifactGateway, implementationDigest, contentArtifactUrl, } from "../src/execution-world/index.js"; import { loadReaderWorkspace } from "./reader-fixture.js"; import { buildArtifactFixture } from "./artifact-fixture.js"; test("immutable-output loading rejects traversal, links, and files larger than the declared bound", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "camino-artifact-read-")); try { await mkdir(path.join(root, "output")); await writeFile(path.join(root, "outside"), "secret"); await writeFile(path.join(root, "output", "file"), "data"); await symlink(path.join(root, "outside"), path.join(root, "output", "linked-file")); await symlink(root, path.join(root, "output", "linked-dir")); const read = await artifactFileReader({ package: path.join(root, "output") }); assert.equal(new TextDecoder().decode(await read("package", "file", 4)), "data"); await assert.rejects(read("package", "file", 3), /exceeds its declared bound/); await assert.rejects(read("package", "linked-file", 100), /Only real directories/); await assert.rejects(read("package", "linked-dir/outside", 100), /Only real directories/); await assert.rejects(read("package", "../outside", 100), /normalized relative/); await assert.rejects(read("unknown", "file", 100), /Unknown package output/); await assert.rejects(read("package", "file", Infinity), /Invalid file byte limit/); await assert.rejects( artifactFileReader({ package: path.join(root, "output", "linked-dir") }), /canonical absolute/, ); } finally { await rm(root, { recursive: true, force: true }); } }); test("gateway serves only declared browser assets with correct Wasm MIME and content identity", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "camino-artifact-world-")); try { const plan = compileExecutionPlan(await loadReaderWorkspace()), fixture = buildArtifactFixture(plan); const wasm = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]); const browser = fixture.manifest.implementations.find( (i) => i.target === "target:org.quixos.execution.javascript@1", )!; browser.files.push({ path: "portable/browser/assets/café.wasm", bytes: wasm.length, digest: createHash("sha256").update(wasm).digest("hex"), mediaType: "application/wasm", }); const shared = { ...browser.files[0]!, path: "share/quixos/shared/core.mjs" }; browser.files.push(shared); const { contentDigest, ...body } = browser; browser.contentDigest = implementationDigest(body); for (const implementation of fixture.manifest.implementations) for (const file of implementation.files) { const target = path.join(root, file.path); await mkdir(path.dirname(target), { recursive: true }); await writeFile(target, file.mediaType === "application/wasm" ? wasm : fixture.bytes); } await mkdir(path.join(root, "share/quixos"), { recursive: true }); await writeFile(path.join(root, "share/quixos/package-artifacts.json"), JSON.stringify(fixture.manifest)); const loaded = await loadArtifactWorld(plan, { [fixture.manifest.packageRevisionId]: root }); const gateway = new BrowserArtifactGateway(loaded); assert.equal(gateway.entries.length, 1); assert.deepEqual((await gateway.response(contentArtifactUrl(shared)))?.body, Buffer.from(fixture.bytes)); const entry = gateway.entries[0]!; const response = await gateway.response(entry.url); assert.equal(response?.status, 200); assert.equal(response?.headers["content-type"], "text/javascript"); assert.equal(response?.headers["cache-control"], "private, max-age=31536000, immutable"); assert.deepEqual(response?.body, Buffer.from(fixture.bytes)); assert.equal((await gateway.response(entry.url, response?.headers.etag))?.status, 304); const wasmUrl = entry.url.replace("module.mjs", "assets/caf%C3%A9.wasm"); const wasmResponse = await gateway.response(wasmUrl); assert.equal(wasmResponse?.headers["content-type"], "application/wasm"); assert.deepEqual(wasmResponse?.body, Buffer.from(wasm)); assert.equal(await gateway.response(entry.url.replace("portable/browser/module.mjs", "bin/service")), undefined); assert.equal(await gateway.response("/nix/store/secret"), undefined); assert.equal(await gateway.response(entry.url.replace(loaded.digest, "0".repeat(64))), undefined); await writeFile(path.join(root, browser.path), "x".repeat(fixture.bytes.length)); await assert.rejects(gateway.response(entry.url), /changed after world validation/); await assert.rejects(loadArtifactWorld(plan, { [fixture.manifest.packageRevisionId]: root }), /Artifact bytes/); } finally { await rm(root, { recursive: true, force: true }); } }); test("checked receipts bind regenerated code, generator identity and manifests", async () => { const { expectedArtifactReceipt, checkArtifactReceipt } = await import("../src/execution-world/receipts.js"); const plan = compileExecutionPlan(await loadReaderWorkspace()); const fixture = buildArtifactFixture(plan); const generator = "a".repeat(64); const { receipt, sources } = expectedArtifactReceipt(plan, fixture.manifest, generator); const read = async (_revision: string, file: string) => sources.get(file)!; await checkArtifactReceipt(plan, fixture.manifest, receipt, generator, read); await assert.rejects( checkArtifactReceipt(plan, fixture.manifest, { ...receipt, generatorDigest: "b".repeat(64) }, generator, read), /trusted compiler/, ); await assert.rejects( checkArtifactReceipt(plan, fixture.manifest, { ...receipt, manifestDigest: "b".repeat(64) }, generator, read), /trusted compiler/, ); await assert.rejects( checkArtifactReceipt(plan, fixture.manifest, receipt, generator, async () => new TextEncoder().encode("substituted"), ), /replaced or omitted/, ); }); test("remote JS modules witness generated adapters without imposing TypeScript on custom services", async () => { const { asyncObjectPlan } = await import("./async-fixture.js"); const { expectedArtifactReceipt, checkArtifactReceipt } = await import("../src/execution-world/receipts.js"); const plan = await asyncObjectPlan(), fixture = buildArtifactFixture(plan, "async:provider-runtime@2"), generator = "a".repeat(64); fixture.manifest.implementations = fixture.manifest.implementations.filter( (implementation) => implementation.target === "server", ); const { receipt, sources } = expectedArtifactReceipt(plan, fixture.manifest, generator); assert.deepEqual( receipt.generated.map((file) => file.path), ["share/quixos/generated/remote.ts"], ); await checkArtifactReceipt(plan, fixture.manifest, receipt, generator, async (_revision, path) => sources.get(path)!); await assert.rejects( checkArtifactReceipt(plan, fixture.manifest, receipt, generator, async () => new Uint8Array()), /replaced or omitted/, ); fixture.manifest.implementations = fixture.manifest.implementations.filter( (implementation) => implementation.compatibility === "quixos-service-v1", ); assert.deepEqual(expectedArtifactReceipt(plan, fixture.manifest, generator).receipt.generated, []); }); test("transaction process services do not promise generated TypeScript modules", async () => { const { expectedArtifactReceipt, checkArtifactReceipt } = await import("../src/execution-world/receipts.js"); const plan = compileExecutionPlan(await loadReaderWorkspace()), fixture = buildArtifactFixture(plan); fixture.manifest.implementations = fixture.manifest.implementations.filter( (i) => i.compatibility === "quixos-service-v1", ); const generator = "a".repeat(64), { receipt } = expectedArtifactReceipt(plan, fixture.manifest, generator); assert.deepEqual(receipt.generated, []); await checkArtifactReceipt(plan, fixture.manifest, receipt, generator, async () => { throw Error("No adapter claimed"); }); }); test("a remote export group in a mixed package witnesses only its own adapter", async () => { const { asyncObjectPlan } = await import("./async-fixture.js"); const { expectedArtifactReceipt } = await import("../src/execution-world/receipts.js"); const plan = await asyncObjectPlan(); const fixture = buildArtifactFixture(plan, "async:controller-runtime@1"); const pkg = plan.definition.packages.find((entry) => entry.revision === fixture.manifest.packageRevisionId)!; fixture.manifest.implementations = fixture.manifest.implementations.filter( (entry) => entry.target === "server" && entry.compatibility === "quixos-js-module-v1", ); for (const implementation of fixture.manifest.implementations) implementation.exports = implementation.exports.filter((id) => pkg.exports.some((entry) => entry.id === id && entry.execution.kind === "remote"), ); assert.deepEqual( expectedArtifactReceipt(plan, fixture.manifest, "a".repeat(64)).receipt.generated.map((file) => file.path), ["share/quixos/generated/remote.ts"], ); });