mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-29 14:45:24 +00:00
feat: move snippets, keybinding actions and macros out of core into plugins
This commit is contained in:
1 parent
d9bba10612
commit
e0c30a4e6f
262 files changed
+9091
-10744
No files matched your search
@@ -98,6 +98,28 @@ describe("snippets activate", () => {
|
||||
expect(after).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("registers its host settings hooks and the variables helper", async () => {
|
||||
server = await startServer();
|
||||
const registry = server.mock.ctx.registry;
|
||||
for (const key of [
|
||||
"snippets.hostImportNormalizer",
|
||||
"snippets.hostPayloadLegacy",
|
||||
"snippets.hostSettingsSync",
|
||||
]) {
|
||||
expect(registry.consume(key), key).toBeDefined();
|
||||
}
|
||||
const variables = registry.consume<{
|
||||
resolve: (c: string, h: { ip?: string } | null) => string;
|
||||
extractInputs: (c: string) => Array<{ key: string }>;
|
||||
}>("snippets.variables");
|
||||
expect(variables?.resolve("ping $HOST", { ip: "10.0.0.1" })).toBe(
|
||||
"ping 10.0.0.1",
|
||||
);
|
||||
expect(variables?.extractInputs("echo $INPUT_1")).toEqual([
|
||||
{ key: "INPUT_1", label: "Input 1" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("fails closed without db:own", async () => {
|
||||
db = await createTestDb(pluginDir);
|
||||
const mock = createMockCtx({
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
createHostSettingsSync,
|
||||
hostImportNormalizer,
|
||||
hostPayloadLegacy,
|
||||
} from "../../src/backend/host-import.js";
|
||||
|
||||
describe("hostImportNormalizer", () => {
|
||||
it("reads a 2.8 export's quick actions and startup snippet", () => {
|
||||
expect(
|
||||
hostImportNormalizer({
|
||||
quickActions: [
|
||||
{ name: "Restart", snippetId: "3" },
|
||||
{ name: "Broken", snippetId: "" },
|
||||
],
|
||||
terminalConfig: JSON.stringify({ startupSnippetId: 5, fontSize: 12 }),
|
||||
}),
|
||||
).toEqual({
|
||||
quickActions: [{ name: "Restart", snippetId: 3 }],
|
||||
startupSnippetId: 5,
|
||||
});
|
||||
});
|
||||
|
||||
it("leaves an export that carries this plugin's settings to core", () => {
|
||||
expect(
|
||||
hostImportNormalizer({
|
||||
pluginSettings: { snippets: { startupSnippetId: 1 } },
|
||||
terminalConfig: { startupSnippetId: 5 },
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("writes nothing for a host without either", () => {
|
||||
expect(hostImportNormalizer({ name: "web" })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("hostPayloadLegacy", () => {
|
||||
it("puts the 2.8 fields back for older clients", () => {
|
||||
expect(
|
||||
hostPayloadLegacy({
|
||||
quickActions: [{ name: "Up", snippetId: 2 }],
|
||||
startupSnippetId: 7,
|
||||
}),
|
||||
).toEqual({
|
||||
quickActions: [{ name: "Up", snippetId: 2 }],
|
||||
terminalConfig: { startupSnippetId: 7 },
|
||||
});
|
||||
expect(hostPayloadLegacy({})).toEqual({ quickActions: [] });
|
||||
});
|
||||
});
|
||||
|
||||
describe("hostSettingsSync", () => {
|
||||
const sync = createHostSettingsSync({
|
||||
findSyncIdById: async (id) => (id === 2 ? "sync-2" : null),
|
||||
findIdBySyncId: async (syncId) => (syncId === "sync-2" ? 9 : null),
|
||||
});
|
||||
|
||||
it("sends sync ids and drops what does not map", async () => {
|
||||
expect(await sync.exportValue("startupSnippetId", 2)).toBe("sync-2");
|
||||
expect(await sync.exportValue("startupSnippetId", 4)).toBeNull();
|
||||
expect(
|
||||
await sync.exportValue("quickActions", [
|
||||
{ name: "A", snippetId: 2 },
|
||||
{ name: "B", snippetId: 4 },
|
||||
]),
|
||||
).toEqual([{ name: "A", snippetId: "sync-2" }]);
|
||||
});
|
||||
|
||||
it("maps sync ids back to this side's rows", async () => {
|
||||
expect(await sync.importValue("startupSnippetId", "sync-2")).toBe(9);
|
||||
expect(await sync.importValue("startupSnippetId", "missing")).toBeNull();
|
||||
expect(
|
||||
await sync.importValue("quickActions", [
|
||||
{ name: "A", snippetId: "sync-2" },
|
||||
]),
|
||||
).toEqual([{ name: "A", snippetId: 9 }]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
extractSnippetInputs,
|
||||
hasSnippetInputs,
|
||||
resolveSnippetContent,
|
||||
} from "../../src/shared/variables.js";
|
||||
|
||||
describe("extractSnippetInputs", () => {
|
||||
it("extracts a single unlabeled input", () => {
|
||||
expect(extractSnippetInputs("ping $INPUT_1")).toEqual([
|
||||
{ key: "INPUT_1", label: "Input 1" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("extracts a labeled input using brace syntax", () => {
|
||||
expect(
|
||||
extractSnippetInputs("nc -zv $HOST ${INPUT_1:Port to check}"),
|
||||
).toEqual([{ key: "INPUT_1", label: "Port to check" }]);
|
||||
});
|
||||
|
||||
it("plain (non-braced) references have no label", () => {
|
||||
expect(extractSnippetInputs("echo $INPUT_2")).toEqual([
|
||||
{ key: "INPUT_2", label: "Input 2" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("de-duplicates repeated references and preserves first-seen order", () => {
|
||||
expect(
|
||||
extractSnippetInputs("$INPUT_2 then $INPUT_1 then $INPUT_2 again"),
|
||||
).toEqual([
|
||||
{ key: "INPUT_2", label: "Input 2" },
|
||||
{ key: "INPUT_1", label: "Input 1" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns an empty array when there are no placeholders", () => {
|
||||
expect(extractSnippetInputs("uptime")).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("hasSnippetInputs", () => {
|
||||
it("returns true when a snippet has an input placeholder", () => {
|
||||
expect(hasSnippetInputs("echo $INPUT_1")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when a snippet has no input placeholder", () => {
|
||||
expect(hasSnippetInputs("echo $HOST")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveSnippetContent", () => {
|
||||
const host = { ip: "10.0.0.5", username: "root", port: 22, name: "web-01" };
|
||||
|
||||
it("substitutes host variables", () => {
|
||||
expect(
|
||||
resolveSnippetContent("ssh $USER@$HOST -p $PORT # $NAME", host),
|
||||
).toBe("ssh root@10.0.0.5 -p 22 # web-01");
|
||||
});
|
||||
|
||||
it("supports brace syntax for host variables", () => {
|
||||
expect(resolveSnippetContent("ping ${HOST}", host)).toBe("ping 10.0.0.5");
|
||||
});
|
||||
|
||||
it("substitutes input placeholders", () => {
|
||||
expect(
|
||||
resolveSnippetContent("nc -zv $HOST ${INPUT_1:Port}", host, {
|
||||
INPUT_1: "8080",
|
||||
}),
|
||||
).toBe("nc -zv 10.0.0.5 8080");
|
||||
});
|
||||
|
||||
it("leaves host variables literal when no host context is given", () => {
|
||||
expect(resolveSnippetContent("ping $HOST", null)).toBe("ping $HOST");
|
||||
});
|
||||
|
||||
it("leaves input placeholders literal when no value was supplied", () => {
|
||||
expect(resolveSnippetContent("echo $INPUT_1", null)).toBe("echo $INPUT_1");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user