296 lines
15 KiB
JavaScript
296 lines
15 KiB
JavaScript
const assert = require("assert");
|
|
const crypto = require("crypto");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const path = require("path");
|
|
const { initialize, hashPayload, buildNode, buildBinaryNode } = require("..");
|
|
|
|
async function main() {
|
|
const projectRoot = path.join(__dirname, "fixture");
|
|
const optimus = initialize(projectRoot);
|
|
|
|
assert.deepStrictEqual(optimus.modules, ["alpha", "beta"]);
|
|
assert.strictEqual(optimus.getSettings("input").prefix, "alpha");
|
|
assert.strictEqual(optimus.loadModule("alpha").loadAlpha("x").payload, "alpha:x");
|
|
assert.throws(() => optimus.loadModule("missing"), /Unknown module/);
|
|
assert.throws(() => optimus.getSettings("missing"), /Unknown settings/);
|
|
|
|
const overlaid = initialize(projectRoot, {
|
|
settingOverlays: ["overlays/remote.json", "overlays/experiment.json"],
|
|
});
|
|
assert.deepStrictEqual(overlaid.getSettings("llm"), {
|
|
client: "lm-studio",
|
|
host: "remote-box",
|
|
port: 1234,
|
|
model: "experimental-model",
|
|
reasoning: null,
|
|
nested: {
|
|
keep: "default",
|
|
replace: "remote",
|
|
extra: true,
|
|
},
|
|
list: ["remote"],
|
|
});
|
|
assert.deepStrictEqual(overlaid.getSettings("run"), { label: "experiment", enabled: true });
|
|
|
|
const expectedHash = crypto.createHash("sha256").update(JSON.stringify({ a: 1 })).digest("hex");
|
|
assert.strictEqual(hashPayload({ a: 1 }), expectedHash);
|
|
|
|
const source = buildNode({ source: true });
|
|
const node = buildNode({ result: true }, { source });
|
|
assert.deepStrictEqual(node.sourceHash, { source: source.hash });
|
|
assert.strictEqual(node.hash, hashPayload(node.payload));
|
|
assert.strictEqual(node.status, "ok");
|
|
assert.deepStrictEqual(node.payload, { result: true });
|
|
|
|
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "optimus-binary-"));
|
|
const binaryPath = path.join(tempRoot, "sample.bin");
|
|
const firstBytes = Buffer.from([0x00, 0x01, 0x02, 0xff]);
|
|
const secondBytes = Buffer.from([0x00, 0x01, 0x03, 0xff]);
|
|
|
|
try {
|
|
fs.writeFileSync(binaryPath, firstBytes);
|
|
const binaryNode = buildBinaryNode(binaryPath, { source });
|
|
const expectedBinaryHash = crypto.createHash("sha256").update(firstBytes).digest("hex");
|
|
assert.deepStrictEqual(binaryNode.sourceHash, { source: source.hash });
|
|
assert.strictEqual(binaryNode.hash, expectedBinaryHash);
|
|
assert.strictEqual(binaryNode.status, "ok");
|
|
assert.strictEqual(binaryNode.payloadBin, binaryPath);
|
|
assert.strictEqual("payload" in binaryNode, false);
|
|
|
|
const binaryViaUtils = optimus.utils.buildBinaryNode(binaryPath);
|
|
assert.strictEqual(binaryViaUtils.hash, expectedBinaryHash);
|
|
|
|
fs.writeFileSync(binaryPath, secondBytes);
|
|
const changedBinaryNode = buildBinaryNode(binaryPath);
|
|
assert.notStrictEqual(changedBinaryNode.hash, binaryNode.hash);
|
|
assert.strictEqual(changedBinaryNode.payloadBin, binaryPath);
|
|
} finally {
|
|
fs.rmSync(tempRoot, { recursive: true, force: true });
|
|
}
|
|
|
|
const durableRoot = fs.mkdtempSync(path.join(os.tmpdir(), "optimus-durable-"));
|
|
try {
|
|
const artifactFile = path.join(durableRoot, "result.json");
|
|
let builds = 0;
|
|
const parentA = buildNode("same-parent-content", { recipe: buildNode("recipe-a") });
|
|
const firstDurable = await optimus.resolveArtifact(artifactFile, { parent: parentA }, async () => { builds++; return "result"; });
|
|
const secondDurable = await optimus.resolveArtifact(artifactFile, { parent: parentA }, async () => { builds++; return "should-not-run"; });
|
|
assert.strictEqual(builds, 1);
|
|
assert.strictEqual(firstDurable.hash, secondDurable.hash);
|
|
assert.ok(secondDurable.artifact);
|
|
|
|
// A parent may be regenerated from a different recipe yet keep identical content.
|
|
// Children key off the parent's content hash, so no downstream rebuild occurs.
|
|
const parentB = buildNode("same-parent-content", { recipe: buildNode("recipe-b") });
|
|
assert.strictEqual(parentA.hash, parentB.hash);
|
|
await optimus.resolveArtifact(artifactFile, { parent: parentB }, async () => { builds++; return "should-still-not-run"; });
|
|
assert.strictEqual(builds, 1);
|
|
|
|
// Changing the complete named parent set is a different recipe.
|
|
await optimus.resolveArtifact(artifactFile, { parent: parentB, thirdWitness: buildNode("new") }, async () => { builds++; return "result"; });
|
|
assert.strictEqual(builds, 2);
|
|
|
|
assert.strictEqual(optimus.invalidateArtifact(artifactFile), true);
|
|
assert.strictEqual(fs.existsSync(artifactFile), false);
|
|
} finally {
|
|
fs.rmSync(durableRoot, { recursive: true, force: true });
|
|
}
|
|
|
|
// Suspicion is durable but does not block downstream work. Local `sus`
|
|
// must be relitigated; inherited suspicion is recalculated from every
|
|
// current parent and can clear without rerunning an unchanged transformer.
|
|
const susRoot = fs.mkdtempSync(path.join(os.tmpdir(), "optimus-sus-"));
|
|
try {
|
|
fs.mkdirSync(path.join(susRoot, "settings"), { recursive: true });
|
|
fs.mkdirSync(path.join(susRoot, "src", "worker"), { recursive: true });
|
|
fs.writeFileSync(path.join(susRoot, "settings", "modules.json"), JSON.stringify({ worker: "./src/worker/index.js" }));
|
|
fs.writeFileSync(path.join(susRoot, "settings", "artifacts.json"), JSON.stringify({ target: { module: "worker", builder: "buildTarget" } }));
|
|
fs.writeFileSync(path.join(susRoot, "settings", "presentation.json"), JSON.stringify({ showOnEnsure: false, showOnBuild: false }));
|
|
fs.writeFileSync(path.join(susRoot, "state.json"), JSON.stringify({ aSus: true, bSus: true, childSus: false, aPayload: "a", bPayload: "b" }));
|
|
fs.writeFileSync(path.join(susRoot, "counts.json"), JSON.stringify({}));
|
|
fs.writeFileSync(path.join(susRoot, "src", "worker", "index.js"), `const fs = require("fs");
|
|
const path = require("path");
|
|
module.exports = utils => {
|
|
const root = ${JSON.stringify(susRoot)};
|
|
const artifact = name => path.join(root, "artifacts", name + ".json");
|
|
const state = () => JSON.parse(fs.readFileSync(path.join(root, "state.json"), "utf8"));
|
|
const bump = name => {
|
|
const filename = path.join(root, "counts.json");
|
|
const counts = JSON.parse(fs.readFileSync(filename, "utf8"));
|
|
counts[name] = (counts[name] || 0) + 1;
|
|
fs.writeFileSync(filename, JSON.stringify(counts));
|
|
};
|
|
async function ensureA() {
|
|
return utils.resolveArtifact(artifact("a"), {}, async () => {
|
|
bump("a");
|
|
const current = state();
|
|
return current.aSus ? utils.sus(current.aPayload) : current.aPayload;
|
|
});
|
|
}
|
|
async function ensureB() {
|
|
return utils.resolveArtifact(artifact("b"), {}, async () => {
|
|
bump("b");
|
|
const current = state();
|
|
return current.bSus ? utils.sus(current.bPayload) : current.bPayload;
|
|
});
|
|
}
|
|
async function ensureChild() {
|
|
const a = await ensureA();
|
|
const b = await ensureB();
|
|
return utils.resolveArtifact(artifact("child"), { a, b }, async () => {
|
|
bump("child");
|
|
const payload = "child:" + a.payload + ":" + b.payload;
|
|
return state().childSus ? utils.sus(payload) : payload;
|
|
});
|
|
}
|
|
async function buildTarget() {
|
|
const child = await ensureChild();
|
|
return utils.resolveArtifact(artifact("target"), { child }, async () => {
|
|
bump("target");
|
|
return "target:" + child.payload;
|
|
});
|
|
}
|
|
return { buildTarget };
|
|
};\n`);
|
|
|
|
const runtime = initialize(susRoot);
|
|
const readNode = name => JSON.parse(fs.readFileSync(path.join(susRoot, "artifacts", `${name}.json`), "utf8"));
|
|
const readCounts = () => JSON.parse(fs.readFileSync(path.join(susRoot, "counts.json"), "utf8"));
|
|
const writeState = patch => {
|
|
const filename = path.join(susRoot, "state.json");
|
|
fs.writeFileSync(filename, JSON.stringify({ ...JSON.parse(fs.readFileSync(filename, "utf8")), ...patch }));
|
|
};
|
|
|
|
await runtime.buildArtifact("target");
|
|
assert.strictEqual(readNode("a").status, "sus");
|
|
assert.strictEqual(readNode("b").status, "sus");
|
|
assert.strictEqual(readNode("child").status, "inherited-sus");
|
|
assert.strictEqual(readNode("target").status, "inherited-sus");
|
|
assert.deepStrictEqual(readCounts(), { a: 1, b: 1, child: 1, target: 1 });
|
|
|
|
// Repairing only one of two suspicious parents must not clear the child.
|
|
writeState({ aSus: false });
|
|
await runtime.buildArtifact("target", { relitigateSus: true });
|
|
assert.strictEqual(readNode("a").status, "ok");
|
|
assert.strictEqual(readNode("b").status, "sus");
|
|
assert.strictEqual(readNode("child").status, "inherited-sus");
|
|
assert.deepStrictEqual(readCounts(), { a: 2, b: 2, child: 1, target: 1 });
|
|
|
|
// Once the last suspicious parent clears with the same payload, inherited
|
|
// descendants clear too, without semantic transformer reruns.
|
|
writeState({ bSus: false });
|
|
await runtime.buildArtifact("target", { relitigateSus: true });
|
|
assert.strictEqual(readNode("b").status, "ok");
|
|
assert.strictEqual(readNode("child").status, "ok");
|
|
assert.strictEqual(readNode("target").status, "ok");
|
|
assert.deepStrictEqual(readCounts(), { a: 2, b: 3, child: 1, target: 1 });
|
|
|
|
// Local sus never clears merely because its parents are clean. It requires
|
|
// relitigation of that artifact itself.
|
|
fs.rmSync(path.join(susRoot, "artifacts", "child.json"), { force: true });
|
|
fs.rmSync(path.join(susRoot, "artifacts", "target.json"), { force: true });
|
|
writeState({ childSus: true });
|
|
await runtime.buildArtifact("target");
|
|
assert.strictEqual(readNode("child").status, "sus");
|
|
assert.strictEqual(readNode("target").status, "inherited-sus");
|
|
const beforeLocalRepair = readCounts();
|
|
|
|
writeState({ childSus: false });
|
|
await runtime.buildArtifact("target");
|
|
assert.strictEqual(readNode("child").status, "sus");
|
|
assert.deepStrictEqual(readCounts(), beforeLocalRepair, "ordinary ensure must reuse local sus rather than silently retry it");
|
|
|
|
await runtime.buildArtifact("target", { relitigateSus: true });
|
|
assert.strictEqual(readNode("child").status, "ok");
|
|
assert.strictEqual(readNode("target").status, "ok");
|
|
const afterLocalRepair = readCounts();
|
|
assert.strictEqual(afterLocalRepair.child, beforeLocalRepair.child + 1);
|
|
assert.strictEqual(afterLocalRepair.target, beforeLocalRepair.target, "status-only repair must not rerun an unchanged descendant");
|
|
} finally {
|
|
fs.rmSync(susRoot, { recursive: true, force: true });
|
|
}
|
|
|
|
const artifact = await optimus.buildArtifact("beta-demo");
|
|
assert.deepStrictEqual(artifact.sourceHash, { alpha: optimus.loadModule("alpha").loadAlpha("demo").hash });
|
|
assert.strictEqual(artifact.payload, "beta:alpha:demo");
|
|
|
|
const all = await optimus.buildAll();
|
|
assert.strictEqual(all, undefined);
|
|
|
|
// setArtifactOutcome is a retained reporting compatibility API. It affects
|
|
// only buildArtifact() console status and never artifact validity.
|
|
const outcomeRoot = fs.mkdtempSync(path.join(os.tmpdir(), "optimus-outcome-"));
|
|
try {
|
|
fs.mkdirSync(path.join(outcomeRoot, "settings"), { recursive: true });
|
|
fs.mkdirSync(path.join(outcomeRoot, "src", "reporter"), { recursive: true });
|
|
fs.writeFileSync(path.join(outcomeRoot, "settings", "modules.json"), JSON.stringify({ reporter: "./src/reporter/index.js" }));
|
|
fs.writeFileSync(path.join(outcomeRoot, "settings", "artifacts.json"), JSON.stringify({ report: { module: "reporter", builder: "buildReport" } }));
|
|
fs.writeFileSync(path.join(outcomeRoot, "settings", "presentation.json"), JSON.stringify({ showOnEnsure: true, showOnBuild: false }));
|
|
fs.writeFileSync(path.join(outcomeRoot, "src", "reporter", "index.js"), `module.exports = utils => ({
|
|
buildReport() {
|
|
utils.setArtifactOutcome("up to date");
|
|
return utils.buildNode("report");
|
|
}
|
|
});\n`);
|
|
|
|
const reporting = initialize(outcomeRoot);
|
|
assert.strictEqual(typeof reporting.utils.setArtifactOutcome, "function");
|
|
const messages = [];
|
|
const originalLog = console.log;
|
|
console.log = (...args) => messages.push(args.join(" "));
|
|
try {
|
|
await reporting.buildArtifact("report");
|
|
} finally {
|
|
console.log = originalLog;
|
|
}
|
|
assert.ok(messages.some(message => message === "Ensuring artifact: report"));
|
|
assert.ok(messages.some(message => message.trim() === "up to date"));
|
|
} finally {
|
|
fs.rmSync(outcomeRoot, { recursive: true, force: true });
|
|
}
|
|
|
|
|
|
// Presentation defaults are quiet on ensure and visible on actual build.
|
|
// The settings are reporting only and do not affect artifact semantics.
|
|
const presentationRoot = fs.mkdtempSync(path.join(os.tmpdir(), "optimus-presentation-"));
|
|
try {
|
|
fs.mkdirSync(path.join(presentationRoot, "settings"), { recursive: true });
|
|
fs.mkdirSync(path.join(presentationRoot, "src", "worker"), { recursive: true });
|
|
fs.writeFileSync(path.join(presentationRoot, "settings", "modules.json"), JSON.stringify({ worker: "./src/worker/index.js" }));
|
|
fs.writeFileSync(path.join(presentationRoot, "settings", "artifacts.json"), JSON.stringify({ demo: { module: "worker", builder: "buildDemo" } }));
|
|
fs.writeFileSync(path.join(presentationRoot, "src", "worker", "index.js"), `const path = require("path");
|
|
module.exports = utils => ({
|
|
buildDemo() {
|
|
return utils.resolveArtifact(path.join(${JSON.stringify(presentationRoot)}, "artifacts", "worker", "demo.json"), {}, async () => "demo");
|
|
}
|
|
});\n`);
|
|
|
|
const messages = [];
|
|
const originalLog = console.log;
|
|
console.log = (...args) => messages.push(args.join(" "));
|
|
try {
|
|
const runtime = initialize(presentationRoot);
|
|
await runtime.buildArtifact("demo");
|
|
assert.ok(messages.some(message => message === "Building artifact: artifacts/worker/demo.json"));
|
|
assert.ok(!messages.some(message => message.startsWith("Ensuring artifact:")));
|
|
|
|
messages.length = 0;
|
|
await runtime.buildArtifact("demo");
|
|
assert.ok(!messages.some(message => message.startsWith("Building artifact:")));
|
|
assert.ok(!messages.some(message => message.startsWith("Ensuring artifact:")));
|
|
} finally {
|
|
console.log = originalLog;
|
|
}
|
|
} finally {
|
|
fs.rmSync(presentationRoot, { recursive: true, force: true });
|
|
}
|
|
|
|
console.log("smoke test passed");
|
|
}
|
|
|
|
main().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|