import './setup_env'; import { afterAll, expect, mock, test } from 'bun:test'; import { createHash } from 'node:crypto'; mock.module('../src/cluster', () => ({ allEnrichedNodes: async () => [], callNodeAbsolute: async () => { throw new Error('No runtime execution'); } })); mock.module('../src/sso', () => ({ resolveSsoEmail: async (headers: Headers) => { const name = headers.get('x-tt-session-v2'); if (name === 'fixture-a-other-domain') return 'fixture-a@elsewhere.example'; return ['fixture-a', 'fixture-b', 'fixture-admin'].includes(name || '') ? `${name}@example.com` : null; } })); mock.module('../src/admin_db', () => ({ isAdminPrefix: async (s: string) => s === 'fixture-admin', normPrefix: (s: string) => s.split('@')[0] })); const run = process.env.TEST_DATABASE_URL ? test : test.skip; const { eddyApi } = await import('../src/routes_eddy'); const { db, closePool } = await import('../src/db_client'); const { eddy_conversation_events, eddy_sso_sessions } = await import('../src/schema'); const { eq, inArray } = await import('drizzle-orm'); const token = 'eddy_sso_' + 'z'.repeat(43); const tokenHash = createHash('sha256').update(token).digest('hex'); const hash = (s: string) => createHash('sha256').update(s).digest('hex'); const body = () => ({ schema: 1, batchId: hash('batch'), taskId: 'fixture-task', taskTitle: 'Synthetic fixture', sourceId: hash('fixture source'), provider: 'codex', machine: 'f'.repeat(16), events: ['same', 'same', '抠图🧪'.repeat(2000)].map((text, i) => ({ id: hash('event' + i), position: i, block: 0, part: 0, parts: 1, kind: 'message', role: 'user', text, redacted: false })) }); const postOnce = (value: any, headers = {}) => eddyApi.request('/conversations', { method: 'POST', headers: { 'Content-Type': 'application/json', ...headers }, body: JSON.stringify(value) }); const post = async (value: any, headers = {}) => { for (let i = 0; ; i++) { const response = await postOnce(value, headers); if (i >= 20 || response.status !== 429 || (await response.clone().json() as any).code !== 'eddy_rate_limited') return response; await Bun.sleep(20); } }; const read = (headers = {}, query = '') => eddyApi.request('/conversations' + query, { headers }); const a = { 'x-tt-session-v2': 'fixture-a' }, b = { 'x-tt-session-v2': 'fixture-b' }; run('verified SSO owns private transcripts; retries and concurrent deliveries are idempotent', async () => { const value = body(); expect((await post(value)).status).toBe(401); expect((await post(value, { 'x-eddy-user': 'fixture-a' })).status).toBe(401); expect((await post(value, { authorization: 'Bearer legacy-release-key' })).status).toBe(401); expect((await read()).status).toBe(401); const responses = await Promise.all([post(value, a), post(value, a), post(value, a)]); for (const response of responses) { expect(response.status).toBe(200); expect(response.headers.get('cache-control')).toBe('no-store'); expect(await response.json()).toEqual({ batchId: value.batchId, accepted: 3 }); } const response = await read(a); expect(response.headers.get('cache-control')).toBe('no-store'); const data: any = await response.json(); expect(data.events.map((e: any) => e.event.text)).toEqual(value.events.map(e => e.text)); expect(data.events.every((e: any) => e.taskId === 'fixture-task')).toBe(true); for (const headers of [b, { 'x-tt-session-v2': 'fixture-admin' }, { 'x-tt-session-v2': 'fixture-a-other-domain' }]) { expect((await (await read(headers)).json() as any).events).toEqual([]); } // User B can upload the same client ids: server ownership participates in the key. expect((await post(value, b)).status).toBe(200); expect((await (await read(b)).json() as any).events.length).toBe(3); }); run('Eddy session uses verified full email; revocation and expiry stop access', async () => { await db.insert(eddy_sso_sessions).values({ tokenHash, email: 'fixture-a@example.com', expiresAt: Date.now() + 60_000 }); const headers = { authorization: 'Bearer ' + token, 'x-eddy-user': 'fixture-b' }; const response: any = await (await read(headers)).json(); expect(response.events.length).toBe(3); const value = { ...body(), taskId: 'another-task' }; expect((await post(value, headers)).status).toBe(200); const filtered: any = await (await read(a, '?task=another-task')).json(); expect(filtered.events.length).toBe(3); await db.update(eddy_sso_sessions).set({ expiresAt: 1 }).where(eq(eddy_sso_sessions.tokenHash, tokenHash)); expect((await post(value, headers)).status).toBe(401); expect((await read(headers)).status).toBe(401); }); run('bounded pagination and size/schema errors do not leak or partially write transcripts', async () => { let cursor: string | null = '0'; const ids = []; do { const response: any = await (await read(a, `?task=fixture-task&limit=2&cursor=${cursor}`)).json(); ids.push(...response.events.map((e: any) => e.event.id)); cursor = response.nextCursor; } while (cursor); expect(ids).toEqual(body().events.map(e => e.id)); expect((await read(a, '?source=' + hash('other source'))).status).toBe(200); for (const query of ['?limit=0', '?cursor=-1', '?task=../bad']) expect((await read(a, query)).status).toBe(400); expect((await post({ ...body(), events: [{ ...body().events[0], role: 'system' }] }, a)).status).toBe(400); expect((await post({ ...body(), taskTitle: 'a'.repeat(600 * 1024) }, a)).status).toBe(413); expect((await (await read(a, '?task=fixture-task')).json() as any).events.length).toBe(3); }); const runEddy = process.env.TEST_DATABASE_URL && process.env.EDDY_TEST_SOURCE ? test : test.skip; run('task/session browser lists are private, paginated and separate identical ids on different Macs', async () => { const list = (path: string, headers: Record = a) => eddyApi.request('/conversations/' + path, {headers}); const add = async (taskId: string, machine: string, source: string, title: string, event: string) => { const v = body(); const value = {...v, taskId, machine, sourceId: hash(source), taskTitle: title, events: [{...v.events[0], id: hash(event), position: Number(event.endsWith('2')), text: 'Private body should not enter task list'}]}; expect((await post(value, a)).status).toBe(200); }; const mac = 'f'.repeat(16), other = 'e'.repeat(16); await add('list-shared', mac, 'one', '旧名称', 'list1'); await add('list-shared', mac, 'two', '最新名称', 'list2'); await add('list-shared', other, 'one', '另一台 Mac', 'list3'); await add('list-other', mac, 'one', 'Literal 100%_新', 'list4'); for (const path of ['tasks', `sources?task=list-shared&machine=${mac}`]) { expect((await list(path, {})).status).toBe(401); expect((await list(path, {'x-eddy-user': 'fixture-a'})).status).toBe(401); for (const headers of [b, {'x-tt-session-v2': 'fixture-admin'}, {'x-tt-session-v2': 'fixture-a-other-domain'}]) expect((await (await list(path + (path.includes('?') ? '&' : '?') + 'q=list-', headers)).json() as any).items).toEqual([]); } let cursor: string | null = null; const found: any[] = []; do { const r = await list('tasks?q=list-&limit=1' + (cursor ? '&before=' + cursor : '')); expect(r.headers.get('cache-control')).toBe('no-store'); const p: any = await r.json(); found.push(...p.items); cursor = p.nextCursor; } while (cursor); expect(found.length).toBe(3); expect(new Set(found.map(t => t.machine + ':' + t.taskId)).size).toBe(3); const task = found.find(t => t.machine === mac && t.taskId === 'list-shared'); expect(task.taskTitle).toBe('最新名称'); expect(task.sourceCount).toBe(2); expect(task.recordCount).toBe(2); expect(JSON.stringify(found)).not.toContain('Private body'); const literal: any = await (await list('tasks?q=%25_')).json(); expect(literal.items.map((t: any) => t.taskId)).toEqual(['list-other']); const first: any = await (await list(`sources?task=list-shared&machine=${mac}&limit=1`)).json(); const second: any = await (await list(`sources?task=list-shared&machine=${mac}&limit=1&before=${first.nextCursor}`)).json(); expect([first.items[0].sourceId, second.items[0].sourceId]).toEqual([hash('two'), hash('one')]); expect(second.nextCursor).toBeNull(); const history: any = await (await read(a, `?task=list-shared&machine=${other}`)).json(); expect(history.events.length).toBe(1); expect(history.events[0].taskTitle).toBe('另一台 Mac'); for (const path of ['tasks?before=0', 'tasks?q=' + 'x'.repeat(201), 'sources?task=list-shared', 'sources?task=../escape&machine=' + mac]) expect((await list(path)).status).toBe(400); }); run('administrators read every account\'s history by owner; nobody else can, and an Eddy session never grants it', async () => { const admin = { 'x-tt-session-v2': 'fixture-admin' }; const adminGet = (path: string, headers: Record = admin) => eddyApi.request('/admin/conversations' + path, { headers }); const add = async (headers: Record, taskId: string, source: string, text: string) => { const v = body(); expect((await post({...v, taskId, sourceId: hash(source), taskTitle: 'Admin ' + taskId, events: [{...v.events[0], id: hash(taskId + source), text}]}, headers)).status).toBe(200); }; await add(a, 'admin-view-a', 'a-src', 'A body'); await add(b, 'admin-view-b', 'b-src', 'B body'); const mac = 'f'.repeat(16); for (const path of ['/users', '/tasks?q=admin-view', `/sources?owner=fixture-a@example.com&task=admin-view-a&machine=${mac}`]) for (const headers of [{}, a, { 'x-eddy-user': 'fixture-admin' }]) expect([401, 403]).toContain((await adminGet(path, headers)).status); const adminToken = 'eddy_sso_' + 'q'.repeat(43); await db.insert(eddy_sso_sessions).values({ tokenHash: hash(adminToken), email: 'fixture-admin@example.com', expiresAt: Date.now() + 60_000 }); expect((await adminGet('/users', { authorization: 'Bearer ' + adminToken })).status).toBe(401); await db.delete(eddy_sso_sessions).where(eq(eddy_sso_sessions.tokenHash, hash(adminToken))); const users = await adminGet('/users'); expect(users.headers.get('cache-control')).toBe('no-store'); const owners: any[] = (await users.json() as any).items; for (const owner of ['fixture-a@example.com', 'fixture-b@example.com']) { const row = owners.find(o => o.owner === owner); expect(row.taskCount).toBeGreaterThanOrEqual(1); expect(row.recordCount).toBeGreaterThanOrEqual(1); expect(row.machineCount).toBeGreaterThanOrEqual(1); } const all: any = await (await adminGet('/tasks?q=admin-view')).json(); expect(all.items.map((t: any) => [t.owner, t.taskId]).sort()).toEqual([['fixture-a@example.com', 'admin-view-a'], ['fixture-b@example.com', 'admin-view-b']]); const onlyB: any = await (await adminGet('/tasks?q=admin-view&owner=FIXTURE-B@example.com')).json(); expect(onlyB.items.map((t: any) => t.taskId)).toEqual(['admin-view-b']); const sources: any = await (await adminGet(`/sources?owner=fixture-b@example.com&task=admin-view-b&machine=${mac}`)).json(); expect(sources.items.map((s: any) => s.sourceId)).toEqual([hash('b-src')]); const transcript: any = await (await adminGet(`?owner=fixture-b@example.com&task=admin-view-b&machine=${mac}&source=${hash('b-src')}`)).json(); expect(transcript.events.map((e: any) => e.event.text)).toEqual(['B body']); for (const path of [`/sources?task=admin-view-b&machine=${mac}`, `?task=admin-view-b&machine=${mac}`, '/tasks?owner=not-an-email']) expect((await adminGet(path)).status).toBe(400); // The personal endpoints stay per person, administrators included. expect((await (await eddyApi.request('/conversations/tasks?q=admin-view', { headers: admin })).json() as any).items).toEqual([]); }); runEddy('actual Eddy Python client syncs synthetic Codex/Claude logs through HTTP into Postgres', async () => { const source = process.env.EDDY_TEST_SOURCE!; const probeToken = 'eddy_sso_' + 'y'.repeat(43); const probeHash = hash(probeToken); await db.insert(eddy_sso_sessions).values({ tokenHash: probeHash, email: 'fixture-a@example.com', expiresAt: Date.now() + 60_000 }); const server = Bun.serve({ hostname: '127.0.0.1', port: 0, fetch: request => { const url = new URL(request.url); url.pathname = url.pathname.replace(/^\/api\/eddy/, ''); return eddyApi.fetch(new Request(url, request)); } }); try { const child = Bun.spawn([source + '/vendor/python/bin/python3', source + '/tests/conversation_server_probe.py', `http://127.0.0.1:${server.port}`, probeToken], { stdout: 'pipe', stderr: 'pipe', env: { ...process.env, PYTHONNOUSERSITE: '1' } }); const [exit, stdout, stderr] = await Promise.all([child.exited, new Response(child.stdout).text(), new Response(child.stderr).text()]); expect(stderr).toBe(''); expect(exit).toBe(0); expect(JSON.parse(stdout).ok).toBe(true); console.log('Synthetic cross-repository probe:', stdout.trim()); } finally { server.stop(true); await db.delete(eddy_sso_sessions).where(eq(eddy_sso_sessions.tokenHash, probeHash)); } }, 20_000); run('quota responses provide safe retry hints and do not partially materialize events', async () => { process.env.EDDY_CONVERSATION_OWNER_ROWS='1'; try { const value={...body(),taskId:'quota-http-fixture',taskTitle:'PRIVATE'}; const response=await post(value,a); expect(response.status).toBe(429); expect(response.headers.get('retry-after')).toBe('3600'); const data:any=await response.json(); expect(data.code).toBe('eddy_storage_quota'); expect(JSON.stringify(data)).not.toContain('PRIVATE'); expect((await (await read(a,'?task=quota-http-fixture')).json() as any).events).toEqual([]); } finally { delete process.env.EDDY_CONVERSATION_OWNER_ROWS; } }); afterAll(async () => { if (process.env.TEST_DATABASE_URL) { await db.delete(eddy_conversation_events).where(inArray(eddy_conversation_events.owner, ['fixture-a@example.com', 'fixture-b@example.com'])); await db.delete(eddy_sso_sessions).where(eq(eddy_sso_sessions.tokenHash, tokenHash)); } await closePool(); });