Merge branch 'main' of https://github.com/jimrandomh/faceclaw into ring-health-fixes

This commit is contained in:
James Babcock committed 2026-11-01 11:00:38 -08:00
commit fcea0b369a
218 files changed
+14438 -2259

No files matched your search

+239
View File
@@ -0,0 +1,239 @@
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');
function load(file, modules = {}, globals = {}) {
const sandbox = { exports: {}, console, JSON, Promise, Date, Math, setTimeout, clearTimeout, require: id => {
if (!(id in modules)) throw new Error(`Unstubbed import: ${id}`);
return modules[id];
}, ...globals };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, sandbox);
return sandbox.exports;
}
/** Values from the sandbox carry its own Array/Object prototypes; compare their plain JSON form. */
const plain = value => JSON.parse(JSON.stringify(value));
const SPOTIFY = 'com.spotify.music';
const types = load('app/native/media-types.ts');
const base = { ...types.DEFAULT_MEDIA_STATE, accessEnabled: true };
const playing = (extra = {}) => ({ ...base, available: true, playbackState: 'playing', title: 'Song', ...extra });
const paused = (extra = {}) => ({ ...base, available: true, playbackState: 'paused', title: 'Song', ...extra });
function controllerApi() {
return load('app/native/media-controller.ts', {
'@nativescript/core': { Utils: {} },
'./image-files': {}, './media-apps': {}, './settings-store': {}, './notification-access': {},
'./media-types': types,
'./spotify': { spotifyService: () => null },
'./spotify-player': { SPOTIFY_ANDROID_PACKAGE: SPOTIFY },
});
}
/** A media-session bridge and a Web API player stand-in, each recording the commands it gets. */
function composite(sessionState, spotifyState) {
const api = controllerApi();
const calls = [];
const fake = (name, state, extra = {}) => ({
accessPrompt: `${name} prompt`, accessLabel: `${name} label`, accessAction: `${name} action`, skipRebuildsQueue: true,
state, queue: [], volume: name === 'session' ? 30 : 70, lastPlayingAtMs: 0, listeners: [],
onStateChange(listener) { this.listeners.push(listener); listener(this.state); return () => calls.push(`${name}:unsubscribe`); },
snapshot() { return this.state; },
start: async () => calls.push(`${name}:start`), stop: async () => calls.push(`${name}:stop`),
playPause: async () => calls.push(`${name}:playPause`), skipNext: async () => calls.push(`${name}:next`),
skipPrevious: async () => calls.push(`${name}:previous`),
skipToQueueItem: async id => calls.push(`${name}:skipTo:${id}`),
getQueue() { calls.push(`${name}:queue`); return this.queue; },
getMediaVolumePercent() { return this.volume; }, setMediaVolumePercent: value => calls.push(`${name}:volume:${value}`),
getAlbumArt: () => name,
hasAccess: () => name === 'session', requestAccess: async () => calls.push(`${name}:access`),
sendCustomAction: async action => calls.push(`${name}:custom:${action}`),
refresh: async () => calls.push(`${name}:refresh`),
...extra,
});
const session = fake('session', sessionState);
const spotify = fake('spotify', spotifyState);
let now = 100;
const bridge = new api.AndroidMediaControllerBridge(session, () => (spotifyState ? spotify : null), () => now);
return { api, bridge, calls, session, spotify, tick: ms => { now += ms; } };
}
test('the Android bridge chooses between the media session and Spotify\'s Web API player', () => {
const { chooseAndroidMediaSource: choose } = controllerApi();
const spotifySession = (extra) => ({ ...extra, packageName: SPOTIFY });
// Signed out (or no Web API player): always the session.
assert.equal(choose('spotify', paused(), null, 0, 0), 'session');
assert.equal(choose('spotify', paused(), { ...playing(), accessEnabled: false }, 0, 0), 'session');
// Without notification access, Spotify when it has something, else the session's access prompt.
assert.equal(choose('session', { ...base, accessEnabled: false }, paused(), 0, 0), 'spotify');
assert.equal(choose('session', { ...base, accessEnabled: false }, base, 0, 0), 'session');
// Playing wins.
assert.equal(choose('session', paused({ packageName: 'org.videolan.vlc' }), playing(), 0, 0), 'spotify');
assert.equal(choose('spotify', playing({ packageName: 'org.videolan.vlc' }), paused(), 0, 0), 'session');
// Both playing: Spotify's own session is the same playback; another player keeps the current choice.
assert.equal(choose('spotify', spotifySession(playing()), playing(), 0, 0), 'session');
assert.equal(choose('spotify', playing({ packageName: 'org.videolan.vlc' }), playing(), 0, 0), 'spotify');
assert.equal(choose('session', playing({ packageName: 'org.videolan.vlc' }), playing(), 0, 0), 'session');
// Neither playing: Spotify's session, then the one that played last, then whichever has something.
assert.equal(choose('spotify', spotifySession(paused()), paused(), 0, 50), 'session');
assert.equal(choose('session', paused({ packageName: 'org.videolan.vlc' }), paused(), 10, 50), 'spotify');
assert.equal(choose('spotify', paused({ packageName: 'org.videolan.vlc' }), paused(), 50, 10), 'session');
assert.equal(choose('spotify', paused({ packageName: 'org.videolan.vlc' }), paused(), 0, 0), 'session');
assert.equal(choose('session', base, paused(), 0, 0), 'spotify');
assert.equal(choose('session', base, base, 0, 0), 'session');
});
test('a Spotify session keeps its transport and art and gains the Web API\'s rows and device volume', async () => {
const actions = [{ action: 'shuffle', name: 'Shuffle: off' }, { action: 'repeat', name: 'Repeat: off' }, { action: 'like', name: 'Like song' }];
const c = composite(playing({ packageName: SPOTIFY, appName: 'Spotify', title: 'Song a', artVersion: 4 }),
playing({ packageName: SPOTIFY, appName: 'Spotify · Kitchen', title: 'Song a', customActions: actions }));
let state = c.bridge.snapshot();
assert.equal(state.title, 'Song a');
assert.equal(state.appName, 'Spotify · Kitchen');
assert.deepEqual(plain(state.customActions.map(action => action.action)), ['shuffle', 'repeat', 'like']);
assert.equal(c.bridge.accessPrompt, 'session prompt');
await c.bridge.playPause(); await c.bridge.skipNext();
await c.bridge.sendCustomAction('like');
assert.equal(c.bridge.getAlbumArt(112), 'session');
// Volume follows Spotify (a Connect speaker's own volume, or this phone's).
assert.equal(c.bridge.getMediaVolumePercent(), 70);
c.bridge.setMediaVolumePercent(55);
assert.deepEqual(c.calls, ['session:playPause', 'session:next', 'spotify:custom:like', 'spotify:volume:55']);
// After a skip the API still describes the previous track: "Like song" waits until it catches up.
c.session.state = { ...c.session.state, title: 'Song b' };
state = c.bridge.snapshot();
assert.deepEqual(plain(state.customActions.map(action => action.action)), ['shuffle', 'repeat']);
assert.equal(state.appName, 'Spotify');
// Another player's session gets no Spotify rows, and its own volume.
c.session.state = playing({ packageName: 'org.videolan.vlc', appName: 'VLC' });
state = c.bridge.snapshot();
assert.equal(state.customActions, undefined);
assert.equal(c.bridge.getMediaVolumePercent(), 30);
});
test('Spotify playing on another device, with no session here, follows the Web API player', async () => {
const c = composite({ ...base, status: 'No session' }, playing({ packageName: SPOTIFY, appName: 'Spotify · MacBook', title: 'Remote', artVersion: 1 }));
const first = c.bridge.snapshot();
assert.equal(first.title, 'Remote');
await c.bridge.playPause(); await c.bridge.skipPrevious(); c.bridge.setMediaVolumePercent(20);
assert.equal(c.bridge.getAlbumArt(64), 'spotify');
assert.deepEqual(c.calls, ['spotify:playPause', 'spotify:previous', 'spotify:volume:20']);
// Switching sources changes the art version even when the sources' versions match.
c.spotify.state = paused({ packageName: SPOTIFY, artVersion: 1, notice: 'Controlling Spotify playback needs Spotify Premium.' });
c.session.state = playing({ packageName: 'org.videolan.vlc', title: 'Local', artVersion: 1 });
const second = c.bridge.snapshot();
assert.equal(second.title, 'Local');
assert.notEqual(second.artVersion, first.artVersion);
// A Spotify failure still shows while the session is the one shown.
assert.equal(second.notice, 'Controlling Spotify playback needs Spotify Premium.');
});
test('the queue comes from the session, or the Web API when a Spotify session lists none, and jumps go to its source', async () => {
const c = composite(playing({ packageName: SPOTIFY }), playing({ packageName: SPOTIFY }));
c.session.queue = [{ id: 7, title: 'Next', active: false }];
assert.equal(c.bridge.getQueue()[0].id, 7);
await c.bridge.skipToQueueItem(7);
c.session.queue = [];
c.spotify.queue = [{ id: 1, title: 'Up next', subtitle: 'Band', active: false }];
assert.equal(c.bridge.getQueue()[0].title, 'Up next');
await c.bridge.skipToQueueItem(1);
assert.deepEqual(c.calls.filter(call => call.includes('skipTo')), ['session:skipTo:7', 'spotify:skipTo:1']);
// Not Spotify's session: its (empty) queue stands.
c.session.state = playing({ packageName: 'org.videolan.vlc' });
assert.deepEqual(plain(c.bridge.getQueue()), []);
});
test('a Spotify session changing track re-reads the Web API at once; listeners hear both sources', () => {
const c = composite(playing({ packageName: SPOTIFY, title: 'Song a' }), playing({ packageName: SPOTIFY, title: 'Song a' }));
const heard = [];
const unsubscribe = c.bridge.onStateChange(state => heard.push(state.title));
assert.deepEqual(c.calls, ['spotify:refresh']);
c.session.listeners[0]({ ...c.session.state, positionMs: 5 });
assert.deepEqual(c.calls, ['spotify:refresh']); // same track and play state
c.session.state = playing({ packageName: SPOTIFY, title: 'Song b' });
c.session.listeners[0](c.session.state);
c.spotify.listeners[0](c.spotify.state);
assert.deepEqual(c.calls, ['spotify:refresh', 'spotify:refresh']);
assert.ok(heard.length >= 4);
unsubscribe();
assert.deepEqual(c.calls.slice(2), ['session:unsubscribe', 'spotify:unsubscribe']);
});
test('signed out of Spotify, the Android bridge is the media session', async () => {
const c = composite(playing({ packageName: SPOTIFY, title: 'Song' }), { ...base, accessEnabled: false, status: 'Not signed in to Spotify.' });
const state = c.bridge.snapshot();
assert.equal(state.title, 'Song');
assert.equal(state.customActions, undefined);
await c.bridge.playPause(); await c.bridge.start(); await c.bridge.requestAccess();
assert.equal(c.bridge.hasAccess(), true);
assert.equal(c.bridge.getMediaVolumePercent(), 30);
assert.deepEqual(c.calls, ['session:playPause', 'session:start', 'session:access']);
});
function browser({ installed = true, signedIn = true, enabled = true } = {}) {
const api = load('app/native/media-browser.ts', {
'@nativescript/core': { Utils: {} }, './media-apps': {}, './settings-store': {},
'./media-types': types,
'./spotify': { spotifyService: () => null },
'./spotify-player': { SPOTIFY_ANDROID_PACKAGE: SPOTIFY },
});
const calls = [];
const vlc = { packageName: 'org.videolan.vlc', serviceClass: 'VlcBrowser', appName: 'VLC' };
const spotifyService = { packageName: SPOTIFY, serviceClass: 'SpotifyMediaBrowserService', appName: 'Spotify' };
const services = {
listBrowsableApps: refresh => { calls.push(`services:list:${Boolean(refresh)}`); return installed ? [spotifyService, vlc] : [vlc]; },
connect: async app => { calls.push(`services:connect:${app.packageName}`); return 'vlc-root'; },
browse: async parentId => { calls.push(`services:browse:${parentId}`); return []; },
playFromMediaId: mediaId => calls.push(`services:play:${mediaId}`),
disconnect: () => calls.push('services:disconnect'),
};
const libraryApp = { packageName: SPOTIFY, serviceClass: '', appName: 'Spotify' };
const library = {
app: libraryApp,
listBrowsableApps: () => (signedIn ? [libraryApp] : []),
connect: async app => {
calls.push(`library:connect:${app.serviceClass}`);
if (!signedIn) throw new Error('Sign in under Spotify in the Music app menu first.');
return 'root';
},
browse: async parentId => { calls.push(`library:browse:${parentId}`); return []; },
playFromMediaId: mediaId => calls.push(`library:play:${mediaId}`),
disconnect: () => calls.push('library:disconnect'),
};
const bridge = new api.AndroidMediaBrowserBridge(services, () => library, () => enabled);
return { bridge, calls, vlc, spotifyService };
}
test('Spotify\'s library row opens the Web API library, or explains how to sign in', async () => {
const b = browser();
const apps = b.bridge.listBrowsableApps(true);
assert.deepEqual(plain(apps.map(app => app.appName)), ['Spotify', 'VLC']);
assert.equal(await b.bridge.connect(apps[0]), 'root');
await b.bridge.browse('playlists');
b.bridge.playFromMediaId('playlist:mine');
assert.equal(await b.bridge.connect(b.vlc), 'vlc-root');
await b.bridge.browse('vlc-root');
b.bridge.disconnect();
await assert.rejects(b.bridge.browse('root'), /Not connected/);
assert.deepEqual(b.calls, ['services:list:true', 'library:connect:', 'library:browse:playlists', 'library:play:playlist:mine',
'library:disconnect', 'services:connect:org.videolan.vlc', 'services:browse:vlc-root', 'services:disconnect']);
const signedOut = browser({ signedIn: false });
const [spotify] = signedOut.bridge.listBrowsableApps();
await assert.rejects(signedOut.bridge.connect(spotify), /Sign in under Spotify/);
// Not installed: the library is offered once signed in (it plays on other devices), unless Spotify is ignored.
assert.deepEqual(plain(browser({ installed: false }).bridge.listBrowsableApps().map(app => app.appName)), ['VLC', 'Spotify']);
assert.deepEqual(plain(browser({ installed: false, signedIn: false }).bridge.listBrowsableApps().map(app => app.appName)), ['VLC']);
assert.deepEqual(plain(browser({ installed: false, enabled: false }).bridge.listBrowsableApps().map(app => app.appName)), ['VLC']);
});
+29 -24
View File
@@ -349,11 +349,13 @@ test('the mirror\'s crop follows Preview scaling, the Glanceboard and the lock,
// The real chrome layer over the given geometry, with platform pieces stubbed:
// a 4px-per-character font that paints each character as a 2x6 block, up to
// `notifications` 24px notification icons (keyed n0, n1, ...), and a phone
// `notifications` notification icons (keyed n0, n1, ...), and a phone
// battery at 80%.
const graphics = require('../.test-build/app/graphics/image.js');
const shellScene = require('../.test-build/app/graphics/shell-scene.js');
const displayList = require('../.test-build/app/graphics/display-list.js');
const notificationTypes = require('../.test-build/app/native/notification-types.js');
const ICON = notificationTypes.NOTIFICATION_ICON_SIZE;
function chromeLayer(g, { notifications = 0, phoneBattery = null, windows = 1, trayIcons = [] } = {}) {
const font = { lineHeight: 12, ascent: 10, measureText: (text) => text.length * 4,
drawText: (image, x, y, text, value) => { for (let i = 0; i < text.length; i++) image.fillRect(x + i * 4, y + 2, 2, 6, value); } };
@@ -369,9 +371,10 @@ function chromeLayer(g, { notifications = 0, phoneBattery = null, windows = 1, t
'../../native/notification-icons': { ALL_NOTIFICATIONS: 0x7fffffff, readActiveNotificationIcons: (max) => {
requested.push(max);
const count = Math.min(max, notifications);
return { icons: Array.from({ length: count }, () => new graphics.GrayImage(24, 24, 200)),
return { icons: Array.from({ length: count }, () => new graphics.GrayImage(ICON, ICON, 200)),
keys: Array.from({ length: count }, (_, index) => `n${index}`), stale: false };
} },
'../../native/notification-types': notificationTypes,
'../../native/phone-battery': { readPhoneBatteryState: () => ({ battery: phoneBattery, charging: false }) },
'../../util/render-freshness': { renderPassAllowsStaleData: () => false },
'../dashboard-settings': { batteryDisplayModeSetting: { get: () => 'icon' }, phoneBatteryVisibilitySetting: settings,
@@ -536,9 +539,9 @@ test('the switcher row lays out windows, free space, notifications, widgets, bat
// Right to left from the row's end (x=608), centred under its separator
// line (y=366): "Thu Oct 2 9:41" (56px) flush with it; the battery block (label
// 20 + gap 5 + gauge 21) 16px before it; the 30px widget 10px before that;
// then the two notification icons, 8px clear of the widget. The surface
// starts 2px before them, room for a selection box round the first.
const clockX = 608 - 56, batteryLeft = clockX - 16 - 46, trayLeft = batteryLeft - 40, iconsLeft = trayLeft - 8 - 56 + 4;
// then the two notification icons (4px apart), 8px clear of the widget.
// The surface starts 2px before them, room for a selection box round the first.
const clockX = 608 - 56, batteryLeft = clockX - 16 - 46, trayLeft = batteryLeft - 40, iconsLeft = trayLeft - 8 - 2 * (ICON + 4) + 4;
assert.deepEqual([status.x, status.y, status.image.width, status.image.height], [iconsLeft - 2, 367, 608 - iconsLeft + 2, 35]);
const at = (p, x, y) => p.image.withDrawsBaked().pixels[(y - p.y) * p.image.width + x - p.x];
// The clock's text runs 8 rows down the top bar's layout, which sits 5 rows into the row.
@@ -567,19 +570,20 @@ test('past two rows of windows, they scroll, and notification icons give way to
assert.equal(g.switcherRowHeight(), 72);
assert.equal(heightChanges(), 1);
assert.deepEqual([row.y, row.image.height], [366, 72]);
// Notification icons drop from the end until the two status rows leave
// three window slots a row (32 + 20 + 3 * 40 - 8, and the gap): 28 stay,
// 15 in the first row (416px) and 13 with the clock in the second (432px).
assert.equal(chrome.statusItems().length, 28);
assert.deepEqual([status.x, status.y, status.image.height], [608 - 432 - 2, 367, 71]);
// Notification icons (29px apart) drop from the end until the two status
// rows leave three window slots a row (32 + 20 + 3 * 40 - 8, and the gap):
// 27 stay, 15 in the first row (431px) and 12 with the clock in the second
// (416px).
assert.equal(chrome.statusItems().length, 27);
assert.deepEqual([status.x, status.y, status.image.height], [608 - 431 - 2, 367, 71]);
const at = (p, x, y) => p.image.withDrawsBaked().pixels[(y - p.y) * p.image.width + x - p.x];
assert.deepEqual([at(status, 608 - 416, 366 + 5 + 2), at(status, 608 - 432, 366 + 36 + 5 + 2)], [200, 200]);
assert.deepEqual([at(status, 608 - 431, 366 + 5 + 2), at(status, 608 - 416, 366 + 36 + 5 + 2)], [200, 200]);
// Three windows a row; the rest scroll, with the chevron after the second
// row's, at the end of the windows rather than of the row.
assert.equal(chrome.windowIndexAt(32 + 10 + 2 * 40 + 4, 380, 12), 2);
assert.equal(chrome.windowIndexAt(32 + 10 + 3 * 40 + 4, 380, 12), null);
assert.equal(chrome.windowIndexAt(32 + 10 + 2 * 40 + 4, 420, 12), 5);
const windowsRight = 608 - 432 - 8;
const windowsRight = 608 - 431 - 8;
const chevron = row.image.pixels.findIndex((v) => v === 140);
assert.deepEqual([chevron % row.image.width + row.x, Math.floor(chevron / row.image.width) + row.y], [windowsRight - 6, 366 + 36 + 18 - 5]);
});
@@ -655,11 +659,11 @@ test('the status bar takes a second row of its own before the windows do', () =>
chrome.paintParts();
assert.equal(heightChanges(), 1, 'reported once');
assert.deepEqual(g.rect('min'), before);
// Both rows right-aligned against the row's end: 8 icons (220px) over 6
// and the clock (236px), a top bar's height apart.
assert.deepEqual([status.x, status.y, status.image.height], [608 - 236 - 2, 367, 63]);
// Both rows right-aligned against the row's end: 8 icons (228px) over 6
// and the clock (242px), a top bar's height apart.
assert.deepEqual([status.x, status.y, status.image.height], [608 - 242 - 2, 367, 63]);
const at = (p, x, y) => p.image.withDrawsBaked().pixels[(y - p.y) * p.image.width + x - p.x];
assert.deepEqual([at(status, 608 - 220, 371 + 2), at(status, 608 - 236, 371 + 28 + 2), at(status, 608 - 56, 371 + 28 + 8 + 2)],
assert.deepEqual([at(status, 608 - 228, 371 + 2), at(status, 608 - 242, 371 + 28 + 2), at(status, 608 - 56, 371 + 28 + 8 + 2)],
[200, 200, 210]);
assert.deepEqual(keysOf(chrome.statusItems()), Array.from({ length: 14 }, (_, i) => `n${i}`));
// The windows keep one row, against the window.
@@ -673,19 +677,20 @@ test('windows past one row take a second, where a selected one sits in a box', (
g.settings.appSwitcherPositionSetting.value = 'bottom';
g.settings.statusBarPositionSetting.value = 'bottom';
const { chrome, state } = chromeLayer(g, { notifications: 14, windows: 9 });
state.selectedIndex = 8;
state.selectedIndex = 7;
const [row, status] = chrome.paintParts();
assert.equal(g.switcherRowHeight(), 72);
// Eight slots a row (what the 236px status rows leave); the ninth window
// Seven slots a row (what the 242px status rows leave); the eighth window
// starts the second row, under the first.
assert.deepEqual([chrome.windowIndexAt(31 + 7 * 40, 380, 9), chrome.windowIndexAt(31, 410, 9), chrome.windowIndexAt(71, 410, 9)], [7, 8, null]);
assert.deepEqual([chrome.windowIndexAt(31 + 6 * 40, 380, 9), chrome.windowIndexAt(31 + 7 * 40, 380, 9), chrome.windowIndexAt(31, 410, 9),
chrome.windowIndexAt(71, 410, 9), chrome.windowIndexAt(111, 410, 9)], [6, null, 7, 8, null]);
const at = (p, x, y) => p.image.pixels[(y - p.y) * p.image.width + x - p.x];
// Its selection is a box (as for a sidebar overflow column), and the
// separator runs unbroken over the first row.
assert.deepEqual([at(row, 31, 402 + 18), at(row, 31 + 5, 402 + 7)], [150, 120]);
assert.equal(at(row, 50, 366), 40);
// The status rows line up with the window rows.
assert.equal(status.image.withDrawsBaked().pixels[(366 + 36 + 5 + 2 - status.y) * status.image.width + 608 - 236 - status.x], 200);
assert.equal(status.image.withDrawsBaked().pixels[(366 + 36 + 5 + 2 - status.y) * status.image.width + 608 - 242 - status.x], 200);
});
test('a popup app switcher shows a box of window icons over the dimmed window while focused', () => {
@@ -777,13 +782,13 @@ test('a notification the switcher selects sits in a box, filled with its icon in
Object.assign(state, { selectedIndex: -1, selectedStatusItem: notification('n1'), focus: 'sidebar' });
let [row, status] = chrome.paintParts();
assert.deepEqual(keysOf(chrome.statusItems()), ['n0', 'n1', 'n2']);
// Clock only: the icons end 8px before its 8px lead-in, 28px apart, each
// 7 rows into the row (the top bar's layout, 5 rows down).
const iconsLeft = 608 - 56 - 8 - 8 - 3 * 28 + 4, iconY = 366 + 7, n1 = iconsLeft + 28;
// Clock only: the icons end 8px before its 8px lead-in, 4px apart, each
// centred in the top bar's 28 rows (its layout 5 rows down the row).
const step = ICON + 4, iconsLeft = 608 - 56 - 8 - 8 - 3 * step + 4, iconY = 366 + 5 + ((28 - ICON) >> 1), n1 = iconsLeft + step;
const at = (p, x, y) => p.image.withDrawsBaked().pixels[(y - p.y) * p.image.width + x - p.x];
// The box: 2px either side of the icon, 1px above and below, white; the
// icon inside it inverted (bright, so to black); its neighbours as ever.
assert.deepEqual([at(status, n1 - 2, iconY + 12), at(status, n1 + 25, iconY + 12), at(status, n1 + 12, iconY - 1)], [255, 255, 255]);
assert.deepEqual([at(status, n1 - 2, iconY + 12), at(status, n1 + ICON + 1, iconY + 12), at(status, n1 + 12, iconY - 1)], [255, 255, 255]);
assert.equal(at(status, n1 + 12, iconY - 2), 1);
assert.deepEqual([at(status, n1 + 5, iconY + 5), at(status, iconsLeft + 5, iconY + 5)], [1, 200]);
// No window is selected: the separator runs unbroken over the windows.
@@ -0,0 +1,150 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
let url = initialUrl;
let model = initialModel;
let webSearch = false;
const dummySetting = {
id: 'setting', label: 'Setting', get: () => '', set() {}, displayValue: () => '',
};
const urlSetting = {
id: 'compatible-url', label: 'Compatible API URL', get: () => url, set: value => { url = value; },
displayValue: () => url,
};
const modelSetting = {
id: 'assistant-model', label: 'Assistant model', get: () => model, set: value => { model = value; },
displayValue: () => model,
};
const searchSetting = {
id: 'venice-web-search', label: 'Venice web search', get: () => webSearch,
set: value => { webSearch = value; }, toggle: () => { webSearch = !webSearch; },
defaultValue: false, description: 'Venice-only web search.',
};
const row = setting => ({ label: setting.label, description: setting.description, onSelect() {} });
const dashboardSettings = new Proxy({
assistantModelSetting: modelSetting,
assistantOpenAiCompatibleUrlSetting: urlSetting,
assistantOpenAiCompatibleKeySetting: { ...dummySetting, id: 'compatible-key', label: 'Compatible API key' },
assistantOpenAiCompatibleModelSetting: { ...dummySetting, id: 'compatible-model', label: 'Compatible model ID' },
assistantHermesUrlSetting: { ...dummySetting, id: 'assistant-hermes-url', label: 'Hermes URL' },
assistantHermesKeySetting: { ...dummySetting, id: 'assistant-hermes-key', label: 'Hermes key' },
veniceWebSearchSetting: searchSetting,
enumSettingMenuItem: row,
textSettingMenuItem: row,
toggleSettingMenuItem: row,
}, {
get(target, property) {
return property in target ? target[property] : dummySetting;
},
});
const load = loader({ global: { isIOS: false, isAndroid: true } }, {
'./remote-input-menu': { remoteInputMenuItem: () => ({ label: 'Remote input' }) },
'@nativescript/core': { knownFolders: {} },
'../../graphics/ui-fonts': { getDefaultSmallFont: () => ({}) },
'../../graphics/logo': { getDashboardLogo: () => null },
'../../graphics/textwrap': { wrapText: () => [] },
'../../version': { FACECLAW_VERSION: 'test' },
'../../native/local-llm': {
cancelLocalModelDownload() {}, deleteLocalModel() {}, startLocalModelDownload() {},
LOCAL_MODEL: { label: 'Qwen', sizeBytes: 1_000_000_000 },
localModelState: () => ({ status: 'missing' }), onLocalModelStateChanged: () => () => {},
},
'../../native/asr-model': {
ASR_MODELS: {
moonshine: { label: 'Moonshine', sizeBytes: 1 },
'whisper-base-en': { label: 'Whisper Base English', sizeBytes: 1 },
},
asrModelState: () => ({}), cancelAsrModelDownload() {},
deleteAsrModel() {}, startAsrModelDownload() {}, onAsrModelStateChanged: () => () => {},
},
'../../apps/files/text-viewer': { TextViewerLayer: class {} },
'../../analytics/privacy-disclosure': { IOS_PRIVACY_NOTICE: '' },
'../menu': {
drawRightValueMenuItem() {}, openModalMenu() {},
submenuItem: label => ({ label, onSelect() {} }),
},
'../shell/shell': { shell: { noteUserActivity() {} } },
'../dashboard-settings': dashboardSettings,
'../../native/wear-bridge': { wearBridge: {} },
'./settings-panel': {
SettingsPanelLayer: class { constructor(sections) { this.sections = sections; } },
openSettingsSubMenu() {},
},
'../font-picker': { uiFontPickerMenuItem: () => ({ label: 'Font' }) },
'../openai-compatible-model-picker': {
compatibleModelDiscoveryMenuItem: () => ({ label: 'Discover models' }),
},
'./sse': {},
});
const panel = load('app/ui/dashboard/settings-menus.ts').createSettingsPanelLayer();
const section = panel.sections.find(item => item.label === 'Assistant');
assert.ok(section);
return {
section,
labels: () => section.items.map(item => item.label),
setUrl: value => { url = value; },
setModel: value => { model = value; },
searchSetting,
};
}
test('Venice web search row follows the current URL and never appears for other providers', () => {
const env = settingsEnv();
assert.equal(env.labels().includes('Venice web search'), false);
assert.equal(env.searchSetting.defaultValue, false);
assert.equal(env.searchSetting.get(), false);
env.setUrl(' https://api.venice.ai/api/v1/ ');
assert.equal(env.labels().includes('Venice web search'), true);
env.setUrl('https://openwebui.example/api');
assert.equal(env.labels().includes('Venice web search'), false);
env.setUrl('https://api.venice.ai/api/v1/chat/completions');
assert.equal(env.labels().includes('Venice web search'), true);
env.setUrl('https://api.venice.ai.evil.example/api/v1');
assert.equal(env.labels().includes('Venice web search'), false);
env.setUrl('http://api.venice.ai/api/v1');
assert.equal(env.labels().includes('Venice web search'), false);
});
test('Venice web search row also requires the OpenAI-compatible model to be selected', () => {
const env = settingsEnv('https://api.venice.ai/api/v1', 'sonnet');
assert.equal(env.labels().includes('Venice web search'), false);
env.setModel('hermes');
assert.equal(env.labels().includes('Venice web search'), false);
env.setModel('openai-compatible');
assert.equal(env.labels().includes('Venice web search'), true);
});
test('Hermes URL and key rows appear only while Hermes agent is the selected model', () => {
const env = settingsEnv('', 'auto');
// Labels come from the module's VM realm; compare by value.
const hermesRows = () => JSON.stringify(env.labels().filter(label => label.startsWith('Hermes')));
assert.equal(hermesRows(), '[]');
env.setModel('openai-compatible');
assert.equal(hermesRows(), '[]');
env.setModel('hermes');
assert.equal(hermesRows(), JSON.stringify(['Hermes URL', 'Hermes key']));
const labels = env.labels();
assert.equal(labels.indexOf('Hermes URL'), labels.indexOf('Assistant model') + 1, 'Hermes rows follow the model row');
});
test('OpenAI-compatible rows appear only while OpenAI-compatible is the selected model', () => {
const env = settingsEnv('https://api.venice.ai/api/v1', 'auto');
const compatibleRows = () => JSON.stringify(env.labels().filter(label =>
label.startsWith('Compatible') || label === 'Discover models' || label === 'Venice web search'));
assert.equal(compatibleRows(), '[]');
env.setModel('hermes');
assert.equal(compatibleRows(), '[]');
env.setModel('openai-compatible');
assert.equal(compatibleRows(), JSON.stringify([
'Compatible API URL', 'Compatible API key', 'Discover models', 'Compatible model ID', 'Venice web search',
]));
const labels = env.labels();
assert.equal(labels.indexOf('Compatible API URL'), labels.indexOf('Assistant model') + 1, 'compatible rows follow the model row');
});
+25 -3
View File
@@ -2,7 +2,7 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function harness(provider = 'elevenlabs', continuous = true) {
function harness(provider = 'elevenlabs', continuous = true, extraOptions = {}) {
const timers = new Map();
let timerId = 0;
const sockets = [];
@@ -30,7 +30,7 @@ function harness(provider = 'elevenlabs', continuous = true) {
const Provider = load(`app/native/${provider}-stt.ts`)[names[provider]];
const { ReconnectingSttClient } = load('app/native/reconnecting-stt.ts');
const client = new ReconnectingSttClient(options => new Provider(options), {
apiKey: 'test', onTranscript: event => events.push(event),
apiKey: 'test', ...extraOptions, onTranscript: event => events.push(event),
onStatus: status => statuses.push(status), onError: error => errors.push(error),
}, () => continuous);
client.start();
@@ -203,7 +203,10 @@ test('provider selection falls back to on-device without a key and connects the
FaceclawWebSocket: class { constructor(url, _listener, header, value) { sockets.push({ url, header, value }); } },
} } },
}, { './cloud-stt': { CLOUD_STT_SAMPLE_RATE: 16000, encodeBase64: () => '', toJavaBytes: bytes => bytes } });
const { createCloudSttClient, usesCloudStt } = load('app/native/cloud-stt-provider.ts');
const { createCloudSttClient, usesCloudStt, transcriptionKey } = load('app/native/cloud-stt-provider.ts');
// A running capture is shared only on the same provider and language.
assert.equal(transcriptionKey({ provider: 'soniox', language: ' JA ' }), 'soniox:ja');
assert.notEqual(transcriptionKey({ provider: 'soniox' }), transcriptionKey({ provider: 'soniox', language: 'ja' }));
const statuses = [];
const options = { onTranscript() {}, onStatus: status => statuses.push(status), onError() {} };
const selection = { provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: ' ', sonioxApiKey: 'soniox-key' };
@@ -217,4 +220,23 @@ test('provider selection falls back to on-device without a key and connects the
createCloudSttClient({ ...selection, provider: 'elevenlabs', elevenLabsApiKey: ' xi ' }, options, () => false).start();
assert.deepEqual(sockets.map(({ url, header, value }) => [new URL(url).host, header, value]),
[['stt-rt.soniox.com', null, null], ['api.elevenlabs.io', 'xi-api-key', 'xi']]);
createCloudSttClient({ ...selection, provider: 'elevenlabs', elevenLabsApiKey: 'xi', language: ' ja ' }, options, () => false).start();
assert.equal(new URL(sockets.at(-1).url).searchParams.get('language_code'), 'ja');
});
test('a spoken-language hint reaches each provider on the wire, and is left out to auto-detect', () => {
for (const language of ['ja', '']) {
const elevenlabs = harness('elevenlabs', true, { language });
assert.equal(new URL(elevenlabs.sockets[0].url).searchParams.get('language_code'), language || null);
const soniox = harness('soniox', true, { language });
soniox.open();
assert.deepEqual(JSON.parse(soniox.sockets[0].sent[0]).language_hints, language ? ['ja'] : undefined);
const openai = harness('openai', true, { language });
openai.open();
const transcription = JSON.parse(openai.sockets[0].sent[0]).session.audio.input.transcription;
assert.equal(transcription.model, 'gpt-realtime-whisper');
assert.equal(transcription.language, language || undefined);
}
});
+76
View File
@@ -0,0 +1,76 @@
const test = require('node:test');
const assert = require('node:assert');
const { execFileSync } = require('node:child_process');
const { readFileSync, readdirSync, statSync } = require('node:fs');
const { join } = require('node:path');
test('config_document converts Android XML, passes JSON documents through and rejects unusable files', () => {
execFileSync('python3', ['-c', `
import importlib.util, json, pathlib, sys, tempfile
sys.path.insert(0, 'scripts')
spec = importlib.util.spec_from_file_location('ios_config', 'scripts/ios_config.py')
ios = importlib.util.module_from_spec(spec); spec.loader.exec_module(ios)
import config_document as m
xml = b'''<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="terminal.connections">[{&quot;url&quot;:&quot;g2mirror://test@host&quot;}]</string>
<boolean name="flag" value="true" />
<string name="empty"></string>
<long name="number" value="42" />
<float name="ratio" value="0.25" />
<string name="unicode">&#960; &amp; &lt; &#128267;&#10;next</string>
</map>'''
with tempfile.TemporaryDirectory() as d:
p = pathlib.Path(d) / 'settings'
p.write_bytes(xml)
doc = json.loads(m.read_document(p))
assert doc['schema'] == 1 and doc['platform'] == 'android', doc
assert doc['settings'] == {'terminal.connections': '[{"url":"g2mirror://test@host"}]', 'flag': True, 'empty': '', 'number': 42, 'ratio': 0.25, 'unicode': '\\u03c0 & < \\U0001f50b\\nnext'}, doc
text = json.dumps({'schema': 2, 'platform': 'ios', 'settings': {'a': [1, {'b': None is None}]}, 'platformSettings': {'ios.x': 'y'}})
p.write_text(text)
assert m.read_document(p) == text
ios.atomic_write(p, b'{}')
assert p.stat().st_mode & 0o777 == 0o600
for invalid in [b'{}', b'[]', b'{"schema":3,"settings":{}}', b'{"schema":true,"settings":{}}', b'{"schema":2.0,"settings":{}}', b'{"schema":2}',
b'{"schema":2,"settings":{"x":null}}', b'{"schema":2,"settings":{"x":NaN}}', b'{"schema":2,"settings":{"x":1e999}}',
b'{"schema":2,"settings":{"x":1,"x":2}}', b'{"schema":2,"settings":{},"platformSettings":[]}',
b'{"schema":2,"platform":1,"settings":{}}', b'{"schema":2,"settings":{"":"x"}}',
b'<map><set name="x"/></map>', b'<map><boolean name="x" value="maybe"/></map>',
b'<map><string name="x">a</string><string name="x">b</string></map>', b'<map><string name="x">oops']:
p.write_bytes(invalid)
try: m.read_document(p)
except (ValueError, m.ET.ParseError): pass
else: raise AssertionError(f'accepted invalid config {invalid!r}')
`], { stdio: 'inherit' });
});
// FaceclawSettingsPortReceiver exports and imports the keys the app keeps in NativeScript's
// ApplicationSettings (prefs.db) from that file, by a fixed list. A new ApplicationSettings
// caller needs its keys added there (nativeScriptKey) and here.
test('settings stored outside FaceclawSettings are the ones the Android config port knows about', () => {
const known = new Map([
['app/g2/device-addresses.ts', ['deviceAddress.', 'deviceIdentity.']],
['app/phone-ui/onboarding-state.ts', ['onboarding.complete', 'onboarding.previewOnly']],
['app/apps/evenhub/session.ts', ['evenhub:']],
// iOS-only: NSUserDefaults holds every setting there.
['app/native/ios-bluetooth.ts', null],
['app/native/settings-store.ios.ts', null],
]);
const users = [];
const walk = (dir) => {
for (const name of readdirSync(dir)) {
const path = join(dir, name);
if (statSync(path).isDirectory()) walk(path);
else if (path.endsWith('.ts') && /\bApplicationSettings\b/.test(readFileSync(path, 'utf8'))) users.push(path);
}
};
walk('app');
assert.deepStrictEqual(users.sort(), [...known.keys()].sort());
const receiver = readFileSync('App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.kt', 'utf8');
for (const [file, keys] of known) {
for (const key of keys ?? []) {
assert.ok(receiver.includes(`"${key}"`), `${file}: FaceclawSettingsPortReceiver.nativeScriptKey is missing ${key}`);
assert.ok(readFileSync(file, 'utf8').includes(key) || file.endsWith('session.ts'), `${file} no longer uses ${key}`);
}
}
});
+2 -2
View File
@@ -91,7 +91,7 @@ test('a restored conversation keeps its Hermes session, and Hermes turns get a l
requests.push(request);
return request;
} },
'../native/llama': { streamLocalQwen: () => assert.fail('no local model') },
'../native/local-llm': { streamLocalModel: () => assert.fail('no local model') },
'./bridge-client': { assistantBridge: {} }, './tool-registry': { toolRegistry: {} },
'../prompts': { ASSISTANT_SYSTEM_PROMPT_BASE: '', buildAssistantSystemPrompt: () => '', describeAssistantContext: () => '' },
});
@@ -122,7 +122,7 @@ test('URL normalization adds /v1 to a bare host and drops the endpoint path', ()
});
test('the Hermes model resolves only with a URL, carrying the key and base URL', () => {
const load = loader({ global: { isIOS: true } }, { '../native/llama': { isLocalModelReady: () => false, LOCAL_MODEL: { id: 'q', label: 'Q' } } });
const load = loader({ global: { isIOS: true } }, { '../native/local-llm': { isLocalModelReady: () => false, isLocalModelSupported: () => true, LOCAL_MODEL: { id: 'q', label: 'Q' } } });
const { resolveAssistantModel } = load('app/assistant/models.ts');
assert.equal(resolveAssistantModel('hermes', { anthropic: '', openai: '' }), null);
const resolved = resolveAssistantModel('hermes', { anthropic: '', openai: '', hermesUrl: ' http://hermes-host:8642 ', hermesKey: ' k ' });
+39 -9
View File
@@ -71,6 +71,30 @@ test('iOS installed apps launch, recover missing packages, and close before unin
assert.deepEqual(calls, [['launch', 'installed', 'test.app'], ['reinstall', 'test.app'], ['close', 'test.app'], ['uninstall', 'test.app']]);
});
test('iOS launch failures on a zeroed package fall back to the store page; other failures still report', async () => {
const calls = [], failures = [], app = { packageId: 'test.app', name: 'Test' };
let zeroed = true;
const modules = {
"../ui/input-monitor": load("app/ui/input-monitor.ts", {}),
'../apps/all-apps': { ALL_APPS: [{ appId: 'evenhub' }] },
'../apps/evenhub/installed-apps': { getInstalledEvenHubAppById: () => app,
discardZeroedEvenHubPackage: id => { calls.push(['discard', id]); return zeroed; } },
'../apps/evenhub/updates': { isInstalledPackagePresent: () => true },
'../apps/evenhub': { openEvenHubStoreForPackage: (_ctx, value) => calls.push(['reinstall', value.packageId]) },
'../apps/evenhub/manager': { launchInstalledPackage: async () => { calls.push(['launch']); throw new Error('Could not read the EHPK manifest.'); } },
};
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => modules[id] ?? {} });
const host = Object.create(IosPreviewController.prototype);
host.buildAppContext = app => app; host.fail = error => failures.push(error.message); host.requestShellRender = () => {};
await host.launchApp('installed');
assert.deepEqual(calls, [['launch'], ['discard', 'test.app'], ['reinstall', 'test.app']]);
assert.deepEqual(failures, []);
calls.length = 0; zeroed = false;
await host.launchApp('installed');
assert.deepEqual(calls, [['launch'], ['discard', 'test.app']]);
assert.deepEqual(failures, ['Could not read the EHPK manifest.']);
});
test('iOS settings prompts honor password input and report cancellation', async () => {
const prompts = [], writes = [];
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '@nativescript/core' ? {
@@ -342,26 +366,27 @@ test('iOS preview voice uses the phone microphone, while connected capture stays
test('iOS continuous capture is shared by windows and restarts after a disconnect, a lock or backgrounding', async () => {
const calls = [], claims = [], providers = [];
// Models the bridge's continuous state: listening on a source, or draining after a stop.
const bridge = { source: null, draining: false, recording: '',
const bridge = { source: null, draining: false, recording: '', rawSession: undefined,
supportsContinuous: voice => voice.provider !== 'onboard',
get continuousSource() { return this.source; },
get continuousActive() { return !!this.source || this.draining; },
prepare: async () => true,
startGlassesCapture: async (_session, voice, _log, _endpointing, continuous) => {
calls.push(['glasses', continuous]); providers.push(`${voice.provider}:${voice.whisperServerLanguage}`); bridge.source = 'glasses'; },
calls.push(['glasses', continuous]); providers.push(`${voice.provider}:${voice.language}`); bridge.source = 'glasses'; },
startPhoneCapture: async (_voice, _log, _endpointing, continuous) => { calls.push(['phone', continuous]); bridge.source = 'phone'; },
drain() { if (this.source) { this.source = null; this.draining = true; } },
stopContinuousCapture: async () => { calls.push(['stop']); bridge.drain(); },
handleSessionEnded() { if (bridge.source === 'glasses') bridge.drain(); },
stopPhoneCapture() { if (bridge.source === 'phone') bridge.drain(); },
recordTo: name => { bridge.recording = name; } };
recordTo: name => { bridge.recording = name; },
setRawSession: session => { bridge.rawSession = session; } };
const settings = { ...voiceSettings, voiceProviderSetting: { get: () => 'whisper-server' },
whisperServerUrlSetting: { get: () => 'http://whisper.local:8000' }, whisperServerLanguageSetting: { get: () => 'en' },
saveTranscribeAudioSetting: { get: () => true } };
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', {
require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: bridge } : id === '../ui/dashboard-settings' ? settings
: id === '../ui/shell/voice-activity' ? { voiceActivity: { setActive: (active, holder) => claims.push([active, holder]) } }
: id === '../native/cloud-stt-provider' ? { transcriptionKey: voice => `${voice.provider}:${voice.whisperServerLanguage}` } : {},
: id === '../native/cloud-stt-provider' ? { transcriptionKey: voice => `${voice.provider}:${voice.language}` } : {},
});
const host = Object.create(IosPreviewController.prototype);
Object.assign(host, { active: true, glassesLocked: false, communicator: {}, state: { phase: 'connected' }, sessionConnected: true,
@@ -381,14 +406,17 @@ test('iOS continuous capture is shared by windows and restarts after a disconnec
host.stopContinuousVoiceCapture('calculator');
assert.deepEqual(calls, [['glasses', true]], 'closing one window keeps the other listening');
// Translate needs a multilingual provider: the shared capture drains and
// restarts on it (Whisper left to detect the language), and moves back to
// the main provider once Translate closes.
calls.length = 0; host.startContinuousVoiceCapture('translate', { multilingualProvider: 'soniox' }); await settle();
// Translate needs its own provider and language: the shared capture drains
// and restarts on them, again when only the language changes, and moves
// back to the main provider (and Whisper's own language) once Translate closes.
calls.length = 0; host.startContinuousVoiceCapture('translate', { transcription: { provider: 'soniox', language: '' } }); await settle();
assert.deepEqual(calls, [['stop']]);
drained(); await settle();
assert.deepEqual(calls, [['stop'], ['glasses', true]]);
assert.deepEqual(providers.slice(-2), ['whisper-server:en', 'soniox:']);
calls.length = 0; host.startContinuousVoiceCapture('translate', { transcription: { provider: 'soniox', language: 'ja' } }); drained(); await settle();
assert.deepEqual(calls, [['stop'], ['glasses', true]]);
assert.equal(providers.at(-1), 'soniox:ja');
calls.length = 0; host.stopContinuousVoiceCapture('translate'); drained(); await settle();
assert.deepEqual(calls, [['stop'], ['glasses', true]]);
assert.equal(providers.at(-1), 'whisper-server:en');
@@ -400,11 +428,13 @@ test('iOS continuous capture is shared by windows and restarts after a disconnec
session('connected'); await settle();
assert.deepEqual(calls, [['glasses', true]]);
// Locking drains; unlocking resumes.
// Locking drains; unlocking resumes. EvenHub apps' raw mic tap follows the lock too.
calls.length = 0; host.setGlassesLocked(true); drained(); await settle();
assert.deepEqual(calls, []);
assert.equal(bridge.rawSession, null, 'locking unbinds the raw mic session');
host.setGlassesLocked(false); await settle();
assert.deepEqual(calls, [['glasses', true]]);
assert.equal(bridge.rawSession, host.communicator, 'unlocking rebinds it to the connected session');
// A failed capture is not retried in a loop, only on the next session.
calls.length = 0; bridge.source = null; drained(); await settle();
+8 -2
View File
@@ -56,6 +56,8 @@ function controller(music = fakeMusic()) {
'./image-files': { grayImageFromPacket: bytes => (bytes && bytes.length ? { bytes } : null), PHOTO_TONE: { gamma: 2.2, dither: true } },
'./kotlin-data': { fromData: data => data },
'./media-types': types,
// Signed out of Spotify: the composite bridge follows Apple Music alone.
'./spotify.ios': { spotifyService: () => null },
}, {
FaceclawAppleMusic: { shared: () => music },
FaceclawKitIosProtocol: { new: () => ({ grayPacketBgraGammaDither: (packet, gamma, dither) => {
@@ -155,7 +157,11 @@ test('access requests share one prompt, and denied or restricted access points t
});
function browser(music = fakeMusic()) {
const api = load('app/native/media-browser.ios.ts', { './media-types': types }, {
const api = load('app/native/media-browser.ios.ts', {
'./media-types': types,
'./spotify.ios': { spotifyService: () => null },
'./spotify-player': { SPOTIFY_IOS_BUNDLE_ID: 'com.spotify.client' },
}, {
FaceclawAppleMusic: { shared: () => music },
});
return { bridge: api.mediaBrowserBridge, music };
@@ -165,7 +171,7 @@ test('the Apple Music library browses only with access and drops answers after d
const f = browser();
const [app] = f.bridge.listBrowsableApps();
assert.equal(app.appName, 'Apple Music');
await assert.rejects(f.bridge.connect({ ...app, packageName: 'other' }), /Only Apple Music/);
await assert.rejects(f.bridge.connect({ ...app, packageName: 'other' }), /Not an Apple Music library/);
await assert.rejects(f.bridge.browse('root'), /Not connected/);
assert.equal(await f.bridge.connect(app), 'root');
+4 -4
View File
@@ -12,7 +12,7 @@ function assistant(provider) {
const request = { url, body: JSON.parse(body), headers, listener, cancelled: false };
requests.push(request); return { cancel: () => { request.cancelled = true; } };
} },
'../native/llama': { streamLocalQwen: () => assert.fail('iOS must not invoke a local LLM') },
'../native/local-llm': { streamLocalModel: () => assert.fail('cloud providers must not invoke the local LLM') },
'./bridge-client': { assistantBridge: {} }, './tool-registry': { toolRegistry: registry },
'../prompts': { ASSISTANT_SYSTEM_PROMPT_BASE: 'test', buildAssistantSystemPrompt: () => 'test', describeAssistantContext: () => '' },
});
@@ -100,8 +100,8 @@ test('iOS SSE adapter forwards the Kotlin stream events once and stops after can
assert.deepEqual(seen.at(-1), 'http:429:slow down');
});
test('iOS only offers cloud assistant models and preserves old local-model conversations as Auto', () => {
const api = loader({ global: { isIOS: true } }, { '../native/llama': { LOCAL_MODEL: { label: 'Qwen', id: 'qwen' }, isLocalModelReady: () => false } })('app/assistant/models.ts');
test('iOS without Apple Intelligence support only offers cloud assistant models and preserves old local-model conversations as Auto', () => {
const api = loader({ global: { isIOS: true } }, { '../native/local-llm': { LOCAL_MODEL: { label: 'Apple Intelligence', id: 'apple-foundation-model' }, isLocalModelReady: () => false, isLocalModelSupported: () => false } })('app/assistant/models.ts');
assert.equal(api.ASSISTANT_MODEL_CHOICES.includes('qwen'), false);
assert.equal(api.supportedAssistantModel('qwen'), 'auto');
assert.equal(api.resolveAssistantModel('auto', { openai: 'key', anthropic: '' }).provider, 'openai');
@@ -119,7 +119,7 @@ test('cancelling during a tool prevents subsequent tool side effects and a stall
const calls = [], failures = [];
const { DirectAssistantBackend } = loader({ setTimeout: fn => { deadline = fn; return 1; }, clearTimeout() {} }, {
'../native/openai': { streamOpenAiResponse: options => { stream = options; return { cancel() {} }; } },
'../native/anthropic': {}, '../native/llama': {},
'../native/anthropic': {}, '../native/local-llm': {},
})('app/assistant/direct-backend.ts');
const backend = new DirectAssistantBackend();
const options = { provider: 'openai', messages: [], buildTools: () => [], resolveToolName: name => name,
+1
View File
@@ -229,6 +229,7 @@ test('iOS assistant calendar tool awaits events, reports denied access and failu
const { registerSystemTools } = load('app/assistant/system-tools.ts', {
'../native/calendar': f.api, '../native/calendar-permissions': f.permissions,
'../native/media-controller': { mediaControllerBridge: { start: async () => {} } },
'../native/spotify': { spotifyService: () => null },
'../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {},
}, { global: { isIOS: true } });
registerSystemTools({ registerSystemTool: (definition, handler) => registered.set(definition.name, handler) });
+2 -1
View File
@@ -32,7 +32,8 @@ function harness(refused = new Set()) {
NSObject, NativeClass: () => cls => cls, setTimeout: (fn, delay) => { timers.push({ fn, delay }); return timers.length; }, clearTimeout: id => { timers[id - 1].cleared = true; },
FaceclawKitIosGlassesSession: { alloc: () => ({ initWithRightAddressLeftAddressRingAddress: () => session }) },
FaceclawKitFaceclawBleCommunicatorListener: {}, FaceclawKitIosAncsListener: {}, FaceclawKitIosAudioPacketListener: {},
FaceclawKitFaceclawCompassListener: {}, console: { log() {}, warn() {} },
FaceclawKitFaceclawCompassListener: {}, FaceclawKitFaceclawImuListener: {}, FaceclawKitIosAmbientLightListener: {},
FaceclawKitIosMicStatusListener: {}, console: { log() {}, warn() {} },
}, {
'@nativescript/core': { File: {}, knownFolders: {}, path: {} },
'../g2/firmware-compat': { REQUIRED_FACECLAW_FIRMWARE_VERSION: 37 },
-21
View File
@@ -1,21 +0,0 @@
const test = require('node:test');
const { execFileSync } = require('node:child_process');
test('iOS config codec preserves Android XML and JSON types and rejects malformed imports', () => {
execFileSync('python3', ['-c', `
import importlib.util, pathlib, tempfile
spec = importlib.util.spec_from_file_location('config', 'scripts/ios_config.py')
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
values = {'terminal.connections': '[{"url":"g2mirror://test@host"}]', 'flag': True, 'empty': '', 'number': 42, 'ratio': 0.25, 'unicode': 'π & < 🔋\\nnext'}
with tempfile.TemporaryDirectory() as d:
p = pathlib.Path(d) / 'settings'
for xml in [False, True]:
m.atomic_write(p, m.encode_config(values, xml))
assert m.read_config(p) == values
assert p.stat().st_mode & 0o777 == 0o600
for invalid in [b'{}', b'<map><set name="x"/></map>', b'<map><boolean name="x" value="maybe"/></map>', b'<map><string name="x">a</string><string name="x">b</string></map>', b'{"schema":1,"settings":{"x":null}}', b'{"schema":1,"settings":{"x":NaN}}', b'<map><string name="x">oops']:
p.write_bytes(invalid)
try: m.read_config(p)
except (ValueError, m.ET.ParseError): pass
else: raise AssertionError('accepted invalid config')
`], { stdio: 'inherit' });
});
+32
View File
@@ -297,3 +297,35 @@ test('EvenHub assistant tools skip malformed names and schemas with a warning in
assert.match(warnings[4], /"union": parameters can't use oneOf, anyOf or allOf/);
h.session.close();
});
test('iOS binary downloads copy out of the response NSData, which bufferFromData does not retain', async () => {
const native = new Uint8Array([0x45, 0x48, 0x50, 0x4b, 1, 2, 3]);
const { downloadBinary } = loader({}, {
'@nativescript/core': { Http: { request: async () => ({ statusCode: 200, headers: {}, content: { toArrayBuffer: () => native.buffer } }) } },
'../util/http': { withUserAgent: headers => headers ?? {} },
})('app/native/binary-http.ios.ts');
const { bytes } = await downloadBinary('https://cdn.example/app.ehpk', 1000);
native.fill(0); // The released NSData's pages are reused or zeroed.
assert.deepEqual(Array.from(bytes), [0x45, 0x48, 0x50, 0x4b, 1, 2, 3]);
});
test('zeroed installed packages are discarded with their registry entry kept; real packages are left alone', () => {
const files = new Map(), deleted = [];
const installed = loader({}, {
fzstd: require('fzstd'),
'../../native/file-access': { readBinaryFile: path => files.get(path) ?? null, readTextFile() {}, writeBinaryFile() {},
appFilesDirPath: () => '/app', deletePathRecursively: path => { deleted.push(path); files.delete(path); } },
'../../native/settings-store': { getStringSetting: (_key, fallback) => fallback, setStringSetting() {} },
'../../native/icon-image': {}, '../../graphics/icons': {}, '../../graphics/image': {},
})('app/apps/evenhub/installed-apps.ts');
const path = installed.installedEvenHubPackagePath('test.app');
files.set(path, pack({ 'app.json': '{"package_id":"test.app"}' }));
assert.equal(installed.discardZeroedEvenHubPackage('test.app'), false);
files.set(path, new Uint8Array([...new Uint8Array(1000), 1]));
assert.equal(installed.discardZeroedEvenHubPackage('test.app'), false);
assert.equal(installed.discardZeroedEvenHubPackage('missing.app'), false);
assert.deepEqual(deleted, []);
files.set(path, new Uint8Array(4096));
assert.equal(installed.discardZeroedEvenHubPackage('test.app'), true);
assert.deepEqual(deleted, [path], 'only the EHPK goes; the icon and registry entry stay for Reinstall');
});
+61 -7
View File
@@ -25,8 +25,10 @@ function fixture() {
const observers = new Map(), phoneState = { protectedDataAvailable: true };
let settingsChanged, remoteHost, controller, bridge = null;
let now = 1000, nextTask = 0, screenOn = true, shellOptions;
const battery = {};
const tasks = new Map(), sent = [], previews = [], received = [], errors = [];
const boardStats = { starts: 0, stops: 0, paints: 0 };
const registered = [], inProcessApps = [];
const settings = { lock: true, enabled: true, tap: true, hold: true, tilt: true, duration: 3000, depth: 0 };
const clock = { Date: class extends Date { static now() { return now; } },
setTimeout: (fn, ms) => { tasks.set(++nextTask, { fn, at: now + ms }); return nextTask; },
@@ -43,11 +45,11 @@ function fixture() {
// The launcher repaints into whichever display target the controller has after a switch.
requestRender() { void controller?.displayReady.then(() => controller.display?.submitSurfaceFrame('launcher', launcherPixels, fullRect, 'launcher')); }, setScreenOn() {} };
const shell = {
configure: options => { shellOptions = options; }, registerWindow() {}, focusWindow() {},
configure: options => { shellOptions = options; }, registerWindow: window => registered.push(window), focusWindow() {},
wake: () => { screenOn = true; shellOptions.onScreenStateChanged(true); },
sleep: () => { screenOn = false; shellOptions.onScreenStateChanged(false); },
isScreenOn: () => screenOn, getWindows: () => [window], foregroundWindow: () => window,
setBatteryLevels() {}, underlayDim: () => 1, getFocus: () => 'app', hasOverlay: () => false,
setBatteryLevels: levels => Object.assign(battery, levels), underlayDim: () => 1, getFocus: () => 'app', hasOverlay: () => false,
describeInputTarget: () => 'test app',
paintScene: () => new Uint8Array([0, 0]), paintSurface: () => [{ image: new images.GrayImage(640, 480, 0), x: 0, y: 0 }],
receiveInput: async input => {
@@ -105,13 +107,14 @@ function fixture() {
constructor() { super(); bridge = this; }
on(kind, fn) { this.listeners[kind] = fn; return () => { if (this.listeners[kind] === fn) delete this.listeners[kind]; }; }
onStateChange(fn) { return this.on('state', fn); } onRingEvent(fn) { return this.on('ring', fn); }
onWearState(fn) { return this.on('wear', fn); } onBatteryState() { return noop(); } onFirmwareInfo() { return noop(); }
onFrameMetrics() { return noop(); } addCompassListener() { return noop(); } onAncsRelayFrame() { return noop(); }
onWearState(fn) { return this.on('wear', fn); } onBatteryState(fn) { return this.on('battery', fn); } onFirmwareInfo() { return noop(); }
onFrameMetrics() { return noop(); } onPreviewAnimationFrame() { return noop(); } addCompassListener() { return noop(); } onAncsRelayFrame() { return noop(); }
onAncsAuthorization() { return noop(); } setRequiresAncs() {} rightWriteLimit() { return 20; } onPhoneLockSignal() {}
async writeRawToRight() {} async configureBrightness() {} async setBrightness() {} async enableWearDetectionAndRequestState() {} async playBuzzerSequence() {} async close() {}
emitState(phase, status = phase) { if (phase === 'connected' || phase === 'disconnected') this.phase = phase; this.listeners.state?.({ phase, status }); }
emitRing(input) { this.listeners.ring?.(input); }
emitWear(wearing) { this.listeners.wear?.(wearing); }
emitBattery(state) { this.listeners.battery?.(state); }
async start() { this.emitState('connected', 'Connected.'); }
async disconnect() { this.emitState('disconnected', 'Disconnected.'); }
waitForFrameFinished() { return Promise.resolve('sent'); }
@@ -136,9 +139,10 @@ function fixture() {
'../native/notification-icons.ios': { bindIosNotifications() {}, iosNotificationsChanged() {}, onIosNotificationPopup: () => () => {}, readActiveNotifications: () => [] },
'../native/notification-sources': { shouldShowNotificationOnGlasses: () => true },
"../native/compass.ios": { bindCompassSession() {}, receiveCompassEvent() {} },
'../native/bandwidth-benchmark.ios': { bindBandwidthBenchmarkSession() {} },
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) },
knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {}, stopPhoneCapture() {}, onSpeechEnd: () => () => {} } },
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {}, stopPhoneCapture() {}, setRawSession() {}, onSpeechEnd: () => () => {} } },
'../native/cloud-stt-provider': { transcriptionKey: voice => voice.provider },
'../ui/shell/voice-activity': { voiceActivity: { setActive() {}, isActive: () => false, subscribe: () => () => {} } },
'../native/faceclaw-communicator.ios': { FaceclawCommunicatorBridge, resolveIosPeripherals: async addresses => addresses },
@@ -157,10 +161,16 @@ function fixture() {
'../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': { onPhoneShownChanged: () => () => {}, phoneUiButton: () => null },
'../apps/evenhub/updates': {}, '../apps/evenhub': {},
'../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' },
'../apps/notifications/notifications-app': { NOTIFICATIONS_SURFACE_ID: 'window:notifications', NOTIFICATIONS_WINDOW_ID: 'notifications',
createNotificationsAppWindow: options => {
const app = { options, window: { windowId: 'notifications', surfaceId: 'window:notifications', appId: 'notifications', heightMode: 'min' } };
inProcessApps.push(app);
return app;
} },
'../apps/all-apps': { ALL_APPS: [{ appId: 'glanceboard', glanceboard: provider }] },
'../apps/onboarding/onboarding-progress': { claimGlassesOnboardingAutoLaunch: () => false },
'../ui/shell/worker-window': {}, '../ui/shell/in-process-window': {},
'../ui/dashboard-settings': { brightnessSetting: { get: () => 'auto' }, brightnessSettingToLevel: () => null, getBrightnessPreferences: () => ({ auto: true, level: 50, minimum: 2, maximum: 100, curve: '0:0,1000:100', fadeMs: 280 }), lockScreenEnabledSetting: { get: () => settings.lock }, onAnySettingChanged: fn => { settingsChanged = fn; return () => {}; }, previewColorSetting: { get: () => 'white' } },
'../ui/dashboard-settings': { brightnessSetting: { get: () => 'auto' }, brightnessSettingToLevel: () => null, getBrightnessPreferences: () => ({ auto: true, level: 50, minimum: 2, maximum: 100, curve: '0:0,1000:100', fadeMs: 280 }), lockScreenEnabledSetting: { get: () => settings.lock }, ringConnectionModeSetting: { get: () => 'glasses' }, onAnySettingChanged: fn => { settingsChanged = fn; return () => {}; }, previewColorSetting: { get: () => 'white' } },
'../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) },
'../apps/ios-availability': { iosAppUnavailableReason: () => null },
'../graphics/glyph-wire': { prepareFrameDraws: () => null },
@@ -202,7 +212,8 @@ function fixture() {
}
async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); }
async function connect() { await controller.connect(); await render(); }
return { controller, shell, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers,
return { controller, shell, battery, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers,
get shellOptions() { return shellOptions; }, registered, inProcessApps,
get bridge() { return bridge; }, connect, wear: wearing => bridge.emitWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render };
}
const assertBlank = pixels => assert.ok(pixels.every(p => p === 0));
@@ -242,3 +253,46 @@ test('keyboard sessions cannot deliver text after the glasses lock or phone leav
editor.discard(); assert.deepEqual(calls.at(-1), ['discard']);
});
test('ring battery reported through the glasses reaches the shell while connected', async () => {
const f = fixture();
await f.connect();
f.bridge.emitBattery({ battery: 70, chargingStatus: 0, ringBattery: 55, ringChargingStatus: 1 });
assert.deepEqual([f.battery.headset, f.battery.ring, f.battery.ringCharging], [70, 55, true]);
// An unavailable ring reading clears the indicator; the headset keeps its last level.
f.bridge.emitBattery({ battery: -1, chargingStatus: 0, ringBattery: -1, ringChargingStatus: -1 });
assert.deepEqual([f.battery.headset, f.battery.ring, f.battery.ringCharging], [70, null, null]);
f.bridge.emitBattery({ battery: 70, chargingStatus: 0, ringBattery: 40, ringChargingStatus: 0 });
assert.deepEqual([f.battery.ring, f.battery.ringCharging], [40, false]);
await f.controller.disconnect(); await f.render();
assert.deepEqual([f.battery.ring, f.battery.ringCharging], [null, null]);
});
test('the switcher can open the Notifications window in the background, once, with a live surface', async () => {
const f = fixture();
await f.render();
const opened = f.shellOptions.openNotificationsWindow();
assert.equal(opened.opened, true);
const [app] = f.inProcessApps;
assert.equal(opened.window, app);
assert.ok(f.registered.includes(app.window), 'registered with the shell for the switcher to foreground');
// A frame painted at once (the shell foregrounds and repaints the window
// straight away) lands on the surface configured ahead of it.
void app.options.submitFrame([{ image: new images.GrayImage(640, 480, 90), x: 0, y: 0 }], 0, 1);
await f.render();
const surface = f.controller.display.surfaces.get('window:notifications');
assert.ok(surface, 'surface configured');
assert.equal(surface.pixels[0], 90);
assert.equal(surface.visible, false, 'stays hidden until the shell foregrounds it');
// Already open: the same window, nothing new created or registered.
const registeredBefore = f.registered.length;
const again = f.shellOptions.openNotificationsWindow();
assert.deepEqual([again.opened, again.window, f.inProcessApps.length, f.registered.length], [false, app, 1, registeredBefore]);
// Closed (the selection moved on): frames stop, and the next open makes a new window.
app.options.removeSurface(); app.options.onClosed();
void app.options.submitFrame([{ image: new images.GrayImage(640, 480, 90), x: 0, y: 0 }], 0, 2);
await f.render();
assert.equal(f.controller.display.surfaces.has('window:notifications'), false);
assert.equal(f.shellOptions.openNotificationsWindow().opened, true);
assert.equal(f.inProcessApps.length, 2);
});
+248
View File
@@ -0,0 +1,248 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const flush = () => new Promise(resolve => setImmediate(resolve));
// Values built inside the loader's VM context have that realm's prototypes.
const plain = value => JSON.parse(JSON.stringify(value));
/** A stand-in for FaceclawSystemLanguageModel.swift: records requests and lets tests emit its events. */
function fakeNative(availability = 'available') {
const instances = [];
const cls = {
availability: () => availability,
new() {
const native = {
request: null, resolved: [], cancelled: false, eventHandler: null,
start(json) { this.request = JSON.parse(json); },
resolveToolCalls(json) { this.resolved.push(JSON.parse(json)); },
cancel() { this.cancelled = true; },
emit(event) { if (this.eventHandler) this.eventHandler(JSON.stringify(event)); },
};
instances.push(native);
return native;
},
};
return { cls, instances, setAvailability(value) { availability = value; } };
}
function loadLocalLlm(native, extraGlobals = {}) {
const load = loader({ FaceclawSystemLanguageModel: native.cls, setTimeout, clearTimeout, ...extraGlobals }, {});
return load('app/native/local-llm.ios.ts');
}
function collector() {
const seen = { deltas: [], done: [], errors: [] };
return {
seen,
callbacks: {
onTextDelta: (_delta, full) => seen.deltas.push(full),
onDone: result => seen.done.push(result),
onError: message => seen.errors.push(message),
},
};
}
test('iOS local LLM converts provider-neutral history into transcript entries and a final prompt', () => {
const api = loadLocalLlm(fakeNative());
const request = api.buildSystemModelRequest({
system: 'Be brief.',
maxTokens: 200,
tools: [{ name: 'timer_set', description: 'Set a timer', input_schema: { type: 'object', properties: { seconds: { type: 'integer' } } } }],
messages: [
{ role: 'user', content: 'Set a timer' },
{ role: 'assistant', content: [{ type: 'text', text: 'On it.' }, { type: 'tool_use', id: 'fm_1', name: 'timer_set', input: { seconds: 60 } }] },
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'fm_1', content: 'Timer set' }] },
{ role: 'assistant', content: 'Done.' },
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'gone', content: 'boom', is_error: true }, { type: 'text', text: 'And now?' }] },
{ role: 'assistant', content: [{ type: 'provider_item', provider: 'openai', item: {} }] },
{ role: 'user', content: 'What time is it?' },
],
});
assert.equal(request.system, 'Be brief.');
assert.equal(request.prompt, 'What time is it?');
assert.equal(request.maxTokens, 200);
assert.equal(request.permissive, false);
assert.deepEqual(plain(request.tools), [{ name: 'timer_set', description: 'Set a timer', parameters: { type: 'object', properties: { seconds: { type: 'integer' } } } }]);
assert.deepEqual(plain(request.history), [
{ type: 'prompt', text: 'Set a timer' },
{ type: 'response', text: 'On it.' },
{ type: 'toolCalls', calls: [{ id: 'fm_1', name: 'timer_set', arguments: '{"seconds":60}' }] },
{ type: 'toolOutput', id: 'fm_1', name: 'timer_set', text: 'Timer set' },
{ type: 'response', text: 'Done.' },
{ type: 'toolOutput', id: 'gone', name: 'tool', text: 'Error: boom' },
{ type: 'prompt', text: 'And now?' },
]);
// Tool-free requests (translate, Q&A) get the permissive guardrails; a
// trailing tool result with nothing to resume falls back to a continuation prompt.
const orphan = api.buildSystemModelRequest({
messages: [{ role: 'user', content: 'x' }, { role: 'assistant', content: [{ type: 'tool_use', id: 'a', name: 't', input: {} }] },
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'a', content: 'ok' }] }],
});
assert.equal(orphan.permissive, true);
assert.equal(orphan.maxTokens, 512);
assert.equal(orphan.prompt, api.CONTINUE_PROMPT);
assert.deepEqual(plain(orphan.history.at(-1)), { type: 'toolOutput', id: 'a', name: 't', text: 'ok' });
});
test('iOS local LLM streams text deltas, then ends the turn', () => {
const native = fakeNative();
const api = loadLocalLlm(native);
const { seen, callbacks } = collector();
api.streamLocalModel({ apiKey: '', model: 'm', system: 's', messages: [{ role: 'user', content: 'Hi' }], ...callbacks });
const [instance] = native.instances;
assert.equal(instance.request.prompt, 'Hi');
instance.emit({ kind: 'log', message: 'shortened tool descriptions' });
instance.emit({ kind: 'delta', text: 'Hello' });
instance.emit({ kind: 'delta', text: ' there' });
instance.emit({ kind: 'done' });
instance.emit({ kind: 'delta', text: 'late' });
assert.deepEqual(plain(seen.deltas), ['Hello', 'Hello there']);
assert.equal(seen.done.length, 1);
assert.deepEqual(plain(seen.done[0]), { text: 'Hello there', content: [{ type: 'text', text: 'Hello there' }], stopReason: 'end_turn' });
assert.equal(instance.eventHandler, null, 'finished generations drop their handler');
});
test('iOS local LLM reports why the model is unavailable without touching native code', async () => {
const native = fakeNative('appleIntelligenceNotEnabled');
const api = loadLocalLlm(native);
assert.equal(api.isLocalModelReady(), false);
assert.equal(api.isLocalModelSupported(), true);
assert.deepEqual(plain(api.localModelState()), { status: 'unavailable', bytesDownloaded: 0, totalBytes: 0, detail: 'Apple Intelligence off' });
const { seen, callbacks } = collector();
api.streamLocalModel({ apiKey: '', model: 'm', messages: [{ role: 'user', content: 'Hi' }], ...callbacks });
await new Promise(resolve => setTimeout(resolve, 0));
assert.match(seen.errors[0], /Turn on Apple Intelligence/);
assert.equal(native.instances.length, 0);
native.setAvailability('deviceNotEligible');
assert.equal(api.isLocalModelSupported(), false);
native.setAvailability('somethingNew');
assert.equal(api.localModelState().detail, 'unavailable');
const missing = loader({ setTimeout, clearTimeout }, {})('app/native/local-llm.ios.ts');
assert.equal(missing.localModelAvailability(), 'unsupportedOS', 'pre-iOS-26 builds have no Swift class to call');
native.setAvailability('available');
assert.deepEqual(plain(api.localModelState()), { status: 'ready', bytesDownloaded: 0, totalBytes: 0 });
});
test('iOS local LLM maps refusals to the refusal stop reason and other failures to errors', () => {
const native = fakeNative();
const api = loadLocalLlm(native);
const first = collector();
api.streamLocalModel({ apiKey: '', model: 'm', messages: [{ role: 'user', content: 'x' }], ...first.callbacks });
native.instances[0].emit({ kind: 'error', code: 'refusal', message: 'declined' });
assert.equal(first.seen.done[0].stopReason, 'refusal');
const second = collector();
api.streamLocalModel({ apiKey: '', model: 'm', messages: [{ role: 'user', content: 'x' }], ...second.callbacks });
native.instances[1].emit({ kind: 'error', code: 'rateLimited', message: 'iOS is limiting the on-device model' });
assert.deepEqual(plain(second.seen.errors), ['iOS is limiting the on-device model']);
});
test('iOS local LLM cancellation stops the native generation and silences later events', () => {
const native = fakeNative();
const api = loadLocalLlm(native);
const { seen, callbacks } = collector();
const handle = api.streamLocalModel({ apiKey: '', model: 'm', messages: [{ role: 'user', content: 'x' }], ...callbacks });
handle.cancel();
native.instances[0].emit({ kind: 'delta', text: 'late' });
native.instances[0].emit({ kind: 'done' });
assert.equal(native.instances[0].cancelled, true);
assert.deepEqual(seen, { deltas: [], done: [], errors: [] });
});
test('iOS assistant runs the on-device model through the shared tool loop, resuming the parked generation', async () => {
const native = fakeNative();
const calls = [], deltas = [], errors = [], done = [];
const registry = {
listTools: () => [{ name: 'timer.set', description: 'Set a timer', inputSchema: { type: 'object', properties: { seconds: { type: 'integer' } }, required: ['seconds'] } }],
callTool: async (name, args) => { calls.push({ name, args }); return { ok: true, content: 'Timer set' }; },
};
const mocks = {
'./bridge-client': { assistantBridge: {} }, './tool-registry': { toolRegistry: registry },
'../prompts': { ASSISTANT_SYSTEM_PROMPT_BASE: 'base prompt', buildAssistantSystemPrompt: () => 'cloud prompt', describeAssistantContext: () => 'Context: now' },
};
const load = loader({ global: { isIOS: true, isAndroid: false }, FaceclawSystemLanguageModel: native.cls, setTimeout, clearTimeout }, mocks);
mocks['../native/local-llm'] = load('app/native/local-llm.ios.ts');
const { AssistantSession } = load('app/assistant/session.ts');
const session = new AssistantSession({ kind: 'direct', llm: { selection: 'qwen', provider: 'local', model: 'apple-foundation-model', apiKey: '' } }, registry);
const send = text => session.sendUtterance(text, {}, { onTextDelta: (_d, full) => deltas.push(full),
onToolActivity() {}, onError: error => errors.push(error), onTurnDone: result => done.push(result) });
send('Set a one-minute timer');
assert.equal(native.instances.length, 1);
const generation = native.instances[0];
assert.equal(generation.request.system, 'base prompt', 'the local provider keeps the system prompt stable');
assert.equal(generation.request.prompt, 'Set a one-minute timer\n\n(Context: now)');
assert.deepEqual(plain(generation.request.tools.map(tool => tool.name)), ['timer_set']);
generation.emit({ kind: 'toolCalls', calls: [{ id: 'fm_abc', name: 'timer_set', arguments: '{"seconds":60}' }] });
await flush();
assert.deepEqual(plain(calls), [{ name: 'timer.set', args: { seconds: 60 } }]);
assert.equal(native.instances.length, 1, 'tool results resume the parked generation instead of starting another');
assert.deepEqual(plain(generation.resolved), [[{ id: 'fm_abc', content: 'Timer set', isError: false }]]);
generation.emit({ kind: 'delta', text: 'Your timer is running.' });
generation.emit({ kind: 'done' });
assert.equal(done.length, 1);
assert.equal(errors.length, 0);
assert.equal(deltas.at(-1), 'Your timer is running.');
assert.equal(session.isTurnActive(), false);
// The next turn starts a fresh generation that carries the whole exchange as transcript history.
send('Thanks');
const next = native.instances[1];
assert.deepEqual(plain(next.request.history), [
{ type: 'prompt', text: 'Set a one-minute timer\n\n(Context: now)' },
{ type: 'toolCalls', calls: [{ id: 'fm_abc', name: 'timer_set', arguments: '{"seconds":60}' }] },
{ type: 'toolOutput', id: 'fm_abc', name: 'timer_set', text: 'Timer set' },
{ type: 'response', text: 'Your timer is running.' },
]);
session.cancel();
assert.equal(next.cancelled, true);
});
test('a parked iOS generation survives unrelated requests and is abandoned if its results never arrive', () => {
const native = fakeNative();
let expire = null;
const timers = {
setTimeout: (fn, ms) => { if (ms > 0) expire = fn; else fn(); return 1; },
clearTimeout: () => { expire = null; },
};
const api = loadLocalLlm(native, timers);
const turn = collector();
api.streamLocalModel({ apiKey: '', model: 'm', messages: [{ role: 'user', content: 'x' }], tools: [], ...turn.callbacks });
native.instances[0].emit({ kind: 'toolCalls', calls: [{ id: 'fm_1', name: 't', arguments: 'not json' }] });
assert.deepEqual(plain(turn.seen.done[0].content), [{ type: 'tool_use', id: 'fm_1', name: 't', input: {} }]);
// e.g. Translate asking the model something while the assistant's tool runs.
const other = collector();
api.streamLocalModel({ apiKey: '', model: 'm', messages: [{ role: 'user', content: 'translate this' }], ...other.callbacks });
assert.equal(native.instances.length, 2);
assert.equal(native.instances[0].cancelled, false);
assert.equal(typeof expire, 'function');
expire();
assert.equal(native.instances[0].cancelled, true);
// Results for the abandoned call now start a fresh generation with the fallback prompt.
api.streamLocalModel({ apiKey: '', model: 'm', ...turn.callbacks, messages: [
{ role: 'user', content: 'x' },
{ role: 'assistant', content: turn.seen.done[0].content },
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'fm_1', content: 'ok' }] },
] });
assert.equal(native.instances.length, 3);
assert.equal(native.instances[2].request.prompt, api.CONTINUE_PROMPT);
});
test('iOS offers the on-phone model where Apple Intelligence is supported', () => {
const local = { LOCAL_MODEL: { label: 'Apple Intelligence', id: 'apple-foundation-model' }, isLocalModelReady: () => true, isLocalModelSupported: () => true };
const api = loader({ global: { isIOS: true } }, { '../native/local-llm': local })('app/assistant/models.ts');
assert.equal(api.ASSISTANT_MODEL_CHOICES.includes('qwen'), true);
assert.equal(api.supportedAssistantModel('qwen'), 'qwen');
assert.equal(api.assistantModelLabel('qwen'), 'Apple Intelligence (on-phone)');
const resolved = api.resolveAssistantModel('auto', { openai: '', anthropic: '' });
assert.equal(resolved.provider, 'local');
assert.equal(resolved.model, 'apple-foundation-model');
assert.equal(api.resolveAssistantModel('auto', { openai: 'key', anthropic: '' }).provider, 'openai', 'API keys still win under Auto');
local.isLocalModelReady = () => false;
assert.equal(api.resolveAssistantModel('qwen', { openai: '', anthropic: '' }), null);
});
+18
View File
@@ -0,0 +1,18 @@
// macOS integration test for the loopback OAuth redirect receiver compiled into iOS
// (Spotify sign-in): builds FaceclawLoopbackRedirect.m with a probe and talks HTTP to it.
const { execFileSync } = require('node:child_process');
const { mkdtempSync, rmSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { join, resolve } = require('node:path');
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-loopback-'));
try {
execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-I', 'App_Resources/iOS/src',
'tests/native/ios-loopback-redirect-test.m', 'App_Resources/iOS/src/FaceclawLoopbackRedirect.m',
'-o', join(dir, 'loopback-test')], { cwd: root, stdio: 'inherit' });
// A port unlikely to be taken on a dev machine; the app's own port is in spotify-auth.ts.
execFileSync(join(dir, 'loopback-test'), ['43899'], { stdio: 'inherit' });
} catch {
process.exitCode = 1;
} finally {
rmSync(dir, { recursive: true, force: true });
}
+88 -31
View File
@@ -1,51 +1,108 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { loader } = require('./helpers/load-typescript.cjs');
const generator = require('../scripts/generate-pebble-icons.cjs');
class FakeImage {
constructor(name) { this.name = name; }
clone() { return new FakeImage(this.name); }
fillRoundedRect() {}
fillRect() {}
}
function setup(notifications, render = name => new FakeImage(name)) {
const icons = loader({}, {
'../graphics/image': { GrayImage: class extends FakeImage { constructor() { super('drawn-bell'); } } },
'../graphics/icons': { renderIcon: (name, size) => { assert.equal(size, 24); return render(name); } },
function setup(notifications) {
const load = loader({}, {
'../g2/ancs-client': { AncsClient: class {}, ANCS_CONNECT_MESSAGE: '' },
'./notification-sources': { rememberNotificationSources() {} },
})('app/native/notification-icons.ios.ts');
});
const icons = load('app/native/notification-icons.ios.ts');
icons.bindIosNotifications({ read: () => notifications });
return icons;
return { icons, pebble: load('app/graphics/pebble-icons.ts') };
}
const notification = (key, packageName, category) => ({ key, packageName, category });
const notification = (key, packageName, category, appName = '') => ({ key, packageName, category, appName });
test('tray icons follow each source\'s first notification category', () => {
const icons = setup([
notification('a', 'com.apple.mobilephone', '2'),
notification('b', 'com.apple.mobilephone', '1'),
notification('c', 'com.apple.mobilemail', '6'),
notification('d', 'com.example.tv', '11'),
/** Which bundled icon an image is (icons are compared by their pixels). */
function iconName(pebble, image) {
const data = loader({}, {})('app/graphics/pebble-icons-data.ts').PEBBLE_ICON_DATA;
return Object.keys(data).find(name => Buffer.from(pebble.pebbleIcon(name).pixels).equals(Buffer.from(image.pixels)));
}
test('tray icons come from the source app, else its notification category, one per source', () => {
const { icons, pebble } = setup([
notification('a', 'net.whatsapp.WhatsApp', '4'),
notification('b', 'net.whatsapp.WhatsApp', '4'),
notification('c', 'com.apple.mobilephone', '2', 'Phone'),
notification('d', 'com.apple.MobileSMS', '4', 'Messages'),
notification('e', 'com.example.tv', '11', 'Some TV App'),
]);
const result = icons.readActiveNotificationIcons(10, false);
assert.deepEqual([...result.keys], ['a', 'c', 'd']);
assert.deepEqual([...result.icons].map(icon => icon.name), ['phone-missed', 'mail', 'tv']);
assert.deepEqual([...result.keys], ['a', 'c', 'd', 'e']);
assert.deepEqual([...result.icons].map(icon => iconName(pebble, icon)), ['whatsApp', 'missedCall', 'sms', 'tv']);
assert.equal(icons.readActiveNotificationIcons(1, false).icons.length, 1);
});
test('unknown categories use the bell; a failed rasterization uses the drawn bell', () => {
const icons = setup([notification('a', 'x', '0'), notification('b', 'y', '12'), notification('c', 'z', '')]);
assert.deepEqual([...icons.readActiveNotificationIcons(10, false).icons].map(icon => icon.name), ['bell', 'bell', 'bell']);
assert.equal(icons.readNotificationIconByKey('missing', false).icon, null);
test('apps match by bundle ID ignoring case, by regional prefix, then by display name', () => {
const { icons, pebble } = setup([
notification('a', 'NET.WHATSAPP.WHATSAPP', '0'),
notification('b', 'com.abbott.librelink.de', '0', 'LibreLink'),
notification('c', 'com.ABCDE12345.loopkit.Loop', '0', 'Loop'),
notification('d', 'com.example.unknown', '0', 'loopfollow'),
notification('e', 'com.example.unknown2', '0', 'Some Loop App'),
]);
const byKey = key => iconName(pebble, icons.readNotificationIconByKey(key, false).icon);
assert.deepEqual(['a', 'b', 'c', 'd', 'e'].map(byKey), ['whatsApp', 'glucose', 'glucose', 'glucose', 'generic']);
});
const failing = setup([notification('a', 'x', '4')], () => null);
assert.equal(failing.readNotificationIconByKey('a', false).icon.name, 'drawn-bell');
test('unknown categories use the generic bell; an unknown key has no icon', () => {
const { icons, pebble } = setup([notification('a', 'x', '0'), notification('b', 'y', '12'), notification('c', 'z', '')]);
assert.deepEqual([...icons.readActiveNotificationIcons(10, false).icons].map(icon => iconName(pebble, icon)), ['generic', 'generic', 'generic']);
assert.equal(icons.readNotificationIconByKey('missing', false).icon, null);
});
test('icons are copies, so callers can draw on them', () => {
const shared = new FakeImage('message-circle');
const icons = setup([notification('a', 'x', '4')], () => shared);
assert.notEqual(icons.readNotificationIconByKey('a', false).icon, shared);
const { icons, pebble } = setup([notification('a', 'net.whatsapp.WhatsApp', '4')]);
const first = icons.readNotificationIconByKey('a', false).icon;
first.pixels.fill(77);
assert.equal(iconName(pebble, icons.readNotificationIconByKey('a', false).icon), 'whatsApp');
});
test('every icon the tables use is bundled, 25px square, quantized, and not blank', () => {
const load = loader({}, {});
const table = load('app/native/notification-app-icons.ts');
const { pebbleIcon, PEBBLE_ICON_SIZE } = load('app/graphics/pebble-icons.ts');
const { PEBBLE_ICON_DATA } = load('app/graphics/pebble-icons-data.ts');
assert.equal(PEBBLE_ICON_SIZE, load('app/native/notification-types.ts').NOTIFICATION_ICON_SIZE);
for (const name of table.allNotificationAppIcons()) assert.ok(name in PEBBLE_ICON_DATA, `${name} is not bundled`);
for (const name of Object.keys(PEBBLE_ICON_DATA)) {
const icon = pebbleIcon(name);
assert.deepEqual([icon.width, icon.height], [25, 25], name);
assert.ok(icon.pixels.every(value => value === 0 || value === 128 || value === 255), `${name} has unquantized pixels`);
assert.ok(icon.pixels.some(value => value === 255), `${name} is blank`);
}
});
test('the icon generator inverts Pebble art: black ink lights up, white fills hide what is under them', () => {
const svg = `<svg viewBox="0 0 25 25" xmlns="http://www.w3.org/2000/svg">
<line x1="2" y1="4" x2="22" y2="4" stroke="#000" stroke-width="2"/>
<line x1="2" y1="10" x2="22" y2="10" stroke="#000" stroke-width="2"/>
<rect x="10" y="8" width="6" height="6" fill="#fff"/>
<g transform="translate(0 10)"><rect x="4" y="6" width="3" height="3" fill="black"/></g>
<g display="none"><rect x="0" y="0" width="25" height="25" fill="#000"/></g>
</svg>`;
const pixels = generator.renderIcon(svg);
const at = (x, y) => pixels[y * 25 + x];
// A 2px stroke on a grid line covers exactly the two pixel rows either side of it.
assert.deepEqual([at(5, 2), at(5, 3), at(5, 4), at(5, 5)], [0, 255, 255, 0]);
// Round caps reach a pixel past each end.
assert.equal(at(1, 3), 255);
assert.equal(at(0, 3), 0);
// The white rect drawn later hides the second line where they overlap.
assert.deepEqual([at(8, 9), at(12, 9), at(17, 9)], [255, 0, 255]);
// Black fills light up, through a group's transform; hidden groups draw nothing.
assert.deepEqual([at(5, 17), at(5, 20)], [255, 0]);
assert.equal(at(24, 24), 0);
// Its encoding round-trips through the app's decoder.
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pebble-icons-'));
const dataFile = path.join(dir, 'pebble-icons-data.ts');
fs.writeFileSync(dataFile, `export const PEBBLE_ICON_SIZE = 25;\nexport const PEBBLE_ICON_DATA = { probe: "${generator.encode(pixels)}" } as const;\n`);
const decoded = loader({}, { './pebble-icons-data': loader({}, {})(dataFile) })('app/graphics/pebble-icons.ts').pebbleIcon('probe');
assert.deepEqual([...decoded.pixels], [...pixels]);
});
+12
View File
@@ -0,0 +1,12 @@
// Exercise the iOS resource sampler's mach/malloc/rusage queries natively (on macOS).
const { execFileSync } = require('node:child_process');
const { mkdtempSync, rmSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { join, resolve } = require('node:path');
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-resource-usage-'));
try {
execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-I', 'App_Resources/iOS/src',
'tests/native/ios-resource-usage-test.m', 'App_Resources/iOS/src/FaceclawResourceUsage.m',
'-o', join(dir, 'resource-usage-test')], { cwd: root, stdio: 'inherit' });
execFileSync(join(dir, 'resource-usage-test'), [], { stdio: 'inherit' });
} finally { rmSync(dir, { recursive: true, force: true }); }
+186
View File
@@ -0,0 +1,186 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
// The developer demos' sensor plumbing on iOS: FaceclawCommunicatorBridge over a
// fake IosGlassesSession, and the imu/ambient-light/audio-monitor/mic-control-link
// .ios.ts modules over that bridge.
function harness() {
const calls = [];
const listeners = { imu: new Set(), light: new Set(), status: new Set(), audio: new Set() };
// Like the Kotlin facade, the NSData add* methods return an adapter handle for remove*.
const handleFor = (set, proxy) => { const handle = { proxy }; set.add(handle); return handle; };
const session = {
setRequiredFirmwareRevisionRevision() {}, setListenerListener() {}, setPreviewAnimationListenerListener() {},
start() { calls.push(['start']); return true; },
close() { calls.push(['close']); },
setImuReportEnabledEnableReportFrq(enable, freq) { calls.push(['imu', enable, freq]); },
addImuListenerListener(proxy) { listeners.imu.add(proxy); },
removeImuListenerListener(proxy) { listeners.imu.delete(proxy); },
queryAmbientLight() { calls.push(['als-query']); },
setAmbientLightPollingEnableIntervalMsMinDeltaHeartbeatMsBindToLease(...args) { calls.push(['als-poll', ...args]); },
addAmbientLightListenerNsDataListener: proxy => handleFor(listeners.light, proxy),
removeAmbientLightListenerListener(handle) { listeners.light.delete(handle); },
addMicStatusListenerNsDataListener: proxy => handleFor(listeners.status, proxy),
removeMicStatusListenerListener(handle) { listeners.status.delete(handle); },
addAudioMonitorListenerNsDataListener: proxy => handleFor(listeners.audio, proxy),
removeAudioMonitorListenerListener(handle) { listeners.audio.delete(handle); },
isAudioCaptureActive: () => true,
sendFaceclawMicControlRecordLabelRightTempleLeftTemple(record, label, right, left) {
calls.push(['mic-control', Array.from(record), label, right, left]);
return label !== 'refused';
},
};
class NSObject { static new() { return new this(); } }
const load = loader({
NSObject, NativeClass: () => cls => cls, setTimeout, clearTimeout,
FaceclawKitIosGlassesSession: { alloc: () => ({ initWithRightAddressLeftAddressRingAddress: () => session }) },
FaceclawKitFaceclawBleCommunicatorListener: {}, FaceclawKitIosAncsListener: {}, FaceclawKitIosAudioPacketListener: {},
FaceclawKitFaceclawCompassListener: {}, FaceclawKitFaceclawImuListener: {}, FaceclawKitIosAmbientLightListener: {},
FaceclawKitIosMicStatusListener: {}, console: { log() {}, warn() {} },
}, {
'@nativescript/core': { File: {}, knownFolders: {}, path: {} },
'../g2/firmware-compat': { REQUIRED_FACECLAW_FIRMWARE_VERSION: 37 },
// NSData stands in as the bytes themselves.
'./kotlin-data': { toData: bytes => bytes, fromData: data => new Uint8Array(data) },
'./ios-graphics': { previewPixels() {} },
'./ios-bluetooth': { iosBluetooth: {} },
});
const bridgeModule = load('app/native/faceclaw-communicator.ios.ts');
return { load, session, calls, listeners, FaceclawCommunicatorBridge: bridgeModule.FaceclawCommunicatorBridge };
}
function alsRecord(alsValue) {
const body = new Uint8Array(24);
body.set([0x41, 0x4c, 1, 2, 0x0b]);
body[6] = alsValue & 0xff; body[7] = alsValue >> 8;
return body;
}
test('the bridge is the active session from start() until close()', async () => {
const h = harness();
const bridge = new h.FaceclawCommunicatorBridge({ right: 'r', left: 'l' });
assert.equal(h.FaceclawCommunicatorBridge.getActive(), null, 'not before start');
await bridge.start();
assert.equal(h.FaceclawCommunicatorBridge.getActive(), bridge);
const closing = bridge.close();
assert.equal(h.FaceclawCommunicatorBridge.getActive(), null, 'cleared as soon as close() is called');
await closing;
assert.deepEqual(h.calls, [['start'], ['close']]);
});
test('a start queued behind close() does not make a closed bridge active', async () => {
const h = harness();
const bridge = new h.FaceclawCommunicatorBridge({ right: 'r', left: 'l' });
const started = bridge.start();
const closed = bridge.close();
await Promise.all([started, closed]);
assert.equal(h.FaceclawCommunicatorBridge.getActive(), null);
});
test('the sensor modules are no-ops without a running session', () => {
const h = harness();
const imu = h.load('app/native/imu.ios.ts');
const light = h.load('app/native/ambient-light.ios.ts');
const monitor = h.load('app/native/audio-monitor.ios.ts');
imu.setImuReportEnabled(true, 200);
light.setAmbientLightPolling(true);
light.queryAmbientLight();
imu.addImuListener(() => assert.fail('no session'))();
light.addAmbientLightListener(() => assert.fail('no session'))();
assert.equal(monitor.audioMonitorCommunicator(), null);
assert.equal(monitor.isFaceclawAudioCaptureActive(), false);
monitor.addAudioMonitorListeners(null, () => {}, () => {})();
assert.deepEqual(h.calls, []);
});
test('IMU control and samples go through the active session', async () => {
const h = harness();
const bridge = new h.FaceclawCommunicatorBridge({ right: 'r', left: 'l' });
await bridge.start();
const imu = h.load('app/native/imu.ios.ts');
imu.setImuReportEnabled(true, 199.6);
imu.setImuReportEnabled(false, -5);
const readings = [];
const off = imu.addImuListener(reading => readings.push({ ...reading }));
const [proxy] = h.listeners.imu;
proxy.onImuDataXYZEventSource(0.5, -1, 9.75, 3);
off();
assert.equal(h.listeners.imu.size, 0);
assert.deepEqual(readings, [{ x: 0.5, y: -1, z: 9.75, source: 3 }]);
assert.equal(imu.imuSourceLabel(3), 'L arm');
assert.deepEqual(h.calls.slice(1), [['imu', true, 200], ['imu', false, 0]]);
});
test('ambient light: defaults, query, and decoded reports over NSData', async () => {
const h = harness();
const bridge = new h.FaceclawCommunicatorBridge({ right: 'r', left: 'l' });
await bridge.start();
const light = h.load('app/native/ambient-light.ios.ts');
light.setAmbientLightPolling(true, { intervalMs: 250, minDelta: 0, heartbeatMs: 1000, bindToLease: true });
light.setAmbientLightPolling(false);
light.queryAmbientLight();
assert.deepEqual(h.calls.slice(1), [
['als-poll', true, 250, 0, 1000, true],
['als-poll', false, 500, 1, 5000, true],
['als-query'],
]);
const reports = [];
const off = light.addAmbientLightListener(report => reports.push(report));
const [handle] = h.listeners.light;
handle.proxy.onNsDataAmbientLightBody(alsRecord(1234));
handle.proxy.onNsDataAmbientLightBody(new Uint8Array([1, 2, 3])); // not a report: dropped
off();
assert.equal(h.listeners.light.size, 0, 'removed by the handle the session returned');
assert.equal(reports.length, 1);
assert.equal(reports[0].alsValue, 1234);
assert.equal(reports[0].reason, 'poll');
assert.equal(reports[0].opened, true);
assert.equal(reports[0].passive, true);
assert.equal(reports[0].readOk, true);
});
test('the passive audio monitor sees packets and mic status per temple', async () => {
const h = harness();
const bridge = new h.FaceclawCommunicatorBridge({ right: 'r', left: 'l' });
await bridge.start();
const monitor = h.load('app/native/audio-monitor.ios.ts');
const communicator = monitor.audioMonitorCommunicator();
assert.equal(communicator, bridge);
assert.equal(monitor.isFaceclawAudioCaptureActive(), true);
const packets = [], statuses = [];
const off = monitor.addAudioMonitorListeners(communicator,
(data, arm, arrivalMs) => packets.push([Array.from(data), arm, arrivalMs]),
(body, arm) => statuses.push([Array.from(body), arm]));
const [audio] = h.listeners.audio, [status] = h.listeners.status;
audio.proxy.onNsDataAudioPacketDataArmArrivalMs(new Uint8Array([1, 2]), 'L', 42);
audio.proxy.onNsDataAudioPacketDataArmArrivalMs(new Uint8Array([3]), '', 43);
status.proxy.onNsDataMicStatusBodyArm(new Uint8Array([0x4d, 0x43]), 'R');
off();
assert.equal(h.listeners.audio.size + h.listeners.status.size, 0);
assert.deepEqual(packets, [[[1, 2], 'L', 42], [[3], '?', 43]]);
assert.deepEqual(statuses, [[[0x4d, 0x43], 'R']]);
assert.deepEqual(h.calls, [['start']], 'monitoring sends nothing');
});
test('mic-control link sends records and hears status on iOS', async () => {
const h = harness();
const link = h.load('app/native/mic-control-link.ios.ts');
assert.equal(link.micControlSession(), null);
const bridge = new h.FaceclawCommunicatorBridge({ right: 'r', left: 'l' });
await bridge.start();
const session = link.micControlSession();
assert.equal(session, bridge);
assert.equal(link.sendMicControlRecord(session, new Uint8Array([0x4d, 0x43, 1, 4]), 'query', true, true), true);
assert.equal(link.sendMicControlRecord(session, new Uint8Array([0x4d, 0x43, 1, 4]), 'refused', true, false), false);
const heard = [];
const off = link.addMicStatusListener(session, (body, arm) => heard.push([Array.from(body), arm]));
[...h.listeners.status][0].proxy.onNsDataMicStatusBodyArm(new Uint8Array([7]), 'L');
off();
assert.equal(h.listeners.status.size, 0);
assert.deepEqual(heard, [[[7], 'L']]);
assert.deepEqual(h.calls.slice(1), [
['mic-control', [0x4d, 0x43, 1, 4], 'query', true, true],
['mic-control', [0x4d, 0x43, 1, 4], 'refused', true, false],
]);
});
+532
View File
@@ -0,0 +1,532 @@
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');
function load(file, modules = {}, globals = {}) {
const sandbox = { exports: {}, console, JSON, Promise, Date, Math, setTimeout, clearTimeout, require: id => {
if (!(id in modules)) throw new Error(`Unstubbed import: ${id}`);
return modules[id];
}, ...globals };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, sandbox);
return sandbox.exports;
}
const CLIENT_ID = '0123456789abcdef0123456789abcdef';
/** Values from the sandbox carry its own Array/Object prototypes; compare their plain JSON form. */
const plain = value => JSON.parse(JSON.stringify(value));
/** settings-store stand-in: synchronous change notifications. */
function fakeSettings(initial = {}) {
const values = new Map(Object.entries(initial));
const listeners = new Set();
return {
values,
getStringSetting: (key, fallback) => (values.has(key) ? values.get(key) : fallback),
setStringSetting: (key, value) => { values.set(key, value); for (const listener of listeners) listener(key); },
onSettingsStoreChanged: listener => { listeners.add(listener); return () => listeners.delete(listener); },
};
}
/** Manual timers: run() fires everything due, in order. */
function fakeTimers() {
let now = 1_000_000; let nextId = 1;
const pending = new Map();
return {
now: () => now,
setTimeout: (fn, delay) => { const id = nextId++; pending.set(id, { fn, at: now + (delay || 0) }); return id; },
clearTimeout: id => pending.delete(id),
advance(ms) {
now += ms;
for (const [id, timer] of [...pending].sort((a, b) => a[1].at - b[1].at)) {
if (timer.at <= now && pending.has(id)) { pending.delete(id); timer.fn(); }
}
},
delays: () => [...pending.values()].map(timer => timer.at - now),
};
}
/**
* Spotify's endpoints as a route table: "METHOD /path" -> response or function(call).
* Records every call with its parsed URL, headers and body.
*/
function fakeFetch(routes) {
const calls = [];
const fetch = async (url, init) => {
const parsed = new URL(url);
const call = { url, method: init.method, path: parsed.pathname.replace('/v1', ''), query: Object.fromEntries(parsed.searchParams),
headers: init.headers, body: init.body };
calls.push(call);
let route = routes[`${init.method} ${call.path}`];
if (typeof route === 'function') route = route(call);
if (Array.isArray(route)) route = route.length > 1 ? route.shift() : route[0];
const { status = 200, body = '' } = route ?? { status: 404, body: '{"error":{"status":404,"message":"no route"}}' };
return { status, text: async () => (typeof body === 'string' ? body : JSON.stringify(body)) };
};
return { fetch, calls };
}
function loadSpotify({ settings = fakeSettings(), timers = null } = {}) {
const globals = timers ? { setTimeout: timers.setTimeout, clearTimeout: timers.clearTimeout } : {};
const types = load('app/native/media-types.ts');
const api = load('app/native/spotify-web-api.ts', {}, globals);
const auth = load('app/native/spotify-auth.ts', { './settings-store': settings, './spotify-web-api': api }, globals);
const player = load('app/native/spotify-player.ts', { './media-types': types, './spotify-web-api': api }, globals);
const library = load('app/native/spotify-library.ts', { './spotify-player': player }, globals);
const service = load('app/native/spotify-service.ts', {
'./settings-store': settings, './spotify-auth': auth, './spotify-library': library, './spotify-player': player, './spotify-web-api': api,
}, globals);
return { types, api, auth, player, library, service, settings };
}
function signedIn(extra = {}) {
return fakeSettings({
'music.spotify.clientId': CLIENT_ID,
'music.spotify.account': JSON.stringify({ clientId: CLIENT_ID, accessToken: 'access-1', refreshToken: 'refresh-1',
expiresAtMs: 9e15, userId: 'jim', displayName: 'Jim', product: 'premium', ...extra }),
});
}
const tokens = { accessToken: async force => (force ? 'fresh' : 'stale') };
function track(id, extra = {}) {
return { type: 'track', uri: `spotify:track:${id}`, name: `Song ${id}`, duration_ms: 200_000,
artists: [{ name: 'Band' }, { name: 'Guest' }], album: { name: 'Record', images: [
{ url: 'https://i.scdn.co/640', width: 640, height: 640 },
{ url: 'https://i.scdn.co/300', width: 300, height: 300 },
{ url: 'https://i.scdn.co/64', width: 64, height: 64 }] }, ...extra };
}
function playbackBody(extra = {}) {
return { device: { id: 'phone', name: "Jim's iPhone", type: 'Smartphone', is_active: true, volume_percent: 70, supports_volume: false },
is_playing: true, progress_ms: 30_000, shuffle_state: false, repeat_state: 'off', context: { uri: 'spotify:playlist:p1' },
item: track('a'), currently_playing_type: 'track', actions: { disallows: { resuming: true } }, ...extra };
}
test('the Web API client parses playback, retries a 401 with a fresh token and names player errors', async () => {
const { api } = loadSpotify();
const f = fakeFetch({
'GET /me/player': [{ status: 401, body: '' }, { body: playbackBody({
item: { type: 'episode', uri: 'spotify:episode:e', name: 'Ep', duration_ms: 1000, images: [], show: { name: 'Show', images: [{ url: 'u', width: 300 }] } },
}) }],
'PUT /me/player/play': { status: 404, body: { error: { status: 404, message: 'Player command failed: No active device found', reason: 'NO_ACTIVE_DEVICE' } } },
'POST /me/player/next': { status: 403, body: { error: { status: 403, message: 'Premium required', reason: 'PREMIUM_REQUIRED' } } },
'PUT /me/player/pause': { status: 204 },
});
const client = new api.SpotifyClient(f.fetch, tokens);
const playback = await client.playback();
assert.deepEqual(f.calls.map(call => call.headers.Authorization), ['Bearer stale', 'Bearer fresh']);
assert.equal(f.calls[0].query.additional_types, 'episode');
assert.equal(playback.playing, true); assert.equal(playback.device.type, 'Smartphone'); assert.equal(playback.device.volumePercent, 70);
assert.deepEqual(plain(playback.disallows), ['resuming']); assert.equal(playback.contextUri, 'spotify:playlist:p1');
assert.equal(playback.item.type, 'episode'); assert.equal(playback.item.artists, 'Show'); assert.equal(playback.item.images[0].url, 'u');
await assert.rejects(client.play({ contextUri: 'spotify:album:x', offsetUri: 'spotify:track:y' }),
error => error instanceof api.SpotifyApiError && error.reason === 'NO_ACTIVE_DEVICE' && /Open Spotify/.test(error.message));
const play = f.calls.find(call => call.path === '/me/player/play');
assert.deepEqual(JSON.parse(play.body), { context_uri: 'spotify:album:x', offset: { uri: 'spotify:track:y' } });
assert.equal(play.headers['Content-Type'], 'application/json');
await assert.rejects(client.next(), /needs Spotify Premium/);
// Body-less commands still send an (empty) body, or Spotify answers 411.
await client.pause();
assert.equal(f.calls.at(-1).body, '');
assert.equal(f.calls.find(call => call.method === 'GET').body, undefined);
});
test('the Web API client follows next links and reads the renamed playlist item field', async () => {
const { api } = loadSpotify();
const f = fakeFetch({
'GET /playlists/p1/items': call => (call.query.offset === '2'
? { body: { items: [{ track: track('c') }], next: null } }
: { body: { items: [{ item: track('a') }, { item: null }, { item: track('b') }], next: 'https://api.spotify.com/v1/playlists/p1/items?offset=2' } }),
'GET /me/following': { body: { artists: { items: [{ id: 'ar', name: 'Artist', uri: 'spotify:artist:ar' }], next: null } } },
'GET /search': { body: { tracks: { items: [track('s')] }, playlists: { items: [null, { id: 'pl', name: 'Mix', owner: { id: 'o' } }] } } },
});
const client = new api.SpotifyClient(f.fetch, tokens);
assert.deepEqual(plain((await client.playlistItems('p1')).map(item => item.name)), ['Song a', 'Song b', 'Song c']);
assert.equal(f.calls[0].query.limit, '50');
assert.equal((await client.followedArtists())[0].name, 'Artist');
const results = await client.search('daft punk', ['track', 'playlist'], 50);
assert.equal(f.calls.at(-1).query.limit, '10'); // development-mode maximum
assert.equal(f.calls.at(-1).query.q, 'daft punk');
assert.equal(results.tracks[0].artists, 'Band, Guest');
assert.equal(results.playlists.length, 1); assert.equal(results.playlists[0].uri, 'spotify:playlist:pl');
});
test('sign-in builds a PKCE authorize URL, exchanges the code and stores the account', async () => {
const settings = fakeSettings({ 'music.spotify.clientId': CLIENT_ID });
const { auth } = loadSpotify({ settings });
const f = fakeFetch({
'POST /api/token': { body: { access_token: 'access-1', refresh_token: 'refresh-1', expires_in: 3600 } },
'GET /me': { body: { id: 'jim', display_name: 'Jim', product: 'premium' } },
});
let opened = null;
const webSignIn = {
createPkce: () => ({ verifier: 'verifier-1', challenge: 'challenge-1' }),
randomState: () => 'state-1',
open: async (url, port, redirectPath) => {
opened = { url, port, redirectPath };
return 'http://127.0.0.1:43823/spotify-callback?code=AQB%2Fcode&state=state-1';
},
cancel: () => {},
};
const account = await auth.signInToSpotify(f.fetch, webSignIn, () => 5_000);
const authorize = new URL(opened.url);
assert.equal(authorize.origin + authorize.pathname, 'https://accounts.spotify.com/authorize');
assert.equal(authorize.searchParams.get('client_id'), CLIENT_ID);
assert.equal(authorize.searchParams.get('redirect_uri'), 'http://127.0.0.1:43823/spotify-callback');
assert.equal(authorize.searchParams.get('code_challenge_method'), 'S256');
assert.equal(authorize.searchParams.get('code_challenge'), 'challenge-1');
assert.match(authorize.searchParams.get('scope'), /user-modify-playback-state .*user-library-modify/);
assert.deepEqual([opened.port, opened.redirectPath], [43823, '/spotify-callback']);
const exchange = Object.fromEntries(new URLSearchParams(f.calls[0].body));
assert.deepEqual(exchange, { grant_type: 'authorization_code', code: 'AQB/code', redirect_uri: 'http://127.0.0.1:43823/spotify-callback',
client_id: CLIENT_ID, code_verifier: 'verifier-1' });
assert.equal(f.calls[1].headers.Authorization, 'Bearer access-1');
assert.equal(account.displayName, 'Jim'); assert.equal(account.expiresAtMs, 5_000 + 3_600_000);
assert.equal(auth.readSpotifyAccount().refreshToken, 'refresh-1');
// Another client ID: the stored sign-in no longer counts.
settings.setStringSetting('music.spotify.clientId', 'f'.repeat(32));
assert.equal(auth.readSpotifyAccount(), null);
});
test('redirect parsing rejects mismatched state and reports a declined sign-in', () => {
const { auth } = loadSpotify();
assert.equal(auth.parseSpotifyRedirect('http://127.0.0.1:1/cb?state=s&code=a%20b', 's'), 'a b');
assert.throws(() => auth.parseSpotifyRedirect('http://127.0.0.1:1/cb?state=x&code=a', 's'), /didn't match/);
assert.throws(() => auth.parseSpotifyRedirect('http://127.0.0.1:1/cb?error=access_denied&state=s', 's'), /declined/);
assert.throws(() => auth.parseSpotifyRedirect('http://127.0.0.1:1/cb?state=s', 's'), /no authorization code/);
});
test('the session refreshes an expired token once for concurrent callers and signs out when revoked', async () => {
const settings = signedIn({ expiresAtMs: 0 });
const { auth } = loadSpotify({ settings });
let refreshes = 0;
const f = fakeFetch({ 'POST /api/token': () => {
refreshes++;
return refreshes === 1 ? { body: { access_token: 'access-2', refresh_token: 'refresh-2', expires_in: 3600 } }
: { status: 400, body: { error: 'invalid_grant', error_description: 'Refresh token revoked' } };
} });
const session = new auth.SpotifySession(f.fetch, () => 10_000);
assert.deepEqual(await Promise.all([session.accessToken(false), session.accessToken(false)]), ['access-2', 'access-2']);
assert.equal(refreshes, 1);
assert.equal(Object.fromEntries(new URLSearchParams(f.calls[0].body)).refresh_token, 'refresh-1');
// The rotated refresh token is kept; the new access token is used until it nears expiry.
assert.equal(auth.readSpotifyAccount().refreshToken, 'refresh-2');
assert.equal(await session.accessToken(false), 'access-2'); assert.equal(refreshes, 1);
await assert.rejects(session.accessToken(true), /Sign in again/);
assert.equal(auth.readSpotifyAccount(), null);
await assert.rejects(session.accessToken(false), /Not signed in/);
});
function playerFixture({ routes = {}, settings = signedIn() } = {}) {
const timers = fakeTimers();
const s = loadSpotify({ settings, timers });
const f = fakeFetch({ 'GET /me/player': { body: playbackBody() }, 'GET /me/library/contains': { body: [false] },
'GET /me/player/queue': { body: { currently_playing: track('a'), queue: [track('b'), track('c', { artists: [{ name: 'Other' }] })] } },
...routes });
const downloads = []; const decodes = []; const system = { volume: 40, set: [] };
const service = new s.service.SpotifyService({
packageName: 'com.spotify.client',
fetch: f.fetch,
webSignIn: { createPkce: () => ({}), randomState: () => '', open: async () => '', cancel: () => {} },
download: async url => { downloads.push(url); return new Uint8Array([1, 2, 3]); },
decodeArt: (_bytes, size) => { decodes.push(size); return { size }; },
systemVolume: { get: () => system.volume, set: value => system.set.push(value) },
});
// The player reads time from Date.now by default; give it the fake clock.
const player = new s.player.SpotifyPlayer({ client: service.client, packageName: 'com.spotify.client',
isSignedIn: () => s.auth.readSpotifyAccount() !== null,
download: async url => { downloads.push(url); return new Uint8Array([1, 2, 3]); },
decodeArt: (_bytes, size) => { decodes.push(size); return { size }; },
systemVolume: { get: () => system.volume, set: value => system.set.push(value) }, now: timers.now });
const flush = () => new Promise(resolve => setImmediate(resolve));
const settle = async () => { for (let i = 0; i < 6; i++) await flush(); };
return { ...s, f, timers, service, player, downloads, decodes, system, settle };
}
test('the player polls while listened to and maps playback, art and actions into the media state', async () => {
const p = playerFixture();
const states = [];
const unsubscribe = p.player.onStateChange(state => states.push(state));
assert.equal(states[0].available, false); assert.equal(states[0].accessEnabled, true);
p.timers.advance(0); await p.settle();
const state = p.player.snapshot();
assert.equal(state.available, true); assert.equal(state.playbackState, 'playing');
assert.equal(state.title, 'Song a'); assert.equal(state.artist, 'Band, Guest'); assert.equal(state.album, 'Record');
assert.equal(state.appName, 'Spotify'); assert.equal(state.packageName, 'com.spotify.client');
assert.equal(state.canPlayPause, true); assert.equal(state.canSkipNext, true);
assert.deepEqual(plain(state.customActions.map(action => action.name)), ['Shuffle: off', 'Repeat: off', 'Like song']);
// The 300px image (the smallest at least 128 wide), downloaded once and decoded per size.
assert.deepEqual(p.downloads, ['https://i.scdn.co/300']);
assert.deepEqual(plain(p.player.getAlbumArt(112)), { size: 112 }); p.player.getAlbumArt(112);
assert.deepEqual(p.decodes, [112]);
// Position advances between polls.
p.timers.advance(1_000);
assert.equal(p.player.snapshot().positionMs, 31_000);
// Polling continues on a playing interval, and stops with the last listener.
assert.ok(p.timers.delays().some(delay => delay > 0 && delay <= 5_000));
unsubscribe();
assert.deepEqual(p.timers.delays(), []);
});
test('pausing applies at once, resuming with no active device starts on an open phone, and failures become a notice', async () => {
let active = true;
const p = playerFixture({ routes: {
'PUT /me/player/pause': { status: 204 },
'PUT /me/player/play': call => (call.query.device_id || active ? { status: 204 }
: { status: 404, body: { error: { status: 404, reason: 'NO_ACTIVE_DEVICE', message: 'none' } } }),
'GET /me/player/devices': { body: { devices: [
{ id: 'mac', name: 'MacBook', type: 'Computer', is_active: false },
{ id: 'iphone', name: 'iPhone', type: 'Smartphone', is_active: false },
{ id: 'tv', name: 'TV', type: 'TV', is_restricted: true }] } },
'POST /me/player/next': { status: 403, body: { error: { status: 403, reason: 'PREMIUM_REQUIRED', message: 'x' } } },
} });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
await p.player.playPause();
assert.equal(p.player.snapshot().playbackState, 'paused');
assert.equal(p.f.calls.filter(call => call.path === '/me/player/pause').length, 1);
active = false;
await p.player.playPause();
const plays = p.f.calls.filter(call => call.path === '/me/player/play');
assert.deepEqual(plays.map(call => call.query.device_id ?? ''), ['', 'iphone']);
assert.deepEqual(JSON.parse(plays[1].body), {});
await p.player.skipNext();
assert.match(p.player.snapshot().notice, /Premium/);
// A settle poll re-reads soon after each command.
assert.ok(p.timers.delays().some(delay => delay <= 600));
p.timers.advance(8_100);
assert.equal(p.player.snapshot().notice, '');
});
test('the queue lists the current track first and jumping skips forward to the chosen item', async () => {
const p = playerFixture({ routes: { 'POST /me/player/next': { status: 204 } } });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
assert.deepEqual(plain(p.player.getQueue()), []); // first read in flight
await p.settle();
assert.deepEqual(plain(p.player.getQueue().map(item => [item.id, item.title, item.subtitle, item.active])),
[[0, 'Song a', 'Band, Guest', true], [1, 'Song b', 'Band, Guest', false], [2, 'Song c', 'Other', false]]);
await p.player.skipToQueueItem(2);
assert.equal(p.f.calls.filter(call => call.path === '/me/player/next').length, 2);
assert.equal(p.player.snapshot().title, 'Song c');
assert.deepEqual(plain(p.player.getQueue().map(item => item.title)), ['Song c']);
});
test('volume goes to a speaker through Spotify and to this phone through the system volume', async () => {
const p = playerFixture({ routes: {
'GET /me/player': { body: playbackBody({ device: { id: 'spk', name: 'Kitchen', type: 'Speaker', is_active: true, volume_percent: 30, supports_volume: true } }) },
'PUT /me/player/volume': { status: 204 },
} });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
assert.equal(p.player.snapshot().appName, 'Spotify · Kitchen');
assert.equal(p.player.getMediaVolumePercent(), 30);
p.player.setMediaVolumePercent(32); p.player.setMediaVolumePercent(34);
assert.equal(p.player.getMediaVolumePercent(), 34);
await p.settle();
const volumes = p.f.calls.filter(call => call.path === '/me/player/volume');
// One request in flight; the latest value follows it.
assert.deepEqual(volumes.map(call => [call.query.volume_percent, call.query.device_id]), [['32', 'spk'], ['34', 'spk']]);
const phone = playerFixture();
phone.player.onStateChange(() => {});
phone.timers.advance(0); await phone.settle();
assert.equal(phone.player.getMediaVolumePercent(), 40);
phone.player.setMediaVolumePercent(55);
assert.deepEqual(phone.system.set, [55]);
assert.equal(phone.f.calls.some(call => call.path === '/me/player/volume'), false);
});
test('shuffle, repeat and like run their Web API calls and relabel at once', async () => {
const p = playerFixture({ routes: {
'PUT /me/player/shuffle': { status: 204 }, 'PUT /me/player/repeat': { status: 204 }, 'PUT /me/library': { status: 200 },
} });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
await p.player.sendCustomAction('shuffle');
await p.player.sendCustomAction('repeat');
await p.player.sendCustomAction('like');
assert.deepEqual(plain(p.player.snapshot().customActions.map(action => action.name)), ['Shuffle: on', 'Repeat: all', 'Unlike song']);
const calls = p.f.calls.filter(call => call.method === 'PUT');
assert.deepEqual(calls.map(call => [call.path, call.query.state ?? call.query.uris]),
[['/me/player/shuffle', 'true'], ['/me/player/repeat', 'context'], ['/me/library', 'spotify:track:a']]);
});
test('signed out, the player reports no access and never calls the API', async () => {
const p = playerFixture({ settings: fakeSettings({ 'music.spotify.clientId': CLIENT_ID }) });
p.player.onStateChange(() => {});
p.timers.advance(0); await p.settle();
assert.equal(p.player.snapshot().accessEnabled, false);
assert.equal(p.f.calls.length, 0);
});
test('the library tree lists owned playlists as browsable and plays tracks within their context', async () => {
const s = loadSpotify({ settings: signedIn() });
const f = fakeFetch({
'GET /me/playlists': { body: { items: [
{ id: 'mine', name: 'Mine', owner: { id: 'jim' } },
{ id: 'theirs', name: 'Theirs', owner: { id: 'spotify', display_name: 'Spotify' } },
{ id: 'collab', name: 'Collab', collaborative: true, owner: { id: 'friend' } }], next: null } },
'GET /me/player/recently-played': { body: { items: [
{ track: track('a'), context: { uri: 'spotify:album:al' } }, { track: track('a'), context: null }, { track: track('b'), context: null }] } },
});
const plays = [];
const library = new s.library.SpotifyLibrary({ client: new s.api.SpotifyClient(f.fetch, tokens), packageName: 'com.spotify.client',
player: { startPlayback: async request => { plays.push(request); return true; } },
isSignedIn: () => true, userId: () => 'jim' });
const [app] = library.listBrowsableApps();
assert.equal(await library.connect(app), 'root');
assert.deepEqual(plain((await library.browse('root')).map(row => row.title)), ['Recently played', 'Playlists', 'Liked Songs', 'Albums', 'Artists']);
const playlists = await library.browse('playlists');
assert.deepEqual(plain(playlists.map(row => [row.title, row.browsable, row.subtitle])),
[['Mine', true, ''], ['Theirs', false, 'Spotify'], ['Collab', true, '']]);
const recent = await library.browse('recent');
assert.deepEqual(plain(recent.map(row => row.mediaId)), ['track:spotify:track:a|spotify:album:al', 'track:spotify:track:b']);
library.playFromMediaId(recent[0].mediaId);
library.playFromMediaId(recent[1].mediaId);
library.playFromMediaId('track:spotify:track:z|liked');
library.playFromMediaId('liked');
library.playFromMediaId('playlist:mine');
assert.deepEqual(plain(plays), [
{ contextUri: 'spotify:album:al', offsetUri: 'spotify:track:a' },
{ uris: ['spotify:track:b'] },
{ contextUri: 'spotify:user:jim:collection', offsetUri: 'spotify:track:z' },
{ contextUri: 'spotify:user:jim:collection' },
{ contextUri: 'spotify:playlist:mine' },
]);
library.disconnect();
await assert.rejects(library.browse('root'), /Not connected/);
});
test('search-and-play starts the best match and explains failures', async () => {
const p = playerFixture({ routes: {
'GET /search': call => ({ body: call.query.type === 'album'
? { albums: { items: [{ id: 'al', name: 'Discovery', uri: 'spotify:album:al', artists: [{ name: 'Daft Punk' }] }] } }
: { tracks: { items: [] } } }),
'PUT /me/player/play': { status: 204 },
} });
assert.equal(await p.service.playSearch('discovery', 'album'), 'the album Discovery by Daft Punk');
const play = p.f.calls.find(call => call.path === '/me/player/play');
assert.deepEqual(JSON.parse(play.body), { context_uri: 'spotify:album:al' });
await assert.rejects(p.service.playSearch('nothing'), /found no track/);
});
test('the service resets the player when the signed-in account changes, not when tokens refresh', async () => {
const p = playerFixture();
const reset = [];
p.service.player.accountChanged = () => reset.push('reset');
const changes = [];
p.service.onChange(() => changes.push('change'));
const account = JSON.parse(p.settings.values.get('music.spotify.account'));
p.settings.setStringSetting('music.spotify.account', JSON.stringify({ ...account, accessToken: 'access-9' }));
assert.deepEqual(reset, []);
p.service.signOut();
assert.deepEqual(reset, ['reset']);
assert.equal(p.service.account(), null);
assert.ok(changes.length >= 2);
});
function composite(appleState, spotifyState, spotifyLastPlayingAtMs = 0) {
const types = load('app/native/media-types.ts');
const calls = [];
const fakeBridge = (name, state) => ({
accessLabel: `${name} label`, accessAction: `${name} action`, skipRebuildsQueue: name === 'spotify', state, lastPlayingAtMs: 0,
onStateChange(listener) { listener(this.state); return () => {}; },
snapshot() { return this.state; }, start: async () => calls.push(`${name}:start`), stop: async () => {},
playPause: async () => calls.push(`${name}:playPause`), skipNext: async () => calls.push(`${name}:next`),
getAlbumArt: () => name, getQueue: () => [], refresh: async () => calls.push(`${name}:refresh`),
hasAccess: () => name === 'apple', requestAccess: async () => calls.push(`${name}:access`),
});
const apple = fakeBridge('apple', appleState);
const spotify = fakeBridge('spotify', spotifyState);
spotify.lastPlayingAtMs = spotifyLastPlayingAtMs;
const api = load('app/native/media-controller.ios.ts', {
'@nativescript/core': { Dialogs: {}, Utils: {} }, './image-files': {}, './kotlin-data': {}, './media-types': types,
'./spotify.ios': { spotifyService: () => null },
});
const bridge = new api.IosMediaControllerBridge(apple, () => (spotifyState ? spotify : null), () => 50);
return { api, bridge, calls, apple, spotify, types };
}
test('the iOS bridge follows whichever player is playing and routes commands to it', async () => {
const types = load('app/native/media-types.ts');
const base = { ...types.DEFAULT_MEDIA_STATE, accessEnabled: true };
const playing = { ...base, available: true, playbackState: 'playing', title: 'Playing' };
const paused = { ...base, available: true, playbackState: 'paused', title: 'Paused' };
const { chooseMediaSource } = composite(base, null).api;
assert.equal(chooseMediaSource('apple-music', base, null, 0, 0), 'apple-music');
assert.equal(chooseMediaSource('apple-music', { ...base, accessEnabled: false }, base, 0, 0), 'spotify');
assert.equal(chooseMediaSource('apple-music', paused, playing, 0, 0), 'spotify');
assert.equal(chooseMediaSource('spotify', playing, paused, 0, 0), 'apple-music');
assert.equal(chooseMediaSource('spotify', playing, playing, 0, 0), 'spotify'); // no flapping
assert.equal(chooseMediaSource('spotify', paused, paused, 20, 10), 'apple-music'); // played last
assert.equal(chooseMediaSource('apple-music', paused, paused, 0, 0), 'spotify');
assert.equal(chooseMediaSource('apple-music', paused, base, 0, 0), 'apple-music');
const c = composite(paused, { ...playing, notice: '' });
assert.equal(c.bridge.snapshot().title, 'Playing');
assert.equal(c.bridge.skipRebuildsQueue, true);
await c.bridge.playPause(); await c.bridge.refresh();
assert.deepEqual(c.calls, ['spotify:playPause', 'spotify:refresh']);
assert.equal(c.bridge.getAlbumArt(10), 'spotify');
const firstArt = c.bridge.snapshot().artVersion;
// Switching players changes the art version even when the sources' versions match.
c.spotify.state = { ...paused, notice: 'Spotify needs Premium' };
c.apple.state = { ...playing, title: 'Apple' };
const state = c.bridge.snapshot();
assert.equal(state.title, 'Apple'); assert.notEqual(state.artVersion, firstArt);
// A Spotify failure still shows while Apple Music is the player shown.
assert.equal(state.notice, 'Spotify needs Premium');
assert.equal(c.bridge.hasAccess(), true); // Apple Music's permission
await c.bridge.requestAccess();
assert.equal(c.calls.at(-1), 'apple:access');
const idle = composite(base, base);
assert.equal(idle.bridge.snapshot().status, 'Nothing is playing.');
});
test('the assistant offers Spotify search-and-play once signed in and refreshes before reading media state', async () => {
let account = null; const searches = []; const refreshes = [];
const spotify = {
account: () => account,
playSearch: async (query, kind) => { searches.push([query, kind]); if (query === 'fail') throw new Error('No device'); return 'Song by Band'; },
};
const registered = new Map(); const systemTools = new Map();
const { registerSystemTools } = load('app/assistant/system-tools.ts', {
'../native/calendar': {}, '../native/calendar-permissions': {},
'../native/media-controller': { mediaControllerBridge: { start: async () => {}, refresh: async () => refreshes.push('refresh'),
snapshot: () => ({ accessEnabled: true, playbackState: 'playing', title: 'Song', artist: 'Band', appName: 'Spotify' }) } },
'../native/spotify': { spotifyService: () => spotify },
'../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {},
}, { global: { isIOS: true } });
registerSystemTools({
registerSystemTool: (definition, handler) => systemTools.set(definition.name, handler),
register: registration => registered.set(registration.spec.name, registration),
});
assert.match((await systemTools.get('media.now_playing')()).content, /Playing: Song by Band in Spotify/);
assert.deepEqual(refreshes, ['refresh']);
const tool = registered.get('media.spotify_play');
assert.equal(tool.spec.availability, 'always');
assert.equal(tool.isAvailable(), false);
account = { userId: 'jim' };
assert.equal(tool.isAvailable(), true);
assert.equal((await tool.handler({ query: ' discovery ', kind: 'album' })).content, 'Playing Song by Band on Spotify.');
await tool.handler({ query: 'x', kind: 'podcast' });
assert.deepEqual(plain(searches), [['discovery', 'album'], ['x', 'track']]);
assert.equal((await tool.handler({ query: 'fail' })).error, 'No device');
assert.equal((await tool.handler({ query: ' ' })).ok, false);
});
+48 -3
View File
@@ -7,7 +7,7 @@ function harness() {
const native = { startWithEndpointing(endpointing) { this.endpointing = endpointing; return ''; }, startPhoneWithEndpointing(endpointing) { this.endpointing = endpointing; this.phone = true; return this.error ?? ''; },
startPcmCaptureWithEndpointingPhone(endpointing, phone) { this.pcmCapture = { endpointing, phone }; return this.error ?? ''; },
cancel() { this.cancels++; }, cancels: 0, finish() { this.finished = true; }, acceptPacket: p => audio.push(p) };
const audio = [], timers = new Map(), commands = [], text = [], clouds = [];
const audio = [], timers = new Map(), commands = [], text = [], clouds = [], analyzers = [];
// Stands in for cloud-stt-provider: any provider but "onboard" is a keyed cloud one.
const provider = {
usesCloudStt: voice => voice.provider !== 'onboard',
@@ -19,6 +19,17 @@ function harness() {
clouds.push(client); return client;
},
};
// Stands in for speech-analyzer-stt: SpeechAnalyzer (iOS 26+) when the test says the device has it.
let analyzerSupported = false;
const analyzer = {
speechAnalyzerSupported: () => analyzerSupported,
createSpeechAnalyzerClient(options, continuous) {
const client = { options, continuous, pcm: [], started: false, finished: 0, stopped: false,
start() { this.started = true; }, acceptPcm(bytes) { this.pcm.push([...bytes]); },
finish() { this.finished++; }, stop() { this.stopped = true; } };
analyzers.push(client); return client;
},
};
const recordings = [];
class FakeRecorder {
static open(name) { const r = new FakeRecorder(); r.name = name; r.path = `/Downloads/${name}.wav`; r.pcm = []; r.closed = false; recordings.push(r); return r; }
@@ -32,7 +43,8 @@ function harness() {
requestMicrophoneAuthorization: done => { microphoneRequests++; done(3); } },
setTimeout: fn => { timers.set(fn, fn); return fn; }, clearTimeout: fn => timers.delete(fn),
NSData: { dataWithBytesLength: (b, n) => Buffer.from(b, 0, n) }, interop: { handleof: b => b, bufferFromData: d => d },
require: name => name === './cloud-stt-provider' ? provider : name === './wav-recorder.ios' ? { WavRecorder: FakeRecorder } : {} };
require: name => name === './cloud-stt-provider' ? provider : name === './speech-analyzer-stt.ios' ? analyzer
: name === './wav-recorder.ios' ? { WavRecorder: FakeRecorder } : {} };
vm.runInNewContext(ts.transpileModule(fs.readFileSync('app/native/voice-control.ios.ts', 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox);
const bridge = new sandbox.exports.IosVoiceControlBridge();
@@ -43,7 +55,8 @@ function harness() {
return Promise.resolve();
} };
const onboard = { provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: '', sonioxApiKey: '' };
return { bridge, native, session, audio, commands, text, timers, clouds, recordings, release: () => release(),
return { bridge, native, session, audio, commands, text, timers, clouds, analyzers, recordings, release: () => release(),
analyzerSupported: value => { analyzerSupported = value; },
onboard, cloud: { ...onboard, provider: 'soniox', sonioxApiKey: 'key' },
microphoneAuthorization: value => { microphoneAuthorization = value; }, microphoneRequests: () => microphoneRequests,
authorization: value => { authorization = value; }, permissionRequests: () => permissionRequests,
@@ -248,6 +261,38 @@ test('continuous capture needs a cloud provider, shares its mic with push-to-tal
assert.ok(ends >= 1);
});
test('on-device continuous capture runs on SpeechAnalyzer where supported; push-to-talk keeps the dictation recognizer', async () => {
const h = harness();
assert.equal(h.bridge.supportsContinuous(h.onboard), false, 'before iOS 26 on-device needs a cloud provider');
h.analyzerSupported(true);
assert.equal(h.bridge.supportsContinuous(h.onboard), true);
const start = h.bridge.startGlassesCapture(h.session, h.onboard, () => {}, false, true); h.release(); await start;
assert.deepEqual({ ...h.native.pcmCapture }, { endpointing: false, phone: false }, 'native only decodes PCM');
assert.equal(h.native.endpointing, undefined, "Apple's dictation recognizer stays idle");
const [analyzer] = h.analyzers;
assert.equal(analyzer.started, true); assert.equal(analyzer.continuous(), true);
assert.equal(h.clouds.length, 0);
h.native.pcmHandler(new Uint8Array([1, 2, 3, 4]));
assert.deepEqual(analyzer.pcm, [[1, 2, 3, 4]]);
analyzer.options.onTranscript({ text: 'First thought.', isFinal: true, paragraphBreakAfter: true });
const stopped = h.bridge.stopContinuousCapture();
h.event({ kind: 'ended', message: 'Transcription stopped.' });
assert.equal(analyzer.finished, 1);
analyzer.options.onTranscript({ text: 'The tail.', isFinal: true });
await stopped;
assert.equal(analyzer.stopped, true);
assert.deepEqual(h.text.map(event => event.text), ['First thought.', 'The tail.']);
// Push-to-talk (not continuous) still uses FaceclawSpeech's own recognizer.
const ptt = h.bridge.startGlassesCapture(h.session, h.onboard, () => {}); h.release(); await ptt;
assert.equal(h.analyzers.length, 1); assert.equal(h.native.endpointing, false);
});
test('a cloud provider keeps continuous capture even where SpeechAnalyzer is supported', async () => {
const h = harness(); h.analyzerSupported(true);
const start = h.bridge.startGlassesCapture(h.session, h.cloud, () => {}, false, true); h.release(); await start;
assert.equal(h.clouds.length, 1); assert.equal(h.analyzers.length, 0);
});
test('push-to-talk after Transcribe closed starts its own capture instead of joining the finalizing one', async () => {
const h = harness();
const start = h.bridge.startGlassesCapture(h.session, h.cloud, () => {}, false, true); h.release(); await start;
+105 -9
View File
@@ -1,19 +1,26 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function fixture() {
function fixture({ choose } = {}) {
class Observable { get(key) { return this[key]; } notifyPropertyChange() {} }
const { KeyboardInputViewModel } = loader({}, { '@nativescript/core': { Observable } })('app/phone-ui/keyboard-input-view-model.ts');
const model = new KeyboardInputViewModel(), sent = [], text = [];
const dialogs = [];
const Dialogs = { action: async options => { dialogs.push(options); return choose ? choose(options.actions) : options.cancelButtonText; } };
const { KeyboardInputViewModel } = loader({ setTimeout: callback => callback() },
{ '@nativescript/core': { Observable, Dialogs } })('app/phone-ui/keyboard-input-view-model.ts');
const model = new KeyboardInputViewModel(), sent = [], text = [], visibilities = [];
const session = { targets: [{ id: 'assistant', label: 'Send to Assistant' }, { id: 'app', label: 'Type Into App' }],
setText: value => text.push(value), sendTo: id => sent.push(id), discard: () => model.setSession(null) };
setText: value => text.push(value), sendTo: id => sent.push(id), discard: () => model.setSession(null),
visibility: 'both', setVisibility(value) { visibilities.push(value); this.visibility = value; },
history: [] };
model.setSession(session);
return { model, session, sent, text };
const field = { text: '', focused: 0, selection: null, focus() { this.focused++; }, setSelection(start) { this.selection = start; } };
const button = { page: { getViewById: id => id === 'keyboardInputField' ? field : undefined } };
return { model, session, sent, text, visibilities, dialogs, field, button };
}
test('multiline keyboard editor routes Agent and App buttons to distinct shell destinations', () => {
test('multiline keyboard editor routes Assistant and App buttons to distinct shell destinations', () => {
const h = fixture();
assert.equal(h.model.keyboardInputPrimaryTargetLabel, 'Send to Agent');
assert.equal(h.model.keyboardInputSecondaryTargetLabel, 'Type into App');
assert.equal(h.model.keyboardInputPrimaryTargetLabel, 'To Assistant');
assert.equal(h.model.keyboardInputSecondaryTargetLabel, 'To App');
h.model.onKeyboardInputTextChange({ object: { text: 'First line\nSecond line' } });
h.model.onKeyboardInputPrimarySendTap();
h.model.onKeyboardInputSecondarySendTap();
@@ -26,7 +33,96 @@ test('discard and shell-side closure reset the editor; unsupported destinations
assert.equal(h.model.keyboardInputText, '');
assert.equal(h.model.keyboardInputVisibility, 'collapse');
h.model.setSession({ ...h.session, targets: [{ id: 'assistant', label: 'Send' }] });
assert.equal(h.model.keyboardInputPrimaryTargetLabel, 'Send to Agent');
assert.equal(h.model.keyboardInputPrimaryTargetLabel, 'Send');
assert.equal(h.model.keyboardInputSecondaryTargetVisibility, 'collapse');
h.model.onKeyboardInputSecondarySendTap(); assert.deepEqual(h.sent, []);
});
test('visibility button cycles both, phone only, glasses only; glasses-only hides the field but keeps it', () => {
const h = fixture();
const state = () => [h.model.keyboardInputPhoneIconOpacity, h.model.keyboardInputGlassesIconOpacity,
h.model.keyboardInputFieldClass, h.model.keyboardInputGlassesOnlyVisibility];
assert.deepEqual(state(), [1, 1, '', 'collapse']);
h.model.onKeyboardInputVisibilityTap();
assert.deepEqual(state(), [1, 0.3, '', 'collapse']);
h.model.onKeyboardInputVisibilityTap();
assert.deepEqual(state(), [0.3, 1, 'keyboard-input-field-hidden', 'visible']);
h.model.onKeyboardInputGlassesOnlyHintTap({ object: h.button });
assert.equal(h.field.focused, 1);
h.model.onKeyboardInputVisibilityTap();
assert.deepEqual(h.visibilities, ['phone', 'glasses', 'both']);
assert.equal(h.model.keyboardInputVisibilityDescription, 'Shown on phone and glasses');
});
test('history button lists recent inputs on one line each and puts the pick in the field', async () => {
const long = 'x'.repeat(100);
const h = fixture({ choose: actions => actions[1] });
assert.equal(h.model.keyboardInputHistoryOpacity, 0.3);
await h.model.onKeyboardInputHistoryTap({ object: h.button });
assert.equal(h.dialogs.length, 0);
h.session.history = ['first line\n second', long];
h.model.setSession(null); h.model.setSession(h.session);
assert.equal(h.model.keyboardInputHistoryOpacity, 1);
await h.model.onKeyboardInputHistoryTap({ object: h.button });
assert.deepEqual([...h.dialogs[0].actions], ['first line second', `${'x'.repeat(79)}…`]);
assert.equal(h.model.keyboardInputText, long);
assert.equal(h.text.at(-1), long);
assert.equal(h.field.text, long);
assert.equal(h.field.selection, 100);
});
function layerFixture(visibility = 'both', history = []) {
const menu = { items: [], setItems(items) { this.items = items; }, get selectedItem() { return this.items[0]; } };
const { KeyboardInputLayer } = loader({}, {
'./input-dialog': { createInputDialogMenu: () => menu, paintInputDialog() {} },
})('app/ui/shell/keyboard-input.ts');
const stack = new Set(), events = [];
const layer = new KeyboardInputLayer({
actions: { requestRender() {} },
onClosed: () => events.push('closed'),
dismiss: () => { if (stack.delete(layer)) layer.onRemoved(); },
setShownOnGlasses: shown => { if (shown) stack.add(layer); else if (stack.delete(layer)) layer.onRemoved(); },
visibility, onVisibilityChanged: value => events.push(`visibility:${value}`), history,
sendTargets: [{ id: 'app', label: 'Type Into App', onSend: text => events.push(`sent:${text}`) }],
});
if (layer.shownOnGlasses) stack.add(layer);
// The shell's sleep path: the stack is cleared and each layer told.
const clearStack = () => { for (const removed of [...stack]) { stack.delete(removed); removed.onRemoved(); } };
return { layer, stack, events, history, clearStack };
}
test('phone-only takes the dialog off the glasses without ending the session', () => {
const f = layerFixture();
f.layer.setVisibility('phone');
assert.equal(f.stack.size, 0);
assert.deepEqual(f.events, ['visibility:phone']);
f.clearStack();
f.layer.setText('typed while hidden'); f.layer.sendTo('app');
assert.deepEqual(f.events, ['visibility:phone', 'closed', 'sent:typed while hidden']);
f.layer.setVisibility('both');
assert.equal(f.stack.size, 0);
});
test('shown again on the glasses, the dialog closes with the stack as before', () => {
const f = layerFixture('phone');
assert.equal(f.stack.size, 0);
f.layer.setVisibility('glasses');
assert.equal(f.stack.size, 1);
f.layer.setVisibility('both');
assert.deepEqual(f.events, ['visibility:glasses', 'visibility:both']);
f.clearStack();
assert.deepEqual(f.events.at(-1), 'closed');
const discarded = layerFixture('phone');
discarded.layer.discard();
assert.deepEqual(discarded.events, ['closed']);
});
test('sent messages go into the shared history newest first, without duplicates or blanks', () => {
const history = [];
for (const text of ['one', ' ', 'two', 'one ']) {
const f = layerFixture('both', history);
f.layer.setText(text); f.layer.sendTo('app');
}
assert.deepEqual(history, ['one', 'two']);
for (let i = 0; i < 25; i++) {
const f = layerFixture('phone', history);
f.layer.setText(`message ${i}`); f.layer.sendTo('app');
}
assert.equal(history.length, 20);
assert.equal(history[0], 'message 24');
});
@@ -3,6 +3,7 @@ package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertSame
import kotlin.test.assertTrue
@@ -68,6 +69,38 @@ class GraphicsCoresTest {
assertTrue(!GrayPacket.hasLightBackground(IntArray(8 * 8) { 0xff808080.toInt() }, 8, 8))
}
@Test
fun inkBoundsTrimTransparentMargins() {
val art = IntArray(8 * 6)
art[2 * 8 + 3] = 0xff000000.toInt()
art[4 * 8 + 5] = 0x80ffffff.toInt()
art[0] = 0x08ffffff // too faint to count
assertContentEquals(intArrayOf(3, 2, 6, 5), GrayPacket.inkBounds(art, 8, 6))
assertNull(GrayPacket.inkBounds(IntArray(4 * 4), 4, 4))
}
@Test
fun darkInkOnTransparentTurnsLight() {
val darkGray = 0xff202020.toInt()
val text = IntArray(10 * 10)
for (i in 40 until 50) text[i] = darkGray
text[55] = 0x80202020.toInt()
assertTrue(GrayPacket.lightenDarkInk(text, 10, 10))
assertEquals(0xffdfdfdf.toInt(), text[40])
assertEquals(0x80dfdfdf.toInt(), text[55]) // alpha kept
assertEquals(0, text[0] ushr 24) // still transparent
// Light ink, an empty layout, and a light opaque panel are left alone (the panel is
// invertLightBackground's to handle).
val light = IntArray(10 * 10)
light[40] = 0xffe0e0e0.toInt()
assertTrue(!GrayPacket.lightenDarkInk(light, 10, 10))
assertEquals(0xffe0e0e0.toInt(), light[40])
assertTrue(!GrayPacket.lightenDarkInk(IntArray(4), 2, 2))
val panel = IntArray(10 * 10) { 0xfff0f0f0.toInt() }
panel[5] = darkGray
assertTrue(!GrayPacket.lightenDarkInk(panel, 10, 10))
}
@Test
fun toneCurveDarkensMidtones() {
val identity = GrayPacket.toneCurve(1f)
@@ -0,0 +1,151 @@
package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
import kotlin.test.assertTrue
class SettingsTransferTest {
private val androidSettings: Map<String, Any?> = linkedMapOf(
"display.timeFormat" to "24h",
"terminal.connections" to """[{"id":"c1","url":"g2mirror://token@host:8737","enabled":true}]""",
"display.uiFont2" to """{"kind":"ttf","file":"Roboto-Light.ttf","size":15}""",
"navigate.homeAddress" to "[home] 1 Main St",
"assistant.prompt" to "{ \"not\": \"compact\" }",
"voice.enabled" to true,
"flappy.highScore" to "18",
"ratio" to 0.25,
"count" to 42L,
"notifications.sources" to """[{"packageName":"com.tailscale.ipn","showOnGlasses":true}]""",
"deviceIdentity.pairedAtMs" to JsonNumber("1791678209651"),
"deviceAddress.left" to "EC:D7:82:69:3C:CB",
"onboarding.complete" to true,
"evenhub:com.example.app:ls:state" to """{"level":3}""",
"ios.settings.changeToken" to "bookkeeping",
)
@Test
fun exportSplitsPortableAndPlatformSettingsAndExpandsJsonLosslessly() {
val text = SettingsTransfer.export(androidSettings, "android", "0.8.3")
val document = Json.parseObject(text)
assertEquals(JsonNumber("2"), document["schema"])
assertEquals("android", document["platform"])
assertEquals("0.8.3", document["appVersion"])
val settings = document["settings"].asObject()!!
val local = document["platformSettings"].asObject()!!
assertEquals(settings.keys.sorted(), settings.keys.toList())
assertEquals(
setOf("notifications.sources", "deviceIdentity.pairedAtMs", "deviceAddress.left", "onboarding.complete"),
local.keys,
)
assertTrue("ios.settings.changeToken" !in settings && "ios.settings.changeToken" !in local)
// Compact JSON becomes structure; anything that wouldn't round-trip exactly stays a string.
assertEquals("g2mirror://token@host:8737", settings["terminal.connections"].asArray()!![0].asObject()!!["url"])
assertEquals(JsonNumber("3"), settings["evenhub:com.example.app:ls:state"].asObject()!!["level"])
assertEquals("[home] 1 Main St", settings["navigate.homeAddress"])
assertEquals("{ \"not\": \"compact\" }", settings["assistant.prompt"])
assertEquals(true, settings["voice.enabled"])
assertEquals("18", settings["flappy.highScore"])
assertEquals(JsonNumber("0.25"), settings["ratio"])
assertEquals(JsonNumber("42"), settings["count"])
assertEquals(JsonNumber("1791678209651"), local["deviceIdentity.pairedAtMs"])
}
@Test
fun samePlatformImportRestoresEveryValueExactly() {
val plan = SettingsTransfer.planImport(SettingsTransfer.export(androidSettings, "android"), "android")
assertEquals("android", plan.sourcePlatform)
assertTrue(plan.skipped.isEmpty())
val expected = androidSettings - "ios.settings.changeToken"
assertEquals(expected.keys, plan.values.keys)
for ((key, value) in expected) {
val want: Any? = when (value) {
is Double -> JsonNumber.of(value)
is Long -> JsonNumber(value.toString())
else -> value
}
assertEquals(want, plan.values[key], key)
}
}
@Test
fun crossPlatformImportCarriesOnlyPortableSettings() {
val plan = SettingsTransfer.planImport(SettingsTransfer.export(androidSettings, "android"), "ios")
assertEquals(
setOf("notifications.sources", "deviceIdentity.pairedAtMs", "deviceAddress.left", "onboarding.complete"),
plan.skipped.toSet(),
)
assertEquals("24h", plan.values["display.timeFormat"])
assertEquals(androidSettings["terminal.connections"], plan.values["terminal.connections"])
assertTrue(plan.values.keys.none { SettingsTransfer.isPlatformLocal(it) })
}
@Test
fun legacySchemaOneIsClassifiedAtImport() {
val iosExport = """{"schema":1,"settings":{
"ios.ble.peripheral.EC:D7:82:69:3C:CB":"{\"identifier\":\"8A2D\",\"role\":\"left\"}",
"deviceAddress.left":"EC:D7:82:69:3C:CB",
"integrations.evenhub.terminalId":"1429021a30db4586",
"privacy.dataCollection":"full",
"onboarding.complete":true}}"""
val onAndroid = SettingsTransfer.planImport(iosExport, "android")
assertEquals("ios", onAndroid.sourcePlatform)
assertEquals(mapOf<String, Any>("privacy.dataCollection" to "full"), onAndroid.values)
assertEquals(4, onAndroid.skipped.size)
assertEquals(5, SettingsTransfer.planImport(iosExport, "ios").values.size)
// scripts/config_document.py converts Android shared-preferences XML to this shape.
val fromXml = """{"schema":1,"platform":"android","settings":{"deviceAddress.left":"AA","display.mode":"576x288"}}"""
assertEquals(2, SettingsTransfer.planImport(fromXml, "android").values.size)
assertEquals(listOf("deviceAddress.left"), SettingsTransfer.planImport(fromXml, "ios").skipped)
}
@Test
fun handEditedDocumentsFollowTheirSections() {
val edited = """{
"schema": 2,
"settings": { "launcher.folders": { "pinball": "Games" }, "music.lastApp": "kept" },
"platformSettings": { "deviceAddress.left": "AA" }
}"""
val plan = SettingsTransfer.planImport(edited, "ios")
assertNull(plan.sourcePlatform)
// No platform: platformSettings never apply, settings always do, even a platform-local key.
assertEquals(mapOf<String, Any>("launcher.folders" to """{"pinball":"Games"}""", "music.lastApp" to "kept"), plan.values)
assertEquals(listOf("deviceAddress.left"), plan.skipped)
}
@Test
fun malformedDocumentsAreRejected() {
val invalid = listOf(
"not json",
"[]",
"""{"settings":{}}""",
"""{"schema":3,"settings":{}}""",
"""{"schema":2}""",
"""{"schema":2,"settings":[]}""",
"""{"schema":2,"settings":{"x":null}}""",
"""{"schema":2,"settings":{"":"empty name"}}""",
"""{"schema":2,"platform":7,"settings":{}}""",
"""{"schema":2,"platform":"ios","settings":{"x":"a"},"platformSettings":{"x":"b"}}""",
"""{"schema":2,"settings":{"x":"a","x":"b"}}""",
)
for (text in invalid) assertFailsWith<IllegalArgumentException>(text) { SettingsTransfer.planImport(text, "ios") }
assertFailsWith<IllegalArgumentException> { SettingsTransfer.export(mapOf("x" to listOf("a")), "ios") }
}
@Test
fun objectiveCBridgeUsesJsonText() {
val document = SettingsTransfer.exportValues("""{"a":"b","on":true,"n":1.5,"ios.ble.peripheral.X":"{}"}""", "ios", null)!!
assertNull(Json.parseObject(document)["appVersion"])
val reply = Json.parseObject(SettingsTransfer.importPlan(document, "android"))
assertEquals(true, reply["ok"])
assertEquals(mapOf<String, Any?>("a" to "b", "n" to JsonNumber("1.5"), "on" to true), reply["settings"])
assertEquals(listOf<Any?>("ios.ble.peripheral.X"), reply["skipped"])
assertEquals("ios", reply["sourcePlatform"])
val failure = Json.parseObject(SettingsTransfer.importPlan("""{"schema":9,"settings":{}}""", "ios"))
assertEquals(false, failure["ok"])
assertEquals("Unsupported config schema 9", failure["message"])
assertNull(SettingsTransfer.exportValues("[]", "ios", null))
}
}
@@ -197,6 +197,52 @@ class IosSessionFacadeTest {
assertFalse(pumpMainUntil(200) { relayed.size > 1 })
}
@Test
fun nsDataSensorListenersDeliverOnMainAndUnsubscribeByTheirHandles() {
val session = IosGlassesSession(FakeIosLink(), null, IosSessionHost(threadName = "test-session-sensors"), RIGHT, LEFT, "", IosProtocolPlatform)
val core = session.coreForTests()
val light = ArrayList<List<Byte>>()
val status = ArrayList<Pair<List<Byte>, String>>()
val audio = ArrayList<Triple<List<Byte>, String, Long>>()
var offMain = 0
val lightHandle = session.addAmbientLightListenerNsData(object : IosAmbientLightListener {
override fun onNsDataAmbientLight(body: platform.Foundation.NSData) {
if (!NSThread.isMainThread) offMain++
light.add(body.byteArray().toList())
}
})
val statusHandle = session.addMicStatusListenerNsData(object : IosMicStatusListener {
override fun onNsDataMicStatus(body: platform.Foundation.NSData, arm: String) {
if (!NSThread.isMainThread) offMain++
status.add(body.byteArray().toList() to arm)
}
})
val audioHandle = session.addAudioMonitorListenerNsData(object : IosAudioPacketListener {
override fun onNsDataAudioPacket(data: platform.Foundation.NSData, arm: String, arrivalMs: Long) {
if (!NSThread.isMainThread) offMain++
audio.add(Triple(data.byteArray().toList(), arm, arrivalMs))
}
})
val report = byteArrayOf(0x41, 0x4c, 1, 2)
val record = byteArrayOf(0x4d, 0x43, 1)
val packet = byteArrayOf(9, 8, 7)
core.emitAmbientLight(report)
core.emitMicStatus(record, LEFT)
core.emitAudioMonitorPacket(packet, "R", 1234L)
assertTrue(pumpMainUntil(2_000) { light.size == 1 && status.size == 1 && audio.size == 1 })
assertEquals(report.toList(), light[0])
assertEquals(record.toList() to "L", status[0])
assertEquals(Triple(packet.toList(), "R", 1234L), audio[0])
assertEquals(0, offMain)
session.removeAmbientLightListener(lightHandle)
session.removeMicStatusListener(statusHandle)
session.removeAudioMonitorListener(audioHandle)
core.emitAmbientLight(report)
core.emitMicStatus(record, RIGHT)
core.emitAudioMonitorPacket(packet, "L", 5678L)
assertFalse(pumpMainUntil(200) { light.size > 1 || status.size > 1 || audio.size > 1 })
}
@Test
fun hostDispatchesToTheCallingQueueAndRunsTheWorker() {
val host = IosSessionHost(threadName = "test-host")
+100
View File
@@ -0,0 +1,100 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const { pcmLevel } = loader()('app/util/audio-level.ts');
function harness() {
const levelListeners = new Set();
let tick = null;
const { MicLevelMeter, paintListeningHeader } = loader({
setInterval(fn) { tick = fn; return 1; },
clearInterval() { tick = null; },
}, {
'../native/voice-control': { voiceControlBridge: {
onMicLevel(listener) { levelListeners.add(listener); return () => levelListeners.delete(listener); },
} },
})('app/ui/listening-header.ts');
const level = value => { for (const listener of levelListeners) listener(value); };
return { MicLevelMeter, paintListeningHeader, level, tick: () => tick?.(), ticking: () => tick !== null, levelListeners };
}
/** Records draws; text is 6px per character, like a fixed-width bitmap face. */
function fakeImage(width = 576) {
const texts = [], rects = [];
return { width, texts, rects,
drawText(_font, x, y, text, gray) { texts.push({ x, y, text, gray }); },
fillRect(x, y, w, h, gray) { rects.push({ x, y, w, h, gray }); } };
}
const font = { ascent: 12, lineHeight: 16, measureText: text => text.length * 6 };
function pcm(amplitude, samples = 800) {
const bytes = new Uint8Array(samples * 2);
for (let i = 0; i < samples; i++) {
const value = (i % 2 ? amplitude : -amplitude) & 0xffff;
bytes[i * 2] = value & 255;
bytes[i * 2 + 1] = value >> 8;
}
return bytes;
}
test('pcm level maps RMS onto a 60 dB meter', () => {
assert.equal(pcmLevel(new Uint8Array(0)), 0);
assert.equal(pcmLevel(pcm(0)), 0);
assert.ok(Math.abs(pcmLevel(pcm(3277)) - 2 / 3) < 0.01, '-20 dBFS sits two thirds up');
assert.ok(pcmLevel(pcm(32767)) > 0.99);
});
test('the meter repaints only when the lit segment count changes, and only while visible', () => {
const h = harness();
const meter = new h.MicLevelMeter();
let renders = 0, visible = true;
meter.start(() => renders++, () => visible);
h.level(0.3); h.level(0.5); h.level(0.2);
h.tick();
assert.equal(renders, 1, 'the peak of the tick lights 4 of 8');
h.level(0.5); h.tick();
assert.equal(renders, 1, 'same segment count, no repaint');
// Off screen the level still tracks, so the next visible paint is current.
visible = false;
h.level(1); h.tick();
assert.equal(renders, 1);
const image = fakeImage();
meter.paint(image, 0, 10, 6);
assert.equal(image.rects.filter(rect => rect.gray === 200).length, 8);
// Silence decays the meter to empty within about a second.
visible = true;
for (let i = 0; i < 5; i++) h.tick();
const after = fakeImage();
meter.paint(after, 0, 10, 6);
assert.equal(after.rects.filter(rect => rect.gray === 200).length, 0);
assert.ok(renders > 1);
meter.stop();
assert.equal(h.ticking(), false);
assert.equal(h.levelListeners.size, 0);
});
test('the header puts title and status on one line, truncated before the meter', () => {
const h = harness();
const meter = new h.MicLevelMeter();
const image = fakeImage(300);
const bodyTop = h.paintListeningHeader(image, font, 'Translate', 'Listening (ElevenLabs)...', meter);
assert.equal(bodyTop, 20 + 18 + 2);
assert.deepEqual(image.texts.map(({ x, y, gray }) => [x, y, gray]), [[24, 20, 200], [24 + 54, 20, 110]]);
assert.equal(image.texts[0].text, 'Translate');
const status = image.texts[1].text;
assert.ok(status.startsWith(' · Listening') && status.endsWith('...'), status);
const meterX = Math.min(...image.rects.map(rect => rect.x));
assert.equal(meterX, 300 - 24 - meter.width);
assert.ok(24 + 54 + status.length * 6 <= meterX - 10, 'status stops short of the meter');
// Segments sit on the text baseline.
assert.ok(image.rects.every(rect => rect.y + rect.h === 20 + font.ascent));
const idle = fakeImage(300);
h.paintListeningHeader(idle, font, 'Transcribe', 'Starting microphone...', null);
assert.equal(idle.rects.length, 0);
assert.equal(idle.texts[1].text, ' · Starting microphone...');
});
+17 -1
View File
@@ -64,6 +64,22 @@ test('context menu surface and selected row both use +4 without changing the app
assert.deepEqual(closed.image.pixels,base.pixels);
assert.deepEqual(wire.presentationRecords(prepareFrameDraws(closed.draws)),[]);
});
test('a submenu that replaces its parent menu skips the parent plane and dims the content once',()=>{
const base=new graphics.GrayImage(100,90,200);
const stack=new LayerStack({paint:()=>base},noopLayerActions,{width:100,height:90});
const layout={x:10,y:8,width:80,minHeight:70,maxHeight:70,dimUnderneath:0.5};
class SubmenuLayer extends MenuLayer { replacesLayerBelow(below) { return below instanceof MenuLayer; } }
const parent=new MenuLayer('PARENT',[{label:'SUB',onSelect(){}}],layout);
stack.push(parent);
stack.push(new SubmenuLayer('CHILD',[{label:'ITEM',onSelect(){}}],layout));
stack.push(new SubmenuLayer('GRANDCHILD',[{label:'ITEM',onSelect(){}}],layout));
const painted=stack.paint();
assert.equal(painted.length,2);
assert.equal(painted[0].image.pixels[0],100);
assert.equal(stack.baseDim(),0.5);
stack.pop();stack.pop();
assert.equal(stack.paint().length,2);
});
test('text page paints at the stack size, not the lens size',()=>{
// An app window's stack is its viewport; a lens-sized plane is rejected by
// the compositor as larger than the window surface.
@@ -85,7 +101,7 @@ test('unselected sidebar icons retain -2 depth through shell cropping, including
'../../graphics/shell-scene':shellScene,'../../graphics/image':graphics,'../../graphics/display-list':require('../.test-build/app/graphics/display-list.js'),
'../../graphics/ui-fonts':{getDefaultSmallFont:()=>font,getDefaultMediumFont:()=>font},
'../../graphics/textwrap':{},'./ambient-cards':{},'../../graphics/battery':{},
'../../native/notification-icons':{},'../../native/phone-battery':{},'../../util/render-freshness':{},
'../../native/notification-icons':{},'../../native/notification-types':require('../.test-build/app/native/notification-types.js'),'../../native/phone-battery':{},'../../util/render-freshness':{},
'../../graphics/icons':{},'../dashboard-settings':{},'../clock-format':{},'../../graphics/bdffont':{},
'../layers':{LayerStack},'../menu':{scrollToKeepSelectionVisible:()=>0},'../metrics':{},
'./geometry':{MIN_WINDOW_HEIGHT:280,minWindowTop:()=>0,windowTop:()=>0,SHELL_OPAQUE_BLACK:1,SIDEBAR_WIDTH:64,TOP_BAR_HEIGHT:24,
+3
View File
@@ -91,6 +91,9 @@ function bridges() {
'./image-files': {},
'./notification-access': { isNotificationListenerEnabled: () => true },
'./media-types': load('app/native/media-types.ts', () => { throw new Error('media-types has no imports'); }),
// Signed out of Spotify's Web API: the bridges are the native session and browser services alone.
'./spotify': { spotifyService: () => null },
'./spotify-player': { SPOTIFY_ANDROID_PACKAGE: 'com.spotify.music' },
};
const globals = { global: { isAndroid: true }, com: { faceclaw: { app: {
FaceclawMediaBrowser: NativeBrowser,
+56 -10
View File
@@ -1,34 +1,80 @@
#import <Foundation/Foundation.h>
#import "FaceclawConfigPort.h"
#include <assert.h>
static NSDictionary *send(NSString *dir, NSUserDefaults *defaults, NSString *domain, NSDictionary *request) {
#include <math.h>
static NSString *jsonText(id object) {
return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:object options:0 error:nil] encoding:NSUTF8StringEncoding];
}
static id parse(NSString *text) {
return [NSJSONSerialization JSONObjectWithData:[text dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
}
/** Stands in for Kotlin SettingsTransfer (tested in tests/kotlin): wraps and unwraps values. */
@interface StubCodec : NSObject <FaceclawConfigCodec>
@property NSString *lastAppVersion;
@property NSDictionary *forcedPlan;
@end
@implementation StubCodec
- (NSString *)exportValuesJson:(NSString *)valuesJson platform:(NSString *)platform appVersion:(NSString *)appVersion {
self.lastAppVersion = appVersion;
return jsonText(@{@"schema":@2, @"platform":platform, @"settings":parse(valuesJson)});
}
- (NSString *)importPlanDocument:(NSString *)document platform:(NSString *)platform {
if (self.forcedPlan) return jsonText(self.forcedPlan);
NSDictionary *doc = parse(document);
if (![doc[@"schema"] isEqual:@2]) return jsonText(@{@"ok":@NO, @"message":@"Unsupported config schema"});
return jsonText(@{@"ok":@YES, @"settings":doc[@"settings"], @"skipped":@[@"deviceAddress.left"], @"sourcePlatform":@"android"});
}
@end
static NSDictionary *send(NSString *dir, NSUserDefaults *defaults, NSString *domain, StubCodec *codec, NSDictionary *request) {
NSData *json = [NSJSONSerialization dataWithJSONObject:request options:0 error:nil];
assert([json writeToFile:[dir stringByAppendingPathComponent:@"request.json"] atomically:YES]);
[FaceclawConfigPort processDirectory:dir defaults:defaults domain:domain];
[FaceclawConfigPort processDirectory:dir defaults:defaults domain:domain codec:codec appVersion:@"1.2.3"];
assert(![NSFileManager.defaultManager fileExistsAtPath:[dir stringByAppendingPathComponent:@"request.json"]]);
return [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[dir stringByAppendingPathComponent:@"response.json"]] options:0 error:nil];
}
int main(int argc, const char **argv) { @autoreleasepool {
NSString *dir = [NSString stringWithUTF8String:argv[1]];
NSString *domain = [@"com.faceclaw.config-test." stringByAppendingString:NSUUID.UUID.UUIDString];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:domain];
[defaults setPersistentDomain:@{@"keep":@"original", @"enabled":@NO} forName:domain];
StubCodec *codec = [StubCodec new];
[defaults setPersistentDomain:@{@"keep":@"original", @"enabled":@NO, @"notANumber":@(NAN)} forName:domain];
NSDictionary *input = @{@"terminal.connections":@"[{\"url\":\"g2mirror://test@host\"}]", @"enabled":@YES, @"empty":@"", @"number":@42, @"unicode":@"π & < \n 🔋"};
NSDictionary *reply = send(dir, defaults, domain, @{@"id":@"1", @"operation":@"push", @"settings":input});
NSDictionary *reply = send(dir, defaults, domain, codec, @{@"id":@"1", @"operation":@"push",
@"document":jsonText(@{@"schema":@2, @"settings":input})});
assert([reply[@"ok"] boolValue] && [reply[@"count"] intValue] == 5);
assert([[defaults persistentDomainForName:domain][@"keep"] isEqual:@"original"]);
assert([reply[@"skipped"] isEqual:@[@"deviceAddress.left"]] && [reply[@"sourcePlatform"] isEqual:@"android"]);
NSDictionary *stored = [defaults persistentDomainForName:domain];
assert([stored[@"keep"] isEqual:@"original"]);
assert(stored[@"enabled"] == (id)kCFBooleanTrue && [stored[@"number"] isEqual:@42]);
NSDictionary *backup = [NSDictionary dictionaryWithContentsOfFile:[dir stringByAppendingPathComponent:@"previous.plist"]];
assert([backup[@"keep"] isEqual:@"original"] && ![backup[@"enabled"] boolValue]);
reply = send(dir, defaults, domain, @{@"id":@"2", @"operation":@"pull"});
reply = send(dir, defaults, domain, codec, @{@"id":@"2", @"operation":@"pull"});
assert([reply[@"id"] isEqual:@"2"] && [reply[@"ok"] boolValue]);
NSDictionary *output = reply[@"config"][@"settings"];
assert([codec.lastAppVersion isEqual:@"1.2.3"]);
NSDictionary *document = parse(reply[@"document"]);
assert([document[@"platform"] isEqual:@"ios"]);
NSDictionary *output = document[@"settings"];
for (NSString *key in input) assert([output[key] isEqual:input[key]]);
assert(!output[@"ios.settings.changeToken"]);
assert([output[@"keep"] isEqual:@"original"] && [reply[@"count"] intValue] == (int)output.count);
assert(!output[@"ios.settings.changeToken"] && !output[@"notANumber"]);
NSDictionary *before = [defaults persistentDomainForName:domain];
reply = send(dir, defaults, domain, @{@"id":@"3", @"operation":@"push", @"settings":@{@"keep":@"changed", @"invalid":@[]}});
reply = send(dir, defaults, domain, codec, @{@"id":@"3", @"operation":@"push", @"document":jsonText(@{@"schema":@9, @"settings":@{}})});
assert(![reply[@"ok"] boolValue] && [reply[@"message"] isEqual:@"Unsupported config schema"]);
codec.forcedPlan = @{@"ok":@YES, @"settings":@{@"keep":@"changed", @"invalid":@[]}};
reply = send(dir, defaults, domain, codec, @{@"id":@"4", @"operation":@"push", @"document":@"{}"});
assert(![reply[@"ok"] boolValue]);
codec.forcedPlan = nil;
// Requests from older scripts sent pre-converted settings rather than a document.
reply = send(dir, defaults, domain, codec, @{@"id":@"5", @"operation":@"push", @"settings":@{@"keep":@"changed"}});
assert(![reply[@"ok"] boolValue]);
assert([[defaults persistentDomainForName:domain] isEqual:before]);
reply = send(dir, defaults, domain, @{@"id":@"4", @"operation":@"bad"});
reply = send(dir, defaults, domain, codec, @{@"id":@"6", @"operation":@"bad"});
assert(![reply[@"ok"] boolValue]);
[defaults removePersistentDomainForName:domain]; [defaults synchronize];
puts("PASS: native config merges typed values, backs up, exports, consumes requests once and rejects invalid imports atomically");
+69
View File
@@ -0,0 +1,69 @@
#import <Foundation/Foundation.h>
#import "FaceclawLoopbackRedirect.h"
/** GET a URL, pumping the main run loop (where the receiver delivers redirects) until it answers. */
static NSInteger get(NSString *url, NSString **body) {
__block NSInteger status = -1;
__block NSString *text = nil;
__block BOOL done = NO;
[[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:url]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
status = error ? -1 : ((NSHTTPURLResponse *)response).statusCode;
text = data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : nil;
done = YES;
}] resume];
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:10];
while (!done && deadline.timeIntervalSinceNow > 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
if (body) *body = text;
return status;
}
static void pumpBriefly(void) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.2]];
}
int main(int argc, const char **argv) {
@autoreleasepool {
NSInteger port = argc > 1 ? atoi(argv[1]) : 43899;
NSString *base = [NSString stringWithFormat:@"http://127.0.0.1:%ld", (long)port];
FaceclawLoopbackRedirect *receiver = [FaceclawLoopbackRedirect new];
NSMutableArray<NSString *> *redirects = [NSMutableArray array];
receiver.redirectHandler = ^(NSString *url) { [redirects addObject:url]; };
NSString *error = [receiver startOnPort:port path:@"/spotify-callback"];
if (error) { NSLog(@"FAIL start: %@", error); return 1; }
// A second listener on the same port reports the conflict instead of failing silently.
FaceclawLoopbackRedirect *second = [FaceclawLoopbackRedirect new];
NSString *conflict = [second startOnPort:port path:@"/spotify-callback"];
if (!conflict || [conflict rangeOfString:@"port"].location == NSNotFound) { NSLog(@"FAIL conflict: %@", conflict); return 1; }
// Other paths (a browser's favicon request) are refused and not reported.
NSString *body = nil;
if (get([base stringByAppendingString:@"/favicon.ico"], &body) != 404) { NSLog(@"FAIL favicon"); return 1; }
pumpBriefly();
if (redirects.count) { NSLog(@"FAIL favicon reported %@", redirects); return 1; }
// The redirect itself: a page for the browser, the full URL (query intact) for the app.
NSString *redirect = [base stringByAppendingString:@"/spotify-callback?code=AQB%2Fx-y_z&state=s1"];
if (get(redirect, &body) != 200 || [body rangeOfString:@"return to Faceclaw"].location == NSNotFound) {
NSLog(@"FAIL redirect page: %@", body); return 1;
}
pumpBriefly();
if (redirects.count != 1 || ![redirects[0] isEqualToString:redirect]) { NSLog(@"FAIL redirect url %@", redirects); return 1; }
// A query-less or prefixed path does not count as the redirect.
if (get([base stringByAppendingString:@"/spotify-callback-extra?code=x"], NULL) != 404) { NSLog(@"FAIL prefix"); return 1; }
// Once stopped, the port is free again.
[receiver stop];
pumpBriefly();
if (get(redirect, NULL) != -1) { NSLog(@"FAIL still listening after stop"); return 1; }
NSString *again = [second startOnPort:port path:@"/spotify-callback"];
if (again) { NSLog(@"FAIL restart: %@", again); return 1; }
[second stop];
NSLog(@"PASS loopback redirect");
return 0;
}
}
+59
View File
@@ -0,0 +1,59 @@
#import <Foundation/Foundation.h>
#import <stdlib.h>
#import <string.h>
#import "FaceclawResourceUsage.h"
static NSDictionary *sample(void) {
NSData *json = [[FaceclawResourceUsage sample] dataUsingEncoding:NSUTF8StringEncoding];
return [NSJSONSerialization JSONObjectWithData:json options:0 error:nil];
}
#define CHECK(cond, ...) do { if (!(cond)) { NSLog(@"FAIL " __VA_ARGS__); return 1; } } while (0)
int main(void) {
@autoreleasepool {
NSDictionary *before = sample();
CHECK([before isKindOfClass:NSDictionary.class], @"sample is not a JSON object");
for (NSString *key in @[@"atMs", @"cpuMs", @"footprintKb", @"footprintPeakKb", @"residentKb", @"mallocKb",
@"threads", @"thermalState", @"lowPower"]) {
CHECK([before[key] isKindOfClass:NSNumber.class], @"missing %@ in %@", key, before);
}
CHECK(before[@"availableKb"] == nil, @"os_proc_available_memory is iOS-only: %@", before);
CHECK([before[@"footprintKb"] longLongValue] > 0 && [before[@"residentKb"] longLongValue] > 0, @"memory %@", before);
CHECK([before[@"footprintPeakKb"] longLongValue] >= [before[@"footprintKb"] longLongValue], @"peak %@", before);
CHECK([before[@"threads"] intValue] >= 1, @"threads %@", before);
// Two busy threads alive at sampling time, ~150 ms of work between them;
// 64 MiB of touched malloc memory.
NSCondition *gate = [NSCondition new];
__block BOOL release = NO;
__block volatile double sink = 0;
for (int t = 0; t < 2; t++) {
[NSThread detachNewThreadWithBlock:^{
NSDate *until = [NSDate dateWithTimeIntervalSinceNow:0.075];
while (until.timeIntervalSinceNow > 0) for (int i = 0; i < 10000; i++) sink += i * 0.5;
[gate lock]; while (!release) [gate wait]; [gate unlock];
}];
}
size_t bytes = 64u << 20;
char *block = malloc(bytes);
memset(block, 1, bytes);
[NSThread sleepForTimeInterval:0.2];
NSDictionary *after = sample();
long long elapsed = [after[@"atMs"] longLongValue] - [before[@"atMs"] longLongValue];
double cpu = [after[@"cpuMs"] doubleValue] - [before[@"cpuMs"] doubleValue];
CHECK(elapsed >= 190 && elapsed < 5000, @"monotonic clock advanced %lld ms over a 200 ms sleep", elapsed);
CHECK(cpu >= 100, @"CPU advanced only %.1f ms over ~150 ms of busy work", cpu);
CHECK([after[@"threads"] intValue] >= [before[@"threads"] intValue] + 2, @"threads %@ -> %@", before[@"threads"], after[@"threads"]);
CHECK([after[@"mallocKb"] longLongValue] - [before[@"mallocKb"] longLongValue] >= 60 * 1024,
@"malloc %@ -> %@ after 64 MiB", before[@"mallocKb"], after[@"mallocKb"]);
CHECK([after[@"footprintKb"] longLongValue] - [before[@"footprintKb"] longLongValue] >= 60 * 1024,
@"footprint %@ -> %@ after touching 64 MiB", before[@"footprintKb"], after[@"footprintKb"]);
[gate lock]; release = YES; [gate broadcast]; [gate unlock];
free(block);
printf("PASS: resource sample reports footprint %lld KiB, malloc %lld KiB, %d threads, %.0f ms CPU over %lld ms\n",
[after[@"footprintKb"] longLongValue], [after[@"mallocKb"] longLongValue], [after[@"threads"] intValue], cpu, elapsed);
}
return 0;
}
+19 -1
View File
@@ -13,7 +13,7 @@ const route = { coordinates: [[-122.4, 37.7], [-122.4, 37.71]], distanceMeters:
const fix = latitude => ({ latitude, longitude: -122.4, timestampMs: Date.now(), bearingDeg: 0, speedMps: 1.5, accuracyMeters: 5 });
function fixture() {
const messages = [], timers = new Set(), timeouts = new Set(), holds = [], maps = [], errors = [], requests = [];
let callbacks, compass, delayRoute = null, delayMap = null, routeError = null;
let callbacks, compass, delayRoute = null, delayMap = null, routeError = null, distanceUnit = 'miles', timeFormat = '24h';
const routeMath = loader()('app/apps/navigate/route-follower.ts');
const tracker = { running: false, isRunning() { return this.running; }, start() { this.running = true; }, stop() { this.running = false; } };
const sensor = { LocationTracker: function (value) { callbacks = value; return tracker; }, COMPASS_CHANGED: 15,
@@ -24,6 +24,8 @@ function fixture() {
'@nativescript/core/globals': {},
'../../graphics/bdffont': { getFont: () => ({}) }, '../../graphics/ui-fonts': { getDefaultSmallFont: () => ({}) },
'../../native/settings-store': { onSettingsStoreChanged() {} },
'../../ui/dashboard-settings': { distanceUnitSetting: { get: () => distanceUnit } },
'../../ui/clock-format': loader({}, { './dashboard-settings': { timeFormatSetting: { get: () => timeFormat } } })('app/ui/clock-format.ts'),
'./navigation-sensors': sensor,
'./route-follower': routeMath,
'./destinations': { findSavedDestinationByName: () => null, rememberRecentDestination() {},
@@ -59,6 +61,8 @@ function fixture() {
tick: () => { for (const callback of timers) callback(); },
expireFix: () => { for (const callback of timeouts) callback(); timeouts.clear(); },
failRoute: () => { routeError = new Error('Route unavailable'); },
setDistanceUnit: value => { distanceUnit = value; },
setTimeFormat: value => { timeFormat = value; },
fix: latitude => send({ type: 'navigation-sensors', event: { kind: 'location', location: fix(latitude), id: 1 } }),
heading: value => compass?.({ command: 15, headingDegrees: value }),
holdMap: () => { let resolve, reject; delayMap = new Promise((done, fail) => { resolve = done; reject = fail; });
@@ -77,6 +81,20 @@ test('shared Navigate worker follows streamed GPS, uses calibrated true heading
assert.equal(h.timers.size, 0); assert.deepEqual(h.errors, []);
});
test('route distances follow the distance unit setting', async () => {
const h = fixture(); const starting = h.startNavigation('Test park', 'walking');
h.fix(37.7); assert.match(await starting, /: 0\.7 mi, about 10 minutes/);
h.fix(37.705); assert.match(h.describeRouteStatus(), /in 0\.3 mi\. 0\.3 mi and/);
h.setDistanceUnit('kilometers'); assert.match(h.describeRouteStatus(), /in 550 m\. 550 m and/);
});
test('arrival times follow the time format setting', async () => {
const h = fixture(); const starting = h.startNavigation('Test park', 'walking');
h.fix(37.7); assert.match(await starting, /arriving \d{1,2}:\d{2}\./);
h.fix(37.705); assert.match(h.describeRouteStatus(), /ETA \d{1,2}:\d{2}\.$/);
h.setTimeFormat('12h'); assert.match(h.describeRouteStatus(), /ETA \d{1,2}:\d{2} [AP]M\.$/);
});
test('closing Navigate while a route request is pending cannot restart guidance or GPS', async () => {
const h = fixture(), release = h.holdRoute();
const starting = h.startNavigation('Test park', 'walking');
+1
View File
@@ -82,6 +82,7 @@ function ui(fontSize = 12) {
'~/util/date-util': { formatRelativeTime: () => '' },
'../native/notification-icons': native,
'../native/notification-sources': prefs,
'../native/notification-types': require('../.test-build/app/native/notification-types.js'),
'../native/notification-access': { isNotificationListenerEnabled: () => true },
'../util/render-freshness': { renderPassAllowsStaleData: () => false },
'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'),
+335
View File
@@ -0,0 +1,335 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const flush = () => new Promise(resolve => setImmediate(resolve));
function assistant({ apiKey = 'fixture-key', baseUrl = 'https://provider.example/api/v1' } = {}) {
const requests = [], calls = [], deltas = [], errors = [], done = [];
const registry = {
listTools: () => [
{ name: 'timer.set', description: 'Set a timer', inputSchema: { type: 'object', properties: { seconds: { type: 'number' } } } },
{ name: 'glasses.show_alert', description: 'Show an alert', inputSchema: { type: 'object', properties: { text: { type: 'string' } } } },
],
callTool: async (name, args) => {
calls.push({ name, args });
return { ok: true, content: `${name} completed` };
},
};
const load = loader({ global: { isIOS: false, isAndroid: true }, setTimeout, clearTimeout }, {
'./sse': { openSseRequest: (url, body, headers, listener) => {
const request = { url, body: JSON.parse(body), headers, listener, cancelled: false };
requests.push(request);
return { cancel: () => { request.cancelled = true; } };
} },
'../native/local-llm': { streamLocalModel: () => assert.fail('unexpected local model'), isLocalModelReady: () => false, isLocalModelSupported: () => true, LOCAL_MODEL: { id: 'qwen', label: 'Qwen' } },
'./bridge-client': { assistantBridge: {} },
'./tool-registry': { toolRegistry: registry },
'../prompts': {
ASSISTANT_SYSTEM_PROMPT_BASE: 'test',
buildAssistantSystemPrompt: () => 'glasses system',
describeAssistantContext: () => '',
},
});
const { AssistantSession } = load('app/assistant/session.ts');
const session = new AssistantSession({ kind: 'direct', llm: {
selection: 'openai-compatible', provider: 'openai-compatible', model: 'fixture-model', apiKey, baseUrl,
} }, registry);
const send = text => session.sendUtterance(text, {}, {
onTextDelta: (_delta, full) => deltas.push(full),
onToolActivity() {},
onError: error => errors.push(error),
onTurnDone: result => done.push(result),
});
const event = (request, value) => request.listener.onLine('data: ' + JSON.stringify(value));
const chunk = (request, delta, finishReason = null) => event(request, { choices: [{ index: 0, delta, finish_reason: finishReason }] });
const finish = (request, reason = 'stop', close = 'done') => {
chunk(request, {}, reason);
if (close === 'done') request.listener.onLine('data: [DONE]');
else request.listener.onComplete();
};
return { load, session, send, event, chunk, finish, requests, calls, deltas, errors, done };
}
test('compatible provider sends full history, nested tools, the exact API root and optional auth', () => {
const h = assistant();
h.send('Remember the blue door');
const first = h.requests[0];
assert.equal(first.url, 'https://provider.example/api/v1/chat/completions');
assert.equal(first.headers.Authorization, 'Bearer fixture-key');
assert.equal(first.headers['Content-Type'], 'application/json');
assert.equal(first.body.model, 'fixture-model');
assert.equal(first.body.stream, true);
assert.equal('max_tokens' in first.body, false, 'provider default output limit is preserved');
assert.equal('reasoning_effort' in first.body, false, 'custom models default to provider reasoning');
assert.deepEqual(first.body.messages.map(m => [m.role, m.content]), [
['system', 'glasses system'], ['user', 'Remember the blue door'],
]);
assert.equal(first.body.tools[0].type, 'function');
assert.equal(first.body.tools[0].function.name, 'timer_set');
assert.equal(first.body.tools[0].function.parameters.type, 'object');
h.chunk(first, { content: 'Remembered.' }); h.finish(first);
h.send('What color?');
assert.deepEqual(h.requests[1].body.messages.slice(-3).map(m => [m.role, m.content]), [
['user', 'Remember the blue door'], ['assistant', 'Remembered.'], ['user', 'What color?'],
]);
h.session.cancel();
const noKey = assistant({ apiKey: '' });
noKey.send('Private request');
assert.equal('Authorization' in noKey.requests[0].headers, false);
noKey.session.cancel();
});
test('Venice web search is opt-in and never leaks to other compatible providers', () => {
const defaultOff = assistant({ baseUrl: 'https://api.venice.ai/api/v1' });
defaultOff.send('Current weather');
assert.equal('venice_parameters' in defaultOff.requests[0].body, false);
defaultOff.session.cancel();
const { isVeniceApiBaseUrl } = defaultOff.load('app/native/openai-chat.ts');
assert.equal(isVeniceApiBaseUrl(' https://api.venice.ai/api/v1/ '), true);
assert.equal(isVeniceApiBaseUrl('https://api.venice.ai/api/v1/chat/completions'), true);
assert.equal(isVeniceApiBaseUrl('http://api.venice.ai/api/v1'), false);
assert.equal(isVeniceApiBaseUrl('https://api.venice.ai.evil.example/api/v1'), false);
assert.equal(isVeniceApiBaseUrl('https://openwebui.example/api'), false);
const enabled = assistant({ baseUrl: 'https://api.venice.ai/api/v1' });
enabled.session.configure({ kind: 'direct', llm: {
selection: 'openai-compatible', provider: 'openai-compatible', model: 'fixture-model',
apiKey: 'fixture-key', baseUrl: 'https://api.venice.ai/api/v1', webSearch: true,
} });
enabled.send('Current weather');
assert.deepEqual(JSON.parse(JSON.stringify(enabled.requests[0].body.venice_parameters)), {
enable_web_search: 'on',
});
enabled.session.cancel();
const openWebUi = assistant({ baseUrl: 'https://openwebui.example/api' });
openWebUi.session.configure({ kind: 'direct', llm: {
selection: 'openai-compatible', provider: 'openai-compatible', model: 'fixture-model',
apiKey: 'fixture-key', baseUrl: 'https://openwebui.example/api', webSearch: true,
} });
openWebUi.send('Current weather');
assert.equal('venice_parameters' in openWebUi.requests[0].body, false);
openWebUi.session.cancel();
});
test('Venice and Open WebUI roots are preserved; only a complete endpoint suffix is removed', () => {
const { normalizeOpenAiCompatibleUrl } = assistant().load('app/native/openai-chat.ts');
assert.equal(normalizeOpenAiCompatibleUrl(' https://api.venice.ai/api/v1/ '), 'https://api.venice.ai/api/v1');
assert.equal(normalizeOpenAiCompatibleUrl('https://api.venice.ai/api/v1/chat/completions'), 'https://api.venice.ai/api/v1');
assert.equal(normalizeOpenAiCompatibleUrl('http://100.64.0.1:3000/api'), 'http://100.64.0.1:3000/api');
assert.equal(normalizeOpenAiCompatibleUrl('https://openwebui.example/api/chat/completions/'), 'https://openwebui.example/api');
assert.equal(normalizeOpenAiCompatibleUrl('https://provider.example/custom/root'), 'https://provider.example/custom/root');
assert.equal(normalizeOpenAiCompatibleUrl('provider.example/v1'), '');
assert.equal(normalizeOpenAiCompatibleUrl('https://provider.example/v1?key=secret'), '');
assert.equal(normalizeOpenAiCompatibleUrl('https://provider.example/v1#fragment'), '');
assert.equal(normalizeOpenAiCompatibleUrl('https://user:secret@provider.example/v1'), '', 'credentials never belong in saved URLs');
});
test('compatible model resolves only with URL and model, and Auto remains unchanged', () => {
const { ASSISTANT_MODEL_CHOICES, resolveAssistantModel } = assistant().load('app/assistant/models.ts');
assert.equal(ASSISTANT_MODEL_CHOICES.includes('openai-compatible'), true);
assert.equal(resolveAssistantModel('openai-compatible', { anthropic: '', openai: '' }), null);
assert.equal(resolveAssistantModel('openai-compatible', {
anthropic: '', openai: '', openAiCompatibleUrl: 'https://provider.example/api/v1', openAiCompatibleModel: '',
}), null);
const resolved = resolveAssistantModel('openai-compatible', {
anthropic: '', openai: '',
openAiCompatibleUrl: ' https://provider.example/api/v1 ',
openAiCompatibleKey: ' secret ', openAiCompatibleModel: ' model-id ',
});
assert.equal(resolved.provider, 'openai-compatible');
assert.equal(resolved.model, 'model-id');
assert.equal(resolved.apiKey, 'secret');
assert.equal(resolved.baseUrl, 'https://provider.example/api/v1');
assert.equal(resolved.effort, undefined);
assert.equal(resolveAssistantModel('auto', {
anthropic: '', openai: '', openAiCompatibleUrl: 'https://provider.example/api/v1', openAiCompatibleModel: 'model-id',
}), null, 'Auto never selects a custom provider');
});
test('streamed tool calls execute through Faceclaw, results continue the turn, and history survives follow-up', async () => {
const h = assistant();
h.send('Set a one-minute timer');
const first = h.requests[0];
h.chunk(first, { content: 'Setting it now.' });
h.chunk(first, {
reasoning_content: 'hidden reasoning',
reasoning_details: [{ type: 'reasoning.encrypted', data: 'opaque-fixture' }],
tool_calls: [{ index: 0, id: 'call-1', type: 'function', function: { name: 'timer_set', arguments: '{"seconds":' } }],
});
h.chunk(first, { tool_calls: [{ index: 0, function: { arguments: '60}' } }] });
h.finish(first, 'tool_calls');
await flush();
assert.equal(h.calls.length, 1);
assert.equal(h.calls[0].name, 'timer.set');
assert.equal(h.calls[0].args.seconds, 60);
assert.equal(h.requests.length, 2);
const second = h.requests[1];
const assistantMessage = second.body.messages.find(m => m.role === 'assistant' && m.tool_calls);
assert.equal(assistantMessage.content, 'Setting it now.');
assert.equal(assistantMessage.tool_calls[0].id, 'call-1');
assert.equal(assistantMessage.tool_calls[0].function.name, 'timer_set');
assert.equal(assistantMessage.tool_calls[0].function.arguments, '{"seconds":60}');
assert.equal(assistantMessage.reasoning_details[0].data, 'opaque-fixture');
const toolMessage = second.body.messages.find(m => m.role === 'tool');
assert.equal(toolMessage.tool_call_id, 'call-1');
assert.equal(toolMessage.content, 'timer.set completed');
assert.equal(JSON.stringify(second.body).includes('hidden reasoning'), false);
h.chunk(second, { content: 'Your timer is running.' });
h.finish(second, 'stop', 'close');
assert.equal(h.session.isTurnActive(), false);
assert.equal(h.done.length, 1);
assert.deepEqual(h.errors, []);
assert.equal(h.deltas.includes('hidden reasoning'), false);
h.send('What did you set?');
assert.match(JSON.stringify(h.requests[2].body), /Your timer is running/);
h.session.cancel();
assert.equal(h.requests[2].cancelled, true);
});
test('reasoning_details streamed in fragments are reassembled by index before replay', async () => {
const h = assistant();
h.send('Set a one-minute timer');
const first = h.requests[0];
h.chunk(first, { reasoning_details: [
{ type: 'reasoning.text', text: 'Need a ', signature: null, format: 'anthropic-claude-v1', index: 0 },
] });
h.chunk(first, { reasoning_details: [{ type: 'reasoning.text', text: 'timer.', index: 0 }] });
h.chunk(first, { reasoning_details: [
{ type: 'reasoning.text', signature: 'sig-after-text', index: 0 },
{ type: 'reasoning.encrypted', data: 'abc', index: 1 },
] });
h.chunk(first, { reasoning_details: [{ type: 'reasoning.encrypted', data: 'def', index: 1 }] });
h.chunk(first, { tool_calls: [{ index: 0, id: 'call-1', function: { name: 'timer_set', arguments: '{"seconds":60}' } }] });
h.finish(first, 'tool_calls');
await flush();
const assistantMessage = h.requests[1].body.messages.find(m => m.role === 'assistant' && m.tool_calls);
assert.deepEqual(assistantMessage.reasoning_details, [
{ type: 'reasoning.text', text: 'Need a timer.', signature: 'sig-after-text', format: 'anthropic-claude-v1', index: 0 },
{ type: 'reasoning.encrypted', data: 'abcdef', index: 1 },
]);
h.chunk(h.requests[1], { content: 'Timer set.' });
h.finish(h.requests[1]);
assert.deepEqual(h.errors, []);
});
test('changing the API endpoint preserves visible history but drops provider-specific state', () => {
const h = assistant();
const original = h.session.config;
const config = (baseUrl) => ({ kind: 'direct', llm: {
selection: 'openai-compatible', provider: 'openai-compatible', model: 'fixture-model', apiKey: 'fixture-key', baseUrl,
} });
h.session.sendUtterance('Remember the blue door', {}, {
onTextDelta() {}, onToolActivity() {}, onError: error => assert.fail(error), onTurnDone() {},
});
const first = h.requests[0];
h.chunk(first, { reasoning_details: [{ type: 'opaque', data: 'server-one-only' }], content: 'Remembered.' });
h.finish(first);
const history = JSON.parse(JSON.stringify(h.session.history()));
assert.equal(JSON.stringify(history).includes('server-one-only'), true);
h.session.configure(config('https://provider-two.example/api/v1'));
h.send('What color?');
const second = h.requests[1];
assert.equal(second.url, 'https://provider-two.example/api/v1/chat/completions');
assert.equal(JSON.stringify(second.body).includes('server-one-only'), false);
assert.equal(JSON.stringify(second.body).includes('Remember the blue door'), true);
h.session.cancel();
const restored = new h.session.constructor(config('https://provider-two.example/api/v1'), undefined, history);
restored.sendUtterance('What color?', {}, {
onTextDelta() {}, onToolActivity() {}, onError: error => assert.fail(error), onTurnDone() {},
});
assert.equal(JSON.stringify(h.requests.at(-1).body).includes('server-one-only'), false);
restored.cancel();
});
test('multiple interleaved tool calls are reassembled independently and all results are returned', async () => {
const h = assistant();
h.send('Set a timer and show an alert');
const first = h.requests[0];
h.chunk(first, { tool_calls: [
{ index: 0, id: 'timer-id', function: { name: 'timer_set', arguments: '{"sec' } },
{ index: 1, id: 'alert-id', function: { name: 'glasses_show_alert', arguments: '{"text":' } },
] });
h.chunk(first, { tool_calls: [
{ index: 0, function: { arguments: 'onds":30}' } },
{ index: 1, function: { arguments: '"Done"}' } },
] });
h.finish(first, 'tool_calls');
await flush();
// Values produced inside the VM have a different Object prototype.
assert.equal(JSON.stringify(h.calls.map(c => [c.name, c.args])), JSON.stringify([
['timer.set', { seconds: 30 }], ['glasses.show_alert', { text: 'Done' }],
]));
const second = h.requests[1];
const tools = second.body.messages.filter(m => m.role === 'tool');
assert.equal(JSON.stringify(tools.map(m => [m.tool_call_id, m.content])), JSON.stringify([
['timer-id', 'timer.set completed'], ['alert-id', 'glasses.show_alert completed'],
]));
h.chunk(second, { content: 'Both actions completed.' });
h.finish(second);
assert.equal(h.session.isTurnActive(), false);
assert.deepEqual(h.errors, []);
});
test('content-only streams work with [DONE], while empty and in-band error streams fail clearly', () => {
const h = assistant();
h.send('Hello');
h.chunk(h.requests[0], { role: 'assistant' });
h.chunk(h.requests[0], { reasoning_content: 'not for the lenses' });
h.chunk(h.requests[0], { content: 'Hello ' });
h.chunk(h.requests[0], { content: 'there' });
h.requests[0].listener.onLine('data: [DONE]');
assert.equal(h.done[0].stopReason, 'end_turn');
assert.equal(h.session.transcript.at(-1).text, 'Hello there');
h.send('Empty'); h.finish(h.requests[1]);
assert.match(h.errors.at(-1), /without a reply/);
h.send('Failure');
h.event(h.requests[2], { error: { message: 'model overloaded' } });
assert.match(h.errors.at(-1), /model overloaded/);
});
test('HTTP errors, malformed tool JSON and premature EOF never execute incomplete tools', async () => {
const h = assistant();
h.send('Unauthorized');
h.requests[0].listener.onHttpError(401, '{"error":{"message":"bad key"}}');
assert.match(h.errors.at(-1), /invalid API key/);
assert.equal(h.session.isTurnActive(), false);
h.send('Malformed');
h.chunk(h.requests[1], { tool_calls: [{ index: 0, id: 'bad', function: { name: 'timer_set', arguments: '{broken' } }] });
h.finish(h.requests[1], 'tool_calls');
await flush();
assert.match(h.errors.at(-1), /malformed tool arguments/);
assert.equal(h.calls.length, 0);
h.send('Truncated');
h.chunk(h.requests[2], { tool_calls: [{ index: 0, id: 'partial', function: { name: 'timer_set', arguments: '{"seconds":60}' } }] });
h.requests[2].listener.onComplete();
await flush();
assert.match(h.errors.at(-1), /before the response completed/);
assert.equal(h.calls.length, 0);
h.send('Length limited');
h.chunk(h.requests[3], { tool_calls: [{ index: 0, id: 'partial', function: { name: 'timer_set', arguments: '{"seconds":60}' } }] });
h.finish(h.requests[3], 'length');
await flush();
assert.match(h.errors.at(-1), /before its tool calls completed/);
assert.equal(h.calls.length, 0);
});
test('cancellation suppresses late deltas and tool side effects', async () => {
const h = assistant();
h.send('Cancel this');
const request = h.requests[0];
h.session.cancel();
h.chunk(request, { content: 'late text', tool_calls: [{ index: 0, id: 'late', function: { name: 'timer_set', arguments: '{}' } }] });
h.finish(request, 'tool_calls');
await flush();
assert.equal(h.calls.length, 0);
assert.equal(h.deltas.length, 0);
assert.equal(h.done.length, 0);
assert.equal(request.cancelled, true);
});
@@ -0,0 +1,160 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function discoveryEnv(responder) {
const requests = [];
const load = loader({}, {
'../util/http': {
fetchTextWithUserAgent: async (url, init) => {
requests.push({ url, init });
return responder(url, init, requests.length);
},
},
'./sse': {},
});
const api = load('app/native/openai-chat-models.ts');
api.clearCompatibleModelDiscoveryCache();
return { api, requests };
}
const response = (body, status = 200) => ({ status, text: async () => typeof body === 'string' ? body : JSON.stringify(body) });
const veniceModel = (id, name, tools, extra = {}) => ({
id,
model_spec: {
name,
capabilities: { supportsFunctionCalling: tools },
...extra,
},
});
const options = {
baseUrl: 'https://api.venice.ai/api/v1',
apiKey: 'venice-key',
};
test('Venice discovery uses its exact API root, auth, timeout, no redirects, and capability metadata', async () => {
const env = discoveryEnv(async () => response({ data: [
veniceModel('chat-only', 'Chat only', false),
veniceModel('offline-tools', 'Offline tools', true, { offline: true }),
veniceModel('tools-model', 'Tools model', true, { betaModel: true }),
{ id: 'generic-model', name: 'Generic model' },
veniceModel('old-model', 'Old model', true, { deprecation: { date: '2027-01-01' } }),
] }));
const models = await env.api.discoverOpenAiCompatibleModels(options);
assert.equal(env.requests.length, 1);
assert.equal(env.requests[0].url, 'https://api.venice.ai/api/v1/models');
assert.equal(env.requests[0].init.method, 'GET');
assert.equal(env.requests[0].init.headers.Authorization, 'Bearer venice-key');
assert.equal(env.requests[0].init.timeoutMs, 10_000);
assert.equal(env.requests[0].init.dontFollowRedirects, true);
assert.equal(models.map(model => model.id).join(','), 'old-model,tools-model,generic-model,chat-only,offline-tools');
const tools = models.find(model => model.id === 'tools-model');
assert.equal(tools.name, 'Tools model');
assert.equal(tools.supportsTools, true);
assert.equal(tools.beta, true);
assert.equal(models.find(model => model.id === 'old-model').deprecated, true);
assert.equal(models.find(model => model.id === 'generic-model').supportsTools, null);
assert.equal(models.find(model => model.id === 'chat-only').supportsTools, false);
assert.equal(models.find(model => model.id === 'offline-tools').offline, true);
});
test('Open WebUI discovery uses /api/models, supports optional auth, and marks tool support unknown', async () => {
const env = discoveryEnv(async () => response({ data: [
{ id: 'qwen-local', name: 'Qwen local' },
{ id: 'second-model' },
] }));
const models = await env.api.discoverOpenAiCompatibleModels({
baseUrl: 'http://100.64.0.1:3000/api', apiKey: '',
});
assert.equal(env.requests[0].url, 'http://100.64.0.1:3000/api/models');
assert.equal('Authorization' in env.requests[0].init.headers, false);
assert.equal(JSON.stringify(models.map(model => [model.id, model.supportsTools])), JSON.stringify([
['qwen-local', null], ['second-model', null],
]));
});
test('successful catalogs are cached briefly per endpoint and credential; refresh bypasses the cache', async () => {
let now = 1000;
const env = discoveryEnv(async (_url, _init, count) => response({ data: [{ id: `model-${count}` }] }));
const deps = { now: () => now };
const first = await env.api.discoverOpenAiCompatibleModels(options, deps);
const cached = await env.api.discoverOpenAiCompatibleModels(options, deps);
assert.equal(first, cached);
assert.equal(env.requests.length, 1);
await env.api.discoverOpenAiCompatibleModels({ ...options, apiKey: 'other-key' }, deps);
assert.equal(env.requests.length, 2, 'a different credential cannot reuse the old response');
await env.api.discoverOpenAiCompatibleModels({ ...options, baseUrl: 'https://other.example/api' }, deps);
assert.equal(env.requests.length, 3, 'a different endpoint has its own catalog');
await env.api.discoverOpenAiCompatibleModels({ ...options, forceRefresh: true }, deps);
assert.equal(env.requests.length, 4, 'explicit refresh always fetches');
now += 61_000;
await env.api.discoverOpenAiCompatibleModels(options, deps);
assert.equal(env.requests.length, 5, 'the short-lived entry expires');
});
test('invalid URLs and unsupported or malformed responses fail without querying alternate endpoints', async () => {
const env = discoveryEnv(async (_url, _init, count) => {
if (count === 1) return response('<html>login</html>');
if (count === 2) return response({ models: [] });
return response({ data: [] });
});
await assert.rejects(env.api.discoverOpenAiCompatibleModels({ baseUrl: 'not-a-url', apiKey: '' }), /valid compatible API URL/);
assert.equal(env.requests.length, 0);
await assert.rejects(env.api.discoverOpenAiCompatibleModels(options), /invalid model list/);
await assert.rejects(env.api.discoverOpenAiCompatibleModels({ ...options, forceRefresh: true }), /did not return an OpenAI-compatible model list/);
assert.deepEqual(env.requests.map(request => request.url), [
'https://api.venice.ai/api/v1/models', 'https://api.venice.ai/api/v1/models',
]);
});
test('HTTP and reachability errors are actionable and never fall back to inference', async () => {
const statuses = [401, 403, 404, 429, 503, 302];
for (const status of statuses) {
const env = discoveryEnv(async () => response({}, status));
await assert.rejects(env.api.discoverOpenAiCompatibleModels(options), error => {
assert.match(error.message, new RegExp(String(status)));
return true;
});
assert.equal(env.requests.length, 1);
assert.equal(env.requests[0].init.method, 'GET');
}
const unreachable = discoveryEnv(async () => { throw new Error('socket details'); });
await assert.rejects(unreachable.api.discoverOpenAiCompatibleModels(options), /Could not reach the configured server/);
});
test('model parsing skips invalid rows, deduplicates IDs, and caps catalog size', () => {
const env = discoveryEnv(async () => response({ data: [] }));
const rows = [null, {}, { id: '' }, { id: 'same', name: 'First' }, { id: 'same', name: 'Duplicate' }];
for (let index = 0; index < 510; index++) rows.push({ id: `model-${String(index).padStart(3, '0')}` });
const parsed = env.api.parseDiscoveredCompatibleModels(rows);
assert.equal(parsed.length, 500);
assert.equal(parsed.filter(model => model.id === 'same').length, 1);
assert.equal(parsed.find(model => model.id === 'same').name, 'First');
});
test('native text HTTP forwards discovery timeout and redirect protections with the app User-Agent', async () => {
const requests = [];
const load = loader({ global: { isAndroid: false, isIOS: true } }, {
'../version': { USER_AGENT: 'Faceclaw/test' },
'@nativescript/core/http': {
request: async init => {
requests.push(init);
return { statusCode: 200, content: { toString: () => '{"data":[]}' } };
},
},
});
const { fetchTextWithUserAgent } = load('app/util/http.ts');
const result = await fetchTextWithUserAgent('https://provider.example/api/models', {
headers: { Authorization: 'Bearer secret' },
timeoutMs: 10_000,
dontFollowRedirects: true,
});
assert.equal(requests[0].timeout, 10_000);
assert.equal(requests[0].dontFollowRedirects, true);
assert.equal(requests[0].headers.Authorization, 'Bearer secret');
assert.equal(requests[0].headers['User-Agent'], 'Faceclaw/test');
assert.equal(JSON.stringify(await result.json()), JSON.stringify({ data: [] }));
});
@@ -0,0 +1,301 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const flush = () => new Promise(resolve => setImmediate(resolve));
class FakeMenuLayer {
constructor(title, items, layout) {
this.title = title;
this.layout = layout;
this.setItems(items);
}
setItems(items, selectedIndex = 0) {
this.items = items;
this.selectedIndex = items.length
? Math.max(0, Math.min(selectedIndex ?? 0, items.length - 1))
: null;
}
get selectedItem() {
return this.selectedIndex === null ? null : this.items[this.selectedIndex];
}
}
class FakeEditTextSettingLayer {
constructor(setting) { this.setting = setting; }
}
function deferred() {
let resolve, reject;
const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
return { promise, resolve, reject };
}
function model(id, extra = {}) {
return {
id,
name: extra.name ?? id,
supportsTools: extra.supportsTools ?? null,
offline: extra.offline ?? false,
deprecated: extra.deprecated ?? false,
beta: extra.beta ?? false,
};
}
function pickerEnv({ models = [], saved = '', discover } = {}) {
const calls = [];
const settings = {
baseUrl: 'https://provider.example/api/v1',
apiKey: 'secret',
model: saved,
getBaseUrl() { return this.baseUrl; },
getApiKey() { return this.apiKey; },
getModel() { return this.model; },
setModel(value) { this.model = value; },
};
const modelSetting = { id: 'compatible-model' };
const renders = [];
const edits = [];
const layers = [{ id: 'settings-base' }];
const remove = layer => layer.onRemoved?.();
const stack = {
getBaseSize: () => ({ width: 320, height: 240 }),
push(layer) { layers.push(layer); },
topMatches(predicate) { return layers.length > 1 && predicate(layers.at(-1)); },
pop() { if (layers.length > 1) remove(layers.pop()); },
popThrough(layer) {
const index = layers.indexOf(layer);
if (index <= 0) return false;
for (const removed of layers.splice(index).reverse()) remove(removed);
return true;
},
};
const ctx = {
stack,
actions: {
requestRender: () => renders.push('render'),
startTextSettingEdit: setting => { edits.push(setting); },
},
};
const load = loader({}, {
'./menu': { MenuLayer: FakeMenuLayer },
'./dashboard-settings': { EditTextSettingLayer: FakeEditTextSettingLayer },
'../native/openai-chat-models': {},
});
const api = load('app/ui/openai-compatible-model-picker.ts');
const discoverImpl = discover ?? (async options => {
calls.push(options);
return models;
});
const picker = new api.OpenAiCompatibleModelPicker(
settings, modelSetting, discoverImpl, stack.getBaseSize(),
);
return {
api, picker, settings, modelSetting, discoverImpl,
calls, renders, edits, layers, ctx,
};
}
function row(picker, text) {
const found = picker.items.find(item => item.label.includes(text));
assert.ok(found, `Missing row ${text}; got: ${picker.items.map(item => item.label).join(' | ')}`);
return found;
}
test('catalog shows capabilities, hides offline models, and saves only an explicit selection', async () => {
const env = pickerEnv({
saved: 'generic-model',
models: [
model('tools-model', { name: 'Tools Model', supportsTools: true }),
model('generic-model', { name: 'Generic Model' }),
model('offline-model', { supportsTools: true, offline: true }),
],
});
env.picker.open(env.ctx);
assert.equal(row(env.picker, 'Discovering').disabled, true);
await flush();
assert.equal(env.settings.model, 'generic-model');
assert.equal(env.calls.length, 1);
assert.equal(env.calls[0].baseUrl, 'https://provider.example/api/v1');
assert.equal(env.calls[0].apiKey, 'secret');
assert.equal(env.calls[0].forceRefresh, false);
assert.equal(env.picker.items.some(item => item.label.includes('offline-model')), false);
assert.match(row(env.picker, 'Tools Model').label, /\[tools\]/);
assert.match(row(env.picker, 'Generic Model').label, /\[tools\?\]/);
assert.ok(row(env.picker, 'Generic Model').label.endsWith(' *'));
assert.equal(env.picker.selectedItem.label, row(env.picker, 'Generic Model').label);
assert.equal(env.picker.layout.width, 280);
assert.equal(env.picker.layout.maxHeight, 200);
await row(env.picker, 'Tools Model').onSelect(env.ctx, env.picker);
assert.equal(env.settings.model, 'tools-model');
assert.equal(env.layers.length, 1);
});
test('refresh bypasses cache and preserves an unlisted saved model without auto-selecting', async () => {
const env = pickerEnv({
saved: 'keep-model',
discover: async options => {
env.calls.push(options);
return [model(env.calls.length === 1 ? 'first-model' : 'second-model')];
},
});
env.picker.open(env.ctx);
await flush();
assert.match(row(env.picker, 'keep-model').label, /saved only/);
assert.equal(env.picker.selectedItem.label, row(env.picker, 'keep-model').label);
await row(env.picker, 'Refresh models').onSelect(env.ctx, env.picker);
await flush();
assert.equal(env.calls.length, 2);
assert.equal(env.calls[1].forceRefresh, true);
assert.equal(env.settings.model, 'keep-model');
assert.ok(row(env.picker, 'second-model'));
assert.ok(row(env.picker, 'keep-model'));
});
test('cancel invalidates a pending result and manual entry remains available while loading', async () => {
const pending = deferred();
const env = pickerEnv({
saved: 'original-model',
discover: options => { env.calls.push(options); return pending.promise; },
});
env.picker.open(env.ctx);
await row(env.picker, 'Cancel discovery').onSelect(env.ctx, env.picker);
assert.equal(env.layers.length, 1);
const renderCount = env.renders.length;
pending.resolve([model('late-model')]);
await flush();
assert.equal(env.settings.model, 'original-model');
assert.equal(env.renders.length, renderCount);
const second = deferred();
const manual = pickerEnv({
saved: 'original-model',
discover: () => second.promise,
});
manual.picker.open(manual.ctx);
await row(manual.picker, 'Enter model manually').onSelect(manual.ctx, manual.picker);
assert.equal(manual.edits.length, 1);
assert.equal(manual.edits[0], manual.modelSetting);
assert.equal(manual.layers.length, 2);
assert.ok(manual.layers[1] instanceof FakeEditTextSettingLayer);
second.resolve([model('late-model')]);
await flush();
assert.equal(manual.settings.model, 'original-model');
assert.ok(manual.layers[1] instanceof FakeEditTextSettingLayer);
});
test('offline models are optional but a saved offline model remains selectable', async () => {
const env = pickerEnv({
saved: 'saved-offline',
models: [
model('online-model'),
model('saved-offline', { offline: true }),
model('other-offline', { offline: true }),
],
});
env.picker.open(env.ctx);
await flush();
assert.ok(row(env.picker, 'saved-offline'));
assert.equal(env.picker.items.some(item => item.label.includes('other-offline')), false);
await row(env.picker, 'Show offline (2)').onSelect(env.ctx, env.picker);
assert.ok(row(env.picker, 'other-offline'));
assert.match(row(env.picker, 'other-offline').label, /offline/);
await row(env.picker, 'Hide offline models').onSelect(env.ctx, env.picker);
assert.equal(env.picker.items.some(item => item.label.includes('other-offline')), false);
assert.equal(env.settings.model, 'saved-offline');
});
test('errors retain the saved model and Try again reloads the current endpoint', async () => {
const first = deferred();
const env = pickerEnv({
saved: 'original-model',
discover: options => {
env.calls.push(options);
if (env.calls.length === 1) return first.promise;
return Promise.resolve([model('new-endpoint-model')]);
},
});
env.picker.open(env.ctx);
env.settings.baseUrl = 'https://other.example/api';
first.resolve([model('old-endpoint-model')]);
await flush();
assert.match(row(env.picker, 'Failed:').description, /URL or key changed/);
assert.equal(env.picker.items.some(item => item.label.includes('old-endpoint-model')), false);
assert.equal(env.settings.model, 'original-model');
await row(env.picker, 'Try again').onSelect(env.ctx, env.picker);
await flush();
assert.equal(env.calls[1].baseUrl, 'https://other.example/api');
assert.equal(env.calls[1].forceRefresh, true);
assert.ok(row(env.picker, 'new-endpoint-model'));
assert.equal(env.settings.model, 'original-model');
});
test('a stale catalog cannot be selected after the endpoint changes', async () => {
const env = pickerEnv({ models: [model('old-endpoint-model')] });
env.picker.open(env.ctx);
await flush();
const staleRow = row(env.picker, 'old-endpoint-model');
env.settings.baseUrl = 'https://other.example/api';
await staleRow.onSelect(env.ctx, env.picker);
assert.equal(env.settings.model, '');
assert.equal(env.layers.length, 2);
});
test('empty catalogs retain recovery actions and settings row requires a URL', async () => {
const env = pickerEnv({ saved: 'keep-model', models: [] });
env.picker.open(env.ctx);
await flush();
assert.ok(row(env.picker, 'No models listed'));
assert.ok(row(env.picker, 'Refresh models'));
assert.ok(row(env.picker, 'Enter model manually'));
env.ctx.stack.pop();
const item = env.api.compatibleModelDiscoveryMenuItem(
env.settings, env.modelSetting, async () => [],
);
env.settings.baseUrl = '';
assert.equal(item.disabled(), true);
env.settings.baseUrl = 'https://provider.example/api/v1';
assert.equal(item.disabled(), false);
item.onSelect(env.ctx);
assert.equal(env.layers.length, 2);
assert.equal(env.layers[1].layout.width, 280);
env.layers[1].onRemoved();
});
test('a temporary overlay does not invalidate a pending result while the picker remains open', async () => {
const pending = deferred();
const env = pickerEnv({ discover: () => pending.promise });
env.picker.open(env.ctx);
env.layers.push({ id: 'temporary-overlay' });
pending.resolve([model('available-model')]);
await flush();
assert.equal(env.picker.items.some(item => item.label.includes('available-model')), true);
assert.equal(env.layers.length, 3);
assert.ok(env.renders.length > 0);
});
test('a discovery error is visible in the modal row and retains manual recovery', async () => {
const env = pickerEnv({
discover: async () => { throw new Error('Model discovery is not supported at this URL (HTTP 404)'); },
});
env.picker.open(env.ctx);
await flush();
assert.match(row(env.picker, 'Failed:').label, /404/);
assert.match(row(env.picker, 'Failed:').description, /Manual model entry/);
assert.ok(row(env.picker, 'Try again'));
assert.ok(row(env.picker, 'Enter model manually'));
env.ctx.stack.pop();
});
test('long labels stay bounded while descriptions retain the exact model ID', async () => {
const longId = `provider/${'x'.repeat(90)}-model`;
const longName = `A very long model name ${'y'.repeat(70)}`;
const env = pickerEnv({
models: [model(longId, { name: longName, supportsTools: true })],
});
env.picker.open(env.ctx);
await flush();
const longRow = row(env.picker, '[tools]');
assert.ok(longRow.label.length <= 54);
assert.ok(longRow.description.includes(longId));
});
@@ -0,0 +1,86 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function sharedEnv() {
const compatible = [], unexpected = [];
const localLlm = {
streamLocalModel: () => unexpected.push('local'),
isLocalModelReady: () => false,
isLocalModelSupported: () => true,
LOCAL_MODEL: { id: 'qwen', label: 'Qwen' },
};
const settings = {
anthropicApiKeySetting: { get: () => '' },
assistantModelSetting: { get: () => 'openai-compatible' },
assistantOpenAiCompatibleUrlSetting: { get: () => 'https://provider.example/api/v1' },
assistantOpenAiCompatibleKeySetting: { get: () => 'compatible-key' },
assistantOpenAiCompatibleModelSetting: { get: () => 'compatible-model' },
openAiApiKeySetting: { get: () => '' },
};
const fail = provider => () => { unexpected.push(provider); };
const load = loader({ global: { isIOS: false, isAndroid: true } }, {
'../../native/anthropic': { streamAnthropicMessage: fail('anthropic') },
'../../native/openai': { streamOpenAiResponse: fail('openai') },
'../../native/local-llm': localLlm,
'../native/local-llm': localLlm,
'../../native/openai-chat': {
streamOpenAiCompatibleChat: options => {
compatible.push(options);
return { cancel() {} };
},
},
'../../ui/dashboard-settings': settings,
'../../prompts': {
CALCULATOR_REWRITE_SYSTEM_PROMPT: 'calculator system',
buildCalculatorRewriteUserMessage: spoken => `rewrite: ${spoken}`,
TRANSLATE_SYSTEM_PROMPT: 'translate system',
buildTranslateUserMessage: speech => `translate: ${speech}`,
},
});
return { load, compatible, unexpected };
}
test('Calculator uses the configured compatible provider and preserves its small output cap', async () => {
const env = sharedEnv();
const { rewriteSpokenProblem } = env.load('app/apps/calculator/calculator-llm.ts');
const result = rewriteSpokenProblem('one plus one');
assert.equal(env.compatible.length, 1);
assert.deepEqual(env.unexpected, []);
const options = env.compatible[0];
assert.equal(options.baseUrl, 'https://provider.example/api/v1');
assert.equal(options.apiKey, 'compatible-key');
assert.equal(options.model, 'compatible-model');
assert.equal(options.maxTokens, 200);
assert.equal(options.messages[0].content, 'rewrite: one plus one');
options.onDone({ text: '1 + 1', content: [], stopReason: 'end_turn' });
assert.equal(await result, '1 + 1');
});
test('Translate resolves and streams through the compatible provider without changing its cancellation contract', () => {
const env = sharedEnv();
const { resolveTranslationModel, streamTranslation } = env.load('app/apps/translate/translate-llm.ts');
const model = resolveTranslationModel();
assert.equal(model.provider, 'openai-compatible');
assert.equal(model.baseUrl, 'https://provider.example/api/v1');
const progress = [], answers = [], errors = [];
const handle = streamTranslation(model, {
speech: 'Hola', context: [], language: 'Spanish',
onProgress: text => progress.push(text),
onDone: text => answers.push(text),
onError: error => errors.push(error),
});
assert.equal(typeof handle.cancel, 'function');
assert.equal(env.compatible.length, 1);
assert.deepEqual(env.unexpected, []);
const options = env.compatible[0];
assert.equal(options.baseUrl, 'https://provider.example/api/v1');
assert.equal(options.model, 'compatible-model');
assert.equal(options.maxTokens, 1024);
assert.equal(options.messages[0].content, 'translate: Hola');
options.onTextDelta('Hello', '<english>Hello</english>');
options.onDone({ text: '<english>Hello</english>', content: [], stopReason: 'end_turn' });
assert.deepEqual(progress, ['Hello']);
assert.deepEqual(answers, ['Hello']);
assert.deepEqual(errors, []);
});
+48
View File
@@ -0,0 +1,48 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
// The resource monitor's platform samplers, over stand-ins for the iOS
// FaceclawResourceUsage JSON and the Android FaceclawResourceUsage LongArray.
function ios(raw) {
const load = loader({ FaceclawResourceUsage: { sample: () => JSON.stringify(raw) } });
return load('app/native/resource-usage.ios.ts');
}
function android(raw) {
const load = loader({ com: { faceclaw: { app: { FaceclawResourceUsage: { sample: () => raw } } } } });
return load('app/native/resource-usage.ts');
}
test('iOS charts footprint, resident and malloc, and reports the jetsam limit, peak and thermal state', () => {
const usage = ios({
atMs: 5000, cpuMs: 1234.5, footprintKb: 200 * 1024, footprintPeakKb: 300 * 1024, residentKb: 150 * 1024,
mallocKb: 64 * 1024, availableKb: 2848 * 1024, threads: 23, thermalState: 2, lowPower: true,
});
assert.deepEqual(Array.from(usage.MEMORY_SERIES), ['footprint', 'resident', 'malloc']);
const sample = usage.sampleResourceUsage();
assert.equal(sample.atMs, 5000);
assert.equal(sample.cpuMs, 1234.5);
assert.equal(sample.threads, 23);
assert.deepEqual(Array.from(sample.memoryMb), [200, 150, 64]);
assert.equal(sample.status, 'limit 3.0G · peak 300M · thermal serious · Low Power Mode');
});
test('iOS omits the limit without jetsam headroom, and fields the native query could not fill', () => {
const sample = ios({ atMs: 1, footprintKb: 1024, thermalState: 0, lowPower: false }).sampleResourceUsage();
assert.equal(sample.cpuMs, 0);
assert.equal(sample.threads, 0);
assert.deepEqual(Array.from(sample.memoryMb), [1, 0, 0]);
assert.equal(sample.status, 'thermal nominal');
});
test('Android charts RSS, native and Java heap, and reports peak RSS and committed Java heap', () => {
const usage = android([9000, 4321, 400 * 1024, 48 * 1024, 12 * 1024, 24 * 1024, 87, 512 * 1024]);
assert.deepEqual(Array.from(usage.MEMORY_SERIES), ['RSS', 'native', 'Java']);
const sample = usage.sampleResourceUsage();
assert.equal(sample.atMs, 9000);
assert.equal(sample.cpuMs, 4321);
assert.equal(sample.threads, 87);
assert.deepEqual(Array.from(sample.memoryMb), [400, 48, 12]);
assert.equal(sample.status, 'peak RSS 512M · Java heap 24M committed');
});
+162
View File
@@ -0,0 +1,162 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
/** FaceclawSpeechAnalyzer.swift's surface; `emit` plays its JSON events. */
class FakeAnalyzer {
static supported = true;
static instances = [];
static new() { const native = new FakeAnalyzer(); FakeAnalyzer.instances.push(native); return native; }
static isSupported() { return FakeAnalyzer.supported; }
constructor() { this.pcm = []; this.finished = 0; this.cancelled = 0; this.language = null; }
startWithLanguage(language) { this.language = language; }
acceptPcm(data) { this.pcm.push([...data]); }
finish() { this.finished++; }
cancel() { this.cancelled++; }
emit(event) { this.eventHandler(JSON.stringify(event)); }
}
function load() {
FakeAnalyzer.instances = [];
FakeAnalyzer.supported = true;
const timers = new Set();
const modules = loader({
FaceclawSpeechAnalyzer: FakeAnalyzer,
NSData: { dataWithBytesLength: (buffer, length) => new Uint8Array(buffer, 0, length) },
interop: { handleof: buffer => buffer },
setTimeout(fn) { timers.add(fn); return fn; },
clearTimeout(fn) { timers.delete(fn); },
}, {});
return { ...modules('app/native/speech-analyzer-stt.ios.ts'), timers };
}
function client(language = '') {
const { SpeechAnalyzerSttClient } = load();
const events = [], statuses = [], errors = [];
let ready = 0;
const c = new SpeechAnalyzerSttClient({
apiKey: '', language, onTranscript: event => events.push({ ...event }), onStatus: status => statuses.push(status),
onError: error => errors.push(error), onReady: () => ready++,
});
c.start();
const native = FakeAnalyzer.instances[0];
return { c, native, events, statuses, errors, ready: () => ready };
}
/** `seconds` of 16 kHz PCM16LE silence. */
const pcm = seconds => new Uint8Array(Math.round(seconds * 16000) * 2);
/** A result as FaceclawSpeechAnalyzer.swift reports it: its audio range and when its last word ends. */
const result = (text, final, start, end, speechEnd = end) => ({ kind: 'transcript', text, final, start, end, speechEnd });
const finals = h => h.events.filter(event => event.isFinal);
test('starts in the requested language and reports readiness as a provider would', () => {
const h = client('es');
assert.equal(h.native.language, 'es');
h.native.emit({ kind: 'status', message: 'Downloading on-device speech model… 40%' });
h.native.emit({ kind: 'ready', locale: 'es_ES' });
assert.deepEqual(h.statuses, ['Downloading on-device speech model… 40%', 'Listening (on-device)...']);
assert.equal(h.ready(), 1);
});
// Recorded from SpeechAnalyzer on macOS 26.4 (scripted speech, 1.5 s quiet, a
// pause, more speech): volatile ranges start where the last final ended, and
// the second final's first word reaches back into the silence (5.04 s; the
// speech starts at 6.18 s).
test('real model output: the first paragraph ends as soon as its last words are final', () => {
const h = client();
h.native.emit({ kind: 'ready', locale: 'en_US' });
h.native.emit(result('Hello there.', false, 0, 4.1));
h.native.emit(result('Hello there.', true, 0, 0.66, 0.6));
h.native.emit(result(' This is the firstst paragraph of the test spoken ', false, 0.66, 4.1815));
h.c.acceptPcm(pcm(5.6815));
h.c.commitSegment();
assert.equal(finals(h).length, 0, 'words before the pause are still provisional');
h.native.emit(result(' This is the firstst paragraph of the test spoken before a pause.', false, 0.66, 7.7815));
h.native.emit(result(' This is the first paragraph of the test, spoken before a pause.', true, 0.66, 4.38, 4.32));
assert.deepEqual(finals(h), [{ text: 'Hello there. This is the first paragraph of the test, spoken before a pause.', isFinal: true, paragraphBreakAfter: true }]);
h.native.emit(result(' And this is a second', false, 4.38, 8.1815));
assert.deepEqual(h.events.at(-1), { text: 'And this is a second', isFinal: false }, 'new speech shows in the new paragraph');
h.native.emit(result(' And this is a second paragraph, which comes after the pause.', true, 5.04, 9.18, 9.12));
h.c.finish();
assert.equal(h.native.finished, 1);
h.native.emit({ kind: 'finished' });
assert.deepEqual(h.events.at(-1), { text: 'And this is a second paragraph, which comes after the pause.', isFinal: true });
assert.equal(finals(h).length, 2);
assert.equal(h.native.cancelled, 1, 'the finished session is released');
h.native.emit(result('late', false, 9.2, 9.5));
assert.equal(h.events.at(-1).text, 'And this is a second paragraph, which comes after the pause.', 'events after stop are ignored');
});
test('a pause after words already final ends the paragraph at once, keeping later provisional text', () => {
const h = client();
h.native.emit({ kind: 'ready', locale: 'en_US' });
h.native.emit(result('Hello there.', true, 0, 1.0));
h.native.emit(result('How are you?', true, 1.0, 1.8));
h.c.acceptPcm(pcm(3.4));
h.native.emit(result('Fine', false, 1.8, 3.4));
h.c.commitSegment();
assert.deepEqual(h.events.slice(-2), [
{ text: 'Hello there. How are you?', isFinal: true, paragraphBreakAfter: true },
{ text: 'Fine', isFinal: false },
]);
});
test('a final ending mid-sentence before the pause waits for the rest of the sentence', () => {
const h = client();
h.native.emit({ kind: 'ready', locale: 'en_US' });
h.c.acceptPcm(pcm(5.7));
h.c.commitSegment();
h.native.emit(result('This is the first paragraph, spoken', true, 0.5, 3.6));
assert.equal(finals(h).length, 0);
h.native.emit(result('before a pause.', true, 3.6, 4.3));
assert.deepEqual(finals(h), [{ text: 'This is the first paragraph, spoken before a pause.', isFinal: true, paragraphBreakAfter: true }]);
});
test('without a final near the pause, a final ending after it starts the next paragraph', () => {
const h = client();
h.native.emit({ kind: 'ready', locale: 'en_US' });
h.native.emit(result('Before.', true, 0, 2.0));
// Background noise kept the pause detector from firing until well after the words.
h.c.acceptPcm(pcm(8));
h.c.commitSegment();
assert.equal(finals(h).length, 0);
h.native.emit(result('After.', true, 2.0, 9.5));
assert.deepEqual(finals(h), [{ text: 'Before.', isFinal: true, paragraphBreakAfter: true }]);
assert.deepEqual(h.events.at(-1), { text: 'After.', isFinal: false });
});
test('a pause with nothing before it adds no break', () => {
const h = client();
h.native.emit({ kind: 'ready', locale: 'en_US' });
h.c.acceptPcm(pcm(3));
h.c.commitSegment();
h.native.emit(result('First words.', true, 0, 4.2));
h.native.emit(result('More.', true, 4.2, 5.0));
assert.equal(finals(h).length, 0);
assert.deepEqual(h.events.at(-1), { text: 'First words. More.', isFinal: false });
});
test('errors end the session and reach the provider\'s error path', () => {
const h = client();
h.native.emit({ kind: 'error', message: "On-device transcription doesn't support xx." });
assert.deepEqual(h.errors, ["On-device transcription doesn't support xx."]);
assert.equal(h.native.cancelled, 1);
});
test('the wrapped client holds audio until the model is ready, then streams it', () => {
const { createSpeechAnalyzerClient, speechAnalyzerSupported } = load();
assert.equal(speechAnalyzerSupported(), true);
FakeAnalyzer.supported = false;
assert.equal(speechAnalyzerSupported(), false);
const events = [];
const wrapped = createSpeechAnalyzerClient({ onTranscript: event => events.push({ ...event }), onStatus() {}, onError() {} }, () => true);
wrapped.start();
const native = FakeAnalyzer.instances[0];
wrapped.acceptPcm(new Uint8Array([1, 0, 2, 0]));
assert.equal(native.pcm.length, 0, 'buffered while the model loads');
native.emit({ kind: 'ready', locale: 'en_US' });
wrapped.acceptPcm(new Uint8Array([3, 0]));
assert.deepEqual(native.pcm, [[1, 0, 2, 0], [3, 0]]);
wrapped.stop();
assert.equal(native.cancelled, 1);
});
+35
View File
@@ -0,0 +1,35 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { wrapText } = require('../.test-build/app/graphics/textwrap.js');
// Monospace: every character is one unit wide, so widths count characters.
const mono = {
measureText: (text) => [...text].length,
getGlyph: () => ({ dwidthX: 1 }),
};
test('breakLongWords keeps words that fit on a line whole', () => {
const text = 'Spotify plays on whichever of your devices is open';
const expected = ['Spotify plays on', 'whichever of your', 'devices is open'];
assert.deepEqual(wrapText(mono, text, 18), expected);
assert.deepEqual(wrapText(mono, text, 18, { breakLongWords: true }), expected);
});
test('breakLongWords splits a word wider than a line, starting it on the current line', () => {
assert.deepEqual(
wrapText(mono, 'see abcdefghijklmnop end', 8, { breakLongWords: true }),
['see abcd', 'efghijkl', 'mnop end'],
);
});
test('a word wider than a line is split when nothing else fits', () => {
assert.deepEqual(wrapText(mono, 'abcdefghij', 4), ['abcd', 'efgh', 'ij']);
assert.deepEqual(wrapText(mono, 'abcdefghij', 4, { breakLongWords: true }), ['abcd', 'efgh', 'ij']);
});
test('wrapping still breaks after slashes and hyphens', () => {
assert.deepEqual(
wrapText(mono, 'URI: http://127.0.0.1:43823/spotify-callback', 20, { breakLongWords: true }),
['URI: http://', '127.0.0.1:43823/', 'spotify-callback'],
);
});
+109 -23
View File
@@ -32,14 +32,13 @@ function settingsHarness({ main = 'onboard', setUp = [] } = {}) {
})('app/apps/translate/translate-settings.ts');
}
test('finals become segments; finals arriving mid-translation merge into one pending segment', () => {
test('finals translate in order; finals arriving mid-request merge into one waiting block', () => {
const model = new TranslationModel();
model.accept({ text: 'Hola, ¿qué', isFinal: false });
assert.equal(model.live, 'Hola, ¿qué');
model.accept({ text: 'Hola, ¿qué tal?', isFinal: true });
assert.equal(model.live, '');
assert.equal(model.segments.length, 1);
const first = model.nextJob();
assert.equal(first.segment.source, 'Hola, ¿qué tal?');
assert.equal(first.source, 'Hola, ¿qué tal?');
assert.deepEqual(plain(first.context), []);
assert.equal(model.nextJob(), null, 'one request at a time');
@@ -49,30 +48,91 @@ test('finals become segments; finals arriving mid-translation merge into one pen
assert.equal(model.segments.length, 2);
assert.equal(model.segments[1].source, 'Muy bien. Gracias.');
model.progress(first.segment, 'Hi, how');
assert.equal(first.segment.translation, 'Hi, how');
model.complete(first.segment, 'Hi, how are you?');
assert.equal(model.progress(first, 'Hi, how'), true);
assert.equal(first.segment.streaming, 'Hi, how');
model.complete(first, 'Hi, how are you?');
const second = model.nextJob();
assert.equal(second.segment.source, 'Muy bien. Gracias.');
assert.equal(second.source, 'Muy bien. Gracias.');
assert.deepEqual(plain(second.context), [{ source: 'Hola, ¿qué tal?', translation: 'Hi, how are you?' }]);
model.complete(second.segment, 'Very well. Thanks.');
model.complete(second, 'Very well. Thanks.');
assert.equal(model.nextJob(), null);
});
test('the live block is translated provisionally, redone as it grows, and settles on a matching final', () => {
const model = new TranslationModel();
model.accept({ text: '今日は', isFinal: false });
const first = model.nextJob();
assert.equal(first.source, '今日は');
model.accept({ text: '今日はいい天気', isFinal: false }); // grows while the request is in flight
assert.equal(model.nextJob(), null);
model.complete(first, 'Today');
assert.deepEqual(plain(model.lines(measure, 100, false)), [{ text: 'Today', gray: 170 }]);
const second = model.nextJob();
assert.equal(second.source, '今日はいい天気');
// The redo streams over the old text only once it is as long.
model.progress(second, 'To');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['Today']);
model.progress(second, 'The weather');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['The weather']);
model.complete(second, 'The weather is nice today');
// A final matching what was translated needs no further request.
model.accept({ text: '今日はいい天気', isFinal: true });
assert.equal(model.nextJob(), null);
assert.deepEqual(plain(model.lines(measure, 100, false)), [{ text: 'The weather is nice today', gray: 230 }]);
});
test('a final that differs from the request in flight makes it moot; a matching one keeps it', () => {
const model = new TranslationModel();
model.accept({ text: 'Bonjour', isFinal: false });
const stale = model.nextJob();
assert.equal(model.accept({ text: 'Bonjour à tous', isFinal: true }), true);
assert.equal(model.isActive(stale), false);
assert.equal(model.progress(stale, 'Hello'), false);
const redo = model.nextJob();
assert.equal(redo.source, 'Bonjour à tous');
model.complete(stale, 'Hello'); // a late reply from the cancelled request is ignored
assert.equal(model.isActive(redo), true);
model.complete(redo, 'Hello everyone');
model.accept({ text: 'Merci', isFinal: false });
const kept = model.nextJob();
assert.equal(model.accept({ text: 'Merci', isFinal: true }), false);
model.complete(kept, 'Thank you');
assert.equal(model.nextJob(), null);
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.gray)), [230, 230]);
});
test('small revisions of a live block wait for a bigger change; its final is always redone', () => {
const model = new TranslationModel();
model.accept({ text: 'abcdef', isFinal: false });
model.complete(model.nextJob(), 'one');
model.accept({ text: 'abcdeg', isFinal: false });
assert.equal(model.nextJob(), null);
model.accept({ text: 'abcdefgh', isFinal: false });
assert.equal(model.nextJob(), null);
model.accept({ text: 'abcdefghi', isFinal: false });
model.complete(model.nextJob(), 'two');
model.accept({ text: 'abcdefghj', isFinal: true });
assert.equal(model.nextJob().source, 'abcdefghj');
});
test('an empty final keeps the live partial it replaces', () => {
const model = new TranslationModel();
model.accept({ text: 'Bonjour', isFinal: false });
model.accept({ text: '', isFinal: true });
assert.equal(model.segments[0].source, 'Bonjour');
assert.equal(model.segments[0].final, true);
});
test('context skips failed and dropped turns and stays bounded', () => {
test('context skips failed, dropped and provisional turns and stays bounded', () => {
const model = new TranslationModel();
const translate = (source, translation) => {
model.accept({ text: source, isFinal: true });
const job = model.nextJob();
if (translation === null) model.fail(job.segment);
else model.complete(job.segment, translation);
if (translation === null) model.fail(job);
else model.complete(job, translation);
};
translate('uno', 'one');
translate('ruido', null);
@@ -87,18 +147,29 @@ test('context skips failed and dropped turns and stays bounded', () => {
const long = new TranslationModel();
for (let i = 0; i < 3; i++) {
long.accept({ text: 'x'.repeat(500), isFinal: true });
long.complete(long.nextJob().segment, 'y'.repeat(500));
long.complete(long.nextJob(), 'y'.repeat(500));
}
long.accept({ text: 'next', isFinal: true });
assert.equal(long.nextJob().context.length, 1, 'the newest turn is kept even when it alone is over the cap');
// A final whose redo failed still shows its provisional translation, but
// that translates other text, so it isn't context.
const stale = new TranslationModel();
stale.accept({ text: 'Ich gehe', isFinal: false });
stale.complete(stale.nextJob(), 'I go');
stale.accept({ text: 'Ich gehe nach Hause', isFinal: true });
stale.fail(stale.nextJob());
assert.deepEqual(plain(stale.lines(measure, 100, false)), [{ text: 'I go', gray: 170 }]);
stale.accept({ text: 'Tschüss', isFinal: true });
assert.deepEqual(plain(stale.nextJob().context), []);
});
test('display lines: translations bright, originals dim until translated or when shown, live dimmest', () => {
test('display lines: settled bright, provisional dimmer, originals dim until translated or when shown', () => {
const model = new TranslationModel();
model.accept({ text: 'Guten Morgen', isFinal: true });
model.complete(model.nextJob().segment, 'Good morning');
model.complete(model.nextJob(), 'Good morning');
model.accept({ text: 'Wie geht es?', isFinal: true });
const asking = model.nextJob().segment;
const asking = model.nextJob();
model.accept({ text: 'Danke', isFinal: false });
assert.deepEqual(plain(model.lines(measure, 100, false)), [
{ text: 'Good morning', gray: 230 },
@@ -107,28 +178,29 @@ test('display lines: translations bright, originals dim until translated or when
]);
model.progress(asking, 'How are');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['Good morning', 'How are', 'Danke']);
assert.deepEqual(plain(model.lines(measure, 100, true).map(line => line.gray)), [120, 230, 120, 230, 90]);
assert.deepEqual(plain(model.lines(measure, 100, true).map(line => line.gray)), [120, 230, 120, 170, 90]);
assert.deepEqual(plain(model.lines(measure, 7, false).slice(0, 2)), [{ text: 'Good', gray: 230 }, { text: 'morning', gray: 230 }]);
// A failed segment keeps its original on screen.
// A failed block keeps its original on screen.
model.fail(asking);
assert.deepEqual(plain(model.lines(measure, 100, false)[1]), { text: 'Wie geht es?', gray: 120 });
// A finished, empty translation is noise: hidden unless originals are shown.
// A settled, empty translation is noise: hidden unless originals are shown.
model.accept({ text: 'mmm', isFinal: true });
model.complete(model.nextJob().segment, '');
model.complete(model.nextJob(), '');
assert.equal(model.lines(measure, 100, false).some(line => line.text === 'mmm'), false);
assert.equal(model.lines(measure, 100, true).some(line => line.text === 'mmm'), true);
});
test('saved text pairs each original with its translation', () => {
test('saved text pairs each final original with its translation', () => {
const model = new TranslationModel();
for (const [source, translation] of [['Ciao', 'Hello'], ['Okay.', 'Okay.'], ['boh', null]]) {
model.accept({ text: source, isFinal: true });
const job = model.nextJob();
if (translation === null) model.fail(job.segment);
else model.complete(job.segment, translation);
if (translation === null) model.fail(job);
else model.complete(job, translation);
}
model.accept({ text: 'ancora', isFinal: false }); // still live: not saved
assert.equal(model.savedText(), 'Ciao\n→ Hello\n\nOkay.\n\nboh\n→ (not translated)');
});
@@ -138,6 +210,8 @@ test('the user message carries earlier turns as context, then the speech', () =>
assert.match(message, /context only/);
assert.match(message, /<speech>Bien<\/speech>\n<english>Fine<\/english>\n\nTranslate this speech/);
assert.ok(message.endsWith('<speech>¿Y tú?</speech>'));
assert.equal(buildTranslateUserMessage('こんにちは', [], 'Japanese'),
'Translate this Japanese speech into English:\n<speech>こんにちは</speech>');
});
test('Auto picks the main provider when it is multilingual, else the first one set up', () => {
@@ -164,3 +238,15 @@ test('an explicit provider is used only while it is set up, and on-device ones a
assert.equal(settings.translateProviderSetting.isDisabled('auto'), false);
assert.deepEqual(plain(settings.translateProviderSetting.values), ['auto', 'elevenlabs', 'whisper', 'soniox', 'whisper-server']);
});
test('the spoken language defaults to auto-detect and resolves to an ISO 639-1 code once chosen', () => {
const settings = settingsHarness();
assert.equal(settings.resolveTranslateLanguage(), '');
assert.equal(settings.translateLanguageSetting.displayValue('auto'), 'Auto-detect');
settings.translateLanguageSetting.set('ja');
assert.equal(settings.resolveTranslateLanguage(), 'ja');
assert.equal(settings.spokenLanguageName('ja'), 'Japanese');
assert.equal(settings.translateLanguageSetting.values[0], 'auto');
assert.ok(settings.translateLanguageSetting.values.slice(1).every(code => /^[a-z]{2}$/.test(code)));
assert.equal(settings.translateLanguageSetting.values.includes('en'), false, 'English is the target, not a source');
});
+2
View File
@@ -20,6 +20,7 @@
"../app/apps/terminal/hub-header.ts",
"../app/g2/ios-peripheral-identity.ts",
"../app/graphics/image.ts",
"../app/graphics/textwrap.ts",
"../app/graphics/battery.ts",
"../app/graphics/plane.ts",
"../app/graphics/shell-scene.ts",
@@ -27,6 +28,7 @@
"../app/ui/menu-core.ts",
"../app/phone-ui/phone-gestures.ts",
"../app/native/java-direct-buffer.ts",
"../app/native/notification-types.ts",
"../app/g2/firmware-compat.ts",
"../app/g2/glasses-hardware-identity.ts",
"../app/g2/ble-proximity.ts",