import './setup_env'; import { expect, test } from 'bun:test'; import { usageQuery, validateUsage } from '../src/eddy_usage_rules'; import { policy, ANONYMOUS_OWNER } from '../src/eddy_ingestion'; const fixture = () => ({schema: 1, batchId: 'a'.repeat(64), events: [{id: 'b'.repeat(32), installation: 'c'.repeat(32), at: Date.now(), name: 'operation.completed', action: 'task.save', source: 'cli', release: '2026.11.03.1', macosMajor: '27', outcome: 'success', durationMs: 30}]}); test('usage accepts only fixed names, small numbers and pseudonymous ids', () => { const parsed = validateUsage(fixture()); expect(parsed.events[0].action).toBe('task.save'); for (const change of [{name: 'private log'}, {action: 'PRIVATE USER INPUT'}, {email: 'private@example.com'}, {path: '/private'}, {text: 'private'}, {durationMs: -1}, {durationMs: true}, {at: 1}, {at: Date.now() + 3600_000}, {installation: 'email@example.com'}, {outcome: undefined}, {source: 'arbitrary'}]) expect(() => validateUsage({...fixture(), events: [{...fixture().events[0], ...change}]})).toThrow(); }); test('oversized, repeated and invalid heartbeat batches fail atomically', () => { for (const events of [[], Array(101).fill(fixture().events[0]), [fixture().events[0], fixture().events[0]], [{...fixture().events[0], name: 'engagement', action: 'portal', durationMs: 60001}], [{...fixture().events[0], name: 'engagement', action: 'portal', durationMs: undefined}]]) expect(() => validateUsage({...fixture(), events})).toThrow(); }); test('metrics limit time range and keep release filters literal', () => { expect(usageQuery({}).days).toBe(28); expect(usageQuery({days: '7', release: '2026.11.03.1'}).release).toBe('2026.11.03.1'); for (const query of [{days: '0'}, {days: '91'}, {days: '1.5'}, {release: "'; drop table x"}]) expect(() => usageQuery(query)).toThrow(); }); const v2 = (changes: Record = {}) => ({schema: 2, batchId: 'a'.repeat(64), events: [{...fixture().events[0], device: '0123456789abcdef', deviceSource: 'hardware', env: 'production', appVersion: '0.9.13+15', ...changes}]}); test('schema 2 names the device, accepts only production and keeps props closed per event', () => { const parsed = validateUsage(v2()); expect(parsed.schema).toBe(2); expect(parsed.events[0].device).toBe('0123456789abcdef'); expect(parsed.events[0].appVersion).toBe('0.9.13+15'); const shell = (name: string, action: string, extra: Record = {}) => v2({name, action, source: 'shell', outcome: undefined, durationMs: undefined, session: 'd'.repeat(32), ...extra}); for (const ok of [ shell('session.started', 'shell', {props: {firstLaunch: true, reason: 'document'}}), shell('engagement', 'shell', {durationMs: 300000}), shell('view.opened', 'desk'), shell('view.opened', 'settings.storage'), shell('home.shown', 'shell', {durationMs: 4000, props: {firstLaunch: false}}), shell('runtime.prepare', 'first-launch', {outcome: 'failure', durationMs: 60000, props: {downloadMs: 41000, failure: 'network'}}), shell('permission.flow', 'screen-recording', {props: {step: 'denied'}}), shell('update.flow', 'runtime', {props: {step: 'applied', from: '2026.11.12-eddy-home.35', to: '2026.11.12-eddy-home.36', waitedMs: 9000}}), shell('device.profile', 'shell', {props: {model: 'Mac15,6', chip: 'm3', memoryGB: 36, macos: '26.6', language: 'zh', location: 'applications'}}), v2({name: 'operation.completed', action: 'task.export', source: 'shell', props: {reuse: 'file', media: true}}), v2({name: 'operation.completed', action: 'agent.configure', source: 'shell', props: {agent: 'claude'}}), ]) expect(() => validateUsage(ok)).not.toThrow(); for (const bad of [ v2({env: 'test'}), v2({env: undefined}), v2({device: undefined}), v2({device: 'email@example.com'}), v2({deviceSource: 'serial'}), v2({appVersion: 'PRIVATE'}), v2({props: {reuse: 'file'}}), // task.save has no props v2({name: 'operation.completed', action: 'task.export', props: {reuse: 'file', path: '/private'}}), v2({name: 'operation.completed', action: 'task.export', props: {reuse: 'all'}}), shell('permission.flow', 'accessibility'), // step is required shell('device.profile', 'shell', {props: {model: 'Kong’s MacBook'}}), shell('update.flow', 'app', {props: {step: 'offered', to: 'a b'}}), shell('runtime.prepare', 'first-launch'), // outcome is required shell('engagement', 'shell', {durationMs: 300001}), shell('view.opened', 'PRIVATE'), shell('home.shown', 'portal'), v2({name: 'home.shown', action: 'shell', source: 'cli'}), // only the Mac app records these v2({name: 'view.opened', action: 'desk', source: 'portal', session: 'd'.repeat(32)}), // not a Portal page ]) expect(() => validateUsage(bad)).toThrow(); // Schema 1 stays as it was: no schema 2 fields, no shell source. expect(() => validateUsage({...fixture(), events: [{...fixture().events[0], device: '0123456789abcdef'}]})).toThrow(); expect(() => validateUsage({...fixture(), events: [{...fixture().events[0], source: 'shell'}]})).toThrow(); }); test('P1: the desk, previews, docking, the Agent entry, failure cards, crashes and storage cleaning stay closed', () => { const shell = (name: string, action: string, extra: Record = {}) => v2({name, action, source: 'shell', outcome: undefined, durationMs: undefined, session: 'd'.repeat(32), ...extra}); for (const ok of [ shell('desk.action', 'record', {outcome: 'success', durationMs: 42000}), shell('preview.change', 'language', {outcome: 'success', durationMs: 21000, props: {value: 'ar'}}), shell('preview.change', 'mock', {outcome: 'failure', props: {value: 'long', on: true}}), shell('dock.attach', 'detached', {durationMs: 600000}), shell('agent.entry', 'terminal', {props: {agent: 'codex', element: false}}), shell('notice.shown', 'failure', {props: {kind: 'space'}}), shell('notice.action', 'give-up', {props: {kind: 'install'}}), v2({name: 'app.crash', action: 'app', source: 'runtime', outcome: undefined, durationMs: undefined, props: {class: 'devicesim', restart: 'restarted'}}), v2({name: 'operation.completed', action: 'storage.clean', source: 'shell', props: {what: 'app-cache', freedMB: 9}}), v2({name: 'operation.completed', action: 'storage.tidy', source: 'shell'}), ]) expect(() => validateUsage(ok)).not.toThrow(); for (const bad of [ shell('preview.change', 'language', {props: {value: 'pt-BR'}}), shell('agent.entry', 'app', {props: {agent: '/Users/x/claude'}}), shell('notice.shown', 'failure'), shell('notice.shown', 'failure', {props: {kind: '没能截图'}}), shell('desk.action', 'copy-color'), v2({name: 'app.crash', action: 'app', source: 'shell', props: {class: 'app'}}), v2({name: 'app.crash', action: 'app', source: 'runtime', props: {signature: 'abc'}}), v2({name: 'operation.completed', action: 'storage.clean', props: {what: 'app-cache:tiktokm'}}), ]) expect(() => validateUsage(bad)).toThrow(); }); test('unsigned uploads share one budget, larger than an account\'s and never more than half the global one', () => { const saved = {...process.env}; try { process.env.EDDY_USAGE_GLOBAL_BYTES = '1000000'; process.env.EDDY_USAGE_GLOBAL_RPM = '100'; process.env.EDDY_CONVERSATION_GLOBAL_BYTES = '1000000'; const account = policy('usage', 'someone@example.com'), anonymous = policy('usage', ANONYMOUS_OWNER); expect(account.ownerRpm).toBe(60); expect(anonymous.ownerBytes).toBe(500000); expect(anonymous.ownerRpm).toBe(50); // halves of the global ones expect(anonymous.dailyRows).toBeGreaterThan(account.dailyRows); expect(policy('conversation', ANONYMOUS_OWNER).ownerRpm).toBe(60); // only usage has the shared key } finally { process.env = saved; } });