forked from Blendman974/faceclaw
Add support for buzzer sound effects on iOS
This commit is contained in:
1 parent
1412db8e82
commit
e50da605e0
19 files changed
+211
-48
No files matched your search
@@ -0,0 +1,40 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { loader } = require('./helpers/load-typescript.cjs');
|
||||
|
||||
const payload = new Uint8Array([5, 4, 1, 0xb8, 0x0b, 25, 80, 0]);
|
||||
|
||||
test('iOS game worker forwards an immutable byte payload through its host and ignores closed windows', async () => {
|
||||
const played = [], worker = { postMessage() {} };
|
||||
const load = loader({ global: { isIOS: true, postMessage: data => worker.onmessage({ data }) } }, {
|
||||
'../../graphics/image': {}, './chrome-layer': { windowIcon() {} },
|
||||
'../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } },
|
||||
'./geometry': { appViewportSize: () => ({ width: 576, height: 260 }) },
|
||||
'../../native/frame-timings': {}, './worker-state': {},
|
||||
'./shell': { shell: { registerWindow() {} } },
|
||||
});
|
||||
const { WorkerAppHost } = load('app/ui/shell/worker-window.ts');
|
||||
const bridge = load('app/native/worker-buzzer.ts');
|
||||
const host = new WorkerAppHost({ appId: 'blocks', worker, configureSurface: async () => {},
|
||||
requestShellRender() {}, removeSurface() {}, playBuzzerSequence: bytes => played.push(bytes) });
|
||||
bridge.playWorkerBuzzerSequence(payload);
|
||||
assert.equal(played.length, 0);
|
||||
const window = host.openWindow({ windowId: 'blocks', title: 'Blocks' });
|
||||
const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1);
|
||||
bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0);
|
||||
assert.deepEqual(played, [payload]);
|
||||
window.close(); bridge.playWorkerBuzzerSequence(payload);
|
||||
assert.equal(played.length, 1);
|
||||
});
|
||||
|
||||
test('Android game bridge retains direct native playback and skips a missing communicator', () => {
|
||||
let active = null;
|
||||
const played = [];
|
||||
const bridge = loader({ global: { isIOS: false },
|
||||
com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => active } } } } })('app/native/worker-buzzer.ts');
|
||||
bridge.playWorkerBuzzerSequence(payload);
|
||||
active = { playBuzzerSequence: buffer => played.push(new Uint8Array(buffer)) };
|
||||
const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1);
|
||||
bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0);
|
||||
assert.deepEqual(played, [payload]);
|
||||
});
|
||||
Vendored
+1
@@ -36,6 +36,7 @@ module.exports = function worker() {
|
||||
'../../graphics/ui-fonts': { getDefaultSmallFont: () => smallFont },
|
||||
'../../native/frame-timings': { finishFrame: noop, logFrame: noop },
|
||||
'../../native/active-display': {},
|
||||
'../../native/worker-buzzer': { playWorkerBuzzerSequence() {} },
|
||||
'../../native/settings-store': { getStringSetting: () => highScore, setStringSetting: (_, value) => { highScore = value; } },
|
||||
'../../ui/sound-effects': {},
|
||||
'../../ui/sound-setting': { loadSoundEnabled: () => true, saveSoundEnabled: () => {} },
|
||||
|
||||
@@ -171,7 +171,7 @@ test('settings-driven repaint runs after font cache invalidation, regardless of
|
||||
});
|
||||
|
||||
test('background glasses input still composites frames; phone resume preserves the session; explicit stop stays stopped', async () => {
|
||||
const tasks = new Map(), screenStates = [], inputs = [], frames = [], previews = [], states = [];
|
||||
const tasks = new Map(), screenStates = [], inputs = [], frames = [], previews = [], states = [], sounds = [];
|
||||
let nextTask = 0, starts = 0, stops = 0, pollStarts = 0, pollStops = 0, session;
|
||||
const window = { windowId: 'launcher', surfaceId: 'launcher', appId: 'launcher', title: 'Apps',
|
||||
setScreenOn: on => screenStates.push(on), requestRender() {} };
|
||||
@@ -185,6 +185,7 @@ test('background glasses input still composites frames; phone resume preserves t
|
||||
async start() { starts++; this.state = { phase: 'connected' }; this.onState(this.state); }
|
||||
async stop() { stops++; this.state = { phase: 'disconnected' }; this.onState(this.state); }
|
||||
setFrame(pixels) { if (this.state.phase === 'connected') frames.push(pixels); }
|
||||
playBuzzerSequence(payload) { sounds.push([...payload]); }
|
||||
wake() {}
|
||||
}
|
||||
const settings = { lockScreenEnabledSetting: { get: () => true }, onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } };
|
||||
@@ -229,7 +230,11 @@ test('background glasses input still composites frames; phone resume preserves t
|
||||
});
|
||||
const flush = () => { for (const [id, fn] of [...tasks]) { if (tasks.delete(id)) fn(); } };
|
||||
const controller = new api.IosPreviewController(image => previews.push(image), assert.fail, state => states.push(state));
|
||||
await controller.actions.playBuzzerSequence(new Uint8Array([5, 4, 0]));
|
||||
assert.equal(sounds.length, 0);
|
||||
controller.resume(); await controller.connect(); flush();
|
||||
await controller.actions.playBuzzerSequence(new Uint8Array([5, 4, 0]));
|
||||
assert.deepEqual(sounds, [[5, 4, 0]]);
|
||||
const previewCount = previews.length, stateCount = states.length;
|
||||
controller.pause(); controller.pause(); // NativeScript also unloads its root page on background entry.
|
||||
assert.equal(stops, 0); assert.ok(screenStates.every(Boolean));
|
||||
@@ -361,3 +366,31 @@ test('iOS compositor rejects missing and removed surfaces before crossing into K
|
||||
assert.throws(() => c.submitSurfaceFrame('nightscout', pixels, rect), /Unknown surface/);
|
||||
assert.equal(nativeSubmissions, 1);
|
||||
});
|
||||
|
||||
|
||||
test('iOS welcome sound waits for a new acknowledged frame and is consumed once', async () => {
|
||||
let pending = true;
|
||||
const played = [], sounds = load('app/ui/sound-effects.ts', {});
|
||||
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', {
|
||||
require: id => ({
|
||||
'../phone-ui/onboarding-state': { isWelcomeSoundPending: () => pending, setWelcomeSoundPending: value => { pending = value; } },
|
||||
'../ui/sound-effects': sounds,
|
||||
})[id] ?? {},
|
||||
setTimeout: fn => { fn(); return 1; },
|
||||
});
|
||||
const host = Object.create(IosPreviewController.prototype);
|
||||
host.acknowledgedFrames = 0;
|
||||
host.actions = { playBuzzerSequence: bytes => played.push([...bytes]) };
|
||||
host.logBluetooth = assert.fail;
|
||||
host.maybePlayWelcomeSound({ phase: 'connecting', frames: 0 });
|
||||
host.maybePlayWelcomeSound({ phase: 'connected', frames: 0 });
|
||||
assert.equal(pending, true); assert.equal(played.length, 0);
|
||||
host.maybePlayWelcomeSound({ phase: 'connected', frames: 1 });
|
||||
assert.equal(pending, false);
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
assert.ok(played.length > 0);
|
||||
const count = played.length;
|
||||
host.maybePlayWelcomeSound({ phase: 'connected', frames: 1 });
|
||||
host.maybePlayWelcomeSound({ phase: 'connected', frames: 2 });
|
||||
assert.equal(played.length, count);
|
||||
});
|
||||
@@ -470,6 +470,57 @@ test('delta ids skip reserved values and advance only on emitted deltas; reconne
|
||||
});
|
||||
|
||||
const compassMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 10);
|
||||
const buzzerMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 5);
|
||||
const buzzerPayload = () => new Uint8Array([5, 4, 2, 0xb8, 0x0b, 25, 80, 0, 1, 0, 0, 20, 0]);
|
||||
test('buzzer snapshots the complete Android payload and waits for both lens ACKs without counting a frame', async t => {
|
||||
const h = harness(t);
|
||||
await h.session.playBuzzerSequence(buzzerPayload());
|
||||
assert.equal(h.transport.sent.length, 0);
|
||||
await h.session.start(addresses);
|
||||
await h.session.playBuzzerSequence(new Uint8Array());
|
||||
assert.equal(buzzerMessages(h.transport).length, 0);
|
||||
h.transport.hold = true;
|
||||
const payload = buzzerPayload(), expected = payload.slice();
|
||||
let complete = false;
|
||||
const playing = h.session.playBuzzerSequence(payload).then(() => { complete = true; });
|
||||
payload.fill(0);
|
||||
await until(() => buzzerMessages(h.transport).length === 1);
|
||||
const sent = buzzerMessages(h.transport)[0];
|
||||
assert.equal(sent.id, 'L'); assert.deepEqual(sent.message.payload, expected);
|
||||
h.transport.ack(sent.id, sent.message, { lenses: [1] }); await tick();
|
||||
assert.equal(complete, false);
|
||||
h.transport.ack(sent.id, sent.message, { lenses: [2] }); await playing;
|
||||
assert.equal(h.session.state.frames, 0);
|
||||
});
|
||||
|
||||
test('buzzer shares display compression and ACK recovery while the frame window is full', async t => {
|
||||
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
|
||||
h.session.setFrame(noisy()); await until(() => imageMessages(h.transport).length === 3);
|
||||
const playing = h.session.playBuzzerSequence(buzzerPayload());
|
||||
await until(() => buzzerMessages(h.transport).length === 1);
|
||||
const sent = buzzerMessages(h.transport)[0];
|
||||
h.transport.ack(sent.id, sent.message, { nack: true });
|
||||
await until(() => buzzerMessages(h.transport).length === 2);
|
||||
const replay = buzzerMessages(h.transport)[1];
|
||||
assert.notEqual(replay.message.magic, sent.message.magic);
|
||||
assert.deepEqual(replay.message.payload, buzzerPayload());
|
||||
h.transport.hold = false; // Subsequent bands of the large frame can finish too.
|
||||
for (const item of h.transport.sent.filter(s => s.message.sid === p.SID.cfw).slice(-4)) ack(h.transport, item);
|
||||
await playing;
|
||||
assert.equal(h.session.state.phase, 'connected');
|
||||
await until(() => h.session.state.frames === 1);
|
||||
});
|
||||
|
||||
test('disconnect cancels queued buzzer work and does not play it on reconnect', async t => {
|
||||
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
|
||||
const playing = h.session.playBuzzerSequence(buzzerPayload());
|
||||
await until(() => buzzerMessages(h.transport).length === 1);
|
||||
h.transport.emit({ kind: 'disconnected', identifier: 'R' }); await playing;
|
||||
await h.session.playBuzzerSequence(buzzerPayload());
|
||||
h.transport.hold = false; await h.session.start(addresses, true);
|
||||
assert.equal(buzzerMessages(h.transport).length, 1);
|
||||
});
|
||||
|
||||
test('compass desired state survives connecting and reconnecting; explicit stop disables it first', async t => {
|
||||
const h = harness(t);
|
||||
h.session.setCompassEnabled(true);
|
||||
|
||||
@@ -98,7 +98,7 @@ test('local package runtimes unpack independently and clean up after closing or
|
||||
assert.equal(files.size, 0);
|
||||
});
|
||||
|
||||
function sessionHarness(overrides = {}) {
|
||||
function sessionHarness(overrides = {}, playBuzzer = () => {}) {
|
||||
const settings = new Map(), logs = [], renders = [];
|
||||
const load = loader({ global: { isIOS: true }, setTimeout, clearTimeout, Promise }, {
|
||||
'@nativescript/core': { ApplicationSettings: { getString: (k, d) => settings.get(k) ?? d, setString: (k, v) => settings.set(k, v) } },
|
||||
@@ -110,7 +110,7 @@ function sessionHarness(overrides = {}) {
|
||||
'../../native/location': {}, '../../native/location-tracker': {}, '../../native/location-permissions': {}, ...overrides,
|
||||
});
|
||||
const api = load('app/apps/evenhub/session.ts');
|
||||
const session = new api.EvenHubSession({ name: 'Probe', packageId: 'test.probe', permissions: [] }, '/test/dist', line => logs.push(line));
|
||||
const session = new api.EvenHubSession({ name: 'Probe', packageId: 'test.probe', permissions: [] }, '/test/dist', line => logs.push(line), '', playBuzzer);
|
||||
session.attachWindow({ requestRender: () => renders.push(1), windowId: 'probe', closeWindow() {} });
|
||||
const web = vm.createContext({ Promise, Map, Date, console });
|
||||
web.window = web;
|
||||
@@ -220,3 +220,17 @@ test('store login and catalog requests use platform signing with the shared wire
|
||||
assert.equal(requests[1].headers.sign, 'fixture-signature');
|
||||
assert.equal(signatures[0], api.signingParts('POST', '/v2/g/login', requests[0].headers.common, '', '', requests[0].body));
|
||||
});
|
||||
|
||||
|
||||
test('EvenHub buzzer RPC uses the host action on iOS and chunks and paces long sequences', async () => {
|
||||
const sounds = loader()('app/ui/sound-effects.ts'), played = [];
|
||||
const h = sessionHarness({ '../../ui/sound-effects': sounds }, bytes => played.push([...bytes]));
|
||||
const steps = Array.from({ length: 49 }, () => ({ freq: 3000, ms: 1, duty: 25 }));
|
||||
await h.session.dispatchExtension('playBuzzer', [steps]);
|
||||
assert.equal(played.length, 2);
|
||||
assert.deepEqual(played[0], [...sounds.buildSoundSequencePayload(steps.slice(0, 48))]);
|
||||
assert.deepEqual(played[1], [...sounds.buildSoundSequencePayload(steps.slice(48))]);
|
||||
await h.session.dispatchExtension('playBuzzer', [[null, { freq: 'bad', ms: 10 }]]);
|
||||
assert.equal(played.length, 2);
|
||||
h.session.close();
|
||||
});
|
||||
@@ -92,6 +92,8 @@ function fixture() {
|
||||
'../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': {}, '../apps/evenhub/updates': {}, '../apps/evenhub': {},
|
||||
'../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' },
|
||||
'../apps/all-apps': { ALL_APPS: [{ appId: 'glanceboard', glanceboard: provider }] },
|
||||
'../phone-ui/onboarding-state': { isWelcomeSoundPending: () => false },
|
||||
'../ui/sound-effects': {},
|
||||
'../ui/shell/worker-window': {}, '../ui/shell/in-process-window': {},
|
||||
'../ui/dashboard-settings': { lockScreenEnabledSetting: { get: () => settings.lock }, onAnySettingChanged: fn => { settingsChanged = fn; return () => {}; }, previewColorSetting: { get: () => 'white' } },
|
||||
'../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) },
|
||||
|
||||
@@ -49,6 +49,7 @@ function worker() {
|
||||
'../../graphics/ui-fonts': { getDefaultSmallFont: noop },
|
||||
'../../native/frame-timings': { finishFrame: noop, logFrame: noop },
|
||||
'../../native/active-display': {},
|
||||
'../../native/worker-buzzer': { playWorkerBuzzerSequence() {} },
|
||||
'../../native/settings-store': { getStringSetting: () => '0', setStringSetting: noop },
|
||||
'../../ui/sound-effects': {}, '../../ui/window-menu': {},
|
||||
'../../ui/sound-setting': { loadSoundEnabled: () => true, saveSoundEnabled: noop },
|
||||
|
||||
Reference in new issue
Block a user