'use strict'; const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const { runIsolated } = require('../../test-fixtures/isolation.js'); // Compare settled date values with native clocks, not raw notification counts: // SystemClock 0.3.2 emits dateChanged even for equal values; a QML date binding // suppresses those, but emits its initial value before Component.onCompleted. const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'nuguland-clock-')); try { fs.symlinkSync(path.join(__dirname, 'Time.qml'), path.join(directory, 'Time.qml')); fs.writeFileSync(path.join(directory, 'shell.qml'), ` import QtQuick import Quickshell ShellRoot { id: test property string phase: "initializing" property var counts: ({ seconds: 0, minutes: 0, service: 0 }) property var events: [] property double initialMinute: 0 property double lastSecond: 0 property int advances: 0 property bool secondsReady: false property var secondTargets: [] property var serviceSeconds: [] function minute(value) { return Math.floor(value / 60000) * 60000; } function fail(message) { console.error("ERROR clock: " + message + " " + JSON.stringify({ phase, wall: Date.now(), counts, advances, secondsReady, secondTargets, serviceSeconds, seconds: seconds.date.getTime(), minutes: minutes.date.getTime(), service: time.now.getTime(), events })); phase = "done"; Qt.quit(); } function observe(name, value) { counts[name]++; events.push({ name, phase, wall: Date.now(), value: value.getTime() }); // Check every service change, not only the final sample of a phase. if ((phase === "minutes" || phase === "closed") && name === "service" && value.getTime() !== minute(value.getTime())) return fail("service emitted seconds while closed"); if ((phase === "seconds" || phase === "reopened") && secondsReady && value.getTime() > lastSecond) { const values = name === "seconds" ? secondTargets : name === "service" ? serviceSeconds : null; if (values && values[values.length - 1] !== value.getTime()) values.push(value.getTime()); } Qt.callLater(test.check); } function secondsPhase(next) { phase = next; advances = 0; secondsReady = false; secondTargets = []; serviceSeconds = []; time.secondsShown = true; Qt.callLater(test.check); } function begin() { // All initial bindings are now installed. Initialization is not a tick. initialMinute = minutes.date.getTime(); phase = "minutes"; if (time.now.getTime() !== initialMinute) return fail("initial minute differs from upstream"); } function check() { const service = time.now.getTime(); const second = seconds.date.getTime(); if (phase === "minutes") { // Either clock may notify first; wait for the same later native minute. if (minutes.date.getTime() > initialMinute && service === minutes.date.getTime()) secondsPhase("seconds"); } else if (phase === "seconds" || phase === "reopened") { // Exclude the synchronous precision refresh using a shared baseline. if (!secondsReady) { if (service === second) { lastSecond = second; secondsReady = true; } return; } // Each following native advance fences delivery of the preceding one. // Either timer may notify first, but occasional later matches cannot // hide missed seconds (e.g. a service updating only every ten seconds). if (secondTargets.length >= 2 && !serviceSeconds.includes(secondTargets[0])) return fail("service missed first native second"); if (secondTargets.length < 3) return; if (!serviceSeconds.includes(secondTargets[1])) return fail("service missed second native second"); lastSecond = second; if (phase === "reopened") { phase = "done"; console.log("clock upstream cadence: PASS " + JSON.stringify({ counts, events })); Qt.quit(); return; } phase = "closed"; advances = 0; time.secondsShown = false; if (time.now.getTime() !== minute(time.now.getTime())) return fail("service did not return to minutes"); } else if (phase === "closed" && second > lastSecond) { // Observe a stable minute over two native seconds. If closing straddles // a boundary, start that observation in the new minute instead. if (minute(second) !== minute(lastSecond)) { advances = 0; lastSecond = second; } else if (service === minute(second)) { lastSecond = second; if (++advances === 2) secondsPhase("reopened"); } } } Component.onCompleted: Qt.callLater(test.begin) SystemClock { id: seconds; precision: SystemClock.Seconds; onDateChanged: test.observe("seconds", date) } SystemClock { id: minutes; precision: SystemClock.Minutes; onDateChanged: test.observe("minutes", date) } Time { id: time; onNowChanged: test.observe("service", now) } Timer { interval: 72000; running: true; onTriggered: test.fail("deadline") } } `); const run = runIsolated('qs', ['--no-color', '-p', path.join(directory, 'shell.qml')], { directory, encoding: 'utf8', timeout: 80000 }); const output = String(run.stdout || '') + String(run.stderr || ''); assert.ifError(run.error); assert.equal(run.status, 0, output); assert.doesNotMatch(output, /WARN|ERROR|Error:|TypeError|ReferenceError/, output); assert.match(output, /clock upstream cadence: PASS/, output); console.log('time-ipc.test.js: PASS'); } finally { fs.rmSync(directory, { recursive: true, force: true }); }