Add tokens and faceclaw-input script, which allows using a CLI utility as an input device for the glasses

This commit is contained in:
James Babcock committed 2026-09-20 00:34:58 -07:00
1 parent 313ba85708
commit ccb320c66f
32 files changed
+1785 -12

No files matched your search

+2
View File
@@ -76,6 +76,7 @@ for (const isIOS of [true, false]) {
} });
const menus = load('app/ui/dashboard/settings-menus.ts', { global: { isIOS }, require: id => {
if (id === '../dashboard-settings') return settings;
if (id === './remote-input-menu') return { remoteInputMenuItem: () => ({ label: 'Input tokens' }) };
if (id === './settings-panel') return { SettingsPanelLayer: class { constructor(sections) { this.sections = sections; } } };
return { LOCAL_MODEL: { sizeBytes: 1000 }, ASR_MODELS: { moonshine: {}, 'whisper-base-en': {} },
uiFontPickerMenuItem: () => ({ label: 'Font' }), terminalFontPickerMenuItem: () => ({ label: 'Terminal font' }) };
@@ -193,6 +194,7 @@ test('background glasses input still composites frames; phone resume preserves t
const settings = { brightnessSetting: { get: () => 'auto' }, brightnessSettingToLevel: () => null, lockScreenEnabledSetting: { get: () => true }, onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } };
const modules = {
"../ui/input-monitor": load("app/ui/input-monitor.ts", {}),
'../remote/service': { startRemoteInput() {} },
'../assistant/system-tools': { registerSystemTools() {} },
'../assistant/window-tools': { registerWindowTools() {} },
'../assistant/navigate-tools': { registerNavigateTools() {} },
+19 -2
View File
@@ -24,7 +24,7 @@ const timings = load('app/native/frame-timings.ts', {});
function fixture() {
const observers = new Map(), phoneState = { protectedDataAvailable: true };
let settingsChanged;
let settingsChanged, remoteHost;
let now = 1000, nextTask = 0, screenOn = true, shellOptions, session;
const tasks = new Map(), sent = [], previews = [], received = [], errors = [];
const boardStats = { starts: 0, stops: 0, paints: 0 };
@@ -74,6 +74,7 @@ function fixture() {
const inputMonitor = load("app/ui/input-monitor.ts", {});
const modules = {
"../ui/input-monitor": inputMonitor,
'../remote/service': { startRemoteInput(host) { remoteHost = host; } },
'../assistant/system-tools': { registerSystemTools() {} },
'../assistant/window-tools': { registerWindowTools() {} },
'../assistant/navigate-tools': { registerNavigateTools() {} },
@@ -139,7 +140,7 @@ function fixture() {
await render();
}
async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); }
return { controller, shell, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers, wear: wearing => session.onWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render };
return { controller, shell, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers, wear: wearing => session.onWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render };
}
const assertBlank = pixels => assert.ok(pixels.every(p => p === 0));
const assertBoard = pixels => {
@@ -319,3 +320,19 @@ test('keyboard sessions cannot deliver text after the glasses lock or phone leav
assert.equal(calls.length, 2);
editor.discard(); assert.deepEqual(calls.at(-1), ['discard']);
});
test('external input shares iOS dispatch, including hold release and phone-lock gating', async () => {
const f = fixture(); await f.controller.connect(); f.wear(false);
assert.equal(f.remoteHost.ready(), true);
assert.equal(f.remoteHost.locked(), false);
await f.remoteHost.input('swipe-left', 'watch');
await f.remoteHost.input('long-press', 'ring');
assert.deepEqual(f.received.map(e => [e.type, e.source]), [
['swipe-left', 'watch'], ['long-press', 'ring'], ['long-press-release', 'ring'],
]);
f.observers.get('lock')();
assert.equal(f.remoteHost.locked(), true);
await f.remoteHost.input('click', 'watch');
assert.equal(f.received.length, 3);
});
+105
View File
@@ -0,0 +1,105 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { PassThrough } = require('node:stream');
const { interactive } = require('../scripts/faceclaw-input.cjs');
const { createEditor } = require('../scripts/faceclaw-input-editor.cjs');
async function until(predicate) {
for (let i = 0; i < 100; i++) {
if (predicate()) return;
await new Promise(resolve => setTimeout(resolve, 5));
}
assert.fail('Timed out waiting for interactive state');
}
async function fixture(t, transmit) {
const input = new PassThrough(), output = new PassThrough(), modes = [], requests = [];
let printed = '', stopped = false;
input.isTTY = output.isTTY = true; input.isRaw = false; output.columns = 100;
input.setRawMode = value => { input.isRaw = value; modes.push(value); };
output.on('data', data => { printed += data.toString(); });
const task = interactive({}, input, output, async (_options, payload) => {
requests.push(payload); if (transmit) return transmit(payload);
}).then(() => { stopped = true; });
t.after(async () => { if (!stopped) input.write('\x03'); await task; input.destroy(); output.destroy(); });
await until(() => printed.includes('Connected.'));
return { input, requests, modes, task, printed: () => printed, stopped: () => stopped,
messages: () => requests.filter(r => r.action !== 'ping') };
}
test('i composes with Enquirer editing and sends text once; editing keys never become gestures', async t => {
const f = await fixture(t);
// Start and type in one input chunk. Left/right, delete/backspace, Ctrl-A/E,
// and Ctrl-W exercise the actual Enquirer input editor, not a stub.
f.input.write('ihello worxd\x1b[D\x1b[D\x1b[3~l\x1b[CX\x7f\x05!\x01Say \x05 extra\x17\r');
await until(() => f.messages().length === 1);
assert.deepEqual(f.messages(), [{ action: 'text', text: 'Say hello world!' }]);
assert.match(f.printed(), /Sent to foreground window/);
f.input.write('\x1b[A');
await until(() => f.messages().length === 2);
assert.deepEqual(f.messages()[1], { action: 'input', gesture: 'swipe-up', source: 'watch' });
});
test('a sends to assistant and i/a remain literal while composing; Unicode is preserved', async t => {
const f = await fixture(t);
f.input.write('aAssistant: café 世界 👋 i a\r');
await until(() => f.messages().length === 1);
assert.deepEqual(f.messages(), [{ action: 'assistant', text: 'Assistant: café 世界 👋 i a' }]);
});
test('Esc discards and clears a draft, then returns to controls with a fresh next draft', async t => {
const f = await fixture(t);
f.input.write('isecret draft\t\x1b[A\x1b[B');
await until(() => f.printed().includes('secret draft'));
f.input.write('\x1b');
await until(() => f.printed().includes('Draft discarded.'));
assert.deepEqual(f.messages(), []);
f.input.write('a\r');
await until(() => f.printed().includes('Empty draft; nothing sent.'));
assert.deepEqual(f.messages(), []);
f.input.write('\r');
await until(() => f.messages().length === 1);
assert.deepEqual(f.messages()[0], { action: 'input', gesture: 'click', source: 'watch' });
f.input.write('ifresh\r');
await until(() => f.messages().length === 2);
assert.equal(f.messages()[1].text, 'fresh');
});
test('Ctrl-C while composing clears the prompt and restores raw mode without sending a draft', async t => {
const f = await fixture(t);
f.input.write('aunfinished');
await until(() => f.printed().includes('unfinished'));
f.input.write('\x03'); await f.task;
assert.deepEqual(f.messages(), []);
assert.deepEqual(f.modes, [true, false]);
assert.equal(f.input.listenerCount('keypress'), 0);
});
test('composition-only token works without input permission and a rejected text does not kill the session', async t => {
let rejectText = true;
const f = await fixture(t, payload => {
assert.notEqual(payload.permission, 'input');
if (payload.action === 'text' && rejectText) {
rejectText = false;
throw Object.assign(new Error('Token lacks text permission.'), { code: 'forbidden' });
}
});
f.input.write('ino access\r');
await until(() => f.printed().includes('Token lacks text permission.'));
assert.equal(f.stopped(), false);
f.input.write('aallowed\r');
await until(() => f.printed().includes('Sent to assistant.'));
assert.deepEqual(f.messages(), [{ action: 'text', text: 'no access' }, { action: 'assistant', text: 'allowed' }]);
});
test('editor validates the API length limit and lets the user fix the draft', async () => {
const input = new PassThrough(), output = new PassThrough(); output.columns = 80;
let printed = ''; output.on('data', data => { printed += data; });
const editor = createEditor('text', input, output);
editor.keypress('x'.repeat(8001), {}); editor.keypress('\r', { name: 'return' });
await until(() => printed.includes('Maximum 8000 characters.'));
editor.keypress('\x01', { name: 'a', ctrl: true });
editor.keypress('\x0b', { name: 'k', ctrl: true });
editor.keypress('valid', {}); editor.keypress('\r', { name: 'return' });
assert.equal(await editor.result, 'valid');
input.destroy(); output.destroy();
});
+72
View File
@@ -0,0 +1,72 @@
// macOS host probe for the production iOS socket implementation. Run separately:
// node tests/remote-input-native.cjs (requires Xcode command-line tools).
const assert = require('node:assert/strict');
const { execFileSync, spawn } = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const net = require('node:net');
const crypto = require('node:crypto');
const { send } = require('../scripts/faceclaw-input.cjs');
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-input-'));
async function main() {
const harness = path.join(directory, 'main.m');
fs.writeFileSync(harness, `
#import "FaceclawRemoteInput.h"
#import <unistd.h>
int main(int argc, const char **argv) { @autoreleasepool {
FaceclawRemoteInput *server = [FaceclawRemoteInput shared];
NSString *secret = [server randomSecret];
if (secret.length != 64 || ![[server tokenDigest:@"test"] isEqual:@"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"]) return 2;
NSString *failure = [server start:atoi(argv[1])];
if (failure.length) return 3;
puts("READY"); fflush(stdout);
for (;;) { @autoreleasepool {
NSString *raw = [server nextRequest];
if (raw) {
NSDictionary *request = [NSJSONSerialization JSONObjectWithData:[raw dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSDictionary *body = [NSJSONSerialization JSONObjectWithData:[request[@"body"] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
if ([body[@"text"] isEqual:@"stop"]) { [server stop]; usleep(300000); puts("STOPPED"); fflush(stdout); return 0; }
NSData *reply = [NSJSONSerialization dataWithJSONObject:@{@"ok":@YES, @"echo":body ?: @{}} options:0 error:nil];
[server complete:[request[@"id"] longLongValue] response:[[NSString alloc] initWithData:reply encoding:NSUTF8StringEncoding]];
}
usleep(1000);
}}
}}
`);
const binary = path.join(directory, 'probe');
execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-framework', 'Security',
'-IApp_Resources/iOS/src', harness, 'App_Resources/iOS/src/FaceclawRemoteInput.m',
'App_Resources/iOS/src/FaceclawCrypto.m', '-o', binary], { stdio: 'inherit' });
const reservation = net.createServer();
await new Promise((resolve, reject) => { reservation.once('error', reject); reservation.listen(0, '127.0.0.1', resolve); });
const port = reservation.address().port;
await new Promise(resolve => reservation.close(resolve));
const child = spawn(binary, [String(port)], { stdio: ['ignore', 'pipe', 'inherit'] });
const exited = new Promise(resolve => child.once('exit', resolve));
try {
await new Promise((resolve, reject) => {
child.stdout.once('data', data => data.toString().includes('READY') ? resolve() : reject(new Error('Probe not ready')));
child.once('exit', code => reject(new Error(`Probe exited: ${code}`)));
child.once('error', reject);
});
const options = { host: '127.0.0.1', port, token: 'fc1_' + crypto.randomBytes(32).toString('hex') };
const reply = await send(options, { action: 'assistant', text: 'Native round trip 👋 世界' });
assert.equal(reply.echo.text, 'Native round trip 👋 世界');
assert.equal(reply.echo.token, options.token);
// Invalid UTF-8 and oversized frames close without becoming JS requests.
for (const frame of [Buffer.from([0xff, 0x0a]), Buffer.from('x'.repeat(65537) + '\n')]) {
await new Promise((resolve, reject) => {
const socket = net.createConnection({ host: options.host, port }, () => socket.end(frame));
socket.on('data', () => reject(new Error('Invalid frame was dispatched')));
socket.on('error', error => error.code === 'ECONNRESET' ? resolve() : reject(error));
socket.on('close', resolve);
});
}
await send(options, { action: 'input', gesture: 'click' });
await assert.rejects(send(options, { action: 'text', text: 'stop' }));
assert.equal(await exited, 0);
console.log('Native socket probe passed: crypto, framing, UTF-8, multiple connections, shutdown.');
} finally { child.kill(); }
}
main().catch(error => { console.error(error); process.exitCode = 1; }).finally(() => fs.rmSync(directory, { recursive: true, force: true }));
+140
View File
@@ -0,0 +1,140 @@
#!/usr/bin/env python3
"""Exercise the real CLI through a pseudo-terminal and a FIN-sensitive TCP peer."""
import json
import os
import pty
import select
import signal
import socket
import subprocess
import termios
import threading
import time
from pathlib import Path
root = Path(__file__).resolve().parents[1]
server = socket.socket()
server.bind(('127.0.0.1', 0))
server.listen(8)
server.settimeout(.1)
requests, failures = [], []
finished = threading.Event()
drop_next = threading.Event()
def serve():
while not finished.is_set():
try:
conn, _ = server.accept()
except socket.timeout:
continue
with conn:
conn.settimeout(2)
data = b''
while b'\n' not in data:
chunk = conn.recv(4096)
if not chunk:
break
data += chunk
request = json.loads(data)
requests.append(request)
# A forwarder may tear down both directions on FIN. Do likewise.
conn.settimeout(.04)
try:
if conn.recv(1, socket.MSG_PEEK) == b'':
failures.append('CLI sent FIN before reply')
continue
except socket.timeout:
pass
if request['action'] == 'input' and drop_next.is_set():
drop_next.clear()
continue
conn.sendall(b'{"ok":true}\n')
thread = threading.Thread(target=serve, daemon=True)
thread.start()
master, slave = pty.openpty()
termios.tcsetwinsize(slave, (24, 100))
original = termios.tcgetattr(slave)
env = {**os.environ, 'FACECLAW_TOKEN': 'fc1_' + 'a' * 64}
child = subprocess.Popen(['node', str(root / 'scripts/faceclaw-input.cjs'), '--port', str(server.getsockname()[1]),
'interactive'], stdin=slave, stdout=slave, stderr=slave, env=env, cwd=root)
output = bytearray()
def wait_for(predicate, timeout=7):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
if select.select([master], [], [], .03)[0]:
output.extend(os.read(master, 8192))
if predicate():
return
assert child.poll() is None, output.decode(errors='replace')
raise AssertionError(output.decode(errors='replace'))
try:
wait_for(lambda: b'Connected.' in output)
assert requests[0]['action'] == 'ping', requests
for sequence, gesture in [(b'\x1b[A', 'swipe-up'), (b'\r', 'click'), (b'\x1b', 'double-click'), (b'\t', 'long-press')]:
os.write(master, sequence)
wait_for(lambda: any(r.get('gesture') == gesture for r in requests))
time.sleep(.1) # Each key follows the previous request's complete lifecycle.
assert child.poll() is None
before = len(requests)
os.write(master, b'ihello worxd\x1b[D\x1b[D\x1b[3~l\x1b[C\x01Say \x05!\r')
wait_for(lambda: b'Sent to foreground window.' in output)
assert [r for r in requests[before:] if r['action'] != 'ping'] == [
{'action': 'text', 'text': 'Say hello world!', 'version': 1, 'token': env['FACECLAW_TOKEN']}]
os.write(master, 'aHello assistant 世界\r'.encode())
wait_for(lambda: b'Sent to assistant.' in output)
assert requests[-1]['action'] == 'assistant' and requests[-1]['text'] == 'Hello assistant 世界'
before = len(requests)
os.write(master, b'idiscard this draft')
wait_for(lambda: b'discard this draft' in output)
os.write(master, b'\x1b')
wait_for(lambda: b'Draft discarded.' in output)
assert not [r for r in requests[before:] if r['action'] != 'ping'], 'Esc leaked an input or sent a draft'
drop_next.set()
os.write(master, b'\x1b[B')
wait_for(lambda: b'without a response' in output)
assert child.poll() is None
wait_for(lambda: output.count(b'Connected.') >= 2)
before = len(requests)
os.write(master, b'\x1b[C')
wait_for(lambda: any(r.get('gesture') == 'swipe-right' for r in requests[before:]))
time.sleep(.1) # Let the final reply arrive before testing orderly exit.
before = len(requests)
os.write(master, b'aunfinished final draft')
wait_for(lambda: b'unfinished final draft' in output)
os.write(master, b'\x03')
# Keep draining prompt cleanup output while waiting for process exit.
wait_for(lambda: child.poll() is not None, timeout=2)
assert child.returncode == 0
assert termios.tcgetattr(slave) == original, 'Terminal mode was not restored'
assert not failures, failures
assert not [r for r in requests[before:] if r['action'] != 'ping'], 'Ctrl-C sent a draft'
assert len([r for r in requests if r.get('gesture') == 'swipe-down']) == 1, 'Failed input was replayed'
# Signals must also restore the terminal while the editor owns the prompt.
# Enquirer's default signal handling exits before our async cleanup runs.
output.clear()
child = subprocess.Popen(['node', str(root / 'scripts/faceclaw-input.cjs'), '--port', str(server.getsockname()[1]),
'interactive'], stdin=slave, stdout=slave, stderr=slave, env=env, cwd=root)
wait_for(lambda: b'Connected.' in output)
os.write(master, b'isignal cleanup draft')
wait_for(lambda: b'signal cleanup draft' in output)
child.send_signal(signal.SIGTERM)
wait_for(lambda: child.poll() is not None, timeout=2)
assert child.returncode == 0
assert termios.tcgetattr(slave) == original, 'Signal exit left the terminal in raw mode'
print('PTY probe passed: immediate connection, repeated keys, text/assistant editing, discard, recovery, Ctrl-C/SIGTERM and terminal restoration.')
finally:
if child.poll() is None:
child.kill()
child.wait()
finished.set()
thread.join(timeout=3)
server.close()
os.close(master)
os.close(slave)
+234
View File
@@ -0,0 +1,234 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const crypto = require('node:crypto');
const net = require('node:net');
const { EventEmitter } = require('node:events');
const { TokenStore, handleRequest, GESTURES } = require('../.test-build/app/remote/protocol.js');
const cli = require('../scripts/faceclaw-input.cjs');
function fixture(permissions = ['input', 'text', 'assistant']) {
let stored = '[]'; const calls = [];
const tokens = new TokenStore(() => stored, value => { stored = value; },
() => crypto.randomBytes(32).toString('hex'), value => crypto.createHash('sha256').update(value).digest('hex'));
const { token, record } = tokens.create('Test app', permissions);
const host = { ready: () => true, locked: () => false, input: async (...args) => calls.push(args),
acceptsText: () => true, text: text => calls.push(['text', text]), assistantAvailable: () => true,
assistant: text => calls.push(['assistant', text]) };
const request = payload => handleRequest(JSON.stringify({ version: 1, token, ...payload }), tokens, host);
return { tokens, token, record, host, calls, request, stored: () => stored };
}
test('tokens are random, persisted only as hashes, and survive reload', () => {
const f = fixture();
assert.match(f.token, /^fc1_[a-f0-9]{64}$/);
assert.ok(!f.stored().includes(f.token));
const another = f.tokens.create('Second', ['input']);
assert.notEqual(another.token, f.token);
assert.equal(f.tokens.authenticate(f.token).name, 'Test app');
assert.equal(f.tokens.authenticate(f.token + 'x'), undefined);
assert.equal(f.tokens.authenticate('fc1_' + '0'.repeat(64)), undefined);
assert.throws(() => f.tokens.create('', ['input']));
assert.throws(() => f.tokens.create('Empty', []));
});
test('permission matrix independently authorizes each operation; edits and revocation take effect immediately', async () => {
for (const permission of ['input', 'text', 'assistant']) {
const f = fixture([permission]);
for (const action of ['input', 'text', 'assistant']) {
const result = await f.request({ action, gesture: 'click', text: 'hello' });
assert.equal(result.ok, action === permission);
if (!result.ok) assert.equal(result.error, 'forbidden');
}
f.tokens.permissions(f.record.id, []);
assert.equal((await f.request({ action: permission, gesture: 'click', text: 'hello' })).error, 'forbidden');
f.tokens.revoke(f.record.id);
assert.equal((await f.request({ action: permission })).error, 'unauthorized');
}
});
test('every supported gesture routes with the selected source, directions require watch', async () => {
const f = fixture();
for (const gesture of GESTURES) {
assert.equal((await f.request({ action: 'input', gesture })).ok, true);
assert.deepEqual(f.calls.at(-1), [gesture, 'watch']);
assert.equal((await f.request({ action: 'input', gesture, source: 'ring' })).ok, !gesture.startsWith('swipe-'));
}
});
test('malformed and unauthorized requests have no side effects', async () => {
const f = fixture();
for (const body of ['null', '[]', '{', 'true']) assert.equal((await handleRequest(body, f.tokens, f.host)).ok, false);
for (const payload of [ { token: 'bad' }, { version: 2 }, { action: 'unlock' },
{ action: 'input', gesture: 'wakeword' }, { action: 'input', gesture: 'click', source: 'phone' },
{ action: 'text', text: '' }, { action: 'text', text: 123 }, { action: 'text', text: 'x'.repeat(8001) },
{ action: 'assistant', text: '\0' } ]) assert.equal((await f.request(payload)).ok, false);
assert.deepEqual(f.calls, []);
});
test('locks block text and assistant; gestures still use lock-screen dispatch', async () => {
const f = fixture(); f.host.locked = () => true;
for (const action of ['text', 'assistant']) assert.equal((await f.request({ action, text: 'hello' })).error, 'locked');
assert.equal((await f.request({ action: 'input', gesture: 'double-click' })).ok, true);
assert.deepEqual(f.calls, [['double-click', 'watch']]);
});
test('readiness and unavailable destinations fail; text whitespace is preserved', async () => {
const f = fixture();
f.host.ready = () => false;
assert.equal((await f.request({ action: 'input', gesture: 'click' })).error, 'unavailable');
f.host.ready = () => true; f.host.acceptsText = () => false;
assert.equal((await f.request({ action: 'text', text: 'hello' })).error, 'unavailable');
f.host.assistantAvailable = () => false;
assert.equal((await f.request({ action: 'assistant', text: 'hello' })).error, 'unavailable');
f.host.acceptsText = () => true;
assert.equal((await f.request({ action: 'text', text: ' a\nb ' })).ok, true);
assert.deepEqual(f.calls, [['text', ' a\nb ']]);
f.host.input = async () => { throw new Error('private detail'); };
assert.equal((await f.request({ action: 'input', gesture: 'click' })).error, 'failed');
});
test('CLI parses one-off commands and maps keyboard input', () => {
const env = { FACECLAW_TOKEN: fixture().token };
assert.deepEqual(cli.parseArgs(['input', 'up'], env).payload, { action: 'input', gesture: 'swipe-up', source: 'watch' });
assert.equal(cli.parseArgs(['assistant', 'a message'], env).payload.text, 'a message');
assert.equal(cli.parseArgs(['interactive'], env).interactive, true);
assert.throws(() => cli.parseArgs(['input', 'up', '--source', 'ring'], env));
assert.throws(() => cli.parseArgs(['text', ''], env));
assert.throws(() => cli.parseArgs(['input', 'tap', '--port', '0'], env));
assert.equal(cli.parseArgs(['--help'], {}).help, true);
assert.deepEqual(['up','down','left','right','return','escape','tab'].map(name => cli.keyGesture({name})),
['swipe-up','swipe-down','swipe-left','swipe-right','click','double-click','long-press']);
});
test('CLI exchanges framed UTF-8 requests with the permission-checking handler', async t => {
const f = fixture(['assistant']);
const server = net.createServer({ allowHalfOpen: true }, socket => {
let text = '';
socket.setEncoding('utf8');
socket.on('data', async chunk => {
text += chunk;
if (!text.endsWith('\n')) return;
const response = JSON.stringify(await handleRequest(text.trimEnd(), f.tokens, f.host)) + '\n';
socket.write(response.slice(0, 4)); socket.end(response.slice(4));
});
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => server.close(resolve)));
const options = { host: '127.0.0.1', port: server.address().port, token: f.token };
await cli.send(options, { action: 'assistant', text: 'hello 世界 👋' });
assert.deepEqual(f.calls, [['assistant', 'hello 世界 👋']]);
await assert.rejects(cli.send(options, { action: 'input', gesture: 'click' }), /lacks input permission/);
});
test('interactive mode orders inputs and restores raw mode on Ctrl-C', async () => {
const input = new EventEmitter(); input.isTTY = true; input.isRaw = false;
const modes = []; input.setRawMode = value => modes.push(value); input.resume = () => {}; input.pause = () => {};
const sent = [];
const task = cli.interactive({}, input, { write: () => {} }, async (_opts, payload) => { if (payload.action === 'input') sent.push(payload.gesture); });
await new Promise(resolve => setImmediate(resolve));
input.emit('keypress', '', { name: 'up' });
input.emit('keypress', '', { name: 'return' });
await new Promise(resolve => setImmediate(resolve));
input.emit('keypress', '', { name: 'c', ctrl: true });
await task;
assert.deepEqual(sent, ['swipe-up', 'click']);
assert.deepEqual(modes, [true, false]);
assert.equal(input.listenerCount('keypress'), 0);
});
test('interactive terminal escape sequences distinguish arrows from standalone Esc', async () => {
const { PassThrough } = require('node:stream');
const input = new PassThrough(); input.isTTY = true;
input.setRawMode = () => {};
const sent = [];
const task = cli.interactive({}, input, { write() {} }, async (_opts, payload) => { if (payload.action === 'input') sent.push(payload.gesture); });
await new Promise(resolve => setImmediate(resolve));
input.write('\x1b[A\r\t');
await new Promise(resolve => setTimeout(resolve, 10));
input.write('\x1b');
await new Promise(resolve => setTimeout(resolve, 100));
input.write('\x03');
await task;
assert.deepEqual(sent, ['swipe-up', 'click', 'long-press', 'double-click']);
input.destroy();
});
test('quitting interactive mode cancels the in-flight request and drops queued keys', async () => {
const input = new EventEmitter(); input.isTTY = true;
const modes = []; input.setRawMode = value => modes.push(value); input.resume = input.pause = () => {};
let signal;
const task = cli.interactive({}, input, { write() {} }, (_opts, payload, abortSignal) => {
if (payload.action === 'ping') return Promise.resolve();
signal = abortSignal;
return new Promise((_resolve, reject) => signal.addEventListener('abort', () => reject(new Error('cancelled'))));
});
await new Promise(resolve => setImmediate(resolve));
input.emit('keypress', '', { name: 'up' });
input.emit('keypress', '', { name: 'down' });
input.emit('keypress', '', { name: 'c', ctrl: true });
await task;
assert.equal(signal.aborted, true);
assert.deepEqual(modes, [true, false]);
});
test('text -n preserves the message and suppresses terminal submission only', async () => {
const f = fixture(); const env = { FACECLAW_TOKEN: f.token };
const received = []; f.host.text = (text, submit) => received.push({ text, submit });
for (const args of [['text', 'hello'], ['text', '-n', 'hello'], ['text', '-n', 'line one\nline two'], ['text', '--', '-n']]) {
const { payload } = cli.parseArgs(args, env);
assert.equal((await f.request(payload)).ok, true);
}
assert.deepEqual(received, [{ text: 'hello', submit: true }, { text: 'hello', submit: false },
{ text: 'line one\nline two', submit: false }, { text: '-n', submit: true }]);
assert.throws(() => cli.parseArgs(['assistant', '-n', 'hello'], env), /only valid with text/);
assert.equal((await f.request({ action: 'text', text: 'hello', submit: 'false' })).error, 'bad_request');
});
test('ping authenticates and checks input permission without generating an input or requiring connected glasses', async () => {
const f = fixture(['input']); f.host.ready = () => false;
assert.deepEqual(await f.request({ action: 'ping', permission: 'input' }), { ok: true });
assert.equal((await f.request({ action: 'ping', token: 'bad' })).error, 'unauthorized');
f.tokens.permissions(f.record.id, ['text']);
assert.equal((await f.request({ action: 'ping', permission: 'input' })).error, 'forbidden');
assert.deepEqual(f.calls, []);
});
test('interactive checks connectivity immediately, reports failures and remains alive until Ctrl-C', async () => {
const input = new EventEmitter(); input.isTTY = true; input.setRawMode = input.resume = input.pause = () => {};
const requests = [], output = []; let stopped = false;
const task = cli.interactive({}, input, { write: text => output.push(text) }, async (_opts, payload) => {
requests.push(payload); throw new Error('Connection refused');
}).then(() => { stopped = true; });
await new Promise(resolve => setImmediate(resolve));
assert.deepEqual(requests, [{ action: 'ping' }]);
assert.match(output.join(''), /Connection refused/); assert.equal(stopped, false);
input.emit('keypress', '', { name: 'c', ctrl: true }); await task;
});
test('a lost response does not end interactive mode or replay an input on reconnect', async () => {
const input = new EventEmitter(); input.isTTY = true; input.setRawMode = input.resume = input.pause = () => {};
const requests = [], output = []; let fail = true, stopped = false;
const task = cli.interactive({}, input, { write: text => output.push(text) }, async (_opts, payload) => {
requests.push(payload);
if (payload.action === 'input' && fail) { fail = false; throw new Error('Faceclaw closed the connection without a response.'); }
}).then(() => { stopped = true; });
try {
await new Promise(resolve => setImmediate(resolve));
input.emit('keypress', '', { name: 'up' });
await new Promise(resolve => setImmediate(resolve));
assert.equal(stopped, false); assert.match(output.join(''), /without a response/);
input.emit('keypress', '', { name: 'down' }); // Discarded while offline.
await new Promise(resolve => setTimeout(resolve, 3100));
input.emit('keypress', '', { name: 'return' });
await new Promise(resolve => setImmediate(resolve));
assert.deepEqual(requests.filter(r => r.action === 'input').map(r => r.gesture), ['swipe-up', 'click']);
} finally { input.emit('keypress', '', { name: 'c', ctrl: true }); await task; }
});
test('client keeps the sending side open until the reply, including across a forwarding proxy', async t => {
let prematureEnd = false;
const server = net.createServer(socket => {
let text = '', replied = false;
socket.on('end', () => { if (!replied) prematureEnd = true; });
socket.on('data', chunk => {
text += chunk.toString();
if (text.includes('\n')) setTimeout(() => { replied = true; if (!socket.destroyed) socket.end('{"ok":true}\n'); }, 25);
});
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => server.close(resolve)));
const options = { host: '127.0.0.1', port: server.address().port, token: fixture().token };
for (let i = 0; i < 4; i++) await cli.send(options, { action: 'input', gesture: 'click' });
assert.equal(prematureEnd, false);
});
+40
View File
@@ -0,0 +1,40 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { listenerAddresses, createListenerPool } = require('../.test-build/app/remote/listeners.js');
const address = (name, address, pointToPoint = true) => ({ name, address, pointToPoint });
const interfaces = [address('wlan0', '192.168.1.2', false), address('wlan0', 'fd7a:115c:a1e0::2', false),
address('rmnet0', '100.70.1.2'), address('tun0', '100.70.2.3'), address('tun0', 'fd7a:115c:a1e0::1234'),
address('utun2', '10.0.1.2'), address('utun2', 'fe80::1')];
test('automatic detection selects only tailnet addresses of the Tailscale tunnel, plus localhost', () => {
assert.deepEqual(listenerAddresses(interfaces, 'tailscale'), ['100.70.2.3', '127.0.0.1', 'fd7a:115c:a1e0::1234']);
assert.deepEqual(listenerAddresses([address('tun1', '100.70.1.2')], 'tailscale'), ['127.0.0.1']);
assert.deepEqual(listenerAddresses([address('tailscale0', '100.70.1.2', false)], 'tailscale'), ['100.70.1.2', '127.0.0.1']);
});
test('explicit tunnel selection persists by name; absent tunnel never falls back to LAN or wildcard', () => {
assert.deepEqual(listenerAddresses(interfaces, 'interface:tun0'), ['100.70.2.3', '127.0.0.1', 'fd7a:115c:a1e0::1234']);
assert.deepEqual(listenerAddresses(interfaces, 'interface:utun2'), ['10.0.1.2', '127.0.0.1']);
for (const choice of ['localhost', 'interface:wlan0', 'interface:rmnet0', 'interface:missing', '0.0.0.0'])
assert.deepEqual(listenerAddresses(interfaces, choice), ['127.0.0.1']);
assert.deepEqual(listenerAddresses([address('tun0', '0.0.0.0'), address('tun0', '::')], 'interface:tun0'), ['127.0.0.1']);
});
test('listener pool preserves USB, adds/removes tunnel sockets and routes replies with unique IDs', () => {
const sockets = [];
const pool = createListenerPool(() => {
const socket = { start: (port, address) => { socket.address = address; return ''; }, stop: () => { socket.stopped = true; },
nextRequest: () => socket.pending ? (socket.pending = false, JSON.stringify({ id: 1, body: '{}', expiresAt: Date.now() + 5000 })) : null,
complete: (id, response) => socket.reply = { id, response }, pending: true };
sockets.push(socket); return socket;
});
pool.start(8791, ['127.0.0.1']);
pool.start(8791, ['127.0.0.1', '100.70.2.3']);
assert.equal(sockets.length, 2);
const first = JSON.parse(pool.nextRequest()), second = JSON.parse(pool.nextRequest());
assert.notEqual(first.id, second.id);
pool.complete(first.id, 'first'); pool.complete(second.id, 'second');
assert.deepEqual(sockets.map(s => s.reply.response), ['first', 'second']);
pool.start(8791, ['127.0.0.1']);
assert.equal(sockets[0].stopped, undefined); assert.equal(sockets[1].stopped, true);
pool.start(8791, ['127.0.0.1', '100.70.2.3']);
pool.complete(second.id, 'late'); assert.equal(sockets[2].reply, undefined);
pool.stop(); assert.ok(sockets.every(s => s.stopped));
});
+43
View File
@@ -0,0 +1,43 @@
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 crypto = require('node:crypto');
const { parseArgs } = require('../scripts/faceclaw-input.cjs');
const { TokenStore, handleRequest } = require('../.test-build/app/remote/protocol.js');
function evaluate(source, context) {
vm.runInNewContext(ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
}
test('CLI text and text -n reach the real terminal client with exactly one Enter or none', async () => {
const sent = [], native = { exports: {}, require: () => ({}), global: { isIOS: true }, NSUTF8StringEncoding: 4,
NSString: { stringWithString: text => ({ lengthOfBytesUsingEncoding: () => Buffer.byteLength(text),
dataUsingEncoding: () => ({ base64EncodedStringWithOptions: () => Buffer.from(text).toString('base64') }) }) } };
evaluate(fs.readFileSync('app/native/g2mirror-client.ts', 'utf8'), native);
const client = new native.exports.G2MirrorClient({});
client.ws = { sendText: json => sent.push(JSON.parse(json)) };
client.handleMessage(JSON.stringify({ type: 'connect', command: 'test' }));
// Run the production worker message handler, without starting its network/render loops.
const source = ts.createSourceFile('worker.ts', fs.readFileSync('app/apps/terminal/terminal-app.worker.ts', 'utf8'), ts.ScriptTarget.Latest, true);
const handler = source.statements.find(node => ts.isExpressionStatement(node) && ts.isBinaryExpression(node.expression) && node.expression.left.getText(source) === 'global.onmessage');
assert.ok(handler);
const worker = { global: {}, windows: new Map([['terminal', { kind: 'view', client }]]) };
evaluate(handler.getText(source), worker);
let stored = '[]';
const tokens = new TokenStore(() => stored, value => { stored = value; }, () => 'a'.repeat(64),
value => crypto.createHash('sha256').update(value).digest('hex'));
const { token } = tokens.create('CLI', ['text']);
const host = { ready: () => true, locked: () => false, acceptsText: () => true,
text: (text, submit) => worker.global.onmessage({ data: { type: 'text-input', windowId: 'terminal', text, submit } }) };
for (const noSubmit of [false, true]) {
const text = 'café 世界';
const args = parseArgs(['text', ...(noSubmit ? ['-n'] : []), text], { FACECLAW_TOKEN: token });
assert.equal((await handleRequest(JSON.stringify({ version: 1, token, ...args.payload }), tokens, host)).ok, true);
const message = sent.at(-1);
assert.equal(Buffer.from(message.data, 'base64').toString('utf8'), text + (noSubmit ? '' : '\r'));
assert.equal(message.delays === undefined, noSubmit);
}
});
+2
View File
@@ -11,6 +11,8 @@
"outDir": "../.test-build"
},
"include": [
"../app/remote/protocol.ts",
"../app/remote/listeners.ts",
"../app/g2/ancs-client.ts",
"../app/g2/ble-protocol.ts",
"../app/graphics/lvgl-font.ts",