40 lines
2.4 KiB
TypeScript
40 lines
2.4 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { directoryCommand, fileCommand, parseTasks, quoteShell, remember, terminalShortcut } from "../plugins/terminal/model.ts";
|
|
|
|
test("paths are shell-quoted without evaluating metacharacters", () => {
|
|
assert.equal(directoryCommand("C:\\a'b\\$notes", "powershell"), "Set-Location -LiteralPath 'C:\\a''b\\$notes'");
|
|
assert.equal(quoteShell("/tmp/it's $(bad)", "bash"), "'/tmp/it'\\''s $(bad)'");
|
|
assert.equal(directoryCommand("C:\\two words", "cmd"), 'cd /d "C:\\two words"');
|
|
assert.equal(directoryCommand("C:\\two words", "node"), 'process.chdir("C:\\\\two words")');
|
|
assert.throws(() => quoteShell("C:\\%PATH%", "cmd"));
|
|
assert.throws(() => fileCommand("a\nwhoami.py", "powershell"));
|
|
});
|
|
|
|
test("run file selects a runner and rejects non-executable documents", () => {
|
|
assert.equal(fileCommand("D:\\project\\hello world.py", "powershell"), "python 'D:\\project\\hello world.py'");
|
|
assert.equal(fileCommand("/project/app.mjs", "bash"), "node '/project/app.mjs'");
|
|
assert.throws(() => fileCommand("notes.md", "powershell"));
|
|
assert.throws(() => fileCommand("app.js", "node"));
|
|
});
|
|
|
|
test("task config validates every entry before offering execution", () => {
|
|
assert.deepEqual(parseTasks('{"tasks":[{"label":"Test","command":"npm test","profile":"pwsh"}]}'), [{ label: "Test", command: "npm test", profile: "pwsh" }]);
|
|
for (const text of ['{}', '{"tasks":[null]}', '{"tasks":[{"label":"x","command":" "}]}', '{"tasks":[{"label":"x","command":"echo hi","profile":4}]}']) assert.throws(() => parseTasks(text));
|
|
});
|
|
|
|
test("history deduplicates most recent entries and is bounded", () => {
|
|
assert.deepEqual(remember(["one", "two"], "two"), ["two", "one"]);
|
|
assert.equal(remember(Array.from({ length: 100 }, (_, i) => String(i)), "new").length, 100);
|
|
assert.deepEqual(remember(["one"], " "), ["one"]);
|
|
});
|
|
|
|
test("physical key shortcuts distinguish new, split, window, and hide", () => {
|
|
const key = { ctrlKey: true, metaKey: false, altKey: false, shiftKey: true, code: "Backquote", key: "~" };
|
|
assert.equal(terminalShortcut(key), "new");
|
|
assert.equal(terminalShortcut({ ...key, altKey: true }), "window");
|
|
assert.equal(terminalShortcut({ ...key, code: "Digit5", key: "%" }), "split");
|
|
assert.equal(terminalShortcut({ ...key, shiftKey: false, code: "KeyJ", key: "j" }), "toggle");
|
|
assert.equal(terminalShortcut({ ...key, shiftKey: false, code: "KeyC", key: "c" }), null);
|
|
});
|