Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 captureConfigBinding,
4 editCaptureConfig,
5 niriConfigConflict,
6 niriConfigIncludes,
7} from "./captureConfigEdit.ts";
8import { readKdlNodes } from "./captureConfigKdl.ts";
9
10const app = "com.t3tools.T3Code";
11const binding = captureConfigBinding("niri", app, "Ctrl+Shift+2");
12
13describe("Niri capture config edits", () => {
14 it("inserts into the existing top-level binds, preserving unrelated text", () => {
15 const before =
16 '// My desktop\ninput { keyboard { xkb { layout "us"; }; }; }\nbinds {\n Mod+Q { spawn "kitty"; } // keep me\n}\n';
17 const result = editCaptureConfig(before, "niri", app, "install");
18 expect(result.after).toBe(before.replace("\n}\n", `\n ${binding}\n}\n`));
19 expect(readKdlNodes(result.after).filter((node) => node.name === "binds")).toHaveLength(1);
20 });
21 it("ignores commented, nested, and string-valued binds and braces", () => {
22 const before = `// binds { fake\n/* binds { /* nested */ } */\n/- binds { Ctrl+Shift+2 { quit; } }\nrecent-windows { binds { Alt+Tab { next-window; } }; }\nspawn-at-startup "echo" r#"binds { }"#\n"binds" {\n Mod+Q { spawn "a } \\" b"; }\n}\n`;
23 const valid = before.replace('"a } \\" b"', '"a } b"');
24 const result = editCaptureConfig(valid, "niri", app, "install");
25 expect(result.after).toContain(` ${binding}\n}\n`);
26 expect(result.after.startsWith(valid.slice(0, valid.indexOf('"binds" {')))).toBe(true);
27 });
28 it.each(["binds {}", "binds { Mod+Q { quit; }; }", "input { }\n", ""])(
29 "handles compact or absent binds: %s",
30 (before) => {
31 const result = editCaptureConfig(before, "niri", app, "install");
32 const block = readKdlNodes(result.after).find((node) => node.name === "binds")!;
33 expect(block.children.some((node) => node.name === "Ctrl+Shift+2")).toBe(true);
34 },
35 );
36 it("preserves CRLF and the UTF-8 BOM", () => {
37 const before = "\uFEFFbinds {\r\n}\r\n";
38 expect(editCaptureConfig(before, "niri", app, "install").after).toBe(
39 `\uFEFFbinds {\r\n ${binding}\r\n}\r\n`,
40 );
41 });
42 it("does not duplicate an existing binding, including a custom chord", () => {
43 const before = `binds {\n ${binding.replace("Ctrl+Shift+2", "Alt+Ctrl+Y")}\n}\n`;
44 const result = editCaptureConfig(before, "niri", app, "install");
45 expect(result.after).toBe(before);
46 expect(result.shortcut).toBe("Ctrl+Alt+Y");
47 });
48 it("replaces and removes only this app's capture binding", () => {
49 const other = captureConfigBinding("niri", "com.t3tools.Other", "Ctrl+Alt+4");
50 const before = `binds {\n ${binding}\n ${other}\n}\n`;
51 const result = editCaptureConfig(before, "niri", app, "install", "Ctrl+Alt+Y");
52 expect(result.after).toContain(other);
53 expect(result.after).not.toContain("Ctrl+Shift+2");
54 expect(result.after).toContain("Ctrl+Alt+Y");
55 const removed = editCaptureConfig(result.after, "niri", app, "remove");
56 expect(removed.after).toBe(`binds {\n ${other}\n}\n`);
57 });
58 it.each([
59 ["; ", ""],
60 [" /* first \\\nsecond */; ", "/* first \\\nsecond */"],
61 ])("removes or rebinds a compact binding with its semicolon: %j", (separator, kept) => {
62 const before = `binds { ${binding}${separator}Mod+Q { close-window; }; }\n`;
63 const rest = `binds { ${kept} Mod+Q { close-window; }; }\n`;
64 const rebound = captureConfigBinding("niri", app, "Ctrl+Alt+Y");
65 expect.soft(editCaptureConfig(before, "niri", app, "remove").after).toBe(rest);
66 expect
67 .soft(editCaptureConfig(before, "niri", app, "install", "Ctrl+Alt+Y").after)
68 .toBe(rest.replace(/}\n$/, `\n ${rebound}\n}\n`));
69 });
70 it("removes a semicolon-terminated binding line", () => {
71 const before = `binds {\n ${binding};\n Mod+Q { quit; }\n}\n`;
72 expect(editCaptureConfig(before, "niri", app, "remove").after).toBe(
73 "binds {\n Mod+Q { quit; }\n}\n",
74 );
75 });
76 it("refuses occupied keys regardless of modifier order, without removing them", () => {
77 const before = "binds { Shift+Control+2 { quit; } }";
78 expect(niriConfigConflict(before, app, "Ctrl+Shift+2")).toBe(true);
79 expect(() => editCaptureConfig(before, "niri", app, "install")).toThrow("already used");
80 });
81 it.each(["binds {", "binds {}\nbinds {}", "/* unfinished", 'spawn "unfinished'])(
82 "fails safely for malformed structure: %s",
83 (source) => {
84 expect(() => editCaptureConfig(source, "niri", app, "install")).toThrow();
85 },
86 );
87 it("reads only real top-level include directives", () => {
88 expect(
89 niriConfigIncludes(
90 '// include "no"\n/- include "no"\ninclude "keys.kdl"\ninclude optional=true "extra.kdl"',
91 ),
92 ).toEqual([
93 { path: "keys.kdl", optional: false },
94 { path: "extra.kdl", optional: true },
95 ]);
96 });
97});
98
99describe("Hyprland capture config edits", () => {
100 it.each(["hyprland", "hyprland-lua"] as const)(
101 "is idempotent and reversible for %s",
102 (format) => {
103 const before =
104 format === "hyprland"
105 ? "# Keep my preferences\n$mainMod = SUPER\n"
106 : "-- Keep my preferences\nhl.config({ general = { border_size = 2 } })\n";
107 const added = editCaptureConfig(before, format, app, "install");
108 expect(added.after.startsWith(before)).toBe(true);
109 expect(editCaptureConfig(added.after, format, app, "install").after).toBe(added.after);
110 const changed = editCaptureConfig(added.after, format, app, "install", "Ctrl+Alt+Y");
111 expect(changed.after).not.toContain(captureConfigBinding(format, app, "Ctrl+Shift+2"));
112 expect(changed.after).toContain(captureConfigBinding(format, app, "Ctrl+Alt+Y"));
113 expect(editCaptureConfig(changed.after, format, app, "remove").after.trim()).toBe(
114 before.trim(),
115 );
116 },
117 );
118 it("does not steal an existing legacy shortcut", () => {
119 expect(() =>
120 editCaptureConfig("bind = SHIFT CTRL, 2, exec, kitty\n", "hyprland", app, "install"),
121 ).toThrow("already used");
122 });
123 it.each([
124 "return {}",
125 "local M = {}\n return M",
126 '--[[\nhl.bind("CTRL + SHIFT + 2", hl.dsp.global("com.t3tools.T3Code:capture-window"))\n]]',
127 ])("does not guess how to edit complex Lua", (source) => {
128 expect(() => editCaptureConfig(source, "hyprland-lua", app, "install")).toThrow("manual edit");
129 });
130 it("edits Lua that returns early inside a function", () => {
131 const before =
132 'local function sync(window)\n if window == nil then\n return\n end\n hl.unbind("SUPER + Q")\nend\nhl.on("window.active", sync)\n';
133 const added = editCaptureConfig(before, "hyprland-lua", app, "install");
134 expect(added.after).toBe(
135 `${before}\n${captureConfigBinding("hyprland-lua", app, "Ctrl+Shift+2")}\n`,
136 );
137 expect(editCaptureConfig(added.after, "hyprland-lua", app, "remove").after.trim()).toBe(
138 before.trim(),
139 );
140 });
141 it.each(['Ctrl+"\nexec', "Ctrl+;", "2", "Mod+2"])(
142 "rejects unsafe or ambiguous keys: %s",
143 (keys) => {
144 expect(() => editCaptureConfig("", "hyprland", app, "install", keys)).toThrow();
145 },
146 );
147});