Files
my-pi/pi-ssh/test/agent-connection.test.ts
T

58 lines
2.9 KiB
TypeScript

import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { getConfiguredHost, parseConnectInput, SSH_CONNECT_TOOL_METADATA } from "../src/agent-connection.ts";
test("defines the reviewed agent-controlled SSH connection tool", () => {
assert.equal(SSH_CONNECT_TOOL_METADATA.name, "ssh_connect");
assert.equal(SSH_CONNECT_TOOL_METADATA.executionMode, "sequential");
assert.match(SSH_CONNECT_TOOL_METADATA.description, /separate step/);
assert.match(SSH_CONNECT_TOOL_METADATA.description, /wait for success/);
assert.deepEqual(SSH_CONNECT_TOOL_METADATA.parameters.required, ["hostId"]);
assert.ok(SSH_CONNECT_TOOL_METADATA.parameters.properties.remotePath);
assert.deepEqual(parseConnectInput({ hostId: " packaging-server " }), { hostId: "packaging-server" });
assert.deepEqual(parseConnectInput({ hostId: "packaging-server", remotePath: "~/api" }), {
hostId: "packaging-server",
remotePath: "~/api",
});
assert.throws(() => parseConnectInput({ hostId: "user@host" }), /invalid pi-ssh host id/);
assert.throws(() => parseConnectInput({ hostId: "packaging-server", remotePath: "relative" }), /remote path/);
});
test("unknown hosts report bounded imported host ID alternatives", () => {
const host = {
hostName: "example.test",
user: "builder",
port: 22,
auth: { type: "password" as const, password: "secret" },
hostKey: { algorithm: "ssh-ed25519", fingerprint: "SHA256:fixture" },
};
assert.equal(getConfiguredHost({ version: 1, hosts: { "packaging-server": host } }, "packaging-server"), host);
assert.throws(
() => getConfiguredHost({ version: 1, hosts: { "packaging-server": host, "build-server": host } }, "connect-packaging-server"),
/available imported host IDs: build-server, packaging-server/,
);
const manyHosts = Object.fromEntries(Array.from({ length: 12 }, (_, index) => [`host-${String(index).padStart(2, "0")}`, host]));
assert.throws(
() => getConfiguredHost({ version: 1, hosts: manyHosts }, "missing"),
(error: unknown) => {
assert.match(String(error), /host-00, host-01, host-02/);
assert.match(String(error), /… \(\+2 more\)/);
assert.doesNotMatch(String(error), /example\.test|secret/);
return true;
},
);
assert.throws(() => getConfiguredHost({ version: 1, hosts: {} }, "missing"), /no hosts are imported/);
});
test("removes manual and implicit SSH connection surfaces", async () => {
const source = await readFile(new URL("../index.ts", import.meta.url), "utf8");
assert.match(source, /\.\.\.SSH_CONNECT_TOOL_METADATA/);
assert.doesNotMatch(source, /registerCommand\(["']ssh["']/);
assert.doesNotMatch(source, /registerFlag\(["']ssh["']/);
assert.doesNotMatch(source, /getFlag\(["']ssh["']/);
assert.doesNotMatch(source, /appendEntry\(["']pi-ssh-config["']/);
assert.doesNotMatch(source, /pi\.on\(["']user_bash["']/);
});