import { expect, test } from 'bun:test'; import { conversationQuery, conversationListQuery, validateConversation } from '../src/eddy_conversation_rules'; const batch = () => ({ schema: 1, batchId: 'a'.repeat(64), taskId: 'fixture-task', taskTitle: 'Synthetic fixture', sourceId: 'b'.repeat(64), provider: 'codex', machine: 'c'.repeat(16), events: [{ id: 'd'.repeat(64), position: 100, block: 0, part: 0, parts: 2, kind: 'message', role: 'user', text: '可见消息🧪', redacted: false }] }); test('conversation protocol preserves Unicode and excludes unknown raw fields', () => { const raw = { ...batch(), rawFile: '/private/path', token: 'not accepted', events: [{ ...batch().events[0], reasoning: 'not accepted' }] }; const parsed = validateConversation(raw); expect(parsed.events[0].text).toBe('可见消息🧪'); expect(JSON.stringify(parsed)).not.toContain('private/path'); expect(JSON.stringify(parsed)).not.toContain('not accepted'); }); test('malformed, oversized, hidden-role and duplicate conversation events are rejected', () => { for (const change of [{ schema: 2 }, { provider: 'other' }, { taskId: '../escape' }, { machine: 'hostname' }, { events: [] }, { events: Array(65).fill(batch().events[0]) }, ...[{ role: 'system' }, { kind: 'reasoning' }, { text: 'a'.repeat(16385) }, { position: -1 }, { parts: 0 }, { part: 2 }, { redacted: undefined }] .map(event => ({ events: [{ ...batch().events[0], ...event }] })), { events: [batch().events[0], batch().events[0]] }, ]) expect(() => validateConversation({ ...batch(), ...change })).toThrow(); }); test('private reads validate cursors, selectors and bounded page sizes', () => { expect(conversationQuery({})).toEqual({ cursor: 0, limit: 100 }); expect(conversationQuery({ cursor: '123', limit: '2', task: 'fixture-task' }).cursor).toBe(123); for (const query of [{ cursor: '-1' }, { cursor: 'NaN' }, { limit: '501' }, { limit: '1.5' }, { task: '../x' }, { source: 'x' }]) expect(() => conversationQuery(query)).toThrow(); }); test('history lists bound pagination, literal search and require a machine for session selection', () => { expect(conversationListQuery({ q: ' 空间 %_ ' }).search).toBe('空间 %_'); expect(conversationQuery({ machine: 'f'.repeat(16) }).machine).toBe('f'.repeat(16)); expect(conversationListQuery({ task: 'a', machine: 'f'.repeat(16) }, true).taskId).toBe('a'); for (const q of [{ before: '0' }, { before: '-1' }, { before: 'Infinity' }, { limit: '101' }, { q: 'x'.repeat(201) }, { machine: 'host' }]) expect(() => conversationListQuery(q)).toThrow(); expect(() => conversationListQuery({ task: 'a' }, true)).toThrow(); expect(() => conversationQuery({machine: 'HOST'})).toThrow(); });