// 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 { 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")); });