Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/test/create-agent-steps.test.mjs
iris 5de0ff59f4 swarm-ui: mount the create-agent form only while its dialog is open, and list the internet setting
The dialog rendered its form even when closed, so every visit to the agents page
or the start page fetched the hive, permission and archetype lists, a half-filled
form was kept, and the hint to reopen the form for a fresh archetype list was
wrong. It now mounts per open. The summary lists the unrestricted internet box
like every other setting, and create another clears it with the rest. The
swarm-ui doc says both.
2026-11-25 19:49:31 +01:00

122 lines
4.3 KiB
JavaScript

// The create-agent dialog's steps: order, what gates next, and the summary.
// Run by `npm test` with Node's type stripping.
import assert from "node:assert/strict";
import { test } from "node:test";
import {
lastReachableStep,
nameProblem,
nextStep,
previousStep,
STEPS,
stepProblem,
summaryRows,
} from "../src/pages/CreateAgentSteps.ts";
const ready = {
name: "ruth",
hive: "h1",
permissionsReady: true,
inferenceProblem: null,
};
test("the steps run in order and stop at both ends", () => {
assert.deepEqual(STEPS, [
"identity",
"instructions",
"permissions",
"resources",
"summary",
]);
assert.equal(nextStep("identity"), "instructions");
assert.equal(nextStep("resources"), "summary");
assert.equal(nextStep("summary"), "summary");
assert.equal(previousStep("summary"), "resources");
assert.equal(previousStep("identity"), "identity");
});
test("a name is 1-63 lowercase letters, digits and hyphens", () => {
assert.equal(nameProblem("ruth-2"), null);
assert.equal(nameProblem("a".repeat(63)), null);
assert.notEqual(nameProblem(""), null);
assert.notEqual(nameProblem("a".repeat(64)), null);
assert.notEqual(nameProblem("Ruth"), null);
assert.notEqual(nameProblem("ru th"), null);
// The reason names the cause, uppercase first.
assert.match(nameProblem("Ruth"), /uppercase/);
assert.match(nameProblem("ru th"), /lowercase letters, digits and hyphens/);
assert.match(nameProblem("a".repeat(64)), /63/);
});
test("identity needs a valid name and a hive before next", () => {
assert.equal(stepProblem("identity", ready), null);
assert.notEqual(stepProblem("identity", { ...ready, name: "" }), null);
assert.notEqual(stepProblem("identity", { ...ready, name: "No Way" }), null);
assert.equal(stepProblem("identity", { ...ready, hive: "" }), "pick a hive");
});
test("permissions waits for the list, resources for a valid inference", () => {
assert.notEqual(
stepProblem("permissions", { ...ready, permissionsReady: false }),
null,
);
assert.equal(stepProblem("permissions", ready), null);
assert.equal(
stepProblem("resources", { ...ready, inferenceProblem: "bad model" }),
"bad model",
);
assert.equal(stepProblem("instructions", { ...ready, name: "" }), null);
});
test("the summary lists what is sent and what stays at its default", () => {
const rows = (over) =>
Object.fromEntries(
summaryRows({
name: "ruth",
hive: "h1",
archetype: undefined,
permissions: undefined,
caps: {},
inference: {},
agentsMd: "",
internet: false,
...over,
}).map((r) => [r.label, r.value]),
);
const plain = rows({});
assert.equal(plain.name, "ruth");
assert.equal(plain.archetype, "none: a custom agent");
assert.equal(plain.permissions, "the default set");
assert.equal(plain.internet, "only the allowed domains");
assert.equal(rows({ internet: true }).internet, "unrestricted");
assert.equal(plain["resource caps"], "none set");
assert.equal(plain.inference, "none set");
assert.equal(plain.instructions, "none");
const set = rows({
archetype: "reviewer",
permissions: ["meta", "inbox"],
caps: { cpu_quota: "200%", memory_max: "2G" },
inference: { model: "opus", subagent_models: ["haiku", "sonnet"] },
agentsMd: " Be brief.\n",
});
assert.equal(set.archetype, "reviewer");
assert.equal(set.permissions, "meta, inbox");
assert.equal(set["resource caps"], "cpu quota 200%; memory max 2G");
assert.equal(set.inference, "model opus; subagent models haiku, sonnet");
assert.equal(set.instructions, "AGENTS.md, 2 words");
assert.equal(rows({ permissions: [] }).permissions, "none");
assert.equal(rows({ agentsMd: "x" }).instructions, "AGENTS.md, 1 word");
});
test("the tabs go forward as far as the operator has been, not past a bad step", () => {
// On the summary (4) having been there: every step is open.
assert.equal(lastReachableStep(4, -1, 4), 4);
// Back on the identity step (0) after reaching the summary: still open to 4.
assert.equal(lastReachableStep(4, -1, 0), 4);
// The name went bad (step 0): only the current step is open.
assert.equal(lastReachableStep(4, 0, 0), 0);
// A bad permissions step (2) stops the tabs there.
assert.equal(lastReachableStep(4, 2, 0), 2);
// Never behind the current step.
assert.equal(lastReachableStep(1, 0, 3), 3);
});