Files
web-terminal/test/history.test.ts
Yaojia Wang e36ca272ed feat(v0.3): O2 — resume past Claude sessions
- src/http/history.ts: listSessions() reads ~/.claude/projects/*/*.jsonl
  (mtime-sorted, top 50), parseSessionMeta() extracts cwd + first user prompt
  (pure, 4 unit tests); GET /sessions
- terminal-session: opts.initialInput (typed ~700ms after the shell is ready)
- tabs.newTabForResume(cwd,id): new tab in the project dir running
  'claude --resume <id>'
- public/history.ts: 🕘 panel listing sessions (project · time · preview) with
  Resume buttons
- Verified: /sessions returns 44 real sessions; panel lists project/time/preview.
  216 tests green.
2026-06-18 08:04:04 +02:00

34 lines
1.5 KiB
TypeScript

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: '' })
})
})