|
| 1 | +/** |
| 2 | + * Issue #3114: Session export API — download full transcript as JSONL/Markdown. |
| 3 | + */ |
| 4 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 5 | +import Fastify from 'fastify'; |
| 6 | +import { registerSessionDataRoutes } from '../routes/session-data.js'; |
| 7 | +import type { RouteContext } from '../routes/context.js'; |
| 8 | +import type { SessionInfo } from '../session.js'; |
| 9 | +import type { ParsedEntry } from '../transcript.js'; |
| 10 | + |
| 11 | +const SESSION_ID = 'export-test-session'; |
| 12 | +const AUTH_TOKEN = 'test-token'; |
| 13 | + |
| 14 | +function makeSession(): SessionInfo { |
| 15 | + return { |
| 16 | + id: SESSION_ID, |
| 17 | + windowId: '', |
| 18 | + displayName: 'export-test', |
| 19 | + workDir: '/tmp', |
| 20 | + byteOffset: 0, |
| 21 | + monitorOffset: 0, |
| 22 | + status: 'idle', |
| 23 | + createdAt: Date.now(), |
| 24 | + lastActivity: Date.now(), |
| 25 | + stallThresholdMs: 30_000, |
| 26 | + permissionStallMs: 60_000, |
| 27 | + permissionMode: 'default', |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +const sampleEntries: ParsedEntry[] = [ |
| 32 | + { role: 'user', contentType: 'text', text: 'Hello, how are you?' }, |
| 33 | + { role: 'assistant', contentType: 'thinking', text: 'Let me think about this...' }, |
| 34 | + { role: 'assistant', contentType: 'text', text: 'I am doing well, thanks!' }, |
| 35 | + { role: 'assistant', contentType: 'tool_use', text: 'ReadFile', toolName: 'ReadFile', toolUseId: 'tc-1' }, |
| 36 | + { role: 'assistant', contentType: 'tool_result', text: 'file contents here', toolUseId: 'tc-1' }, |
| 37 | + { role: 'assistant', contentType: 'tool_error', text: 'Permission denied', toolUseId: 'tc-2' }, |
| 38 | + { role: 'system', contentType: 'permission_request', text: 'Allow write to foo.txt' }, |
| 39 | +]; |
| 40 | + |
| 41 | +function buildApp(withData = false): { app: ReturnType<typeof Fastify>; sessions: Record<string, unknown> } { |
| 42 | + const app = Fastify({ logger: false }); |
| 43 | + app.addHook('onRequest', async (req) => { |
| 44 | + req.authKeyId = null; |
| 45 | + req.tenantId = 'system'; |
| 46 | + }); |
| 47 | + |
| 48 | + const session = makeSession(); |
| 49 | + const sessions = { |
| 50 | + getSession: vi.fn((id: string) => id === SESSION_ID ? session : undefined), |
| 51 | + readTranscript: vi.fn(async () => ({ |
| 52 | + messages: withData ? sampleEntries : [], |
| 53 | + total: withData ? sampleEntries.length : 0, |
| 54 | + page: 1, |
| 55 | + limit: 100_000, |
| 56 | + hasMore: false, |
| 57 | + })), |
| 58 | + }; |
| 59 | + |
| 60 | + const ctx = { |
| 61 | + sessions, |
| 62 | + auth: { authEnabled: false }, |
| 63 | + config: {}, |
| 64 | + metrics: { getSessionMetrics: vi.fn() }, |
| 65 | + monitor: {}, |
| 66 | + eventBus: { subscribe: vi.fn() }, |
| 67 | + channels: {}, |
| 68 | + toolRegistry: { processEntries: vi.fn(), getSessionTools: vi.fn(() => []), getToolDefinitions: vi.fn(() => []) }, |
| 69 | + sseLimiter: { acquire: vi.fn(() => ({ allowed: false, reason: 'test' })) }, |
| 70 | + } as unknown as RouteContext; |
| 71 | + |
| 72 | + registerSessionDataRoutes(app, ctx); |
| 73 | + return { app, sessions }; |
| 74 | +} |
| 75 | + |
| 76 | +describe('Issue #3114: Session export API', () => { |
| 77 | + it('returns 400 for unsupported format', async () => { |
| 78 | + const { app } = buildApp(); |
| 79 | + const res = await app.inject({ |
| 80 | + method: 'GET', |
| 81 | + url: `/v1/sessions/${SESSION_ID}/export?format=csv`, |
| 82 | + }); |
| 83 | + expect(res.statusCode).toBe(400); |
| 84 | + expect(res.json()).toEqual({ error: expect.stringContaining('Invalid format') }); |
| 85 | + await app.close(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns 404 for non-existent session', async () => { |
| 89 | + const { app } = buildApp(); |
| 90 | + const res = await app.inject({ |
| 91 | + method: 'GET', |
| 92 | + url: '/v1/sessions/nonexistent/export?format=jsonl', |
| 93 | + }); |
| 94 | + expect(res.statusCode).toBe(404); |
| 95 | + await app.close(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns 404 for session with no transcript data', async () => { |
| 99 | + const { app } = buildApp(false); // no data |
| 100 | + const res = await app.inject({ |
| 101 | + method: 'GET', |
| 102 | + url: `/v1/sessions/${SESSION_ID}/export?format=jsonl`, |
| 103 | + }); |
| 104 | + expect(res.statusCode).toBe(404); |
| 105 | + expect(res.json()).toEqual({ error: expect.stringContaining('No transcript data') }); |
| 106 | + await app.close(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('returns NDJSON for jsonl format with data', async () => { |
| 110 | + const { app } = buildApp(true); // with data |
| 111 | + const res = await app.inject({ |
| 112 | + method: 'GET', |
| 113 | + url: `/v1/sessions/${SESSION_ID}/export?format=jsonl`, |
| 114 | + }); |
| 115 | + expect(res.statusCode).toBe(200); |
| 116 | + expect(res.headers['content-type']).toContain('application/x-ndjson'); |
| 117 | + expect(res.headers['content-disposition']).toContain(`session-${SESSION_ID}.jsonl`); |
| 118 | + |
| 119 | + const lines = res.body.split('\n').filter(Boolean); |
| 120 | + expect(lines.length).toBe(sampleEntries.length); |
| 121 | + for (const line of lines) { |
| 122 | + const parsed = JSON.parse(line); |
| 123 | + expect(parsed).toHaveProperty('role'); |
| 124 | + expect(parsed).toHaveProperty('contentType'); |
| 125 | + } |
| 126 | + await app.close(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('returns markdown for markdown format with data', async () => { |
| 130 | + const { app } = buildApp(true); |
| 131 | + const res = await app.inject({ |
| 132 | + method: 'GET', |
| 133 | + url: `/v1/sessions/${SESSION_ID}/export?format=markdown`, |
| 134 | + }); |
| 135 | + expect(res.statusCode).toBe(200); |
| 136 | + expect(res.headers['content-type']).toContain('text/markdown'); |
| 137 | + expect(res.headers['content-disposition']).toContain(`session-${SESSION_ID}.md`); |
| 138 | + expect(res.body).toContain('# Session Export'); |
| 139 | + expect(res.body).toContain('export-test'); |
| 140 | + expect(res.body).toContain('Hello, how are you?'); |
| 141 | + expect(res.body).toContain('Let me think about this'); |
| 142 | + expect(res.body).toContain('🔧 Tool: ReadFile'); |
| 143 | + expect(res.body).toContain('<details>'); |
| 144 | + await app.close(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('defaults to jsonl format when no format specified', async () => { |
| 148 | + const { app } = buildApp(true); |
| 149 | + const res = await app.inject({ |
| 150 | + method: 'GET', |
| 151 | + url: `/v1/sessions/${SESSION_ID}/export`, |
| 152 | + }); |
| 153 | + expect(res.statusCode).toBe(200); |
| 154 | + expect(res.headers['content-type']).toContain('application/x-ndjson'); |
| 155 | + await app.close(); |
| 156 | + }); |
| 157 | + |
| 158 | + it('markdown includes thinking blocks', async () => { |
| 159 | + const { app } = buildApp(true); |
| 160 | + const res = await app.inject({ |
| 161 | + method: 'GET', |
| 162 | + url: `/v1/sessions/${SESSION_ID}/export?format=markdown`, |
| 163 | + }); |
| 164 | + expect(res.body).toContain('💭 Thinking'); |
| 165 | + expect(res.body).toContain('Let me think about this...'); |
| 166 | + await app.close(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('markdown includes permission request', async () => { |
| 170 | + const { app } = buildApp(true); |
| 171 | + const res = await app.inject({ |
| 172 | + method: 'GET', |
| 173 | + url: `/v1/sessions/${SESSION_ID}/export?format=markdown`, |
| 174 | + }); |
| 175 | + expect(res.body).toContain('🔐 Permission Request'); |
| 176 | + expect(res.body).toContain('Allow write to foo.txt'); |
| 177 | + await app.close(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('markdown includes tool error with warning', async () => { |
| 181 | + const { app } = buildApp(true); |
| 182 | + const res = await app.inject({ |
| 183 | + method: 'GET', |
| 184 | + url: `/v1/sessions/${SESSION_ID}/export?format=markdown`, |
| 185 | + }); |
| 186 | + expect(res.body).toContain('⚠️ Tool error'); |
| 187 | + expect(res.body).toContain('Permission denied'); |
| 188 | + await app.close(); |
| 189 | + }); |
| 190 | +}); |
0 commit comments