|
| 1 | +import { describe, it, expect, afterEach } from "vitest"; |
| 2 | +import { LLMock } from "../llmock.js"; |
| 3 | +import type { ChatCompletionRequest, SSEChunk } from "../types.js"; |
| 4 | + |
| 5 | +function parseSSEChunks(body: string): SSEChunk[] { |
| 6 | + return body |
| 7 | + .split("\n\n") |
| 8 | + .filter((line) => line.startsWith("data: ") && !line.includes("[DONE]")) |
| 9 | + .map((line) => JSON.parse(line.slice(6)) as SSEChunk); |
| 10 | +} |
| 11 | + |
| 12 | +describe("async fixture response (function responses)", () => { |
| 13 | + let mock: LLMock | null = null; |
| 14 | + |
| 15 | + afterEach(async () => { |
| 16 | + if (mock) { |
| 17 | + await mock.stop(); |
| 18 | + mock = null; |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + it("resolves a sync function response", async () => { |
| 23 | + mock = new LLMock({ port: 0 }); |
| 24 | + mock.on({ userMessage: "sync-fn" }, () => ({ content: "sync-factory-result" })); |
| 25 | + await mock.start(); |
| 26 | + |
| 27 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 28 | + method: "POST", |
| 29 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 30 | + body: JSON.stringify({ |
| 31 | + model: "gpt-4o", |
| 32 | + messages: [{ role: "user", content: "sync-fn" }], |
| 33 | + stream: false, |
| 34 | + }), |
| 35 | + }); |
| 36 | + |
| 37 | + expect(res.status).toBe(200); |
| 38 | + const json = await res.json(); |
| 39 | + expect(json.choices[0].message.content).toBe("sync-factory-result"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("resolves an async function response", async () => { |
| 43 | + mock = new LLMock({ port: 0 }); |
| 44 | + mock.on({ userMessage: "async-fn" }, async () => { |
| 45 | + return { content: "async-factory-result" }; |
| 46 | + }); |
| 47 | + await mock.start(); |
| 48 | + |
| 49 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 50 | + method: "POST", |
| 51 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 52 | + body: JSON.stringify({ |
| 53 | + model: "gpt-4o", |
| 54 | + messages: [{ role: "user", content: "async-fn" }], |
| 55 | + stream: false, |
| 56 | + }), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(res.status).toBe(200); |
| 60 | + const json = await res.json(); |
| 61 | + expect(json.choices[0].message.content).toBe("async-factory-result"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("receives the request object in the factory function", async () => { |
| 65 | + mock = new LLMock({ port: 0 }); |
| 66 | + mock.on({ userMessage: "echo-model" }, (req: ChatCompletionRequest) => ({ |
| 67 | + content: `model=${req.model}`, |
| 68 | + })); |
| 69 | + await mock.start(); |
| 70 | + |
| 71 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 72 | + method: "POST", |
| 73 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 74 | + body: JSON.stringify({ |
| 75 | + model: "gpt-4o-mini", |
| 76 | + messages: [{ role: "user", content: "echo-model" }], |
| 77 | + stream: false, |
| 78 | + }), |
| 79 | + }); |
| 80 | + |
| 81 | + expect(res.status).toBe(200); |
| 82 | + const json = await res.json(); |
| 83 | + expect(json.choices[0].message.content).toBe("model=gpt-4o-mini"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("works with streaming responses from a factory", async () => { |
| 87 | + mock = new LLMock({ port: 0 }); |
| 88 | + mock.on({ userMessage: "stream-fn" }, () => ({ content: "streamed-from-factory" })); |
| 89 | + await mock.start(); |
| 90 | + |
| 91 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 92 | + method: "POST", |
| 93 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 94 | + body: JSON.stringify({ |
| 95 | + model: "gpt-4o", |
| 96 | + messages: [{ role: "user", content: "stream-fn" }], |
| 97 | + stream: true, |
| 98 | + }), |
| 99 | + }); |
| 100 | + |
| 101 | + expect(res.status).toBe(200); |
| 102 | + const chunks = parseSSEChunks(await res.text()); |
| 103 | + const content = chunks.map((c) => c.choices?.[0]?.delta?.content ?? "").join(""); |
| 104 | + expect(content).toBe("streamed-from-factory"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("works with onMessage convenience method", async () => { |
| 108 | + mock = new LLMock({ port: 0 }); |
| 109 | + mock.onMessage("convenience-fn", (req: ChatCompletionRequest) => ({ |
| 110 | + content: `msg-count=${req.messages.length}`, |
| 111 | + })); |
| 112 | + await mock.start(); |
| 113 | + |
| 114 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 115 | + method: "POST", |
| 116 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 117 | + body: JSON.stringify({ |
| 118 | + model: "gpt-4o", |
| 119 | + messages: [ |
| 120 | + { role: "system", content: "you are helpful" }, |
| 121 | + { role: "user", content: "convenience-fn" }, |
| 122 | + ], |
| 123 | + stream: false, |
| 124 | + }), |
| 125 | + }); |
| 126 | + |
| 127 | + expect(res.status).toBe(200); |
| 128 | + const json = await res.json(); |
| 129 | + expect(json.choices[0].message.content).toBe("msg-count=2"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("static response still works alongside function responses", async () => { |
| 133 | + mock = new LLMock({ port: 0 }); |
| 134 | + mock.on({ userMessage: "static" }, { content: "plain-static" }); |
| 135 | + mock.on({ userMessage: "dynamic" }, () => ({ content: "from-function" })); |
| 136 | + await mock.start(); |
| 137 | + |
| 138 | + const [staticRes, dynamicRes] = await Promise.all([ |
| 139 | + fetch(`${mock.url}/v1/chat/completions`, { |
| 140 | + method: "POST", |
| 141 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 142 | + body: JSON.stringify({ |
| 143 | + model: "gpt-4o", |
| 144 | + messages: [{ role: "user", content: "static" }], |
| 145 | + stream: false, |
| 146 | + }), |
| 147 | + }), |
| 148 | + fetch(`${mock.url}/v1/chat/completions`, { |
| 149 | + method: "POST", |
| 150 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 151 | + body: JSON.stringify({ |
| 152 | + model: "gpt-4o", |
| 153 | + messages: [{ role: "user", content: "dynamic" }], |
| 154 | + stream: false, |
| 155 | + }), |
| 156 | + }), |
| 157 | + ]); |
| 158 | + |
| 159 | + expect(staticRes.status).toBe(200); |
| 160 | + expect(dynamicRes.status).toBe(200); |
| 161 | + |
| 162 | + const staticJson = await staticRes.json(); |
| 163 | + const dynamicJson = await dynamicRes.json(); |
| 164 | + |
| 165 | + expect(staticJson.choices[0].message.content).toBe("plain-static"); |
| 166 | + expect(dynamicJson.choices[0].message.content).toBe("from-function"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("returns 500 when factory throws", async () => { |
| 170 | + mock = new LLMock({ port: 0 }); |
| 171 | + mock.on({ userMessage: "boom" }, () => { |
| 172 | + throw new Error("factory exploded"); |
| 173 | + }); |
| 174 | + await mock.start(); |
| 175 | + |
| 176 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 177 | + method: "POST", |
| 178 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 179 | + body: JSON.stringify({ |
| 180 | + model: "gpt-4", |
| 181 | + messages: [{ role: "user", content: "boom" }], |
| 182 | + stream: false, |
| 183 | + }), |
| 184 | + }); |
| 185 | + |
| 186 | + expect(res.status).toBe(500); |
| 187 | + }); |
| 188 | + |
| 189 | + it("returns 500 when async factory rejects", async () => { |
| 190 | + mock = new LLMock({ port: 0 }); |
| 191 | + mock.on({ userMessage: "reject" }, async () => { |
| 192 | + throw new Error("async rejection"); |
| 193 | + }); |
| 194 | + await mock.start(); |
| 195 | + |
| 196 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 197 | + method: "POST", |
| 198 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 199 | + body: JSON.stringify({ |
| 200 | + model: "gpt-4", |
| 201 | + messages: [{ role: "user", content: "reject" }], |
| 202 | + stream: false, |
| 203 | + }), |
| 204 | + }); |
| 205 | + |
| 206 | + expect(res.status).toBe(500); |
| 207 | + }); |
| 208 | + |
| 209 | + it("returns 500 when factory returns invalid response shape", async () => { |
| 210 | + mock = new LLMock({ port: 0 }); |
| 211 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 212 | + mock.on({ userMessage: "bad" }, () => ({ notAValidField: true }) as any); |
| 213 | + await mock.start(); |
| 214 | + |
| 215 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 216 | + method: "POST", |
| 217 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 218 | + body: JSON.stringify({ |
| 219 | + model: "gpt-4", |
| 220 | + messages: [{ role: "user", content: "bad" }], |
| 221 | + stream: false, |
| 222 | + }), |
| 223 | + }); |
| 224 | + |
| 225 | + expect(res.status).toBe(500); |
| 226 | + }); |
| 227 | + |
| 228 | + it("works with async factory and streaming", async () => { |
| 229 | + mock = new LLMock({ port: 0 }); |
| 230 | + mock.on({ userMessage: "async-stream" }, async () => { |
| 231 | + await new Promise((r) => setTimeout(r, 10)); |
| 232 | + return { content: "async-streamed-result" }; |
| 233 | + }); |
| 234 | + await mock.start(); |
| 235 | + |
| 236 | + const res = await fetch(`${mock.url}/v1/chat/completions`, { |
| 237 | + method: "POST", |
| 238 | + headers: { "Content-Type": "application/json", Authorization: "Bearer test" }, |
| 239 | + body: JSON.stringify({ |
| 240 | + model: "gpt-4", |
| 241 | + messages: [{ role: "user", content: "async-stream" }], |
| 242 | + stream: true, |
| 243 | + }), |
| 244 | + }); |
| 245 | + |
| 246 | + expect(res.status).toBe(200); |
| 247 | + const chunks = parseSSEChunks(await res.text()); |
| 248 | + const content = chunks.map((c) => c.choices?.[0]?.delta?.content ?? "").join(""); |
| 249 | + expect(content).toBe("async-streamed-result"); |
| 250 | + }); |
| 251 | +}); |
0 commit comments