repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
quickshell/nuguland/features/clock/caldav-ipc.test.js
Raw'use strict';
const path = require('path');
const { runIpcSelfTests } = require('../../test-fixtures/ipc-selftest.js');
// The production CalDav service in an isolated Quickshell: fetch results, setup
// validation and state serialization, without network, keyring, or a settings write.
runIpcSelfTests({
files: ['CalDav.qml', 'caldav.js'].map(file => path.join(__dirname, file)),
body: `
CalDav { id: caldav }
function check(): string {
const cases = [];
const ok = (name, pass) => cases.push({ name: name, pass: !!pass });
const now = new Date();
const today = now.getFullYear() + String(now.getMonth() + 1).padStart(2, "0") + String(now.getDate()).padStart(2, "0");
const report = (kind, body) => "@@CAL /p/" + kind + "/\\t#5b8cff\\t" + kind + "\\n<d:multistatus><c:calendar-data>BEGIN:" + kind + "\\n" + body + "\\nEND:" + kind + "</c:calendar-data></d:multistatus>\\n";
caldav.applyFetch(report("VEVENT", "UID:e\\nSUMMARY:event\\nDTSTART:" + today + "T090000Z") + report("VTODO", "UID:t\\nSUMMARY:task\\nDUE;VALUE=DATE:" + today) + report("VTODO", "UID:old\\nSUMMARY:old\\nDUE;VALUE=DATE:19900101"));
ok("fetch keeps events and in-window tasks", caldav.status === "ok" && !caldav.stale && caldav.events.map(ev => ev.uid).join() === "e,t");
const synced = caldav.lastSyncMs;
ok("a successful fetch records its sync time", synced > 0 && Math.abs(Date.now() - synced) < 60000);
caldav.applyFetch("@@CAL /p/VEVENT/\\t#5b8cff\\tVEVENT\\n");
ok("failed fetch keeps last-good events and turns stale", caldav.status === "offline" && caldav.stale && caldav.events.length === 2);
ok("failed fetch keeps the last sync time", caldav.lastSyncMs === synced && JSON.parse(caldav.stateJson()).lastSyncMs === synced);
caldav.applyFetch(report("VEVENT", "UID:other\\nSUMMARY:other\\nDTSTART:" + today + "T100000Z") + "@@CAL /p/VTODO/\\t#5b8cff\\tVTODO\\n");
ok("one failed calendar keeps every last-good event", caldav.status === "offline" && caldav.stale && caldav.events.map(ev => ev.uid).join() === "e,t");
caldav.applyFetch("__NO_SECRET__\\n");
ok("missing secret asks for setup", caldav.status === "needs-setup" && caldav.events.length === 2);
caldav.saveSetup(" https://dav.example/ ", "alice", "");
ok("incomplete setup keeps drafts", caldav.secretError.length > 0 && caldav.setupUrlDraft === " https://dav.example/ " && caldav.setupUsernameDraft === "alice");
caldav.saveSetup("dav.example", "alice", "fixture-only");
const state = JSON.parse(caldav.stateJson());
ok("invalid url is rejected before any write", caldav.secretError.length > 0 && caldav.setupPasswordDraft === "fixture-only" && state.username === "");
ok("state reports a password draft without its value", state.setupDraftHasPassword === true && caldav.stateJson().indexOf("fixture-only") < 0 && state.eventCount === 2 && state.calendarCount === 0);
return JSON.stringify({ fail: cases.filter(c => !c.pass).length, cases: cases });
}`,
methods: [['caldav', 'check()']]
});
console.log('caldav-ipc.test.js: ALL PASS');