test: verify browser WASM config parity
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
import { assert, assertEquals } from "jsr:@std/assert";
|
||||
import {
|
||||
commitConfigTree,
|
||||
fetchConfigEntry,
|
||||
fetchConfigTree,
|
||||
previewConfigTree,
|
||||
} from "../../src/lib/workspace/config-source/api.ts";
|
||||
|
||||
function response(body: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
Deno.test("config source API stays workspace-scoped and separates preview from commit", async () => {
|
||||
const calls: Array<{ url: string; init?: RequestInit }> = [];
|
||||
const fetcher = ((input: string | URL | Request, init?: RequestInit) => {
|
||||
calls.push({ url: String(input), init });
|
||||
return Promise.resolve(response({ ok: true }));
|
||||
}) as typeof fetch;
|
||||
|
||||
await fetchConfigTree("w/one", fetcher);
|
||||
await fetchConfigEntry("w/one", "profiles/main.dcdl", fetcher);
|
||||
await previewConfigTree("w/one", { changes: [], entrypoints: [] }, fetcher);
|
||||
await commitConfigTree("w/one", {
|
||||
base_revision: 4,
|
||||
base_digest: "sha256:base",
|
||||
changes: [],
|
||||
entrypoints: [],
|
||||
}, fetcher);
|
||||
|
||||
assertEquals(calls.map((call) => call.url), [
|
||||
"/api/w/w%2Fone/config/source-tree",
|
||||
"/api/w/w%2Fone/config/source-tree/entries/profiles%2Fmain.dcdl",
|
||||
"/api/w/w%2Fone/config/source-tree/preview",
|
||||
"/api/w/w%2Fone/config/source-tree/commit",
|
||||
]);
|
||||
assertEquals(calls[2].init?.method, "POST");
|
||||
assertEquals(calls[3].init?.method, "POST");
|
||||
assert(
|
||||
String(calls[3].init?.body).includes('"base_digest":"sha256:base"'),
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("config source API surfaces failed evaluation instead of treating it as a draft write", async () => {
|
||||
const fetcher = (() =>
|
||||
Promise.resolve(
|
||||
new Response("structured diagnostics", { status: 422 }),
|
||||
)) as typeof fetch;
|
||||
let message = "";
|
||||
try {
|
||||
await previewConfigTree("w", { changes: [], entrypoints: [] }, fetcher);
|
||||
} catch (error) {
|
||||
message = String(error);
|
||||
}
|
||||
assert(message.includes("structured diagnostics"));
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
import { assertEquals } from "jsr:@std/assert";
|
||||
// The generated wasm-bindgen loader is JavaScript with an adjacent declaration file.
|
||||
// @ts-expect-error Deno checks the generated JS implementation rather than its .d.ts.
|
||||
import init, {
|
||||
analyze_snapshot,
|
||||
evaluate_snapshot,
|
||||
} from "../../src/lib/workspace/config-source/generated/config_source_wasm.js";
|
||||
import type { ConfigTreeSnapshot, ToolchainContract } from "../../src/lib/workspace/config-source/types.ts";
|
||||
|
||||
const bytes = await Deno.readFile(
|
||||
new URL("../../src/lib/workspace/config-source/generated/config_source_wasm_bg.wasm", import.meta.url),
|
||||
);
|
||||
await init({ module_or_path: bytes });
|
||||
|
||||
const snapshot: ConfigTreeSnapshot = {
|
||||
revision: 4,
|
||||
digest: "sha256:test-tree",
|
||||
entries: {
|
||||
"workspace.dcdl": {
|
||||
path: "workspace.dcdl",
|
||||
content_type: "decodal",
|
||||
content: 'import "./lib/value.dcdl"',
|
||||
content_digest: "sha256:root",
|
||||
},
|
||||
"lib/value.dcdl": {
|
||||
path: "lib/value.dcdl",
|
||||
content_type: "decodal",
|
||||
content: "{ answer = 42; }",
|
||||
content_digest: "sha256:value",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const contract: ToolchainContract = {
|
||||
contract_version: 1,
|
||||
decodal_version: "0.2.0",
|
||||
schema_version: 1,
|
||||
entrypoints: ["workspace.dcdl"],
|
||||
import_policy_version: 1,
|
||||
fingerprint: "sha256:test-contract",
|
||||
};
|
||||
|
||||
Deno.test("generated WASM evaluates the same virtual import contract", () => {
|
||||
const result = evaluate_snapshot(snapshot, contract) as {
|
||||
projections: Array<{ data_json: { answer: number } }>;
|
||||
};
|
||||
assertEquals(result.projections[0].data_json, { answer: 42 });
|
||||
});
|
||||
|
||||
Deno.test("generated WASM diagnostics carry snapshot provenance", () => {
|
||||
const diagnostics = analyze_snapshot(
|
||||
snapshot,
|
||||
"workspace.dcdl",
|
||||
"{ broken = ; }",
|
||||
) as Array<{
|
||||
path: string;
|
||||
revision: number;
|
||||
tree_digest: string;
|
||||
kind: string;
|
||||
}>;
|
||||
assertEquals(diagnostics[0].path, "workspace.dcdl");
|
||||
assertEquals(diagnostics[0].revision, 4);
|
||||
assertEquals(diagnostics[0].tree_digest, "sha256:test-tree");
|
||||
assertEquals(diagnostics[0].kind, "syntax");
|
||||
});
|
||||
Reference in New Issue
Block a user