Files
faceclaw/tests/phone-dnd-notifications.test.cjs
T

183 lines
7.1 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const { NotificationPopupDelay, notificationPopupDelayMs } = require('../.test-build/app/ui/shell/notification-popup-delay.js');
function evaluateMembers(fileName, className, names, globals) {
const source = ts.createSourceFile(fileName, fs.readFileSync(path.join(__dirname, '..', fileName), 'utf8'), ts.ScriptTarget.Latest, true);
const declaration = source.statements.find(node => ts.isClassDeclaration(node) && node.name.text === className);
const members = names.map(name => {
const member = declaration.members.find(node => node.name?.getText(source) === name);
assert.ok(member, `production member ${className}.${name} exists`);
return member.getText(source);
});
const context = { exports: {}, ...globals };
vm.runInNewContext(ts.transpileModule(`export class Harness { ${members.join('\n')} }`, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports.Harness;
}
function controller() {
const state = {
dnd: false, screenOn: false, sourceAllowed: true, wakes: 0, renders: 0,
modals: [], removed: [], active: [{ key: 'message', packageName: 'com.mail', category: 'msg' }],
};
let now = 0;
const timers = [];
const clock = {
schedule(run, delay) {
const timer = { due: now + delay, run, cancelled: false };
timers.push(timer);
return () => { timer.cancelled = true; };
},
advance(ms = 500) {
const end = now + ms;
for (;;) {
const next = timers.filter(t => !t.cancelled && t.due <= end).sort((a, b) => a.due - b.due)[0];
if (!next) break;
now = next.due;
next.cancelled = true;
next.run();
}
now = end;
},
};
const Harness = evaluateMembers('app/g2/dashboard-controller.ts', 'DashboardController', [
'notificationPopups', 'handleAndroidNotificationPosted', 'handleAndroidNotificationRemoved', 'popUpAndroidNotification',
], {
NotificationPopupDelay: class extends NotificationPopupDelay {
constructor(ready) { super(ready, clock.schedule, () => now); }
},
ALL_NOTIFICATIONS: 0x7fffffff,
readActiveNotifications: () => state.active,
shouldPopUpNewNotification: () => state.sourceAllowed,
notificationPopupDelayMs,
isPhoneDoNotDisturbEnabled: () => state.dnd,
shell: {
isScreenOn: () => state.screenOn,
wake: () => { state.wakes++; state.screenOn = true; return true; },
openNotificationModal: (key, woke) => state.modals.push({ key, woke }),
notificationRemoved: key => state.removed.push(key),
},
});
const host = new Harness();
host.requestShellRender = () => state.renders++;
host.appendLog = () => {};
host.formatError = String;
return { state, host, advance: clock.advance };
}
test('phone DND prevents a mirrored notification waking sleeping glasses without deleting it', () => {
const { host, state, advance } = controller();
state.dnd = true;
host.handleAndroidNotificationPosted('message');
advance();
assert.equal(state.wakes, 0);
assert.deepEqual(state.modals, []);
assert.equal(state.renders, 1, 'the notification tray still refreshes');
assert.equal(state.active.length, 1, 'the phone notification stays available for manual viewing');
});
test('a notification captured during phone DND cannot wake after DND turns off during its delay', () => {
const { host, state, advance } = controller();
// Native captured DND before its asynchronous callback; it is off by JS delivery.
host.handleAndroidNotificationPosted('message', false);
advance();
assert.equal(state.wakes, 0);
assert.deepEqual(state.modals, []);
});
test('DND sampled at JS arrival remains restrictive if disabled before the popup timer', () => {
const { host, state, advance } = controller();
state.dnd = true;
host.handleAndroidNotificationPosted('message', true);
state.dnd = false;
advance();
assert.equal(state.wakes, 0);
assert.deepEqual(state.modals, []);
});
test('enabling DND during the popup delay prevents a previously eligible wake', () => {
const { host, state, advance } = controller();
host.handleAndroidNotificationPosted('message', true);
state.dnd = true;
advance();
assert.equal(state.wakes, 0);
assert.deepEqual(state.modals, []);
});
test('DND leaves an already-awake notification popup normal without stealing focus', () => {
const { host, state, advance } = controller();
state.screenOn = true;
state.dnd = true;
host.handleAndroidNotificationPosted('message', false);
advance();
assert.equal(state.wakes, 0);
assert.deepEqual(state.modals, [{ key: 'message', woke: false }]);
});
test('DND silent arrivals are not replayed on disable, but new notifications wake normally', () => {
const { host, state, advance } = controller();
state.dnd = true;
host.handleAndroidNotificationPosted('message', false);
advance();
state.dnd = false;
advance(10_000);
assert.equal(state.wakes, 0);
assert.deepEqual(state.modals, []);
assert.deepEqual(Array.from(host.notificationPopups.pendingKeys()), []);
host.handleAndroidNotificationPosted('message', true);
advance();
assert.equal(state.wakes, 1);
assert.deepEqual(state.modals, [{ key: 'message', woke: true }]);
});
test('a DND popup arriving while awake cannot wake the screen if it sleeps during the delay', () => {
const { host, state, advance } = controller();
state.screenOn = true;
state.dnd = true;
host.handleAndroidNotificationPosted('message', false);
state.screenOn = false;
state.dnd = false;
advance();
assert.equal(state.wakes, 0);
assert.deepEqual(state.modals, []);
});
function appNotificationHost() {
const state = { dnd: true, wakes: 0, modals: [] };
const Harness = evaluateMembers('app/ui/shell/shell.ts', 'Shell', ['postAppNotification'], {
isPhoneDoNotDisturbEnabled: () => state.dnd,
});
const host = new Harness();
host.screenOn = false;
host.appNotifications = new Map();
host.wake = () => { state.wakes++; host.screenOn = true; return true; };
host.openNotificationModal = (key, woke) => state.modals.push({ key, woke });
return { host, state };
}
test('phone DND also prevents an app-generated notification waking sleeping glasses without retaining an unreachable popup', () => {
const { host, state } = appNotificationHost();
host.postAppNotification({ key: 'local-alert' });
assert.equal(state.wakes, 0);
assert.deepEqual(state.modals, []);
assert.equal(host.appNotifications.size, 0);
});
test('app-generated notifications keep normal awake behavior during DND and wake normally when DND is off', () => {
for (const screenOn of [true, false]) {
const { host, state } = appNotificationHost();
host.screenOn = screenOn;
state.dnd = screenOn;
host.postAppNotification({ key: 'local-alert' });
assert.equal(state.wakes, screenOn ? 0 : 1);
assert.deepEqual(state.modals, [{ key: 'local-alert', woke: !screenOn }]);
assert.equal(host.appNotifications.size, 1);
}
});