mirror of
https://github.com/hypit-ai/hypit.git
synced 2026-11-20 20:40:29 +00:00
354 lines
13 KiB
TypeScript
354 lines
13 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { mkdtemp, readFile, realpath, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import test from "node:test";
|
|
|
|
import { FileBuildResultStore } from "../../../runtime-local/src/result-storage/index.js";
|
|
import { createProducerPackageFacet } from "@hypit/producer";
|
|
import { verifyRecordStructure } from "@hypit/kernel";
|
|
import type { LinkedProgram, TypedRecord } from "@hypit/protocol";
|
|
|
|
import { runCli as runCliEngine } from "../../src/main.js";
|
|
import { commandHint } from "../../src/command-hint.js";
|
|
import type { CliDistribution } from "../../src/distribution.js";
|
|
import type { CliDiagnostic as RuntimeDoctorDiagnostic } from "../../src/runtime-port.js";
|
|
import { cliDistribution } from "../distribution-scenario.js";
|
|
|
|
const io = { write: () => {} };
|
|
const runCli: typeof runCliEngine = async (argv, output, selected) => await runCliEngine(argv, output, selected, {
|
|
cwd: tmpdir(),
|
|
resolveProjectRoot: async (explicit) => await realpath(explicit ?? tmpdir()),
|
|
});
|
|
|
|
/**
|
|
* Enough of a Distribution to reach the build handler through an Author
|
|
* Source plus explicit targets.
|
|
*/
|
|
function distribution(
|
|
calls: string[],
|
|
diagnostics: readonly RuntimeDoctorDiagnostic[],
|
|
execution?: object,
|
|
producerError?: string,
|
|
producerOutput?: string,
|
|
producerInlineValueBytes?: number,
|
|
): CliDistribution {
|
|
return cliDistribution({
|
|
bootstrapPackages: producerError === undefined && producerOutput === undefined ? [] : [{
|
|
specifier: "@example/run-frontend",
|
|
contribution: {
|
|
format: "hypit.package@1",
|
|
facets: [createProducerPackageFacet([{
|
|
producer: { module: { name: "example.value", version: "1" }, name: "produce" },
|
|
handler() {
|
|
if (producerError !== undefined) throw new Error(producerError);
|
|
return { kind: "output", value: producerOutput! };
|
|
},
|
|
}])],
|
|
},
|
|
}],
|
|
createCompiler: () => ({
|
|
openEntry: async (path: string) => {
|
|
const source = async (name: string) => ({
|
|
unit: { id: name, name, bytes: new TextEncoder().encode(await readFile(name, "utf8")) },
|
|
frontend: "@hypit/markup@1",
|
|
});
|
|
return {
|
|
entry: await source(path),
|
|
attachments: async () => [],
|
|
};
|
|
},
|
|
supportsFrontend: (id: string) => id === "@hypit/markup@1",
|
|
compileResolvedSource: async (entry: { readonly unit: { readonly id: string } }) => ({
|
|
closure: { entry: entry.unit.id, units: [] },
|
|
program: { closure: { format: "hypit.closure@1", modules: [{
|
|
format: "hypit.module@1",
|
|
name: "example.value",
|
|
version: "1",
|
|
dependencies: [],
|
|
types: ["Value"],
|
|
capabilities: [],
|
|
producers: [{
|
|
name: "produce", inputs: [], result: { kind: "output" as const, name: "result", type: { module: { name: "example.value", version: "1" }, name: "Value" } },
|
|
}],
|
|
}] }, records: [] },
|
|
graph: {
|
|
format: "hypit.graph@1",
|
|
outputs: [{
|
|
id: "result",
|
|
type: { module: { name: "example.value", version: "1" }, name: "Value" },
|
|
result: { kind: "operation-result", operation: "produce-result" },
|
|
}],
|
|
operations: [{
|
|
id: "produce-result",
|
|
producer: { module: { name: "example.value", version: "1" }, name: "produce" },
|
|
inputs: {}, result: { kind: "output" as const, name: "result", record: "record-result" },
|
|
}],
|
|
},
|
|
exports: [{
|
|
name: "result",
|
|
type: { module: { name: "example.value", version: "1" }, name: "Value" },
|
|
ref: { kind: "logical-output", id: "result" },
|
|
}],
|
|
retainedOutputs: [],
|
|
attachments: [],
|
|
}),
|
|
async refineRecord(program: LinkedProgram, record: TypedRecord) {
|
|
verifyRecordStructure(program.closure, record);
|
|
},
|
|
}),
|
|
openProjectResults: async (projectRoot: string) => ({
|
|
location: {
|
|
root: projectRoot,
|
|
selection: { use: "test.results", config: {} },
|
|
},
|
|
repository: new FileBuildResultStore(join(projectRoot, ".hypit", "results")),
|
|
close() {},
|
|
}),
|
|
diagnoseProjectResults: async () => ({ diagnostics: [] }),
|
|
openRuntimeHost: async (path: string) => ({
|
|
profile: path,
|
|
...(producerInlineValueBytes === undefined ? {} : {
|
|
planningPolicy: async () => ({
|
|
producerExecutionLimits: { maxInlineValueBytes: producerInlineValueBytes },
|
|
}),
|
|
}),
|
|
ensureExecution: async () => ({ state: "running" as const }),
|
|
executionStatus: async () => ({ state: "running" as const }),
|
|
resolvePaths: async () => ({}),
|
|
controller: async () => ({
|
|
profile: path,
|
|
dataRoot: "/tmp",
|
|
worker: {
|
|
up: async () => ({ state: "stopped", profile: path, logPath: "/tmp/worker.log" }),
|
|
status: async () => ({ state: "stopped", profile: path, logPath: "/tmp/worker.log" }),
|
|
logs: async () => ({ path: "/tmp/worker.log", text: "" }),
|
|
down: async () => ({ state: "stopped", profile: path, logPath: "/tmp/worker.log" }),
|
|
},
|
|
programs: {
|
|
up: async () => {
|
|
calls.push(`up ${path}`);
|
|
return { dataRoot: "/tmp", programs: [] };
|
|
},
|
|
down: async () => ({ dataRoot: "/tmp", programs: [] }),
|
|
report: async () => ({ dataRoot: "/tmp", programs: [] }),
|
|
},
|
|
}),
|
|
preflight: async () => ({ dataRoot: "/tmp", diagnostics }),
|
|
doctor: async () => ({ dataRoot: "/tmp", diagnostics: [] }),
|
|
openInspection: async () => ({
|
|
dataRoot: "/tmp",
|
|
endpoints: async () => [],
|
|
pricing: async () => [],
|
|
preflight: async () => ({ dataRoot: "/tmp", diagnostics }),
|
|
executionStatus: async () => ({ state: "running" as const }),
|
|
ensureExecution: async () => ({ state: "running" as const }),
|
|
createRuntime: async () => {
|
|
if (execution !== undefined) return execution;
|
|
throw new Error("createRuntime is unavailable");
|
|
},
|
|
close() {},
|
|
}),
|
|
endpoints: async () => [],
|
|
pricing: async () => [],
|
|
invoke: async () => { throw new Error("creation-time invocation is not part of this test"); },
|
|
createRuntime: async () => {
|
|
if (execution !== undefined) return execution;
|
|
throw new Error("createRuntime is unavailable");
|
|
},
|
|
openControl: async () => ({ inspect: async () => undefined }),
|
|
}),
|
|
});
|
|
}
|
|
|
|
async function authorSource(): Promise<string> {
|
|
const root = await mkdtemp(join(tmpdir(), "hypit-build-programs-"));
|
|
const path = join(root, "main.svml");
|
|
await writeFile(path, "author", "utf8");
|
|
return path;
|
|
}
|
|
|
|
test("plan accepts an Author Source with explicit targets", async () => {
|
|
const source = await authorSource();
|
|
let output = "";
|
|
await runCli(
|
|
["plan", source, "--target", "result", "--json"],
|
|
{ write(text) { output += text; } },
|
|
distribution([], []),
|
|
);
|
|
const plan = JSON.parse(output) as { readonly source: string; readonly targets: readonly string[] };
|
|
assert.equal(plan.source, source);
|
|
assert.deepEqual(plan.targets, ["result"]);
|
|
});
|
|
|
|
test("Build fails its cheap preflight before submitting or starting programs", async () => {
|
|
const calls: string[] = [];
|
|
const source = await authorSource();
|
|
await assert.rejects(
|
|
async () => await runCli(
|
|
["build", source, "--target", "result", "--runtime", "/p/hypit.runtime.json"],
|
|
io,
|
|
distribution(calls, [{
|
|
severity: "error",
|
|
code: "MANAGED_PROGRAM_DOWN",
|
|
subject: "whisperx",
|
|
message: "whisperx is not usable",
|
|
}]),
|
|
),
|
|
/Runtime preflight failed/u,
|
|
);
|
|
assert.deepEqual(calls, []);
|
|
});
|
|
|
|
test("Build never provisions programs after a clean preflight", async () => {
|
|
const calls: string[] = [];
|
|
const source = await authorSource();
|
|
await assert.rejects(
|
|
async () => await runCli(
|
|
["build", source, "--target", "result", "--runtime", "/p/hypit.runtime.json"],
|
|
io,
|
|
distribution(calls, []),
|
|
),
|
|
/createRuntime is unavailable/u,
|
|
);
|
|
assert.deepEqual(calls, []);
|
|
});
|
|
|
|
test("Build confirms durable submission before following stable work progress", async () => {
|
|
const calls: string[] = [];
|
|
const source = await authorSource();
|
|
const execution = {
|
|
async build(request: { readonly id: string }) {
|
|
calls.push("build");
|
|
return {
|
|
id: request.id,
|
|
state: {
|
|
targets: ["result"],
|
|
plan: { format: "hypit.plan@1", steps: [], goals: [] },
|
|
records: [],
|
|
},
|
|
view: {
|
|
id: request.id,
|
|
activity: "ready",
|
|
|
|
targets: ["result"],
|
|
requests: { total: 0, completed: 0 },
|
|
operations: [],
|
|
},
|
|
};
|
|
},
|
|
async close() {},
|
|
};
|
|
let output = "";
|
|
|
|
await runCli(
|
|
["build", source, "--target", "result", "--runtime", "/p/hypit.runtime.json", "--follow", "--max-wait-ms", "0"],
|
|
{ write(text) { output += text; } },
|
|
distribution(calls, [], execution),
|
|
);
|
|
|
|
assert.deepEqual(calls, ["build"]);
|
|
const submitted = output.indexOf("Build submitted");
|
|
const working = output.indexOf("· Working");
|
|
const active = output.indexOf("Build still active");
|
|
assert.ok(submitted >= 0 && working > submitted && active > working);
|
|
assert.match(output, /Target\s+result/u);
|
|
assert.match(output, /Work\s+0 requests/u);
|
|
assert.match(output, /Ctrl-C stops watching; the Build continues\./u);
|
|
|
|
output = "";
|
|
let progress = "";
|
|
const projectRoot = await realpath(dirname(source));
|
|
const runtimeProfile = resolve("/p/a selected runtime.json");
|
|
await runCli([
|
|
"build", source, "--target", "result", "--project", projectRoot, "--runtime", runtimeProfile,
|
|
"--follow", "--max-wait-ms", "0", "--json",
|
|
], {
|
|
write(text) { output += text; }, writeProgress(text) { progress += text; },
|
|
}, distribution(calls, [], execution));
|
|
const build = JSON.parse(output).build;
|
|
assert.equal(build.work.state, "working");
|
|
assert.match(progress, /Working/u);
|
|
|
|
output = "";
|
|
await runCli([
|
|
"build", source, "--target", "result", "--project", projectRoot, "--runtime", runtimeProfile,
|
|
], { write(text) { output += text; } }, distribution(calls, [], execution));
|
|
const id = /Watch\s+hypit status (\S+)/u.exec(output)?.[1];
|
|
assert.ok(id);
|
|
assert.ok(output.includes(commandHint(["status", id, "--watch"], { projectRoot, runtimeProfile })));
|
|
assert.ok(output.includes(commandHint(["cancel", id], { projectRoot, runtimeProfile })));
|
|
});
|
|
|
|
test("plan preserves the selected work summary but exits non-zero when cheap preflight fails", async () => {
|
|
const source = await authorSource();
|
|
let output = "";
|
|
let exitCode: number | undefined;
|
|
await runCli(
|
|
["plan", source, "--target", "result", "--runtime", "/p/hypit.runtime.json", "--json"],
|
|
{
|
|
write(text) { output += text; },
|
|
setExitCode(code) { exitCode = code; },
|
|
},
|
|
distribution([], [{
|
|
severity: "error",
|
|
code: "RUNTIME_CREDENTIAL_MISSING",
|
|
message: "credential is absent",
|
|
}]),
|
|
);
|
|
const value = JSON.parse(output) as {
|
|
readonly ok: boolean;
|
|
readonly preflight: { readonly ok: boolean };
|
|
};
|
|
assert.equal(value.ok, false);
|
|
assert.equal(value.preflight.ok, false);
|
|
assert.equal("steps" in value, false);
|
|
assert.equal(exitCode, 1);
|
|
});
|
|
|
|
test("pricing reports a Producer failure with no external Need and exits non-zero in human and JSON modes", async () => {
|
|
const source = await authorSource();
|
|
for (const json of [false, true]) {
|
|
for (const producerError of [undefined, "Timeline end must be after its start"]) {
|
|
let output = "";
|
|
let exitCode = 0;
|
|
await runCli([
|
|
"pricing", source, "--target", "result", "--runtime", "/p/hypit.runtime.json", "--limit", "1",
|
|
...(json ? ["--json"] : []),
|
|
], {
|
|
write(text) { output += text; },
|
|
setExitCode(code) { exitCode = code; },
|
|
}, distribution([], [], undefined, producerError));
|
|
assert.equal(exitCode, producerError === undefined ? 0 : 1);
|
|
if (producerError !== undefined) assert.ok(output.includes(producerError));
|
|
if (json) {
|
|
const value = JSON.parse(output);
|
|
assert.equal(value.requestCount, 0);
|
|
assert.deepEqual(value.groups, []);
|
|
assert.equal(value.producerFailureCount, producerError === undefined ? 0 : 1);
|
|
assert.equal(value.producerFailures.length, value.producerFailureCount);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
test("plan uses the selected Runtime Producer policy before opening execution", async () => {
|
|
const source = await authorSource();
|
|
let output = "";
|
|
let exitCode = 0;
|
|
await runCli([
|
|
"plan", source, "--target", "result", "--runtime", "/p/hypit.runtime.json", "--json",
|
|
], {
|
|
write(text) { output += text; },
|
|
setExitCode(code) { exitCode = code; },
|
|
}, distribution([], [], undefined, undefined, "a value that is too large", 16));
|
|
|
|
const value = JSON.parse(output) as {
|
|
readonly producerFailureCount: number;
|
|
readonly producerFailures: readonly string[];
|
|
};
|
|
assert.equal(exitCode, 1);
|
|
assert.equal(value.producerFailureCount, 1);
|
|
assert.match(value.producerFailures[0] ?? "", /inline Value bytes; limit is 16 bytes/u);
|
|
});
|