// The command menu's matching, completion and keys. Run by `npm test`, which // imports the `.ts` module as it is. import assert from "node:assert/strict"; import { test } from "node:test"; import { completion, controlRoute, enterSends, matchCommands, menuQuery, moveSelection, splitName, textToSend, } from "../src/agent/terminal/command-menu.ts"; const cmd = (name, kind = "command", description = "") => ({ name, kind, description, }); test("the menu is wanted only while the text is one slash word", () => { assert.equal(menuQuery("/"), ""); assert.equal(menuQuery("/comp"), "comp"); assert.equal(menuQuery("/Base:PR"), "base:pr"); assert.equal(menuQuery("hello /comp"), null); assert.equal(menuQuery("/compact now"), null); assert.equal(menuQuery("/compact "), null); assert.equal(menuQuery("//x"), null); assert.equal(menuQuery(""), null); }); test("prefix matches come first, then contains, then description", () => { const all = [ cmd("recompact"), cmd("compact"), cmd("other", "command", "will compact the thing"), cmd("combine"), ]; assert.deepEqual( matchCommands(all, "com").map((c) => c.name), ["combine", "compact", "recompact", "other"], ); }); test("commands sort before skills within a rank, then by name", () => { const all = [ cmd("zeta", "command"), cmd("alpha", "skill"), cmd("beta", "command"), ]; assert.deepEqual( matchCommands(all, "").map((c) => c.name), ["beta", "zeta", "alpha"], ); }); test("nothing matches a query nothing contains", () => { assert.deepEqual(matchCommands([cmd("compact")], "zzz"), []); }); test("taking a command leaves the command and a space", () => { assert.equal(completion(cmd("compact")), "/compact "); assert.equal(completion(cmd("base:pr-review", "skill")), "/base:pr-review "); }); test("the name splits around the matched part, case-insensitively", () => { assert.deepEqual(splitName("compact", "mpa"), { before: "co", hit: "mpa", after: "ct", }); assert.deepEqual(splitName("Compact", "COM"), { before: "", hit: "Com", after: "pact", }); assert.deepEqual(splitName("compact", ""), { before: "compact", hit: "", after: "", }); assert.deepEqual(splitName("compact", "x"), { before: "compact", hit: "", after: "", }); }); test("the arrow keys wrap around the list", () => { assert.equal(moveSelection(0, 3, "ArrowDown"), 1); assert.equal(moveSelection(2, 3, "ArrowDown"), 0); assert.equal(moveSelection(0, 3, "ArrowUp"), 2); assert.equal(moveSelection(1, 3, "ArrowUp"), 0); assert.equal(moveSelection(0, 0, "ArrowDown"), 0); }); test("Enter sends once the selected command is already typed in full", () => { assert.equal(enterSends("/compact", cmd("compact")), true); assert.equal(enterSends("/comp", cmd("compact")), false); assert.equal(enterSends("/", cmd("compact")), false); }); test("exactly /clear, with the backend's command of that name, is a new session", () => { const list = [cmd("compact"), cmd("clear")]; assert.equal(controlRoute("/clear", list), "new-session"); assert.equal(controlRoute("/clear ", list), "new-session"); assert.equal(controlRoute("/clear it", list), null); assert.equal(controlRoute("/compact", list), null); assert.equal(controlRoute("clear", list), null); }); test("without the backend's /clear command, or with a skill of that name, it is text", () => { assert.equal(controlRoute("/clear", []), null); assert.equal(controlRoute("/clear", [cmd("clear", "skill")]), null); }); test("a lone slash word goes without the space completion left", () => { assert.equal(textToSend("/compact "), "/compact"); assert.equal(textToSend("/compact"), "/compact"); assert.equal(textToSend(" /base:pr-review "), "/base:pr-review"); }); test("any other message goes as it is", () => { assert.equal(textToSend("hello "), "hello "); assert.equal(textToSend("/compact now "), "/compact now "); assert.equal(textToSend("fix /etc/hosts"), "fix /etc/hosts"); assert.equal(textToSend(" "), " "); });