返回 CodeWhale
registry.test.mjs
根目录 / crates / tui / plugins / computer-use / tests / registry.test.mjs
1 // Registry tests: registration validation, switching, removal, persistence.
2 import { test, beforeEach } from "node:test";
3 import assert from "node:assert/strict";
4 import fs from "node:fs";
5 import os from "node:os";
6 import path from "node:path";
7
8 const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "cu-reg-test-"));
9 process.env.CODEWHALE_CU_STATE_DIR = tmp;
10
11 const registry = await import("../src/registry.mjs");
12
13 beforeEach(() => {
14 fs.rmSync(tmp, { recursive: true, force: true });
15 fs.mkdirSync(tmp, { recursive: true });
16 });
17
18 test("local computer is always registered and active by default", () => {
19 const reg = registry.list();
20 assert.equal(reg.active, "local");
21 assert.equal(reg.computers.local.transport, "local");
22 assert.equal(reg.computers.local.platform, process.platform);
23 });
24
25 test("register accepts valid ssh and hdc computers and persists them", () => {
26 registry.register({ id: "winbox", transport: "ssh", host: "winbox.lan", user: "me", port: 2222 });
27 registry.register({ id: "pad", transport: "hdc", target: "ABC123" });
28 const reg = registry.list();
29 assert.equal(reg.computers.winbox.host, "winbox.lan");
30 assert.equal(reg.computers.pad.platform, "harmonyos");
31 // persisted across a fresh load
32 const again = registry.load();
33 assert.ok(again.computers.winbox && again.computers.pad);
34 });
35
36 test("register rejects invalid ids, hosts, ports, transports", () => {
37 assert.throws(() => registry.register({ id: "bad id!", transport: "ssh", host: "h" }), (e) => e.code === "invalid_id");
38 assert.throws(() => registry.register({ id: "x", transport: "carrier-pigeon" }), (e) => e.code === "invalid_transport");
39 assert.throws(() => registry.register({ id: "x", transport: "ssh", host: "bad host;rm -rf" }), (e) => e.code === "invalid_host");
40 assert.throws(() => registry.register({ id: "x", transport: "ssh", host: "h", port: 99_999 }), (e) => e.code === "invalid_port");
41 assert.throws(() => registry.register({ id: "local", transport: "ssh", host: "h" }), (e) => e.code === "reserved_id");
42 });
43
44 test("switchTo validates and persists the active computer", () => {
45 registry.register({ id: "pad", transport: "hdc" });
46 registry.switchTo("pad");
47 assert.equal(registry.active().id, "pad");
48 assert.throws(() => registry.switchTo("nope"), (e) => e.code === "unknown_computer");
49 });
50
51 test("remove falls back to local when removing the active computer", () => {
52 registry.register({ id: "pad", transport: "hdc" });
53 registry.switchTo("pad");
54 const res = registry.remove("pad");
55 assert.equal(res.active, "local");
56 assert.throws(() => registry.remove("local"), (e) => e.code === "reserved_id");
57 assert.throws(() => registry.remove("pad"), (e) => e.code === "unknown_computer");
58 });
59
59 lines Plain Text