Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/test/account-api.test.mjs
iris c7e559a09e swarm-ui: lock the backend configuration box when its stored text could not be read
Only a 404 means nothing is stored. Any other failed read used to leave an empty,
editable box with save enabled, so saving one line replaced a configuration that
was never read. The box now stays locked behind an error with a retry button.
The decision is a small tested function, and the swarm-ui doc describes it.
2026-11-25 20:47:35 +01:00

109 lines
3.7 KiB
JavaScript

// The link / list / delete requests for forge and gitlab accounts, and the
// client-side check before a link is sent. Run by `npm test` with Node's
// type stripping.
import assert from "node:assert/strict";
import { test } from "node:test";
import {
apiKeySecrets,
backendEnvUrl,
backendSecretsUrl,
checkApiKey,
checkLink,
configReadKind,
deleteUrl,
linkBody,
linkUrl,
listUrl,
} from "../src/agent/detail/account-api.ts";
test("a gitlab link PUTs to gitlab-accounts, a forge link to forge-accounts", () => {
assert.equal(
linkUrl("h1", "atlas", "gitlab", "gitlab-com"),
"/api/hives/h1/agents/atlas/gitlab-accounts/gitlab-com",
);
assert.equal(
linkUrl("h1", "atlas", "forge", "myforge"),
"/api/hives/h1/agents/atlas/forge-accounts/myforge",
);
});
test("path segments are encoded", () => {
assert.equal(
linkUrl("a/b", "x y", "gitlab", "l?"),
"/api/hives/a%2Fb/agents/x%20y/gitlab-accounts/l%3F",
);
});
test("the body carries the trimmed url and the token untouched", () => {
assert.deepEqual(JSON.parse(linkBody(" https://gitlab.com ", " tok en ")), {
url: "https://gitlab.com",
token: " tok en ",
});
});
test("list and delete hit the linked-accounts and gitlab routes", () => {
assert.equal(
listUrl("h1", "atlas"),
"/api/hives/h1/agents/atlas/linked-accounts",
);
assert.equal(
deleteUrl("h1", "atlas", { kind: "gitlab", name: "gitlab-com" }),
"/api/hives/h1/agents/atlas/gitlab-accounts/gitlab-com",
);
// Control: the other kinds keep their routes.
assert.equal(
deleteUrl("h1", "atlas", { kind: "forgejo", name: "f" }),
"/api/hives/h1/agents/atlas/forge-accounts/f",
);
assert.equal(
deleteUrl("h1", "atlas", { kind: "github", name: "x" }),
"/api/hives/h1/agents/atlas/github-account",
);
});
test("checkLink accepts a good input and names what is wrong otherwise", () => {
assert.equal(checkLink("gitlab-com", "https://gitlab.com", "t"), null);
assert.equal(checkLink("a", " http://gl.lan:8443/x ", "t"), null);
assert.match(checkLink("Bad", "https://gitlab.com", "t"), /label/);
assert.match(checkLink("", "https://gitlab.com", "t"), /label/);
assert.match(checkLink("a".repeat(64), "https://gitlab.com", "t"), /label/);
assert.match(checkLink("a", "gitlab.com", "t"), /URL/);
assert.match(checkLink("a", "ftp://gitlab.com", "t"), /URL/);
assert.match(checkLink("a", "https://gitlab.com", ""), /token/);
});
test("a message never contains the token", () => {
const msg = checkLink("Bad", "nope", "s3cr3t-value");
assert.ok(!String(msg).includes("s3cr3t-value"));
});
test("the backend routes sit under the agent and name the config and the secrets", () => {
assert.equal(
backendEnvUrl("pr1ma", "atlas"),
"/api/hives/pr1ma/agents/atlas/backend-env",
);
assert.equal(
backendSecretsUrl("pr1ma", "atlas"),
"/api/hives/pr1ma/agents/atlas/backend-secrets",
);
});
test("an API key becomes one trimmed ANTHROPIC_API_KEY line", () => {
assert.equal(apiKeySecrets(" sk-ant-x \n"), "ANTHROPIC_API_KEY=sk-ant-x");
});
test("a key the controller would refuse is caught first, and a plain one is not", () => {
assert.equal(checkApiKey("sk-ant-api03-AbC_d-9"), null);
assert.equal(checkApiKey(" "), "key is required");
for (const bad of ["a b", 'a"b', "a'b", "a\\b"]) {
assert.notEqual(checkApiKey(bad), null, bad);
}
});
test("only a 404 means no configuration is stored; any other failure locks the box", () => {
assert.equal(configReadKind(200, true), "stored");
assert.equal(configReadKind(404, false), "none");
assert.equal(configReadKind(500, false), "failed");
assert.equal(configReadKind(403, false), "failed");
assert.equal(configReadKind(0, false), "failed");
});