Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/test/account-api.test.mjs
iris da25f0a1c3 swarm ui: an agent's API key is set from its claude login row, and the advanced backend settings show the configuration and the secrets apart
The claude login row of an opened agent gains an API key line: a badge saying whether a key is stored, set or replace and remove, and advanced. Setting a key sends PUT on the backend-secrets route as ANTHROPIC_API_KEY=<key>; the key is never read back. The old API-key environment dialog becomes the advanced backend settings, with two sections: the configuration, which starts with the stored text and saves or removes it, and the secrets, whose box starts empty and replaces the stored secrets only when filled and saved. The link menu's entry is renamed to match, account-api.ts gets the route helpers and a key check with tests, and the swarm UI doc describes the row.
2026-11-20 21:23:05 +01:00

100 lines
3.3 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,
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);
}
});