const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { loader } = require('./helpers/load-typescript.cjs'); const kotlinServicePath = 'App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt'; function source(file) { return fs.readFileSync(path.resolve(__dirname, '..', file), 'utf8'); } // Structural source contracts only: these do not compile or execute Android/Kotlin. test('structural Kotlin contract: fresh read-only DND getter maps restrictive filters and fails open', () => { const service = source(kotlinServicePath); const getter = service.match(/@JvmStatic\s+fun isPhoneDoNotDisturbEnabled\(context: Context\?\): Boolean \{([\s\S]*?)\n \}/); assert.ok(getter, 'missing static context-based DND getter'); assert.match(getter[1], /context\?\.getSystemService\(Context\.NOTIFICATION_SERVICE\) as\? NotificationManager/); assert.match(getter[1], /manager\?\.currentInterruptionFilter/); assert.match(getter[1], /activeService\?\.currentInterruptionFilter/); assert.match(getter[1], /catch \([^)]*: Throwable\)/); assert.match(getter[1], /NotificationManager\.INTERRUPTION_FILTER_PRIORITY,\s*NotificationManager\.INTERRUPTION_FILTER_NONE,\s*NotificationManager\.INTERRUPTION_FILTER_ALARMS -> true/); assert.match(getter[1], /else -> false/); assert.doesNotMatch(getter[1], /setInterruptionFilter|requestInterruptionFilter|Settings\.|postDelayed/); assert.doesNotMatch(service, /override fun onInterruptionFilterChanged/); assert.match(source('App_Resources/Android/app.gradle'), /minSdkVersion 24\b/); }); test('structural Kotlin contract: posted callback snapshots DND before main-handler delivery, including reconnect', () => { const service = source(kotlinServicePath); const emit = service.match(/private fun emitNotificationPosted\(key: String\?\) \{([\s\S]*?)\n \}/); assert.ok(emit); assert.match(emit[1], /val canWakeScreen = !isPhoneDoNotDisturbEnabled\(activeService\)/); assert.match(emit[1], /listener\.onNotificationPosted\(eventKey, canWakeScreen\)/); assert.ok(emit[1].indexOf('val canWakeScreen') < emit[1].indexOf('emitNotificationEvent(')); assert.equal((emit[1].match(/isPhoneDoNotDisturbEnabled/g) ?? []).length, 1); const dispatch = service.match(/private fun emitNotificationEvent\([\s\S]*?\n \}/)[0]; assert.match(dispatch, /mainHandler\.post \{[\s\S]*deliver\(listener, key\)/); assert.doesNotMatch(dispatch, /isPhoneDoNotDisturbEnabled/); assert.match(service, /for \(key in posted\) \{\s*emitNotificationPosted\(key\)/); assert.match(service, /private fun emitNotificationRemoved\(key: String\?\) \{\s*emitNotificationEvent\(key\) \{ listener, eventKey -> listener\.onNotificationRemoved\(eventKey\) \}/); const callback = source('native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawNotificationListener.kt'); assert.match(callback, /fun onNotificationPosted\(key: String\?, canWakeScreen: Boolean\): Unit/); assert.match(callback, /fun onNotificationRemoved\(key: String\?\): Unit/); }); function notificationBridge() { const tasks = []; let proxy; let observer; const service = { addNotificationListener(value) { proxy = value; }, removeNotificationListener(value) { assert.equal(value, proxy); proxy = undefined; }, addListenerStateObserver(value) { observer = value; }, removeListenerStateObserver(value) { assert.equal(value, observer); observer = undefined; }, }; const api = loader({ global: { isAndroid: true }, setTimeout(fn, delay) { assert.equal(delay, 0); tasks.push(fn); }, com: { faceclaw: { app: { FaceclawMediaNotificationListenerService: service, FaceclawNotificationListener: function (callbacks) { return callbacks; }, } } }, java: { lang: { Runnable: function (callbacks) { return callbacks; } } }, }, { '../graphics/image': {}, './frame-timings': { spanCurrent: (_name, fn) => fn() }, '../util/array-util': { toUint8Array: value => value }, './notification-sources': {}, './image-files': { PHOTO_TONE: { gamma: 1 } }, './notification-types': { NOTIFICATION_ICON_SIZE: 24 }, })('app/native/notification-icons.ts'); return { api, service, get proxy() { return proxy; }, get observer() { return observer; }, flush() { while (tasks.length) tasks.shift()(); }, }; } test('native posted wake permission remains immutable across asynchronous bridge dispatch', () => { const bridge = notificationBridge(); const posted = []; const off = bridge.api.onAndroidNotificationPosted((key, canWakeScreen) => posted.push([key, canWakeScreen])); let dnd = true; bridge.service.isPhoneDoNotDisturbEnabled = () => dnd; const reader = loadDnd({ service: bridge.service }); bridge.proxy.onNotificationPosted('dnd-active', !reader.isPhoneDoNotDisturbEnabled()); dnd = false; bridge.proxy.onNotificationPosted('dnd-off', !reader.isPhoneDoNotDisturbEnabled()); bridge.proxy.onNotificationPosted('legacy'); assert.deepEqual(posted, []); bridge.flush(); assert.deepEqual(posted, [['dnd-active', false], ['dnd-off', true], ['legacy', true]]); dnd = true; assert.equal(reader.isPhoneDoNotDisturbEnabled(), true); dnd = false; assert.equal(reader.isPhoneDoNotDisturbEnabled(), false); bridge.flush(); assert.equal(posted.length, 3, 'DND transitions alone do not replay notification posts'); off(); assert.equal(bridge.proxy, undefined); }); test('removed callbacks retain their key-only contract', () => { const bridge = notificationBridge(); const removed = []; const off = bridge.api.onAndroidNotificationRemoved((...args) => removed.push(args)); bridge.proxy.onNotificationRemoved('gone'); bridge.flush(); assert.deepEqual(removed, [['gone']]); off(); }); test('posted, removed and listener-state observers retain async dispatch and eager cache invalidation', () => { const bridge = notificationBridge(); const posted = []; const removed = []; let changed = 0; let iconReads = 0; bridge.service.getNotificationIconGrayForKey = () => { iconReads++; return new Uint8Array(); }; const offPosted = bridge.api.onAndroidNotificationPosted(key => posted.push(key)); const offRemoved = bridge.api.onAndroidNotificationRemoved(key => removed.push(key)); const offChanged = bridge.api.onAndroidNotificationsChanged(() => changed++); const readIcon = () => bridge.api.readNotificationIconByKey('same-key', false); readIcon(); readIcon(); assert.equal(iconReads, 1); bridge.proxy.onNotificationPosted('same-key', false); readIcon(); assert.equal(iconReads, 2, 'DND-blocked post still invalidates cached icons eagerly'); assert.equal(changed, 0); bridge.flush(); assert.deepEqual(posted, ['same-key']); assert.equal(changed, 1); bridge.proxy.onNotificationRemoved('same-key'); readIcon(); assert.equal(iconReads, 3); assert.deepEqual(removed, []); bridge.flush(); assert.deepEqual(removed, ['same-key']); assert.equal(changed, 2); bridge.observer.run(); readIcon(); assert.equal(iconReads, 4); assert.equal(changed, 2); bridge.flush(); assert.equal(changed, 3); assert.deepEqual(posted, ['same-key'], 'listener-state changes do not replay posts'); offPosted(); offRemoved(); assert.ok(bridge.proxy, 'changed observer retains its native listeners'); offChanged(); assert.equal(bridge.proxy, undefined); assert.equal(bridge.observer, undefined); }); function loadDnd({ isAndroid = true, service, context = {} } = {}) { return loader({ global: { isAndroid }, com: { faceclaw: { app: { FaceclawMediaNotificationListenerService: service } } }, }, { '@nativescript/core': { Utils: { android: { getApplicationContext: () => context } } } })( 'app/native/phone-do-not-disturb.ts', ); } test('phone DND reader samples the native getter with application context on every Android read', () => { const context = {}; let enabled = false; let reads = 0; const api = loadDnd({ context, service: { isPhoneDoNotDisturbEnabled(actualContext) { assert.equal(actualContext, context); reads++; return enabled; }, } }); assert.equal(api.isPhoneDoNotDisturbEnabled(), false); enabled = true; assert.equal(api.isPhoneDoNotDisturbEnabled(), true); enabled = false; assert.equal(api.isPhoneDoNotDisturbEnabled(), false); assert.equal(reads, 3); }); test('phone DND reader returns false when the native boundary is unavailable or throws', () => { for (const service of [undefined, {}, { isPhoneDoNotDisturbEnabled() { throw new Error('unavailable'); } }]) { assert.equal(loadDnd({ service }).isPhoneDoNotDisturbEnabled(), false); } const api = loader({ global: { isAndroid: true } }, { '@nativescript/core': { Application: {} }, })('app/native/phone-do-not-disturb.ts'); assert.equal(api.isPhoneDoNotDisturbEnabled(), false); }); test('phone DND reader returns false outside Android without touching native APIs', () => { const service = { isPhoneDoNotDisturbEnabled() { assert.fail('native getter must not run'); } }; assert.equal(loadDnd({ isAndroid: false, service }).isPhoneDoNotDisturbEnabled(), false); const api = loader({ global: { isIOS: true } }, { '@nativescript/core': { Application: {} }, })('app/native/phone-do-not-disturb.ts'); assert.equal(api.isPhoneDoNotDisturbEnabled(), false); });