Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/test/agent-permissions.test.mjs
iris c3e24fe2c6 swarm ui: ask before actions that cannot be undone, with a danger button and a useConfirm hook
Button gains a danger variant and an autofocus prop; ConfirmDialog gains danger (red confirm, focus on cancel). Destroy, delete linked account, delete claude login, remove schedule and delete flow use it. New useConfirm hook asks from inside a handler (await confirm({...})) without a dialog's state at the call site. It now guards: removing a swarm-wide egress domain, removing a domain for every agent from an agent's popover, removing a stored API-key environment, firing a schedule now, moving an agent's internet to a looser mode, the /clear message, and reload over unsaved instructions (replacing window.confirm). The review try-out's button is now called push a branch for a PR, since it pushes a branch and does not open one, and asks first. loosensInternet is a tested helper.
2026-11-20 02:13:23 +01:00

122 lines
4.4 KiB
JavaScript

// An opened agent card's permissions editor: its starting selection and the
// summary line. Run by `npm test` with Node's type stripping.
import assert from "node:assert/strict";
import { test } from "node:test";
import {
editorSelection,
grantSummary,
internetBody,
internetMode,
internetSummary,
loosensInternet,
MAX_REPOS,
maxReposBody,
parseMaxRepos,
reposSummary,
} from "../src/agent/detail/permissions.ts";
const all = ["messaging", "meta", "inbox", "execution", "manage_root_agent"];
const defaults = ["messaging", "meta", "inbox", "execution"];
test("an agent the swarm sets nothing for starts on the default set", () => {
assert.deepEqual(editorSelection(undefined, all, defaults), defaults);
});
test("a stored list is the starting selection, empty included", () => {
assert.deepEqual(
editorSelection(["meta", "manage_root_agent"], all, defaults),
["meta", "manage_root_agent"],
);
assert.deepEqual(editorSelection([], all, defaults), []);
});
test("a stored name the catalogue does not list is not offered", () => {
assert.deepEqual(editorSelection(["meta", "retired"], all, defaults), [
"meta",
]);
});
test("the summary tells unset, empty and a list apart", () => {
assert.equal(grantSummary(undefined), "the default set");
assert.equal(grantSummary([]), "none");
assert.equal(grantSummary(["meta", "inbox"]), "2 granted");
});
test("an absent internet flag reads as unrestricted, marked legacy", () => {
assert.equal(internetSummary(undefined), "unrestricted (legacy)");
assert.equal(internetSummary(true), "unrestricted");
assert.equal(internetSummary(false), "restricted");
});
test("the internet toggle sends the stored list with the flag", () => {
assert.deepEqual(internetBody(["meta", "inbox"], "restricted"), {
permissions: ["meta", "inbox"],
unrestricted_internet: false,
});
assert.deepEqual(internetBody([], "unrestricted"), {
permissions: [],
unrestricted_internet: true,
});
});
test("with no stored list the toggle leaves the list unset", () => {
assert.deepEqual(internetBody(undefined, "restricted"), {
unrestricted_internet: false,
});
});
test("log is internet_log beside an explicit false, and nothing else reads as it", () => {
assert.equal(internetMode(false, true), "log");
assert.equal(internetMode(false, undefined), "restricted");
assert.equal(internetMode(false, false), "restricted");
assert.equal(internetMode(true, true), "unrestricted");
assert.equal(internetMode(undefined, true), "unrestricted");
assert.match(internetSummary(false, true), /^log /);
assert.equal(internetSummary(false), "restricted");
});
test("the log mode keeps the old flag false and names internet_log; the others leave it out", () => {
assert.deepEqual(internetBody(["meta"], "log"), {
permissions: ["meta"],
unrestricted_internet: false,
internet_log: true,
});
assert.equal("internet_log" in internetBody([], "restricted"), false);
assert.equal("internet_log" in internetBody([], "unrestricted"), false);
});
test("a whole number from 0 to the most is a repository limit", () => {
assert.equal(parseMaxRepos("0"), 0);
assert.equal(parseMaxRepos(" 7 "), 7);
assert.equal(parseMaxRepos(String(MAX_REPOS)), MAX_REPOS);
});
test("anything else is not: empty, negative, fractional, above the most, text", () => {
for (const t of ["", " ", "-1", "1.5", "1e2", String(MAX_REPOS + 1), "x"]) {
assert.equal(parseMaxRepos(t), null, JSON.stringify(t));
}
});
test("the repositories badge tells none from a limit", () => {
assert.equal(reposSummary(undefined), "none");
assert.equal(reposSummary(0), "none");
assert.equal(reposSummary(3), "up to 3");
});
test("the limit's body carries the stored list, so the PUT does not reset it", () => {
assert.deepEqual(maxReposBody(["meta"], 3), {
permissions: ["meta"],
max_repos: 3,
});
assert.deepEqual(maxReposBody(undefined, 0), { max_repos: 0 });
});
test("only a change that lets more traffic through is asked first", () => {
assert.equal(loosensInternet("restricted", "log"), true);
assert.equal(loosensInternet("restricted", "unrestricted"), true);
assert.equal(loosensInternet("log", "unrestricted"), true);
assert.equal(loosensInternet("unrestricted", "restricted"), false);
assert.equal(loosensInternet("unrestricted", "log"), false);
assert.equal(loosensInternet("log", "restricted"), false);
assert.equal(loosensInternet("log", "log"), false);
});