Merge branch 'main' into rayneo-io-support

This commit is contained in:
James Babcock committed 2026-11-04 03:11:15 -08:00
commit 739e81c8a6
47 files changed
+3967 -90

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
// Compile the watch app's platform-independent Swift for the Mac and run its checks.
const { execFileSync } = require('node:child_process');
const { mkdtempSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { join, resolve } = require('node:path');
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-apple-watch-'));
const src = 'apple-watch/Faceclaw';
execFileSync('xcrun', ['swiftc', '-O', `${src}/Protocol.swift`, `${src}/PadStatus.swift`, `${src}/PadGestureRecognizer.swift`,
'tests/native/apple-watch/main.swift', '-o', join(dir, 'apple-watch-test')], { cwd: root, stdio: 'inherit' });
execFileSync(join(dir, 'apple-watch-test'), [], { stdio: 'inherit' });
+1
View File
@@ -157,6 +157,7 @@ function fixture() {
'./lock-screen': { LOCK_SCREEN_SURFACE_ID: 'lock-screen', createLockScreenImage: () => new images.GrayImage(640, 480, 123) },
'./glasses-presence': load('app/g2/glasses-presence.ts', {}),
'./device-addresses': { loadDeviceAddresses: () => ({ right: 'AA', left: 'BB', ring: '' }) }, './ios-peripheral-identity': { deviceAddressError: () => null },
'./wear-remote': { WearRemote: class { schedulePublish() {} } },
'./firmware-compat': load('app/g2/firmware-compat.ts', {}), './reconnect-policy': load('app/g2/reconnect-policy.ts', {}),
'../apps/launcher': { launcherEntries: () => [] },
'../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': { onPhoneShownChanged: () => () => {}, phoneUiButton: () => null },
+222
View File
@@ -0,0 +1,222 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const ts = require('typescript');
const path = require('node:path');
function load(file, context = {}) {
const sandbox = { exports: {}, console, ...context };
sandbox.global = sandbox;
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 types = () => load('app/native/wear-bridge-types.ts');
/** What FaceclawWatchBridge.m does, as far as JS can tell. */
function fakeNative({ available = true, watchApp = true } = {}) {
return {
calls: [], message: null, connection: null, watchApp,
isAvailable() { return available; },
isWatchAppAvailable() { return this.watchApp; },
setMessageHandlerConnectionHandler(message, connection) { this.message = message; this.connection = connection; },
publishStateForce(json, force) { this.calls.push(['state', JSON.parse(json), force]); },
sendEventSticky(json, sticky) { this.calls.push(['event', JSON.parse(json), sticky]); },
sendToWatchJson(pathName, json) { this.calls.push(['send', pathName, JSON.parse(json)]); },
sendAckOkJsReadyMessage(seq, ok, jsReady, message) { this.calls.push(['ack', seq, ok, jsReady, message]); },
};
}
function loadBridge(native) {
return load('app/native/wear-bridge.ios.ts', {
FaceclawWatchBridge: { shared: () => native },
require: () => types(),
}).wearBridge;
}
test('iOS bridge carries the same messages as the Android one', () => {
const native = fakeNative();
const bridge = loadBridge(native);
assert.equal(bridge.isAvailable(), true);
const received = [];
bridge.onMessage(message => received.push(message));
native.message('/faceclaw/input', '{"gesture":"click","seq":3}');
native.message('/faceclaw/press', 'not json');
assert.equal(received[0].path, '/faceclaw/input');
assert.equal(received[0].payload.gesture, 'click');
assert.equal(Object.keys(received[1].payload).length, 0, 'a malformed payload is empty, not a crash');
bridge.publishState({ phase: 'connected' }, true);
bridge.sendToWatch('/faceclaw/battery/request', {});
bridge.sendAck(received[0].nodeId, 3, false, 'refused');
assert.deepEqual(JSON.parse(JSON.stringify(native.calls)), [
['state', { phase: 'connected' }, true],
['send', '/faceclaw/battery/request', {}],
['ack', 3, false, true, 'refused'],
]);
});
test('only streaming assistant snapshots skip the sticky application context', () => {
const native = fakeNative();
const bridge = loadBridge(native);
bridge.sendEvent({ type: 'assistant', phase: 'streaming', text: 'par' });
bridge.sendEvent({ type: 'assistant', phase: 'done', text: 'partial' });
bridge.sendEvent({ type: 'alert', text: 'hi' });
assert.deepEqual(native.calls.map(call => call[2]), [false, true, true]);
});
test('availability follows the watch app, and a change reaches listeners', () => {
const native = fakeNative({ watchApp: false });
const bridge = loadBridge(native);
const seen = [];
bridge.onWatchConnection(connection => seen.push(connection.reachable));
assert.deepEqual(seen, [false], 'the current value is delivered immediately');
native.watchApp = true;
native.connection();
assert.deepEqual(seen, [false, true]);
assert.equal(bridge.getWatchConnection().watchName, 'Apple Watch');
});
test('a phone that cannot use WatchConnectivity turns the bridge into a no-op', () => {
const native = fakeNative({ available: false });
const bridge = loadBridge(native);
assert.equal(bridge.isAvailable(), false);
bridge.publishState({});
bridge.sendEvent({ type: 'alert', text: 'x' });
assert.equal(bridge.getWatchConnection().reachable, false);
assert.equal(native.calls.length, 0);
});
/** A WearRemote over a fake bridge and shell. */
function loadRemote({ phase = 'disconnected', preview = false, now = 1_800_000_000_000 } = {}) {
const bridge = {
messageListener: null, connectionListener: null, sent: [],
isAvailable: () => true,
onMessage(listener) { this.messageListener = listener; return () => {}; },
onWatchConnection(listener) { this.connectionListener = listener; listener({ reachable: true, watchName: 'Apple Watch' }); return () => {}; },
publishState() {},
sendEvent() {},
sendToWatch() {},
sendAck(_node, seq, ok, message) { this.sent.push({ seq, ok, message }); },
};
const levels = { watch: null, watchCharging: null };
const shell = {
onAssistantActivity() {}, onAlertShown() {},
getBatteryLevels: () => levels,
setBatteryLevels(update) { Object.assign(levels, update); },
foregroundWindow: () => null, getForegroundApp: () => null, getWindows: () => [],
isScreenOn: () => true, wake: () => true, isAssistantAvailable: () => false,
};
const setting = value => ({ get: () => value });
const intervals = [];
const realDate = Date;
const clock = { now };
class FakeDate extends realDate { static now() { return clock.now; } }
const inputs = [];
const state = { phase, status: '', glassesLocked: false, glassesWorn: null, silentMode: false, lastHeadsetBattery: null };
const { WearRemote } = load('app/g2/wear-remote.ts', {
Date: FakeDate,
setTimeout: () => 0, clearTimeout() {},
setInterval: fn => { intervals.push(fn); return intervals.length; },
Number, Math, JSON,
require: id => ({
'../analytics/analytics': { flagAnalyticsEvent() {} },
'../apps/evenhub/installed-apps': { getInstalledEvenHubApps: () => [], installedEvenHubAppId: id => id },
'../native/wear-bridge': { wearBridge: bridge, WEAR_PATHS: types().WEAR_PATHS },
'../ui/dashboard-settings': {
onAnySettingChanged() {}, watchCanUnlockSetting: setting(true), watchCrownClockwiseNextSetting: setting(false),
watchMirrorAssistantSetting: setting(true), watchRemoteEnabledSetting: setting(true),
watchVibrateOnInputSetting: setting(true), watchSoundEffectsSetting: setting(false),
},
'../ui/shell/shell': { shell },
'../ui/shell/voice-activity': { voiceActivity: { subscribe() {}, isActive: () => false } },
'../version': { FACECLAW_VERSION: 'test' },
})[id] ?? {},
});
const remote = new WearRemote({
apps: [{ appId: 'timer', title: 'Timer' }],
injectInput: async kind => { inputs.push(kind); },
launchApp: async () => {}, connect: async () => {}, disconnect: async () => {},
setGlassesLocked() {}, requestShellRender() {},
previewInputActive: () => preview,
getState: () => state,
appendLog() {},
});
const deliver = (pathName, payload) => bridge.messageListener({ path: pathName, payload, nodeId: 'apple-watch' });
return { remote, bridge, shell, levels, inputs, intervals, clock, deliver };
}
const settle = () => new Promise(resolve => setImmediate(resolve));
test('gestures are refused with no display, and land on the preview without glasses', async () => {
const none = loadRemote();
none.deliver('/faceclaw/input', { gesture: 'click', seq: 1 });
none.deliver('/faceclaw/press', {});
await settle();
assert.deepEqual(none.inputs, []);
assert.equal(none.bridge.sent[0].ok, false);
assert.match(none.bridge.sent[0].message, /not connected/);
const preview = loadRemote({ preview: true });
preview.deliver('/faceclaw/press', {});
preview.deliver('/faceclaw/input', { gesture: 'click', seq: 2 });
await settle();
assert.deepEqual(preview.inputs, ['ring-press', 'click']);
assert.equal(preview.bridge.sent[0].ok, true);
const glasses = loadRemote({ phase: 'connected' });
glasses.deliver('/faceclaw/input', { gesture: 'swipe-left', seq: 3 });
await settle();
assert.deepEqual(glasses.inputs, ['swipe-left']);
});
test('a watch battery report is shown, kept while fresh, and dropped once stale', async () => {
const { levels, intervals, clock, deliver, bridge } = loadRemote({ phase: 'connected' });
deliver('/faceclaw/battery', { battery: 64, charging: true, at: clock.now });
await settle();
assert.equal(levels.watch, 64);
assert.equal(levels.watchCharging, true);
assert.equal(bridge.sent.length, 0, 'battery reports are never acked');
const tick = () => intervals.forEach(fn => fn());
clock.now += 3 * 60 * 60_000;
tick();
assert.equal(levels.watch, 64, 'a closed watch app reports rarely; three hours is still shown');
clock.now += 2 * 60 * 60_000;
tick();
assert.equal(levels.watch, null, 'five hours says the watch is gone');
});
test('a battery report found long after it was left is not shown as current', async () => {
const { levels, clock, deliver } = loadRemote({ phase: 'connected' });
deliver('/faceclaw/battery', { battery: 90, charging: false, at: clock.now - 5 * 60 * 60_000 });
await settle();
assert.equal(levels.watch, null);
// A live report without a timestamp is current by definition, as Wear OS's always are.
deliver('/faceclaw/battery', { battery: 80, charging: false });
await settle();
assert.equal(levels.watch, 80);
});
test('a watch that does not know its battery shows no level, not an empty one', async () => {
const { levels, deliver } = loadRemote({ phase: 'connected' });
deliver('/faceclaw/battery', { battery: 50, charging: false });
await settle();
assert.equal(levels.watch, 50);
deliver('/faceclaw/battery', { battery: null, charging: false });
await settle();
assert.equal(levels.watch, null);
});
test('the mirrored state carries the Apple Watch feedback settings', () => {
const state = loadRemote({ preview: true }).remote.buildState();
assert.equal(state.vibrateOnInput, true);
assert.equal(state.soundEffects, false);
});
test('the mirrored state says when gestures are landing on the preview', () => {
const state = loadRemote({ preview: true }).remote.buildState();
assert.equal(state.preview, true);
assert.equal(state.connected, false);
assert.equal(loadRemote({ phase: 'connected' }).remote.buildState().connected, true);
});
+222
View File
@@ -0,0 +1,222 @@
// Exercises the watch app's platform-independent logic on the Mac: the gesture
// state machine, the wire format and the pad's status line.
import Foundation
var failures = 0
func check(_ condition: @autoclosure () -> Bool, _ message: String, line: Int = #line) {
if !condition() {
failures += 1
print("FAILED line \(line): \(message)")
}
}
// MARK: - Gestures
final class Harness {
let recognizer = PadGestureRecognizer()
var events: [PadEvent] = []
var time: TimeInterval = 100
init() { recognizer.onEvent = { [unowned self] in self.events.append($0) } }
func down(_ point: CGPoint = .zero) { recognizer.touchDown(at: point, time: time) }
func move(_ point: CGPoint) { recognizer.touchMoved(to: point) }
func up(_ point: CGPoint = .zero) { recognizer.touchUp(at: point, time: time) }
func wait(_ seconds: TimeInterval) { time += seconds; recognizer.advance(to: time) }
}
do {
// A lone tap is only known to be one once the double-tap window closes.
let h = Harness()
h.down(); h.up()
check(h.events == [.press], "a tap waits out the double-tap window: \(h.events)")
h.wait(0.2)
check(h.events == [.press], "still inside the window")
h.wait(0.2)
check(h.events == [.press, .tap], "tap after the window: \(h.events)")
check(h.recognizer.deadline == nil, "nothing left to wait for")
}
do {
let h = Harness()
h.down(); h.up(); h.wait(0.1); h.down(); h.wait(0.05); h.up()
check(h.events == [.press, .press, .doubleTap], "double-tap reports both presses: \(h.events)")
h.wait(1)
check(h.events.count == 3, "no late tap after a double-tap")
}
do {
// Hold: reported at the timeout and again on release, however long it lasts.
let h = Harness()
h.down(); h.wait(0.39)
check(h.events == [.press], "not yet a hold")
h.wait(0.02)
check(h.events == [.press, .longPressStart], "hold starts at the timeout: \(h.events)")
h.wait(5); h.up()
check(h.events == [.press, .longPressStart, .longPressEnd], "hold ends on release: \(h.events)")
}
do {
// Movement during a hold doesn't turn it into a swipe.
let h = Harness()
h.down(); h.wait(0.5); h.move(CGPoint(x: 50, y: 50)); h.up(CGPoint(x: 50, y: 50))
check(h.events == [.press, .longPressStart, .longPressEnd], "a hold stays a hold: \(h.events)")
}
do {
let h = Harness()
h.down(); h.up(); h.wait(0.1); h.down(); h.wait(0.5)
check(h.events == [.press, .press, .shortThenLongPress], "tap then hold: \(h.events)")
h.up(); h.wait(1)
check(h.events.count == 3, "no double-tap or release after tap-then-hold: \(h.events)")
}
do {
let h = Harness()
let start = CGPoint(x: 100, y: 180), end = CGPoint(x: 104, y: 60)
h.down(start); h.move(end); h.wait(1); h.up(end)
check(h.events == [.press, .swipe(start: start, dx: 4, dy: -120)], "a swipe, never a hold: \(h.events)")
}
do {
// A small wobble under the slop is still a tap.
let h = Harness()
h.down(.zero); h.move(CGPoint(x: 3, y: 3)); h.up(CGPoint(x: 3, y: 3)); h.wait(0.4)
check(h.events == [.press, .tap], "wobble is a tap: \(h.events)")
}
do {
// Whatever ends a hold, the phone must hear a release or it sees a stuck long-press.
let h = Harness()
h.down(); h.wait(0.5); h.recognizer.cancel()
check(h.events == [.press, .longPressStart, .longPressEnd], "a cancelled hold is released: \(h.events)")
check(!h.recognizer.isTouching, "idle after cancel")
}
do {
let h = Harness()
h.down(); h.recognizer.cancel(); h.wait(1)
check(h.events == [.press], "a cancelled touch is not a tap: \(h.events)")
}
do {
// The wrist dropped after a complete tap: it was still a tap, and there's no touch left to wait for.
let h = Harness()
h.down(); h.up(); h.recognizer.cancel()
check(h.events == [.press, .tap], "a finished tap survives cancel: \(h.events)")
h.wait(1)
check(h.events.count == 2, "and isn't reported twice")
}
do {
// A touch whose end was never seen must not wedge the next one.
let h = Harness()
h.down(); h.wait(0.5); h.down()
check(h.events == [.press, .longPressStart, .longPressEnd, .press], "a lost touch-up is recovered: \(h.events)")
}
// MARK: - Wire format
do {
let json = """
{"protocol":1,"version":"0.8.6","phase":"connected","status":"ok","connected":true,"preview":false,
"screenOn":true,"locked":false,"worn":null,"listening":false,"battery":78,"charging":false,"silentMode":false,
"caseBattery":64,"caseCharging":null,"foreground":{"appId":"music","title":"Music"},
"windows":[{"windowId":"w1","title":"Music","appId":"music","focused":true,"closeable":false,"acceptsText":true}],
"apps":[{"appId":"timer","title":"Timer"}],"remoteEnabled":true,"vibrateOnInput":false,"soundEffects":true,"canUnlock":false,
"mirrorAssistant":true,"assistantAvailable":true}
"""
let state = PhoneState.parse(json, updatedAt: 42)
check(state != nil, "state parses")
check(state?.connected == true && state?.takesInput == true, "connected")
check(state?.battery == 78 && state?.caseBattery == 64, "batteries")
check(state?.worn == nil && state?.caseCharging == nil, "JSON null stays nil")
check(state?.foreground == AppEntry(appId: "music", title: "Music"), "foreground")
check(state?.windows.first?.acceptsText == true && state?.windows.first?.closeable == false, "window flags")
check(state?.apps.map(\.appId) == ["timer"], "apps")
check(state?.vibrateOnInput == false && state?.soundEffects == true && state?.canUnlock == false, "settings")
check(state?.updatedAt == 42, "updatedAt")
}
do {
// A state from an older or newer phone app parses with defaults rather than failing.
let state = PhoneState.parse("{\"battery\":140,\"unknownField\":[1,2]}", updatedAt: 0)
check(state?.battery == nil, "out-of-range percentage is unknown")
check(state?.remoteEnabled == true && state?.canUnlock == true, "permissive defaults")
check(state?.vibrateOnInput == true && state?.soundEffects == false, "feedback defaults")
check(state?.phase == "disconnected" && state?.takesInput == false, "disconnected default")
check(PhoneState.parse("not json", updatedAt: 0) == nil, "garbage is rejected")
check(PhoneState.parse("[1]", updatedAt: 0) == nil, "a non-object is rejected")
}
do {
let ack = Ack.parse("{\"seq\":7,\"ok\":false,\"jsReady\":false,\"message\":\"nope\"}")
check(ack == Ack(seq: 7, ok: false, jsReady: false, message: "nope"), "ack: \(String(describing: ack))")
check(Ack.parse("{\"seq\":7,\"ok\":true}")?.jsReady == true, "jsReady defaults to true")
}
do {
check(PhoneEvent.parse("{\"type\":\"assistant\",\"phase\":\"done\",\"text\":\"hi\"}") == .assistant(phase: "done", text: "hi"), "assistant event")
check(PhoneEvent.parse("{\"type\":\"alert\",\"text\":\"x\"}") == .alert(text: "x"), "alert event")
check(PhoneEvent.parse("{\"type\":\"other\"}") == nil, "unknown event")
}
do {
var mirror = AssistantMirror()
mirror = mirror.applying(phase: "thinking", text: "")
check(mirror == AssistantMirror(phase: "thinking", text: ""), "thinking")
mirror = mirror.applying(phase: "streaming", text: "Hel")
mirror = mirror.applying(phase: "done", text: "")
check(mirror == AssistantMirror(phase: "done", text: "Hel"), "an empty done keeps the streamed text")
mirror = mirror.applying(phase: "closed", text: "")
check(mirror == AssistantMirror(phase: "idle", text: "Hel"), "closed keeps the text to read")
check(mirror.applying(phase: "bogus", text: "x") == mirror, "unknown phases change nothing")
}
// MARK: - Status line
do {
var up = LinkStatus(activated: true, phoneReachable: true, phoneAppInstalled: true, appRunning: true)
var state = PhoneState()
state.connected = true
state.screenOn = true
state.foreground = AppEntry(appId: "music", title: "Music")
check(padLine(state: nil, link: LinkStatus()).text == "Waiting for phone", "before activation")
check(padLine(state: state, link: LinkStatus(activated: true, phoneAppInstalled: false)).text == "Install Faceclaw on phone", "no phone app")
check(padLine(state: state, link: LinkStatus(activated: true, phoneAppInstalled: true)).text == "No phone", "unreachable")
up.appRunning = false
check(padLine(state: state, link: up).text == "Open Faceclaw on phone", "dashboard not up")
up.appRunning = true
check(padLine(state: nil, link: up).text == "Waiting for phone", "no state yet")
check(padLine(state: state, link: up) == StatusLine(text: "Music", tone: .good), "foreground app")
state.screenOn = false
check(padLine(state: state, link: up).text == "●● Wake", "display off")
state.locked = true
check(padLine(state: state, link: up).text == "Locked", "locked beats display off")
state.charging = true
state.battery = 50
state.caseBattery = 64
state.caseCharging = true
check(padLine(state: state, link: up) == StatusLine(text: "Charging · G2 50%", tone: .busy, detail: "Case 64% ⚡"), "charging")
var off = PhoneState()
check(padLine(state: off, link: up).text == "Disconnected", "disconnected")
off.phase = "retrying"
check(padLine(state: off, link: up).text == "Reconnecting…", "iOS's retrying phase")
off.phase = "disconnected"
off.preview = true
off.screenOn = true
check(padLine(state: off, link: up) == StatusLine(text: "Launcher", tone: .good, detail: "Phone preview"), "phone preview")
off.screenOn = false
check(padLine(state: off, link: up).text == "●● Wake", "preview display off")
off.remoteEnabled = false
check(padLine(state: off, link: up).text == "Watch control off", "remote disabled")
}
if failures > 0 {
print("\(failures) check(s) failed")
exit(1)
}
print("apple-watch logic: all checks passed")