// Copyright (C) 2026 Danilo M. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Run: node desktop/modules/autostart/test_autostart.js // Autostart.js is a QML library, so its .pragma line is stripped before node // evaluates it. const fs = require("fs"); const assert = require("assert"); const src = fs.readFileSync(__dirname + "/Autostart.js", "utf8") .replace(/^\.pragma library$/m, ""); const A = new Function(src + "\nreturn { parse, toggle, remove, add, split, workspace, compose, program, fromExec };")(); const file = [ 'hl.on("hyprland.start", function()', ' hl.exec_cmd("playerctld daemon")', ' -- hl.exec_cmd("[workspace special silent] kitty btop")', ' hl.exec_cmd("app >> \\"$XDG_RUNTIME_DIR/x.log\\" 2>&1")', ' hl.on("other", function() end)', 'end)', '', ].join("\n"); const e = A.parse(file); assert.deepStrictEqual(e.map(x => [x.index, x.cmd, x.enabled]), [ [1, "playerctld daemon", true], [2, "[workspace special silent] kitty btop", false], [3, 'app >> "$XDG_RUNTIME_DIR/x.log" 2>&1', true], ]); // Toggle both ways; untouched lines survive byte for byte. const off = A.toggle(file, e[0]); assert.strictEqual(off.split("\n")[1], ' -- hl.exec_cmd("playerctld daemon")'); assert.strictEqual(A.toggle(off, A.parse(off)[0]), file); assert.strictEqual(A.toggle(file, e[1]).split("\n")[2], ' hl.exec_cmd("[workspace special silent] kitty btop")'); // Quotes round-trip through unquote and quote. const q = A.toggle(file, e[2]); assert.strictEqual(A.toggle(q, A.parse(q)[2]), file); assert.strictEqual(A.remove(file, e[1]).split("\n").length, file.split("\n").length - 1); assert.ok(!A.remove(file, e[1]).includes("btop")); // Added before the hook's own `end)`, not the nested one, quotes escaped. const added = A.add(file, 'say "hi"').split("\n"); assert.strictEqual(added[5], ' hl.exec_cmd("say \\"hi\\"")'); assert.strictEqual(added[6], "end)"); assert.strictEqual(A.add("nothing here", "x"), null); // A stale entry, its line changed since parse, refuses rather than edits. assert.strictEqual(A.toggle(off, e[0]), null); assert.strictEqual(A.remove(A.remove(file, e[0]), e[1]), null); // The rule prefix, and the workspace form the page edits. assert.deepStrictEqual(A.split("[workspace special:dash silent] conky -c x"), { rule: "workspace special:dash silent", body: "conky -c x" }); assert.deepStrictEqual(A.split("conky"), { rule: "", body: "conky" }); assert.deepStrictEqual(A.workspace("workspace special:dash silent"), { ws: "special:dash", silent: true }); assert.deepStrictEqual(A.workspace("workspace 3"), { ws: "3", silent: false }); assert.deepStrictEqual(A.workspace(""), { ws: "", silent: false }); assert.strictEqual(A.workspace("float; size 50% 50%"), null); assert.strictEqual(A.compose("special", true, "btop"), "[workspace special silent] btop"); assert.strictEqual(A.compose("", true, "btop"), "btop"); // The program behind each live shape of command. const H = "/home/you"; assert.strictEqual(A.program("playerctld daemon", H), "playerctld"); assert.strictEqual(A.program("[workspace special silent] kitty --class k btop", H), "kitty"); assert.strictEqual(A.program("~/bin/cal-notif daemon >> \"$X/c.log\" 2>&1", H), "/home/you/bin/cal-notif"); assert.strictEqual(A.program("bash -c 'eval $(gnome-keyring-daemon --start) && dbus-update'", H), "eval"); assert.strictEqual(A.program("bash -c 'echo \"$D\" > /run/x'", H), "echo"); assert.strictEqual(A.program("FOO=1 env BAR=2 exec /usr/libexec/agent", H), "/usr/libexec/agent"); assert.strictEqual(A.program("", H), ""); assert.strictEqual(A.fromExec("firefox %u"), "firefox"); assert.strictEqual(A.fromExec("app --name=%%x %F --flag"), "app --name=%x --flag"); console.log("test_autostart: ok");