Files
Netcatty/electron/capabilities/adapters/cliAdapter.test.cjs
T
fc55e9ac17 feat(vault): let Skills + CLI operate notes with the same approval as MCP (#3512)
* feat(vault): let Skills + CLI operate notes with the same approval as MCP

Expose vault note list, read, write, and markdown import on the tool CLI so agents can save generated documents only after the user approves.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): keep explicit empty note fields in the CLI

A supplied empty string was treated as an omitted flag, so note clears and empty markdown imports never reached the note service.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): allow an explicit empty note body on CLI create

notes create --content "" was rejected as a missing flag, so it did not match vault_notes_create.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): stop echoing imported note bodies and recognize leading CLI flags

A batch import resent every document over IPC, and a leading --json hid the notes command from artifact inference.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): make note CLI import safe and fail writes cleanly

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-09-29 12:22:35 +08:00

176 lines
6.0 KiB
JavaScript

"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const {
getCliRpcMethod,
listCliCapabilities,
buildCatalogCliParams,
} = require("./cliAdapter.cjs");
const { CAPABILITY_STATUS } = require("../constants.cjs");
function fakeCreateError(code, message) {
const err = new Error(message);
err.code = code;
return err;
}
test("getCliRpcMethod resolves implemented cli commands to rpc methods", () => {
assert.equal(getCliRpcMethod(["exec"]), "netcatty/exec");
assert.equal(getCliRpcMethod(["attachment", "read"]), "netcatty/readAttachment");
assert.equal(getCliRpcMethod(["sftp", "list"]), "netcatty/sftp/list");
assert.equal(getCliRpcMethod(["vault", "host", "get"]), "vault/host/get");
assert.equal(getCliRpcMethod(["portforward", "rules", "list"]), "portforward/rules/list");
assert.equal(getCliRpcMethod(["capabilities"]), null);
});
test("listCliCapabilities returns implemented commands by default", () => {
const entries = listCliCapabilities();
assert.ok(entries.some((entry) => entry.id === "terminal.execute"));
assert.ok(entries.some((entry) => entry.id === "attachment.list"));
assert.ok(entries.some((entry) => entry.id === "attachment.read"));
assert.ok(entries.some((entry) => entry.id === "vault.host.get"));
assert.ok(entries.every((entry) => entry.status === CAPABILITY_STATUS.IMPLEMENTED));
assert.ok(entries.every((entry) => entry.rpcMethod));
});
test("listCliCapabilities can include planned commands", () => {
const entries = listCliCapabilities({ status: CAPABILITY_STATUS.PLANNED });
assert.ok(entries.length >= 0);
});
test("buildCatalogCliParams maps vault host get flags", () => {
const params = buildCatalogCliParams("vault.host.get", { hostId: "host-1" }, fakeCreateError);
assert.deepEqual(params, { hostId: "host-1" });
});
test("buildCatalogCliParams maps attachment filename", () => {
const params = buildCatalogCliParams(
"attachment.read",
{ filename: "hosts.csv" },
fakeCreateError,
);
assert.deepEqual(params, { filename: "hosts.csv" });
});
test("buildCatalogCliParams parses snippet variables JSON", () => {
const params = buildCatalogCliParams("vault.snippets.run", {
snippetId: "snip-1",
sessionId: "sess-1",
variables: "{\"name\":\"prod\"}",
}, fakeCreateError);
assert.equal(params.snippetId, "snip-1");
assert.equal(params.sessionId, "sess-1");
assert.deepEqual(params.variables, { name: "prod" });
});
test("buildCatalogCliParams maps snippet multi-line run mode", () => {
const params = buildCatalogCliParams("vault.snippets.create", {
label: "login",
content: "user\npass",
multiLineRunMode: "lineDelay",
}, fakeCreateError);
assert.equal(params.multiLineRunMode, "lineDelay");
});
test("buildCatalogCliParams maps dynamic script group targets", () => {
const params = buildCatalogCliParams("vault.scripts.targets.set", {
scriptId: "script-1",
targetGroups: '["Production","Staging/Web"]',
}, fakeCreateError);
assert.equal(params.scriptId, "script-1");
assert.equal(params.targetGroups, '["Production","Staging/Web"]');
});
test("buildCatalogCliParams maps vault note create and import flags", () => {
const created = buildCatalogCliParams("vault.note.create", {
title: "Runbook",
content: "# Steps",
group: "ops",
}, fakeCreateError);
assert.equal(created.title, "Runbook");
assert.equal(created.content, "# Steps");
assert.equal(created.group, "ops");
const imported = buildCatalogCliParams("vault.note.import", {
fileName: "runbook.md",
content: "# Steps",
documents: "[{\"fileName\":\"a.md\",\"content\":\"# A\"}]",
}, fakeCreateError);
assert.equal(imported.fileName, "runbook.md");
assert.equal(imported.documents, "[{\"fileName\":\"a.md\",\"content\":\"# A\"}]");
});
test("buildCatalogCliParams keeps an explicit empty note create body", () => {
const created = buildCatalogCliParams("vault.note.create", {
title: "Empty",
content: "",
}, fakeCreateError);
assert.equal(created.title, "Empty");
assert.equal(created.content, "");
assert.throws(
() => buildCatalogCliParams("vault.note.create", { title: "Empty" }, fakeCreateError),
/Missing required --content for vault\.note\.create/,
);
assert.throws(
() => buildCatalogCliParams("vault.note.create", { title: "", content: "" }, fakeCreateError),
/Missing required --title for vault\.note\.create/,
);
});
test("buildCatalogCliParams keeps explicit empty note clears and empty imports", () => {
const cleared = buildCatalogCliParams("vault.note.update", {
noteId: "n1",
content: "",
group: "",
}, fakeCreateError);
assert.equal(cleared.noteId, "n1");
assert.equal(cleared.content, "");
assert.equal(cleared.group, "");
assert.equal("title" in cleared, false);
const imported = buildCatalogCliParams("vault.note.import", {
fileName: "empty.md",
content: "",
}, fakeCreateError);
assert.equal(imported.fileName, "empty.md");
assert.equal(imported.content, "");
});
test("buildCatalogCliParams still omits absent note fields and other empty optionals", () => {
const updated = buildCatalogCliParams("vault.note.update", {
noteId: "n1",
}, fakeCreateError);
assert.deepEqual(updated, { noteId: "n1" });
const searched = buildCatalogCliParams("vault.note.get", {
noteId: "note-1",
query: "",
}, fakeCreateError);
assert.deepEqual(searched, { noteId: "note-1" });
});
test("buildCatalogCliParams maps vault note read continuation flags", () => {
const params = buildCatalogCliParams("vault.note.get", {
noteId: "note-1",
offset: "6000",
maxChars: "2000",
expectedUpdatedAt: "10",
query: "Steps",
}, fakeCreateError);
assert.equal(params.noteId, "note-1");
assert.equal(params.offset, 6000);
assert.equal(params.maxChars, 2000);
assert.equal(params.expectedUpdatedAt, 10);
assert.equal(params.query, "Steps");
});
test("buildCatalogCliParams throws for missing required fields", () => {
assert.throws(
() => buildCatalogCliParams("vault.host.get", {}, fakeCreateError),
/Missing required --host-id/,
);
});