import assert from 'node:assert/strict'; import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; import { assertOffscreenPass, offscreenDiagnostic, runOffscreen } from '../../test-fixtures/offscreen.mjs'; // Owner-approved 2026-11-15 (RULES, diagnostic exceptions): Quickshell reports a failed // FileView write at WARN whatever printErrors says. Exactly one, in the failure case only. const writeWarning = /^\s*WARN scene: QML FileView at @features\/config\/Config\.qml\[\d+:\d+\]: Write of \S+\/directory-not-file failed: Unknown error when opening file\.$/; const directory = mkdtempSync(join(tmpdir(), 'nuguland-config-orientation-')); try { for (const file of ['FeatureEntry.qml', 'ConfigSection.qml', 'ConfigRow.qml', 'ConfigOption.qml', 'Theme.qml', 'nugu.json']) copyFileSync(new URL('../../' + file, import.meta.url), join(directory, file)); mkdirSync(join(directory, 'features/config'), { recursive: true }); copyFileSync(new URL('Config.qml', import.meta.url), join(directory, 'features/config/Config.qml')); const config = join(directory, 'preference.json'); function run(name, body, path = config, warning = null) { writeFileSync(join(directory, 'shell.qml'), `import QtQuick import Quickshell import Quickshell.Io import "features/config" ShellRoot { id: test property int step: 0 Theme { id: palette } Config { id: service; theme: palette; configPath: ${JSON.stringify(path)} } FileView { id: external; path: ${JSON.stringify(config)}; blockWrites: true; printErrors: false } function require(ok, message) { if (!ok) { console.error("ERROR", message); Qt.quit(); } } function pass() { console.log("orientation ${name}: PASS"); Qt.quit(); } Timer { interval: 100 running: true repeat: true onTriggered: { ${body} } } Timer { interval: 4000; running: true; onTriggered: { console.error("ERROR deadline"); Qt.quit(); } } } `); const result = runOffscreen(join(directory, 'shell.qml'), { directory, timeout: 6000 }); process.stdout.write(result.output); const marker = new RegExp('orientation ' + name + ': PASS'); if (!warning) return assertOffscreenPass(result, marker); const diagnostics = result.output.split('\n').filter(line => offscreenDiagnostic.test(line)); assert.equal(diagnostics.length, 1, result.output); assert.match(diagnostics[0], warning); assert.ifError(result.error); assert.equal(result.status, 0, result.output); assert.match(result.output, marker); } run('default', ` if (test.step === 0) { test.require(service.orientation === "horizontal" && service.orientationError === "", "missing file defaults horizontal"); test.require(service.setOrientation("bogus") === "horizontal", "invalid input no-op"); test.require(service.setOrientation("vertical") === "vertical", "explicit preference applies"); test.step = 1; } else { test.require(!service.orientationError, "explicit preference saved"); test.pass(); } `); assert.deepEqual(JSON.parse(readFileSync(config, 'utf8')), { orientation: 'vertical' }); run('restart', ` if (service.orientation === "vertical") { test.require(service.setOrientation("bogus") === "vertical", "invalid input keeps persisted preference"); test.pass(); } `); run('rapid', ` if (test.step === 0 && service.orientation === "vertical") { service.setOrientation("horizontal"); service.setOrientation("vertical"); test.require(service.orientation === "vertical", "the latest selection applies at once"); test.step = 1; } else if (test.step > 0 && test.step < 3) { test.step++; } else if (test.step === 3) { test.require(service.orientation === "vertical" && !service.orientationError, "latest selection wins after both writes settle"); test.pass(); } `); assert.deepEqual(JSON.parse(readFileSync(config, 'utf8')), { orientation: 'vertical' }); run('queued-restart', ` if (service.orientation === "vertical") { test.require(!service.orientationError, "fresh service reads the latest preference"); test.pass(); } `); writeFileSync(config, JSON.stringify({ orientation: 'vertical' })); run('external', ` if (test.step === 0 && service.orientation === "vertical") { external.setText(JSON.stringify({ orientation: "horizontal" })); test.step = 1; } else if (test.step === 1 && service.orientation === "horizontal") { external.setText("malformed"); test.step = 2; } else if (test.step === 2 && service.orientationError) { test.require(service.orientation === "horizontal", "invalid external update retains last good orientation"); test.pass(); } `); assert.equal(readFileSync(config, 'utf8'), 'malformed', 'external invalid input never causes writeback'); run('malformed', ` if (test.step === 0 && service.orientationError) { test.require(service.orientation === "horizontal", "malformed startup defaults horizontal"); service.setOrientation("toggle"); test.step = 1; } else if (test.step > 0 && test.step < 3) { test.step++; } else if (test.step === 3) { test.require(service.orientation === "vertical" && !service.orientationError, "selection repairs malformed preference"); test.pass(); } `); const denied = join(directory, 'directory-not-file'); mkdirSync(denied); run('failure', ` if (test.step === 0) { service.setOrientation("vertical"); test.step = 1; } else if (service.orientationError === "orientation not saved; select again") { test.require(service.orientation === "vertical", "write failure retains session intent with explicit unsaved feedback"); test.pass(); } `, denied, writeWarning); } finally { rmSync(directory, { recursive: true, force: true }); }