// The named SSH connections of an agent card: URLs, the save body and the // replace check. Run by `npm test`. import assert from "node:assert/strict"; import { test } from "node:test"; import { connectionBody, connectionUrl, connectionsUrl, parsePort, replaces, sortedConnections, } from "../src/agent/detail/ssh-connections.ts"; const conn = (name, host = "box", port = 22, user = "root") => ({ name, user, host, port, }); test("the routes carry the agent and the connection name", () => { assert.equal(connectionsUrl("iris"), "/api/agents/iris/ssh-connections"); assert.equal( connectionUrl("iris", "k8s-lab-vm"), "/api/agents/iris/ssh-connections/k8s-lab-vm", ); }); test("a name is escaped in the URL", () => { assert.equal( connectionUrl("iris", "a/b c"), "/api/agents/iris/ssh-connections/a%2Fb%20c", ); }); test("the table lists connections by name", () => { const list = [conn("zeta"), conn("alpha"), conn("mid")]; assert.deepEqual( sortedConnections(list).map((c) => c.name), ["alpha", "mid", "zeta"], ); assert.deepEqual( list.map((c) => c.name), ["zeta", "alpha", "mid"], "the list given is not reordered", ); }); test("a save replaces a connection with the same name, ignoring spaces typed", () => { const list = [conn("lab"), conn("prod")]; assert.equal(replaces(list, "lab"), true); assert.equal(replaces(list, " lab "), true); assert.equal(replaces(list, "other"), false); assert.equal(replaces([], "lab"), false); }); test("a port is a whole number or nothing", () => { assert.equal(parsePort("2222"), 2222); assert.equal(parsePort(" 22 "), 22); assert.equal(parsePort(""), null); assert.equal(parsePort("22a"), null); assert.equal(parsePort("-1"), null); assert.equal(parsePort("2.5"), null); }); test("the body trims the fields and needs all of them", () => { assert.deepEqual(connectionBody(" root ", " muede-pc2 ", "2222"), { user: "root", host: "muede-pc2", port: 2222, }); assert.equal(connectionBody("", "host", "22"), null); assert.equal(connectionBody("root", " ", "22"), null); assert.equal(connectionBody("root", "host", "ssh"), null); });