mirror of
https://github.com/hypit-ai/hypit.git
synced 2026-11-19 20:30:29 +00:00
351 lines
14 KiB
TypeScript
351 lines
14 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 { FileBuildResultRepository } from "@hypit/result/node";
|
|
import { createInvocationFrontendFacet } from "@hypit/invocation";
|
|
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 either an
|
|
* Author Source plus explicit Invocation or a saved Invocation preset.
|
|
*/
|
|
function distribution(
|
|
calls: string[],
|
|
diagnostics: readonly RuntimeDoctorDiagnostic[],
|
|
authorSource = "./main.svml",
|
|
execution?: object,
|
|
producerError?: string,
|
|
): CliDistribution {
|
|
return cliDistribution({
|
|
bootstrapPackages: [{
|
|
specifier: "@example/run-frontend",
|
|
contribution: {
|
|
format: "hypit.package@1",
|
|
facets: [createInvocationFrontendFacet({
|
|
id: "@hypit/markup/invocation@1",
|
|
discover: () => ({ author: { source: authorSource }, imports: [] }),
|
|
decode: () => ({
|
|
format: "hypit.invocation-source@1",
|
|
author: { source: authorSource },
|
|
imports: [],
|
|
targets: ["result"],
|
|
overrides: [],
|
|
}),
|
|
}), ...(producerError === undefined ? [] : [createProducerPackageFacet([{
|
|
producer: { module: { name: "example.value", version: "1" }, name: "produce" },
|
|
handler() { throw new Error(producerError); },
|
|
}])])],
|
|
},
|
|
}],
|
|
createCompiler: () => ({
|
|
openEntry: async (path: string) => {
|
|
const source = async (name: string) => ({
|
|
unit: { id: name, name, bytes: new TextEncoder().encode(await readFile(name, "utf8")) },
|
|
frontend: name.endsWith(".svrun") ? "@hypit/markup/invocation@1" : "@hypit/markup@1",
|
|
});
|
|
return {
|
|
entry: await source(path),
|
|
resolveSource: async (_importer: unknown, request: { readonly from: string }) =>
|
|
await source(join(path, "..", request.from)),
|
|
attachments: async () => [],
|
|
};
|
|
},
|
|
supportsFrontend: (id: string) => id === "@hypit/markup@1",
|
|
compileResolvedSource: async (entry: { readonly unit: { readonly id: string } }) => ({
|
|
closure: { entry: entry.unit.id, units: [] },
|
|
provenance: { format: "hypit.author-provenance@1", elements: [] },
|
|
realizations: [],
|
|
program: { closure: { format: "hypit.closure@1", modules: [{
|
|
format: "hypit.module@1",
|
|
name: "example.value",
|
|
version: "1",
|
|
dependencies: [],
|
|
types: ["Value"],
|
|
capabilities: [],
|
|
producers: producerError === undefined ? [] : [{
|
|
name: "produce", inputs: [], result: { kind: "output" as const, name: "result", type: { module: { name: "example.value", version: "1" }, name: "Value" } },
|
|
}],
|
|
}] }, records: producerError === undefined ? [{
|
|
id: "record-result",
|
|
type: { module: { name: "example.value", version: "1" }, name: "Value" },
|
|
value: "ready",
|
|
}] : [] },
|
|
graph: {
|
|
format: "hypit.graph@1",
|
|
outputs: [{
|
|
id: "result",
|
|
type: { module: { name: "example.value", version: "1" }, name: "Value" },
|
|
primary: producerError === undefined
|
|
? { kind: "record", id: "record-result" }
|
|
: { kind: "operation-result", operation: "produce-result" },
|
|
}],
|
|
operations: producerError === undefined ? [] : [{
|
|
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" },
|
|
}],
|
|
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 FileBuildResultRepository(join(projectRoot, ".hypit", "results")),
|
|
close() {},
|
|
}),
|
|
diagnoseProjectResults: async () => ({ diagnostics: [] }),
|
|
openRuntimeHost: async (path: string) => ({
|
|
profile: path,
|
|
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 invocationSource(): Promise<string> {
|
|
const root = await mkdtemp(join(tmpdir(), "hypit-build-programs-"));
|
|
const path = join(root, "build.svrun");
|
|
await writeFile(path, '<?svml using="@hypit/markup/invocation@1"?>\n<svrun/>\n', "utf8");
|
|
await writeFile(join(root, "main.svml"), "author", "utf8");
|
|
return path;
|
|
}
|
|
|
|
test("plan accepts an Author Source with explicit targets and no saved Invocation preset", async () => {
|
|
const preset = await invocationSource();
|
|
const source = await realpath(join(dirname(preset), "main.svml"));
|
|
let output = "";
|
|
await runCli(
|
|
["plan", source, "--project", dirname(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, "main.svml");
|
|
assert.deepEqual(plan.targets, ["result"]);
|
|
});
|
|
|
|
test("Build fails its cheap preflight before submitting or starting programs", async () => {
|
|
const calls: string[] = [];
|
|
const source = await invocationSource();
|
|
await assert.rejects(
|
|
async () => await runCli(
|
|
["build", source, "--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 accepts a Result title and never provisions programs after a clean preflight", async () => {
|
|
const calls: string[] = [];
|
|
const source = await invocationSource();
|
|
await assert.rejects(
|
|
async () => await runCli(
|
|
["build", source, "--title", "first-cut", "--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 invocationSource();
|
|
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,
|
|
createdAt: Date.now(),
|
|
activity: "ready",
|
|
|
|
targets: ["result"],
|
|
requests: { total: 0, completed: 0 },
|
|
acceptedRecords: 0,
|
|
outstandingCommands: 0,
|
|
operations: [],
|
|
},
|
|
};
|
|
},
|
|
async close() {},
|
|
};
|
|
let output = "";
|
|
|
|
await runCli(
|
|
["build", source, "--runtime", "/p/hypit.runtime.json", "--follow", "--max-wait-ms", "0"],
|
|
{ write(text) { output += text; } },
|
|
distribution(calls, [], "./main.svml", 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, "--project", projectRoot, "--runtime", runtimeProfile,
|
|
"--follow", "--max-wait-ms", "0", "--json",
|
|
], {
|
|
write(text) { output += text; }, writeProgress(text) { progress += text; },
|
|
}, distribution(calls, [], "./main.svml", execution));
|
|
const build = JSON.parse(output).build;
|
|
assert.equal(build.work.state, "working");
|
|
assert.match(progress, /Working/u);
|
|
|
|
output = "";
|
|
await runCli([
|
|
"build", source, "--project", projectRoot, "--runtime", runtimeProfile,
|
|
], { write(text) { output += text; } }, distribution(calls, [], "./main.svml", 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 invocationSource();
|
|
let output = "";
|
|
let exitCode: number | undefined;
|
|
await runCli(
|
|
["plan", source, "--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 steps: number;
|
|
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 invocationSource();
|
|
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, "--runtime", "/p/hypit.runtime.json", "--limit", "1",
|
|
...(json ? ["--json"] : []),
|
|
], {
|
|
write(text) { output += text; },
|
|
setExitCode(code) { exitCode = code; },
|
|
}, distribution([], [], "./main.svml", 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);
|
|
}
|
|
}
|
|
}
|
|
});
|