26 lines
1.3 KiB
JavaScript
26 lines
1.3 KiB
JavaScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readChatStream } from '../src/services/chat-stream.ts'
|
|
|
|
function stream(text, everyByte = false) {
|
|
const bytes = new TextEncoder().encode(text)
|
|
return new ReadableStream({ start(controller) {
|
|
if (everyByte) for (const byte of bytes) controller.enqueue(Uint8Array.of(byte))
|
|
else controller.enqueue(bytes)
|
|
controller.close()
|
|
} })
|
|
}
|
|
test('UTF-8 split at every byte and CRLF frames preserve reasoning and content', async () => {
|
|
let text = '', reasoning = ''
|
|
await readChatStream(stream('data: {"choices":[{"delta":{"reasoning_content":"思考"}}]}\r\n\r\ndata: {"choices":[{"delta":{"content":"你好🙂"}}]}\n\ndata: [DONE]\n\n', true), (a,b) => { text += a; reasoning += b })
|
|
assert.equal(text, '你好🙂'); assert.equal(reasoning, '思考')
|
|
})
|
|
test('an SSE error after partial output is surfaced', async () => {
|
|
let text = ''
|
|
await assert.rejects(readChatStream(stream('data: {"choices":[{"delta":{"content":"partial"}}]}\n\ndata: {"error":{"message":"用量缺失,未扣费"}}\n\ndata: [DONE]\n\n'), a => text += a), /未扣费/)
|
|
assert.equal(text, 'partial')
|
|
})
|
|
test('EOF alone is never treated as a completed request', async () => {
|
|
await assert.rejects(readChatStream(stream('data: {"choices":[]}\n\n'), () => {}), /提前结束/)
|
|
})
|