mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
68 lines
4.2 KiB
JavaScript
68 lines
4.2 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const vm = require('node:vm');
|
|
const ts = require('typescript');
|
|
function fixture() {
|
|
const source = ts.createSourceFile('controller.ts', fs.readFileSync('app/g2/dashboard-controller.ts', 'utf8'), ts.ScriptTarget.Latest, true);
|
|
const cls = source.statements.find(n => ts.isClassDeclaration(n) && n.name?.text === 'DashboardController');
|
|
const methods = cls.members.filter(n => ['handleSilentMode', 'showSilentModeExit', 'setPhase'].includes(n.name?.getText(source)));
|
|
const alerts = [], timers = new Map(); let next = 0, on = true, wakes = 0;
|
|
const context = { exports: {}, setTimeout: fn => { timers.set(++next, fn); return next; }, clearTimeout: id => timers.delete(id),
|
|
updateGlassesPresence() {}, shell: { isScreenOn: () => on, wake: () => { on = true; wakes++; }, showAlert: text => alerts.push(text),
|
|
foregroundWindow: () => ({ requestRender() {} }), setBatteryLevels() {}, getBatteryLevels: () => ({}) } };
|
|
vm.runInNewContext(ts.transpileModule(`export class Controller { ${methods.map(n => n.getText(source)).join('\n')} }`, {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
|
}).outputText, context);
|
|
const c = new context.exports.Controller();
|
|
Object.assign(c, { silentMode: false, silentModeFeedback: '', silentModeFeedbackTimer: null, silentModeFeedbackGeneration: 0, phase: 'connected', emit() {},
|
|
cancelEvenHubSuspendTimer() {}, ensureEvenHubSessionActive: async () => true, requestShellRender() {},
|
|
appendLog() {}, formatError: error => String(error) });
|
|
return { c, alerts, timers, wakes: () => wakes, dark: () => { on = false; }, expire() { const callbacks = [...timers.values()]; timers.clear(); callbacks.forEach(fn => fn()); } };
|
|
}
|
|
test('silent activation remains visible on phone; exit waits for the display wake barrier', async () => {
|
|
const h = fixture(); h.c.handleSilentMode(false);
|
|
assert.equal(h.c.silentModeFeedback, '');
|
|
h.c.handleSilentMode(true);
|
|
assert.match(h.c.silentModeFeedback, /enabled/); assert.equal(h.timers.size, 0); assert.equal(h.alerts.length, 0);
|
|
h.c.handleSilentMode(false);
|
|
assert.equal(h.c.silentModeFeedback, 'Silent mode disabled');
|
|
await new Promise(setImmediate);
|
|
assert.deepEqual(h.alerts, ['Silent mode disabled']);
|
|
h.expire(); assert.equal(h.c.silentModeFeedback, '');
|
|
});
|
|
test('repeated state reports do not duplicate feedback; an old timer cannot clear a new activation', async () => {
|
|
const h = fixture(); h.c.handleSilentMode(true); h.c.handleSilentMode(false);
|
|
h.c.handleSilentMode(false); await new Promise(setImmediate); assert.equal(h.alerts.length, 1);
|
|
h.c.handleSilentMode(true); h.expire();
|
|
assert.match(h.c.silentModeFeedback, /enabled/);
|
|
});
|
|
test('silent exit wakes a sleeping display before showing feedback but leaves locked glasses alone', async () => {
|
|
for (const locked of [false, true]) {
|
|
const h = fixture(); h.c.glassesLocked = locked; h.dark();
|
|
h.c.handleSilentMode(true); h.c.handleSilentMode(false); await new Promise(setImmediate);
|
|
assert.equal(h.wakes(), locked ? 0 : 1);
|
|
assert.equal(h.alerts.length, locked ? 0 : 1);
|
|
assert.equal(h.c.silentModeFeedback, 'Silent mode disabled');
|
|
}
|
|
});
|
|
test('an unavailable session, re-entry into silence or disconnect suppresses a stale exit popup', async () => {
|
|
for (const state of ['not-ready', 'silent', 'disconnected']) {
|
|
const h = fixture(); let finish;
|
|
h.c.ensureEvenHubSessionActive = () => new Promise(resolve => { finish = resolve; });
|
|
h.c.handleSilentMode(true); h.c.handleSilentMode(false);
|
|
assert.equal(h.alerts.length, 0);
|
|
if (state === 'silent') h.c.handleSilentMode(true);
|
|
if (state === 'disconnected') h.c.setPhase('disconnected');
|
|
finish(state !== 'not-ready'); await new Promise(setImmediate);
|
|
assert.equal(h.alerts.length, 0);
|
|
}
|
|
});
|
|
|
|
test('disconnect clears feedback and any pending timer without announcing a silent exit', () => {
|
|
const h = fixture(); h.c.handleSilentMode(true); h.c.handleSilentMode(false);
|
|
h.c.setPhase('disconnected');
|
|
assert.equal(h.c.silentModeFeedback, ''); assert.equal(h.timers.size, 0);
|
|
h.expire(); assert.equal(h.c.silentModeFeedback, '');
|
|
});
|