This commit is contained in:
James Babcock committed 2026-10-10 17:34:46 -07:00
commit 5df873e5b1
73 files changed
+3906 -98

No files matched your search

+205
View File
@@ -0,0 +1,205 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const DAY_MS = 24 * 60 * 60 * 1000;
const HOUR_MS = 60 * 60 * 1000;
const START = Date.parse('2026-10-10T12:00:00Z');
/** analytics-store.ts over in-memory files, a settable clock and setting, and a fake server. */
function harness({ level = 'minimal', version = '0.8.3', statuses = [] } = {}) {
const files = new Map();
const sent = [];
const h = {
files, sent, level, version, now: START, statuses: [...statuses],
pendingSend: null,
snapshots: { minimal: { 'device.g2.paired': true }, full: { 'apikey.anthropic': true } },
};
let nextId = 1;
const { AnalyticsStore } = loader({
setTimeout: () => 0,
clearTimeout: () => {},
})('app/analytics/analytics-store.ts');
h.store = new AnalyticsStore({
storage: {
read: name => files.get(name) ?? null,
write: (name, text) => files.set(name, text),
remove: name => files.delete(name),
list: () => [...files.keys()],
removeAll: () => files.clear(),
},
level: () => h.level,
appInfo: () => ({ version: h.version, platform: 'android', build: 'official' }),
now: () => h.now,
randomId: () => `00000000-0000-4000-8000-${String(nextId++).padStart(12, '0')}`,
snapshot: level => ({ ...h.snapshots[level] }),
send: async body => {
sent.push(JSON.parse(body));
if (h.pendingSend) await h.pendingSend;
const status = h.statuses.length ? h.statuses.shift() : 200;
if (status === 'network') throw new Error('offline');
return status;
},
});
h.outbox = () => [...files.keys()].filter(name => name.startsWith('outbox-'));
h.state = () => JSON.parse(files.get('state.json'));
return h;
}
test('Analytics keeps only what the data-collection setting allows', () => {
const h = harness({ level: 'minimal' });
h.store.count('minimal', 'g2.connect');
h.store.count('full', 'app.launch.timer');
h.store.flush();
const pending = JSON.parse(h.files.get('pending.json'));
assert.deepEqual(pending.counters.minimal, { '2026-10-10': { 'g2.connect': 1 } });
assert.deepEqual(pending.counters.full, {});
const off = harness({ level: 'none' });
off.store.count('minimal', 'g2.connect');
off.store.flush();
off.store.applyLevel();
assert.equal(off.files.size, 0);
});
test('Analytics drops bad counter names and flags count once a day', () => {
const h = harness();
h.store.count('minimal', 'Bad Name');
h.store.flag('minimal', 'device.r1.used');
h.store.flag('minimal', 'device.r1.used');
h.store.count('minimal', 'g2.ack-timeout', 3);
h.store.flush();
assert.deepEqual(JSON.parse(h.files.get('pending.json')).counters.minimal['2026-10-10'], {
'device.r1.used': 1, 'g2.ack-timeout': 3,
});
});
test('Analytics seals and uploads a report once a day', async () => {
const h = harness({ level: 'full' });
h.store.count('minimal', 'g2.connect', 2);
h.store.count('full', 'feature.used');
await h.store.uploadIfDue();
assert.equal(h.sent.length, 1);
const [report] = h.sent;
assert.equal(report.schema, 1);
assert.equal(report.level, 'full');
assert.match(report.installId, /^00000000-/);
assert.deepEqual(report.app, { version: '0.8.3', platform: 'android', build: 'official' });
assert.deepEqual(report.counters, { '2026-10-10': { 'g2.connect': 2, 'feature.used': 1 } });
assert.deepEqual(report.snapshot, { 'device.g2.paired': true, 'apikey.anthropic': true });
assert.deepEqual(h.outbox(), []);
h.store.count('minimal', 'g2.connect');
h.now += 3 * HOUR_MS;
await h.store.uploadIfDue();
assert.equal(h.sent.length, 1, 'not due again the same day');
h.now = START + DAY_MS;
await h.store.uploadIfDue();
assert.equal(h.sent.length, 2);
assert.equal(h.sent[1].installId, report.installId);
assert.notEqual(h.sent[1].reportId, report.reportId);
assert.deepEqual(h.sent[1].counters, { '2026-10-10': { 'g2.connect': 1 } });
});
test('Analytics retries a failed upload hourly with the same report id, and drops rejected ones', async () => {
const h = harness({ statuses: ['network', 503] });
h.store.count('minimal', 'g2.connect');
await h.store.uploadIfDue();
assert.equal(h.outbox().length, 1);
h.now += 30 * 60 * 1000;
await h.store.uploadIfDue();
assert.equal(h.sent.length, 1, 'no retry within the hour');
h.now = START + HOUR_MS;
await h.store.uploadIfDue();
assert.equal(h.sent.length, 2);
assert.equal(h.sent[1].reportId, h.sent[0].reportId);
assert.equal(h.outbox().length, 1, 'kept after a 503');
h.now = START + 2 * HOUR_MS;
await h.store.uploadIfDue();
assert.equal(h.sent.length, 3);
assert.deepEqual(h.outbox(), []);
assert.equal(h.state().retryAt, 0);
const rejected = harness({ statuses: [400] });
await rejected.store.uploadIfDue();
assert.deepEqual(rejected.outbox(), [], 'a 400 is not retried');
});
test('Analytics turned off deletes everything, and a new install id is made when it comes back', async () => {
const h = harness();
h.store.count('minimal', 'g2.connect');
await h.store.uploadIfDue();
const firstInstall = h.sent[0].installId;
h.store.count('minimal', 'g2.connect');
h.store.flush();
h.level = 'none';
h.store.applyLevel();
assert.equal(h.files.size, 0);
await h.store.uploadIfDue();
assert.equal(h.sent.length, 1, 'no connection while off');
h.level = 'minimal';
h.now += DAY_MS;
await h.store.uploadIfDue();
assert.equal(h.sent.length, 2);
assert.notEqual(h.sent[1].installId, firstInstall);
assert.deepEqual(h.sent[1].counters, {}, 'counts from before turning off are gone');
});
test('Analytics turned down to minimal drops what was kept for full', async () => {
const h = harness({ level: 'full', statuses: ['network'] });
h.store.count('minimal', 'g2.connect');
h.store.count('full', 'feature.used');
await h.store.uploadIfDue();
h.store.count('full', 'feature.used');
h.store.flush();
h.level = 'minimal';
h.store.applyLevel();
assert.deepEqual(JSON.parse(h.files.get('pending.json')).counters.full, {});
const [outbox] = h.outbox();
assert.deepEqual(JSON.parse(h.files.get(outbox)).snapshot.full, {});
h.now += HOUR_MS;
await h.store.uploadIfDue();
const retried = h.sent[1];
assert.equal(retried.level, 'minimal');
assert.deepEqual(retried.counters, { '2026-10-10': { 'g2.connect': 1 } });
assert.deepEqual(retried.snapshot, { 'device.g2.paired': true });
});
test('Analytics sends counts from before an upgrade under the old version', async () => {
const h = harness({ version: '0.8.3' });
h.store.count('minimal', 'g2.connect');
h.store.flush();
// A new process after the upgrade.
const files = h.files;
const upgraded = harness({ version: '0.8.4' });
for (const [name, text] of files) upgraded.files.set(name, text);
upgraded.store.count('minimal', 'g2.connect', 5);
await upgraded.store.uploadIfDue();
assert.deepEqual(upgraded.sent.map(report => [report.app.version, report.counters['2026-10-10']]), [
['0.8.3', { 'g2.connect': 1 }],
['0.8.4', { 'g2.connect': 5 }],
]);
});
test('Analytics turned off and on during an upload keeps none of the old state', async () => {
const h = harness();
let release;
h.pendingSend = new Promise(resolve => { release = resolve; });
const upload = h.store.uploadIfDue();
await Promise.resolve();
h.level = 'none';
h.store.applyLevel();
h.level = 'minimal';
release();
await upload;
assert.equal(h.files.size, 0, 'the old install id is not written back');
});
+27 -21
View File
@@ -269,31 +269,37 @@ test('iOS compositor rejects missing and removed surfaces before crossing into K
assert.equal(nativeSubmissions, 1);
});
test('iOS welcome sound waits for a new acknowledged frame and is consumed once', async () => {
let pending = true;
const played = [], sounds = load('app/ui/sound-effects.ts', { require: name => { assert.equal(name, '../g2/cfw-message-type'); return load('app/g2/cfw-message-type.ts', {}); } });
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', {
test('iOS opens the on-glasses onboarding on a new acknowledged frame, once per run while due', () => {
const settings = new Map();
const progress = load('app/apps/onboarding/onboarding-progress.ts', {
require: id => ({
'../phone-ui/onboarding-state': { isWelcomeSoundPending: () => pending, setWelcomeSoundPending: value => { pending = value; } },
'../ui/sound-effects': sounds,
})[id] ?? {},
setTimeout: fn => { fn(); return 1; },
'../../native/settings-store': {
getStringSetting: (key, fallback) => settings.get(key) ?? fallback,
setStringSetting: (key, value) => settings.set(key, value),
},
'../../version': { FACECLAW_VERSION: '9.9.9' },
})[id],
});
const launched = [];
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', {
require: id => ({ '../apps/onboarding/onboarding-progress': progress })[id] ?? {},
});
const host = Object.create(IosPreviewController.prototype);
host.acknowledgedFrames = 0;
host.actions = { playBuzzerSequence: bytes => played.push([...bytes]) };
host.logBluetooth = assert.fail;
host.maybePlayWelcomeSound({ phase: 'connecting', frames: 0 });
host.maybePlayWelcomeSound({ phase: 'connected', frames: 0 });
assert.equal(pending, true); assert.equal(played.length, 0);
host.maybePlayWelcomeSound({ phase: 'connected', frames: 1 });
assert.equal(pending, false);
await new Promise(resolve => setImmediate(resolve));
assert.ok(played.length > 0);
const count = played.length;
host.maybePlayWelcomeSound({ phase: 'connected', frames: 1 });
host.maybePlayWelcomeSound({ phase: 'connected', frames: 2 });
assert.equal(played.length, count);
host.launchApp = id => { launched.push(id); return Promise.resolve(); };
host.maybeLaunchOnboarding({ phase: 'connecting', frames: 0 });
host.maybeLaunchOnboarding({ phase: 'connected', frames: 0 });
assert.deepEqual(launched, []);
host.maybeLaunchOnboarding({ phase: 'connected', frames: 1 });
assert.deepEqual(launched, ['onboarding']);
// Still unfinished, but a reconnect in the same run doesn't reopen it.
host.maybeLaunchOnboarding({ phase: 'connected', frames: 2 });
assert.deepEqual(launched, ['onboarding']);
assert.equal(progress.isGlassesOnboardingDue(), true);
progress.markGlassesOnboardingFinished();
assert.equal(progress.isGlassesOnboardingDue(), false);
settings.set('onboarding.glassesFinishedVersion', '9.9.8');
assert.equal(progress.isGlassesOnboardingDue(), true, 'an upgrade makes onboarding due again');
});
test('iOS preview voice uses the phone microphone, while connected capture stays on glasses', async () => {
+1 -2
View File
@@ -153,8 +153,7 @@ function fixture() {
'../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': {}, '../apps/evenhub/updates': {}, '../apps/evenhub': {},
'../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' },
'../apps/all-apps': { ALL_APPS: [{ appId: 'glanceboard', glanceboard: provider }] },
'../phone-ui/onboarding-state': { isWelcomeSoundPending: () => false },
'../ui/sound-effects': {},
'../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' } },
'../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) },
@@ -0,0 +1,33 @@
package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertEquals
class ConnectionCountersTest {
@Test
fun drainReturnsTheCountsSinceTheLastDrain() {
ConnectionCounters.drain()
ConnectionCounters.increment("g2.connect-attempt")
ConnectionCounters.increment("g2.connect-attempt")
ConnectionCounters.increment("g2.session-ready")
assertEquals("""{"g2.connect-attempt":2,"g2.session-ready":1}""", ConnectionCounters.drain())
assertEquals("{}", ConnectionCounters.drain())
}
@Test
fun keepsCountingKnownNamesPastTheNameCap() {
ConnectionCounters.drain()
for (index in 0 until 250) ConnectionCounters.increment("name-$index")
ConnectionCounters.increment("name-0")
val drained = Json.parseObject(ConnectionCounters.drain())
assertEquals(200, drained.size)
assertEquals(JsonNumber("2"), drained["name-0"])
}
@Test
fun slugsFailureReasons() {
assertEquals("cfw-recovery-retry-limit", ConnectionCounters.slug("CFW recovery retry limit"))
assertEquals("left-arm-not-found", ConnectionCounters.slug("left arm not found"))
assertEquals("evenhub-resume-prelude-failed", ConnectionCounters.slug("EvenHub resume prelude failed"))
}
}