|
| 1 | +import { expect, test, describe, beforeAll, afterAll } from 'vitest' |
| 2 | + |
| 3 | +import { post } from 'src/tests/helpers/e2etest.js' |
| 4 | +import { startMockServer, stopMockServer } from '@/tests/mocks/start-mock-server' |
| 5 | + |
| 6 | +describe('AI Search Routes', () => { |
| 7 | + beforeAll(() => { |
| 8 | + startMockServer() |
| 9 | + }) |
| 10 | + afterAll(() => stopMockServer()) |
| 11 | + |
| 12 | + test('/api/ai-search/v1 should handle a successful response', async () => { |
| 13 | + let apiBody = { query: 'How do I create a Repository?', language: 'en', version: 'dotcom' } |
| 14 | + |
| 15 | + const response = await fetch('http://localhost:4000/api/ai-search/v1', { |
| 16 | + method: 'POST', |
| 17 | + headers: { 'Content-Type': 'application/json' }, |
| 18 | + body: JSON.stringify(apiBody), |
| 19 | + }) |
| 20 | + |
| 21 | + expect(response.ok).toBe(true) |
| 22 | + expect(response.headers.get('content-type')).toBe('application/x-ndjson') |
| 23 | + expect(response.headers.get('transfer-encoding')).toBe('chunked') |
| 24 | + |
| 25 | + if (!response.body) { |
| 26 | + throw new Error('ReadableStream not supported in this environment.') |
| 27 | + } |
| 28 | + |
| 29 | + const decoder = new TextDecoder('utf-8') |
| 30 | + const reader = response.body.getReader() |
| 31 | + let done = false |
| 32 | + const chunks = [] |
| 33 | + |
| 34 | + while (!done) { |
| 35 | + const { value, done: readerDone } = await reader.read() |
| 36 | + done = readerDone |
| 37 | + |
| 38 | + if (value) { |
| 39 | + // Decode the Uint8Array chunk into a string |
| 40 | + const chunkStr = decoder.decode(value, { stream: true }) |
| 41 | + chunks.push(chunkStr) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Combine all chunks into a single string |
| 46 | + const fullResponse = chunks.join('') |
| 47 | + // Split the response into individual chunk lines |
| 48 | + const chunkLines = fullResponse.split('\n').filter((line) => line.trim() !== '') |
| 49 | + |
| 50 | + // Assertions: |
| 51 | + |
| 52 | + // 1. First chunk should be the SOURCES chunk |
| 53 | + expect(chunkLines.length).toBeGreaterThan(0) |
| 54 | + const firstChunkMatch = chunkLines[0].match(/^Chunk: (.+)$/) |
| 55 | + expect(firstChunkMatch).not.toBeNull() |
| 56 | + |
| 57 | + const sourcesChunk = JSON.parse(firstChunkMatch?.[1] || '') |
| 58 | + expect(sourcesChunk).toHaveProperty('chunkType', 'SOURCES') |
| 59 | + expect(sourcesChunk).toHaveProperty('sources') |
| 60 | + expect(Array.isArray(sourcesChunk.sources)).toBe(true) |
| 61 | + expect(sourcesChunk.sources.length).toBe(3) |
| 62 | + |
| 63 | + // 2. Subsequent chunks should be MESSAGE_CHUNKs |
| 64 | + for (let i = 1; i < chunkLines.length; i++) { |
| 65 | + const line = chunkLines[i] |
| 66 | + const messageChunk = JSON.parse(line) |
| 67 | + expect(messageChunk).toHaveProperty('chunkType', 'MESSAGE_CHUNK') |
| 68 | + expect(messageChunk).toHaveProperty('text') |
| 69 | + expect(typeof messageChunk.text).toBe('string') |
| 70 | + } |
| 71 | + |
| 72 | + // 3. Verify the complete message is expected |
| 73 | + const expectedMessage = |
| 74 | + 'Creating a repository on GitHub is something you should already know how to do :shrug:' |
| 75 | + const receivedMessage = chunkLines |
| 76 | + .slice(1) |
| 77 | + .map((line) => JSON.parse(line).text) |
| 78 | + .join('') |
| 79 | + expect(receivedMessage).toBe(expectedMessage) |
| 80 | + }) |
| 81 | + |
| 82 | + test('should handle validation errors: query missing', async () => { |
| 83 | + let body = { language: 'en', version: 'dotcom' } |
| 84 | + const response = await post('/api/ai-search/v1', { |
| 85 | + body: JSON.stringify(body), |
| 86 | + headers: { 'Content-Type': 'application/json' }, |
| 87 | + }) |
| 88 | + |
| 89 | + const responseBody = JSON.parse(response.body) |
| 90 | + |
| 91 | + expect(response.ok).toBe(false) |
| 92 | + expect(responseBody['errors']).toEqual([ |
| 93 | + { message: `Missing required key 'query' in request body` }, |
| 94 | + ]) |
| 95 | + }) |
| 96 | + |
| 97 | + test('should handle validation errors: language missing', async () => { |
| 98 | + let body = { query: 'example query', version: 'dotcom' } |
| 99 | + const response = await post('/api/ai-search/v1', { |
| 100 | + body: JSON.stringify(body), |
| 101 | + headers: { 'Content-Type': 'application/json' }, |
| 102 | + }) |
| 103 | + |
| 104 | + const responseBody = JSON.parse(response.body) |
| 105 | + |
| 106 | + expect(response.ok).toBe(false) |
| 107 | + expect(responseBody['errors']).toEqual([ |
| 108 | + { message: `Missing required key 'language' in request body` }, |
| 109 | + { message: `Invalid 'language' in request body 'undefined'. Must be one of: en` }, |
| 110 | + ]) |
| 111 | + }) |
| 112 | + |
| 113 | + test('should handle validation errors: version missing', async () => { |
| 114 | + let body = { query: 'example query', language: 'en' } |
| 115 | + const response = await post('/api/ai-search/v1', { |
| 116 | + body: JSON.stringify(body), |
| 117 | + headers: { 'Content-Type': 'application/json' }, |
| 118 | + }) |
| 119 | + |
| 120 | + const responseBody = JSON.parse(response.body) |
| 121 | + |
| 122 | + expect(response.ok).toBe(false) |
| 123 | + expect(responseBody['errors']).toEqual([ |
| 124 | + { message: `Missing required key 'version' in request body` }, |
| 125 | + { |
| 126 | + message: `Invalid 'version' in request body: 'undefined'. Must be one of: dotcom, ghec, ghes`, |
| 127 | + }, |
| 128 | + ]) |
| 129 | + }) |
| 130 | + |
| 131 | + test('should handle multiple validation errors: query missing, invalid language and version', async () => { |
| 132 | + let body = { language: 'fr', version: 'fpt' } |
| 133 | + const response = await post('/api/ai-search/v1', { |
| 134 | + body: JSON.stringify(body), |
| 135 | + headers: { 'Content-Type': 'application/json' }, |
| 136 | + }) |
| 137 | + |
| 138 | + const responseBody = JSON.parse(response.body) |
| 139 | + |
| 140 | + expect(response.ok).toBe(false) |
| 141 | + expect(responseBody['errors']).toEqual([ |
| 142 | + { message: `Missing required key 'query' in request body` }, |
| 143 | + { |
| 144 | + message: `Invalid 'language' in request body 'fr'. Must be one of: en`, |
| 145 | + }, |
| 146 | + ]) |
| 147 | + }) |
| 148 | +}) |
0 commit comments