repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
quickshell/nuguland/features/clock/caldav-cadence.test.mjs
Rawimport assert from "node:assert/strict";
import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { assertOffscreenPass, runOffscreen } from "../../test-fixtures/offscreen.mjs";
// The production CalDav cadence, offscreen with synthetic secret-tool and curl and a
// configured account: startup and a closed calendar fetch nothing however long they
// wait; opening (the registry's popupOpen and refresh) fetches at once and then on
// every refresh interval; closing stops fetching again. Load evidence, not pixels.
const directory = mkdtempSync(join(tmpdir(), "nuguland-caldav-cadence-"));
try {
const feature = join(directory, "features", "clock");
mkdirSync(feature, { recursive: true });
for (const file of ["CalDav.qml", "caldav.js"])
copyFileSync(new URL(file, import.meta.url), join(feature, file));
const bin = join(directory, "bin");
const log = join(directory, "calls.log");
const config = join(directory, "caldav.json");
mkdirSync(bin);
const programs = {
"secret-tool": "echo secret-tool >> \"$CADENCE_LOG\"\necho fixture-only",
curl: "echo curl >> \"$CADENCE_LOG\"\ncat >/dev/null\necho '<d:multistatus></d:multistatus>'",
};
for (const [name, body] of Object.entries(programs)) {
writeFileSync(join(bin, name), "#!/bin/sh\n" + body + "\n");
chmodSync(join(bin, name), 0o700);
}
writeFileSync(config, JSON.stringify({
url: "https://fixture.invalid/remote.php/dav",
username: "fixture-user",
calendars: [{ href: "/remote.php/dav/calendars/fixture-user/personal/", color: "#5b8cff", components: ["VEVENT"] }],
}));
writeFileSync(join(directory, "shell.qml"), `import QtQuick
import Quickshell
import Quickshell.Io
import "features/clock"
ShellRoot {
id: test
property int phase: 0
property int ticks: 0
// Fetches started (refresh() turns the status to loading) and finished.
property int starts: 0
property int fetches: 0
property int heldStarts: 0
function fail(message) {
console.error("ERROR caldav cadence phase " + phase + ": " + message);
Qt.quit();
}
function next() {
phase++;
ticks = 0;
}
CalDav {
id: caldav
configPath: ${JSON.stringify(config)}
refreshMs: 150
onLastSyncMsChanged: test.fetches++
onStatusChanged: if (status === "loading")
test.starts++
}
// The open mark lands in the call log before the opening it announces.
Process {
id: openMark
command: ["sh", "-c", "echo open >> \\"$CADENCE_LOG\\""]
onExited: {
caldav.popupOpen = true;
caldav.refresh();
}
}
// Closing comes first; a fetch already running may still finish after the mark.
Process {
id: closeMark
command: ["sh", "-c", "echo close >> \\"$CADENCE_LOG\\""]
}
Timer {
interval: 50
running: true
repeat: true
onTriggered: {
test.ticks++;
if (test.phase === 0) {
// Startup, closed: the account loads, nothing fetches across several intervals.
if (test.starts !== 0 || caldav.status !== "idle" && caldav.config.username)
test.fail("a closed calendar fetched or left idle: " + caldav.status);
else if (caldav.config.username === "fixture-user" && test.ticks >= 15) {
openMark.running = true;
test.next();
}
} else if (test.phase === 1 && caldav.status === "ok" && test.fetches >= 3) {
// The opening fetch, then at least two interval refreshes while open.
caldav.popupOpen = false;
test.heldStarts = test.starts;
closeMark.running = true;
test.next();
} else if (test.phase === 2 && test.ticks >= 15) {
if (test.starts !== test.heldStarts)
test.fail("the calendar kept fetching after close");
else {
console.log("caldav cadence qml: PASS");
Qt.quit();
}
}
}
}
Timer {
interval: 8000
running: true
onTriggered: test.fail("deadline")
}
}
`);
const run = runOffscreen(join(directory, "shell.qml"), {
directory, cwd: directory, timeout: 15000,
env: { PATH: `${bin}:/usr/bin`, CADENCE_LOG: log },
});
assertOffscreenPass(run, /caldav cadence qml: PASS/);
const calls = readFileSync(log, "utf8").trim().split("\n");
const opened = calls.indexOf("open");
const closed = calls.indexOf("close");
assert(opened >= 0 && closed > opened, "the fixture opened and closed the calendar: " + calls.join(","));
assert.deepEqual(calls.slice(0, opened), [], "startup and a closed calendar run no keyring or network process");
assert(calls.slice(opened, closed).filter(call => call === "curl").length >= 3, "opening fetches, then every interval while open: " + calls.join(","));
// A fetch already running at close may finish; none starts afterwards.
for (const program of ["secret-tool", "curl"])
assert(calls.slice(closed + 1).filter(call => call === program).length <= 1, "a closed calendar starts no fetch: " + calls.join(","));
console.log("caldav-cadence.test.mjs: PASS (offscreen production cadence; not pointer, keyboard, or pixel proof)");
} finally {
rmSync(directory, { recursive: true, force: true });
}