const assert = require('assert'); const fs = require('fs'); const path = require('path'); const O = require('./osd.js'); // Fixtures lengthen the lifetime; the production glance stays 1.2 s. assert.match(fs.readFileSync(path.join(__dirname, 'Osd.qml'), 'utf8'), /^ property int lifetimeMs: 1200$/m); // --- normalizeEvent: supported events pass, anything else is an error --- for (const name of ['volume', 'microphone', 'brightness', 'media', 'workspace', 'screenshot', 'color', 'error']) assert.strictEqual(O.normalizeEvent(name), name); assert.strictEqual(O.normalizeEvent('bogus'), 'error'); assert.strictEqual(O.normalizeEvent(''), 'error'); // --- roleFor --- assert.strictEqual(O.roleFor('error', 'x'), 'error'); assert.strictEqual(O.roleFor('microphone', 'muted'), 'warning'); assert.strictEqual(O.roleFor('microphone', 'active'), 'warning'); assert.strictEqual(O.roleFor('microphone', '40'), 'info'); assert.strictEqual(O.roleFor('volume', '40'), 'ui.accent'); assert.strictEqual(O.roleFor('brightness', '40'), 'ui.accent'); assert.strictEqual(O.roleFor('media', 'play'), 'info'); // --- normalizedValue: meters only, numeric only, clamped 0..1 --- assert.strictEqual(O.normalizedValue('volume', '72'), 0.72); assert.strictEqual(O.normalizedValue('brightness', '40%'), 0.4); assert.strictEqual(O.normalizedValue('microphone', '150'), 1); assert.strictEqual(O.normalizedValue('volume', '-5'), 0); assert.strictEqual(O.normalizedValue('volume', 'muted'), -1); assert.strictEqual(O.normalizedValue('volume', ''), -1); assert.strictEqual(O.normalizedValue('media', '50'), -1); // --- glyph: every event draws a Nerd Font glyph, never an empty tile --- assert.strictEqual(O.glyph('volume', 'muted'), '\u{F0581}'); assert.strictEqual(O.glyph('volume', '0'), '\u{F0581}'); assert.strictEqual(O.glyph('volume', '20'), '\u{F057F}'); assert.strictEqual(O.glyph('volume', '50'), '\u{F0580}'); assert.strictEqual(O.glyph('volume', '90'), '\u{F057E}'); assert.strictEqual(O.glyph('volume', 'unmuted'), '\u{F057E}'); assert.strictEqual(O.glyph('microphone', 'muted'), '\u{F036D}'); assert.strictEqual(O.glyph('microphone', 'unmuted'), '\u{F036C}'); assert.strictEqual(O.glyph('brightness', '50'), '\u{F00DF}'); assert.strictEqual(O.glyph('media', 'pause'), '\u{F03E4}'); assert.strictEqual(O.glyph('media', 'play'), '\u{F040A}'); assert.strictEqual(O.glyph('screenshot', 'saved'), '\u{F0E51}'); assert.strictEqual(O.glyph('color', '#ff0000'), '\u{F020A}'); assert.strictEqual(O.glyph('error', 'boom'), '\u{F0028}'); assert.strictEqual(O.glyph('workspace', '2'), '\u{F02FC}'); for (const name of O.supportedEvents) for (const value of ['', '0', '50', 'muted', 'unmuted', 'pause', 'x']) assert(O.glyph(name, value).length > 0, `${name} ${value} has a glyph`); // --- displayLabel: lowercase event names, an error's subject / displayValue --- for (const name of O.supportedEvents.filter(name => name !== 'error')) assert.strictEqual(O.displayLabel(name, 'x'), name.toLowerCase()); assert.strictEqual(O.displayLabel('error', 'screenshot not taken · no save path given · add one'), 'screenshot not taken'); assert.strictEqual(O.displayLabel('error', 'screenshot failed: disk full'), 'screenshot failed'); assert.strictEqual(O.displayLabel('error', 'boom'), 'error', 'a message without a subject keeps the generic heading'); assert.deepStrictEqual(O.errorParts('screenshot not taken · no save path given · add one'), { title: 'screenshot not taken', body: 'no save path given · add one' }); // An unsupported event names itself as the error's subject; supported values pass unchanged. assert.strictEqual(O.normalizeValue('bogus', 'unknown'), 'bogus · unknown'); assert.strictEqual(O.normalizeValue('volume', 72), '72'); assert.strictEqual(O.normalizeValue('error', 'boom'), 'boom'); assert.strictEqual(O.normalizeValue('', 'boom'), 'boom'); assert.strictEqual(O.displayLabel('volume'), 'volume'); assert.strictEqual(O.displayLabel(''), ''); assert.strictEqual(O.displayValue('volume', '72'), '72%'); assert.strictEqual(O.displayValue('microphone', 'muted'), 'muted'); assert.strictEqual(O.displayValue('volume', 'unmuted'), 'on'); assert.strictEqual(O.displayValue('error', 'boom'), ''); assert.strictEqual(O.displayValue('color', '#ff0000'), '#ff0000'); // --- iconRole / numericText --- assert.strictEqual(O.iconRole('volume', '0'), 'audio-muted'); assert.strictEqual(O.iconRole('volume', '50'), 'audio-volume'); assert.strictEqual(O.iconRole('microphone', 'muted'), 'microphone-muted'); assert.strictEqual(O.iconRole('error', 'x'), 'danger'); assert.strictEqual(O.iconRole('', ''), 'info'); assert.strictEqual(O.numericText('volume', '33.4'), '33'); assert.strictEqual(O.numericText('media', 'play'), ''); // --- valueIndicator: meter vs non-meter shape --- const meter = O.valueIndicator('volume', '72', 'ui.accent'); assert.strictEqual(meter.normalizedValue, 0.72); assert.strictEqual(meter.progressBar.visible, true); assert.strictEqual(meter.progressBar.role, 'ui.accent'); assert.strictEqual(meter.displayValue, '72%'); assert.strictEqual(meter.hardSeparator, '%'); assert.deepStrictEqual(meter.wheelActions, ['volume-up', 'volume-down']); const failure = O.valueIndicator('error', 'disk full', 'error'); assert.strictEqual(failure.normalizedValue, null); assert.strictEqual(failure.progressBar.visible, false); assert.strictEqual(failure.protectionMessage.visible, true); assert.strictEqual(failure.protectionMessage.text, 'disk full'); assert.strictEqual(O.valueIndicator('error', 'screenshot failed: disk full', 'error').protectionMessage.text, 'disk full'); assert.strictEqual(O.valueIndicator('error', 'screenshot failed: disk full', 'error').displayLabel, 'screenshot failed'); assert.deepStrictEqual(failure.wheelActions, []); console.log('osd.test.js: ALL PASS');