71 lines
2.8 KiB
JavaScript
71 lines
2.8 KiB
JavaScript
import { describe, expect, it } from "bun:test";
|
|
|
|
import {
|
|
consumeCopilotStream,
|
|
consumeSSEFrames,
|
|
splitCopilotResponse,
|
|
} from "./copilot-stream.js";
|
|
|
|
describe("copilot-stream SSE & reasoning utilities", () => {
|
|
it("parses incremental CRLF/LF SSE chunks cleanly", () => {
|
|
const first = consumeSSEFrames("event: meta\r\ndata: {\"model\":\"MiniMax-M2.7\"}\r\n\r\nevent: tex", false);
|
|
expect(first.frames).toEqual([{ event: "meta", data: "{\"model\":\"MiniMax-M2.7\"}" }]);
|
|
expect(first.remainder).toBe("event: tex");
|
|
|
|
const second = consumeSSEFrames(`${first.remainder}t\ndata: {"delta":"hello"}\n\n`, true);
|
|
expect(second.frames).toEqual([{ event: "text", data: "{\"delta\":\"hello\"}" }]);
|
|
});
|
|
|
|
it("extracts structured + inline <think> reasoning without leaking partial tags", () => {
|
|
const midStream = splitCopilotResponse("正文未开始<think>推理一步<thi", "");
|
|
expect(midStream.reasoningActive).toBe(true);
|
|
expect(midStream.reasoning).toBe("推理一步");
|
|
expect(midStream.markdown).toBe("正文未开始");
|
|
|
|
const completed = splitCopilotResponse("<think>内部链路</think>\n### 结论\n一切正常", "结构化思考");
|
|
expect(completed.reasoningActive).toBe(false);
|
|
expect(completed.reasoning).toContain("结构化思考");
|
|
expect(completed.reasoning).toContain("内部链路");
|
|
expect(completed.markdown).toBe("### 结论\n一切正常");
|
|
});
|
|
|
|
it("consumes SSE response streams in order", async () => {
|
|
const encoder = new TextEncoder();
|
|
const chunks = [
|
|
"event: meta\ndata: {\"provider\":\"minimax\",\"model\":\"MiniMax-M2.7\"}\n\n",
|
|
"event: reasoning\ndata: {\"delta\":\"先查新闻\"}\n\n",
|
|
"event: text\ndata: {\"delta\":\"国际局势摘要\"}\n\n",
|
|
"event: done\ndata: {\"generation_mode\":\"llm_verified\",\"zero_token\":false,\"model\":\"MiniMax-M2.7\"}\n\n",
|
|
];
|
|
let index = 0;
|
|
const fakeResponse = {
|
|
headers: new Headers({ "content-type": "text/event-stream" }),
|
|
body: {
|
|
getReader() {
|
|
return {
|
|
async read() {
|
|
if (index >= chunks.length) return { done: true, value: undefined };
|
|
return { done: false, value: encoder.encode(chunks[index++]) };
|
|
},
|
|
async cancel() {},
|
|
releaseLock() {},
|
|
};
|
|
},
|
|
},
|
|
};
|
|
|
|
const seen = { meta: null, reasoning: "", text: "", done: null };
|
|
await consumeCopilotStream(fakeResponse, {
|
|
onMeta: (m) => { seen.meta = m; },
|
|
onReasoningDelta: (d) => { seen.reasoning += d; },
|
|
onTextDelta: (d) => { seen.text += d; },
|
|
onDone: (d) => { seen.done = d; },
|
|
});
|
|
|
|
expect(seen.meta?.model).toBe("MiniMax-M2.7");
|
|
expect(seen.reasoning).toBe("先查新闻");
|
|
expect(seen.text).toBe("国际局势摘要");
|
|
expect(seen.done?.generation_mode).toBe("llm_verified");
|
|
});
|
|
});
|