Spaces:
Running
Running
Download tests/static_compiler.test.mjs from warenterprise/trace2mcp: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/warenterprise/trace2mcp/resolve/main/tests/static_compiler.test.mjs
- Command line
-
hf download hf://spaces/warenterprise/trace2mcp/tests/static_compiler.test.mjs
-
curl -L -o static_compiler.test.mjs https://huggingface.co/spaces/warenterprise/trace2mcp/resolve/main/tests/static_compiler.test.mjs
2.78 kB
| import assert from "node:assert/strict"; | |
| import { readFile } from "node:fs/promises"; | |
| import test from "node:test"; | |
| import { compileTrace, traceMetrics } from "../assets/compiler.mjs"; | |
| const sample = JSON.parse( | |
| await readFile(new URL("../examples/sample_trace.json", import.meta.url), "utf8"), | |
| ); | |
| const golden = JSON.parse( | |
| await readFile(new URL("./golden/sample_graph.json", import.meta.url), "utf8"), | |
| ); | |
| test("static compiler reproduces the sample DAG", () => { | |
| const { workflow, layers } = compileTrace(sample); | |
| const metrics = traceMetrics(sample, workflow, layers); | |
| assert.deepEqual(workflow.nodes.map((node) => node.id), golden.nodes); | |
| assert.deepEqual( | |
| Object.fromEntries(workflow.nodes.map((node) => [node.id, node.depends_on])), | |
| golden.dependencies, | |
| ); | |
| assert.deepEqual(layers, golden.layers); | |
| assert.equal(metrics.maxParallelism, 3); | |
| assert.equal(metrics.criticalPathMs, 1910); | |
| }); | |
| test("static compiler blocks side effects", () => { | |
| const unsafe = structuredClone(sample); | |
| unsafe.events[0].side_effect = "write"; | |
| assert.throws(() => compileTrace(unsafe), /PURE\/READ steps only/); | |
| }); | |
| test("static compiler infers composite result dependencies", () => { | |
| const profile = { name: "War Enterprise", facts: ["AI"] }; | |
| const { workflow } = compileTrace({ | |
| version: "1", | |
| inputs: {}, | |
| events: [ | |
| { id: "profile", tool: "profile", arguments: {}, result: profile, side_effect: "read" }, | |
| { id: "report", tool: "report", arguments: { profile }, result: "done", side_effect: "read" }, | |
| ], | |
| }); | |
| assert.deepEqual(workflow.nodes[1].depends_on, ["profile"]); | |
| assert.deepEqual(workflow.nodes[1].arguments.profile, { $ref: "nodes.profile.result" }); | |
| }); | |
| test("static compiler deduplicates identical reads only", () => { | |
| const { workflow } = compileTrace({ | |
| version: "1", | |
| inputs: { q: "agents" }, | |
| events: [ | |
| { id: "a", tool: "search", arguments: { q: "agents" }, result: [1], side_effect: "read", idempotency: "idempotent" }, | |
| { id: "b", tool: "search", arguments: { q: "agents" }, result: [1], side_effect: "read", idempotency: "idempotent" }, | |
| ], | |
| }); | |
| assert.equal(workflow.nodes.length, 1); | |
| assert.deepEqual(workflow.deduplicated_events, { b: "a" }); | |
| }); | |
| test("static compiler keeps mutable reads and explicit ordering", () => { | |
| const { workflow, layers } = compileTrace({ | |
| version: "1", | |
| inputs: {}, | |
| events: [ | |
| { id: "a", tool: "clock", arguments: {}, result: "10:00", side_effect: "read" }, | |
| { id: "b", tool: "clock", arguments: {}, result: "10:01", side_effect: "read", depends_on: ["a"] }, | |
| ], | |
| }); | |
| assert.equal(workflow.nodes.length, 2); | |
| assert.deepEqual(workflow.nodes[1].depends_on, ["a"]); | |
| assert.deepEqual(layers, [["a"], ["b"]]); | |
| }); | |