|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | | -import { matchFixture, getLastMessageByRole, getTextContent } from "../router.js"; |
| 2 | +import { matchFixture, getLastMessageByRole, getSystemText, getTextContent } from "../router.js"; |
3 | 3 | import type { ChatCompletionRequest, ChatMessage, ContentPart, Fixture } from "../types.js"; |
4 | 4 |
|
5 | 5 | // --------------------------------------------------------------------------- |
@@ -237,6 +237,164 @@ describe("matchFixture — userMessage (RegExp)", () => { |
237 | 237 | }); |
238 | 238 | }); |
239 | 239 |
|
| 240 | +// --------------------------------------------------------------------------- |
| 241 | +// getSystemText |
| 242 | +// --------------------------------------------------------------------------- |
| 243 | + |
| 244 | +describe("getSystemText", () => { |
| 245 | + it("returns empty string when there are no system messages", () => { |
| 246 | + expect(getSystemText([{ role: "user", content: "hi" }])).toBe(""); |
| 247 | + }); |
| 248 | + |
| 249 | + it("returns the single system message text", () => { |
| 250 | + expect( |
| 251 | + getSystemText([ |
| 252 | + { role: "system", content: "You are helpful." }, |
| 253 | + { role: "user", content: "hi" }, |
| 254 | + ]), |
| 255 | + ).toBe("You are helpful."); |
| 256 | + }); |
| 257 | + |
| 258 | + it("joins multiple system messages with newlines in order", () => { |
| 259 | + expect( |
| 260 | + getSystemText([ |
| 261 | + { role: "system", content: "first" }, |
| 262 | + { role: "user", content: "ignored" }, |
| 263 | + { role: "system", content: "second" }, |
| 264 | + ]), |
| 265 | + ).toBe("first\nsecond"); |
| 266 | + }); |
| 267 | + |
| 268 | + it("extracts text from array-of-parts system content", () => { |
| 269 | + expect( |
| 270 | + getSystemText([{ role: "system", content: [{ type: "text", text: "from parts" }] }]), |
| 271 | + ).toBe("from parts"); |
| 272 | + }); |
| 273 | +}); |
| 274 | + |
| 275 | +// --------------------------------------------------------------------------- |
| 276 | +// matchFixture — systemMessage |
| 277 | +// --------------------------------------------------------------------------- |
| 278 | + |
| 279 | +describe("matchFixture — systemMessage (string)", () => { |
| 280 | + it("matches when a system message contains the substring", () => { |
| 281 | + const fixture = makeFixture({ systemMessage: "Atai" }); |
| 282 | + const req = makeReq({ |
| 283 | + messages: [ |
| 284 | + { role: "system", content: "User name is Atai. Timezone America/Los_Angeles." }, |
| 285 | + { role: "user", content: "Who am I?" }, |
| 286 | + ], |
| 287 | + }); |
| 288 | + expect(matchFixture([fixture], req)).toBe(fixture); |
| 289 | + }); |
| 290 | + |
| 291 | + it("does not match when no system message contains the substring", () => { |
| 292 | + const fixture = makeFixture({ systemMessage: "Atai" }); |
| 293 | + const req = makeReq({ |
| 294 | + messages: [ |
| 295 | + { role: "system", content: "User name is Alem." }, |
| 296 | + { role: "user", content: "Who am I?" }, |
| 297 | + ], |
| 298 | + }); |
| 299 | + expect(matchFixture([fixture], req)).toBeNull(); |
| 300 | + }); |
| 301 | + |
| 302 | + it("does not match when there are no system messages", () => { |
| 303 | + const fixture = makeFixture({ systemMessage: "anything" }); |
| 304 | + const req = makeReq({ messages: [{ role: "user", content: "hi" }] }); |
| 305 | + expect(matchFixture([fixture], req)).toBeNull(); |
| 306 | + }); |
| 307 | + |
| 308 | + it("matches across the joined text of multiple system messages", () => { |
| 309 | + const fixture = makeFixture({ systemMessage: "Atai" }); |
| 310 | + const req = makeReq({ |
| 311 | + messages: [ |
| 312 | + { role: "system", content: "Persona: helpful." }, |
| 313 | + { role: "system", content: "Context: name=Atai" }, |
| 314 | + { role: "user", content: "Who am I?" }, |
| 315 | + ], |
| 316 | + }); |
| 317 | + expect(matchFixture([fixture], req)).toBe(fixture); |
| 318 | + }); |
| 319 | + |
| 320 | + it("matches when system content is array-of-parts", () => { |
| 321 | + const fixture = makeFixture({ systemMessage: "Atai" }); |
| 322 | + const req = makeReq({ |
| 323 | + messages: [ |
| 324 | + { role: "system", content: [{ type: "text", text: "name=Atai" }] }, |
| 325 | + { role: "user", content: "Who am I?" }, |
| 326 | + ], |
| 327 | + }); |
| 328 | + expect(matchFixture([fixture], req)).toBe(fixture); |
| 329 | + }); |
| 330 | + |
| 331 | + it("combines with userMessage — both must match", () => { |
| 332 | + const fixture = makeFixture({ userMessage: "Who am I", systemMessage: "Atai" }); |
| 333 | + const matching = makeReq({ |
| 334 | + messages: [ |
| 335 | + { role: "system", content: "name=Atai" }, |
| 336 | + { role: "user", content: "Who am I?" }, |
| 337 | + ], |
| 338 | + }); |
| 339 | + expect(matchFixture([fixture], matching)).toBe(fixture); |
| 340 | + |
| 341 | + const userOnly = makeReq({ |
| 342 | + messages: [ |
| 343 | + { role: "system", content: "name=Alem" }, |
| 344 | + { role: "user", content: "Who am I?" }, |
| 345 | + ], |
| 346 | + }); |
| 347 | + expect(matchFixture([fixture], userOnly)).toBeNull(); |
| 348 | + |
| 349 | + const systemOnly = makeReq({ |
| 350 | + messages: [ |
| 351 | + { role: "system", content: "name=Atai" }, |
| 352 | + { role: "user", content: "Different prompt" }, |
| 353 | + ], |
| 354 | + }); |
| 355 | + expect(matchFixture([fixture], systemOnly)).toBeNull(); |
| 356 | + }); |
| 357 | + |
| 358 | + it("falls through to the next fixture on systemMessage miss", () => { |
| 359 | + const specific = makeFixture( |
| 360 | + { userMessage: "Who am I", systemMessage: "Atai" }, |
| 361 | + { content: "Hi Atai" }, |
| 362 | + ); |
| 363 | + const fallback = makeFixture({ userMessage: "Who am I" }, { content: "Hi user" }); |
| 364 | + const req = makeReq({ |
| 365 | + messages: [ |
| 366 | + { role: "system", content: "name=Alem" }, |
| 367 | + { role: "user", content: "Who am I?" }, |
| 368 | + ], |
| 369 | + }); |
| 370 | + expect(matchFixture([specific, fallback], req)).toBe(fallback); |
| 371 | + }); |
| 372 | +}); |
| 373 | + |
| 374 | +describe("matchFixture — systemMessage (RegExp)", () => { |
| 375 | + it("matches when the joined system text satisfies the regexp", () => { |
| 376 | + const fixture = makeFixture({ systemMessage: /name=Atai/ }); |
| 377 | + const req = makeReq({ |
| 378 | + messages: [ |
| 379 | + { role: "system", content: "ctx: name=Atai, tz=PST" }, |
| 380 | + { role: "user", content: "Who am I?" }, |
| 381 | + ], |
| 382 | + }); |
| 383 | + expect(matchFixture([fixture], req)).toBe(fixture); |
| 384 | + }); |
| 385 | + |
| 386 | + it("does not match when the regexp misses", () => { |
| 387 | + const fixture = makeFixture({ systemMessage: /name=Atai/ }); |
| 388 | + const req = makeReq({ |
| 389 | + messages: [ |
| 390 | + { role: "system", content: "ctx: name=Alem" }, |
| 391 | + { role: "user", content: "Who am I?" }, |
| 392 | + ], |
| 393 | + }); |
| 394 | + expect(matchFixture([fixture], req)).toBeNull(); |
| 395 | + }); |
| 396 | +}); |
| 397 | + |
240 | 398 | // --------------------------------------------------------------------------- |
241 | 399 | // matchFixture — toolCallId |
242 | 400 | // --------------------------------------------------------------------------- |
|
0 commit comments