import { describe, it, expect } from 'vitest' import { parseSessionMeta } from '../src/http/history.js' describe('parseSessionMeta', () => { it('extracts cwd and the first user prompt (string content)', () => { const jsonl = [ JSON.stringify({ type: 'last-prompt', sessionId: 'x' }), JSON.stringify({ type: 'attachment', cwd: '/Users/me/proj', sessionId: 'x' }), JSON.stringify({ type: 'user', message: { role: 'user', content: 'Add monitoring please' } }), ].join('\n') expect(parseSessionMeta(jsonl)).toEqual({ cwd: '/Users/me/proj', preview: 'Add monitoring please' }) }) it('reads the first text block when content is an array', () => { const jsonl = [ JSON.stringify({ type: 'user', cwd: '/p', message: { role: 'user', content: [{ type: 'text', text: 'hello there' }] } }), ].join('\n') expect(parseSessionMeta(jsonl)).toEqual({ cwd: '/p', preview: 'hello there' }) }) it('collapses whitespace and truncates long previews to 120 chars', () => { const long = 'a'.repeat(200) const jsonl = JSON.stringify({ type: 'user', cwd: '/p', message: { role: 'user', content: `line1\n line2 ${long}` } }) const { preview } = parseSessionMeta(jsonl) expect(preview.length).toBe(120) expect(preview.startsWith('line1 line2 ')).toBe(true) }) it('returns nulls/empty when there is no cwd or user message', () => { expect(parseSessionMeta('')).toEqual({ cwd: null, preview: '' }) expect(parseSessionMeta('not json\n{"type":"summary"}')).toEqual({ cwd: null, preview: '' }) }) })