import { describe, it, expect, vi } from 'vitest'; import { ensureContentType, convertSseToJson } from '../lib/request/response-handler.js'; describe('Response Handler Module', () => { describe('ensureContentType', () => { it('should preserve existing content-type', () => { const headers = new Headers(); headers.set('content-type', 'application/json'); const result = ensureContentType(headers); expect(result.get('content-type')).toBe('application/json'); }); it('should add default content-type if missing', () => { const headers = new Headers(); const result = ensureContentType(headers); expect(result.get('content-type')).toBe('text/event-stream; charset=utf-8'); }); it('should not modify original headers', () => { const headers = new Headers(); const result = ensureContentType(headers); expect(headers.has('content-type')).toBe(false); expect(result.has('content-type')).toBe(true); }); }); describe('convertSseToJson', () => { it('should throw error if response has no body', async () => { const response = new Response(null); const headers = new Headers(); await expect(convertSseToJson(response, headers)).rejects.toThrow( 'Response has no body' ); }); it('should parse SSE stream with response.done event', async () => { const sseContent = `data: {"type":"response.started"} data: {"type":"response.done","response":{"id":"resp_123","output":"test"}} `; const response = new Response(sseContent); const headers = new Headers(); const result = await convertSseToJson(response, headers); const body = await result.json(); expect(body).toEqual({ id: 'resp_123', output: 'test' }); expect(result.headers.get('content-type')).toBe('application/json; charset=utf-8'); }); it('should parse SSE stream with response.completed event', async () => { const sseContent = `data: {"type":"response.started"} data: {"type":"response.completed","response":{"id":"resp_456","output":"done"}} `; const response = new Response(sseContent); const headers = new Headers(); const result = await convertSseToJson(response, headers); const body = await result.json(); expect(body).toEqual({ id: 'resp_456', output: 'done' }); }); it('should return original text if no final response found', async () => { const sseContent = `data: {"type":"response.started"} data: {"type":"chunk","delta":"text"} `; const response = new Response(sseContent); const headers = new Headers(); const result = await convertSseToJson(response, headers); const text = await result.text(); expect(text).toBe(sseContent); }); it('should skip malformed JSON in SSE stream', async () => { const sseContent = `data: not-json data: {"type":"response.done","response":{"id":"resp_789"}} `; const response = new Response(sseContent); const headers = new Headers(); const result = await convertSseToJson(response, headers); const body = await result.json(); expect(body).toEqual({ id: 'resp_789' }); }); it('should handle empty SSE stream', async () => { const response = new Response(''); const headers = new Headers(); const result = await convertSseToJson(response, headers); const text = await result.text(); expect(text).toBe(''); }); it('should preserve response status and statusText', async () => { const sseContent = `data: {"type":"response.done","response":{"id":"x"}}`; const response = new Response(sseContent, { status: 200, statusText: 'OK', }); const headers = new Headers(); const result = await convertSseToJson(response, headers); expect(result.status).toBe(200); expect(result.statusText).toBe('OK'); }); }); });