-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathrouter.test.ts
More file actions
1532 lines (1370 loc) · 58.2 KB
/
Copy pathrouter.test.ts
File metadata and controls
1532 lines (1370 loc) · 58.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect } from "vitest";
import {
matchFixture,
getLastMessageByRole,
getSystemText,
getTextContent,
getLastUserText,
} from "../router.js";
import type { ChatCompletionRequest, ChatMessage, ContentPart, Fixture } from "../types.js";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function makeReq(overrides: Partial<ChatCompletionRequest> = {}): ChatCompletionRequest {
return {
model: "gpt-4o",
messages: [{ role: "user", content: "hello" }],
...overrides,
};
}
function makeFixture(
match: Fixture["match"],
response: Fixture["response"] = { content: "ok" },
): Fixture {
return { match, response };
}
// ---------------------------------------------------------------------------
// getLastMessageByRole
// ---------------------------------------------------------------------------
describe("getLastMessageByRole", () => {
it("returns the last message with the matching role", () => {
const messages: ChatMessage[] = [
{ role: "user", content: "first" },
{ role: "assistant", content: "reply" },
{ role: "user", content: "second" },
];
const result = getLastMessageByRole(messages, "user");
expect(result?.content).toBe("second");
});
it("returns null when no message has the given role", () => {
const messages: ChatMessage[] = [{ role: "user", content: "hi" }];
expect(getLastMessageByRole(messages, "tool")).toBeNull();
});
it("returns null for an empty array", () => {
expect(getLastMessageByRole([], "user")).toBeNull();
});
it("returns the only message when there is exactly one match", () => {
const messages: ChatMessage[] = [
{ role: "system", content: "you are helpful" },
{ role: "user", content: "question" },
];
expect(getLastMessageByRole(messages, "system")?.content).toBe("you are helpful");
});
});
// ---------------------------------------------------------------------------
// getTextContent
// ---------------------------------------------------------------------------
describe("getTextContent", () => {
it("returns the string as-is for string content", () => {
expect(getTextContent("hello world")).toBe("hello world");
});
it("returns null for null content", () => {
expect(getTextContent(null)).toBeNull();
});
it("extracts text from array-of-parts content", () => {
const parts: ContentPart[] = [{ type: "text", text: "hello world" }];
expect(getTextContent(parts)).toBe("hello world");
});
it("concatenates multiple text parts", () => {
const parts: ContentPart[] = [
{ type: "text", text: "hello " },
{ type: "text", text: "world" },
];
expect(getTextContent(parts)).toBe("hello world");
});
it("ignores non-text parts in array content", () => {
const parts: ContentPart[] = [
{ type: "image_url", image_url: { url: "https://example.com/img.png" } },
{ type: "text", text: "describe this" },
];
expect(getTextContent(parts)).toBe("describe this");
});
it("returns null for array with no text parts", () => {
const parts: ContentPart[] = [
{ type: "image_url", image_url: { url: "https://example.com/img.png" } },
];
expect(getTextContent(parts)).toBeNull();
});
it("returns null for empty array", () => {
expect(getTextContent([])).toBeNull();
});
it("returns the empty string (NOT null) for an array with a present-but-empty text part", () => {
// Symmetric with the string path: getTextContent("") returns "", so an
// array carrying a present-but-empty text part likewise returns "" — a
// present-but-empty body, distinct from `null` (no text content at all).
const parts: ContentPart[] = [{ type: "text", text: "" }];
expect(getTextContent(parts)).toBe("");
});
});
// ---------------------------------------------------------------------------
// matchFixture — empty / null cases
// ---------------------------------------------------------------------------
describe("matchFixture — empty / null", () => {
it("returns null for an empty fixtures array", () => {
expect(matchFixture([], makeReq())).toBeNull();
});
it("returns null when no fixture matches", () => {
const fixtures = [makeFixture({ userMessage: "goodbye" })];
expect(matchFixture(fixtures, makeReq())).toBeNull();
});
});
// ---------------------------------------------------------------------------
// matchFixture — userMessage
// ---------------------------------------------------------------------------
describe("matchFixture — userMessage (string)", () => {
it("matches when the last user message includes the string", () => {
const fixture = makeFixture({ userMessage: "hello" });
const req = makeReq({ messages: [{ role: "user", content: "say hello world" }] });
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the last user message does not include the string", () => {
const fixture = makeFixture({ userMessage: "goodbye" });
const req = makeReq({ messages: [{ role: "user", content: "hello" }] });
expect(matchFixture([fixture], req)).toBeNull();
});
it("matches against the LAST user message, not an earlier one", () => {
const fixture = makeFixture({ userMessage: "final" });
const req = makeReq({
messages: [
{ role: "user", content: "first message with final word" },
{ role: "assistant", content: "reply" },
{ role: "user", content: "second message" },
],
});
// "final" appears in the first user message but not the last
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match when there is no user message", () => {
const fixture = makeFixture({ userMessage: "hello" });
const req = makeReq({ messages: [{ role: "system", content: "hello system" }] });
expect(matchFixture([fixture], req)).toBeNull();
});
});
describe("matchFixture — userMessage (array content)", () => {
it("matches when user content is array-of-parts with matching text", () => {
const fixture = makeFixture({ userMessage: "hello" });
const req = makeReq({
messages: [{ role: "user", content: [{ type: "text", text: "say hello world" }] }],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when array-of-parts text does not include the string", () => {
const fixture = makeFixture({ userMessage: "goodbye" });
const req = makeReq({
messages: [{ role: "user", content: [{ type: "text", text: "hello" }] }],
});
expect(matchFixture([fixture], req)).toBeNull();
});
it("matches regexp against array-of-parts text", () => {
const fixture = makeFixture({ userMessage: /^hello/i });
const req = makeReq({
messages: [{ role: "user", content: [{ type: "text", text: "Hello world" }] }],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("concatenates multiple text parts for matching", () => {
const fixture = makeFixture({ userMessage: "hello world" });
const req = makeReq({
messages: [
{
role: "user",
content: [
{ type: "text", text: "hello " },
{ type: "text", text: "world" },
],
},
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("skips array content with no text parts", () => {
const fixture = makeFixture({ userMessage: "hello" });
const req = makeReq({
messages: [
{
role: "user",
content: [{ type: "image_url", image_url: { url: "https://example.com" } }],
},
],
});
expect(matchFixture([fixture], req)).toBeNull();
});
it("matches the text prompt when a trailing user message is attachment-only (multimodal image split)", () => {
// Some SDKs (e.g. Microsoft Agent Framework's agent_framework_openai image
// path) serialise a single multimodal turn into a text-only user message
// FOLLOWED by a separate attachment-only user message. The trailing
// image-only message must not shadow the real prompt.
const fixture = makeFixture({ userMessage: "describe this image" });
const req = makeReq({
messages: [
{ role: "user", content: "please describe this image" },
{
role: "user",
content: [{ type: "image_url", image_url: { url: "data:image/png;base64,AAAA" } }],
},
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("keeps matching a trailing user message that HAS text (does not skip a flattened attachment)", () => {
// Contrast with the image split above: when the trailing user message
// carries text (e.g. a pdf flattened to `[Attached document]\n…` by the
// agent) it is NOT skipped — it is the match target. Fixtures for such a
// turn therefore key on the flattened body, not the original prompt.
const fixture = makeFixture({ userMessage: "CopilotKit Quickstart" });
const req = makeReq({
messages: [
{ role: "user", content: "can you tell me what is in this demo pdf I just attached" },
{
role: "user",
content: "[Attached document]\nCopilotKit Quickstart\nAdd AI copilots to your app.",
},
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
});
describe("matchFixture — userMessage (RegExp)", () => {
it("matches when the last user message satisfies the regexp", () => {
const fixture = makeFixture({ userMessage: /^hello/i });
const req = makeReq({ messages: [{ role: "user", content: "Hello world" }] });
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the regexp does not match", () => {
const fixture = makeFixture({ userMessage: /^goodbye/i });
const req = makeReq({ messages: [{ role: "user", content: "Hello world" }] });
expect(matchFixture([fixture], req)).toBeNull();
});
it("uses regexp against the last user message only", () => {
const fixture = makeFixture({ userMessage: /first/ });
const req = makeReq({
messages: [
{ role: "user", content: "first message" },
{ role: "user", content: "second message" },
],
});
expect(matchFixture([fixture], req)).toBeNull();
});
});
// ---------------------------------------------------------------------------
// getSystemText
// ---------------------------------------------------------------------------
describe("getSystemText", () => {
it("returns empty string when there are no system messages", () => {
expect(getSystemText([{ role: "user", content: "hi" }])).toBe("");
});
it("returns the single system message text", () => {
expect(
getSystemText([
{ role: "system", content: "You are helpful." },
{ role: "user", content: "hi" },
]),
).toBe("You are helpful.");
});
it("joins multiple system messages with newlines in order", () => {
expect(
getSystemText([
{ role: "system", content: "first" },
{ role: "user", content: "ignored" },
{ role: "system", content: "second" },
]),
).toBe("first\nsecond");
});
it("extracts text from array-of-parts system content", () => {
expect(
getSystemText([{ role: "system", content: [{ type: "text", text: "from parts" }] }]),
).toBe("from parts");
});
});
// ---------------------------------------------------------------------------
// matchFixture — systemMessage
// ---------------------------------------------------------------------------
describe("matchFixture — systemMessage (string)", () => {
it("matches when a system message contains the substring", () => {
const fixture = makeFixture({ systemMessage: "Atai" });
const req = makeReq({
messages: [
{ role: "system", content: "User name is Atai. Timezone America/Los_Angeles." },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when no system message contains the substring", () => {
const fixture = makeFixture({ systemMessage: "Atai" });
const req = makeReq({
messages: [
{ role: "system", content: "User name is Alem." },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match when there are no system messages", () => {
const fixture = makeFixture({ systemMessage: "anything" });
const req = makeReq({ messages: [{ role: "user", content: "hi" }] });
expect(matchFixture([fixture], req)).toBeNull();
});
it("matches across the joined text of multiple system messages", () => {
const fixture = makeFixture({ systemMessage: "Atai" });
const req = makeReq({
messages: [
{ role: "system", content: "Persona: helpful." },
{ role: "system", content: "Context: name=Atai" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("matches when system content is array-of-parts", () => {
const fixture = makeFixture({ systemMessage: "Atai" });
const req = makeReq({
messages: [
{ role: "system", content: [{ type: "text", text: "name=Atai" }] },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("combines with userMessage — both must match", () => {
const fixture = makeFixture({ userMessage: "Who am I", systemMessage: "Atai" });
const matching = makeReq({
messages: [
{ role: "system", content: "name=Atai" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], matching)).toBe(fixture);
const userOnly = makeReq({
messages: [
{ role: "system", content: "name=Alem" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], userOnly)).toBeNull();
const systemOnly = makeReq({
messages: [
{ role: "system", content: "name=Atai" },
{ role: "user", content: "Different prompt" },
],
});
expect(matchFixture([fixture], systemOnly)).toBeNull();
});
it("falls through to the next fixture on systemMessage miss", () => {
const specific = makeFixture(
{ userMessage: "Who am I", systemMessage: "Atai" },
{ content: "Hi Atai" },
);
const fallback = makeFixture({ userMessage: "Who am I" }, { content: "Hi user" });
const req = makeReq({
messages: [
{ role: "system", content: "name=Alem" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([specific, fallback], req)).toBe(fallback);
});
});
describe("matchFixture — systemMessage (string[] AND)", () => {
it("matches when every substring is present in the system text", () => {
const fixture = makeFixture({ systemMessage: ["name=Atai", "tz=PST"] });
const req = makeReq({
messages: [
{ role: "system", content: "ctx: name=Atai\nctx: tz=PST\nctx: misc" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when any substring is missing", () => {
const fixture = makeFixture({ systemMessage: ["name=Atai", "tz=PST"] });
const req = makeReq({
messages: [
{ role: "system", content: "ctx: name=Atai\nctx: tz=EST" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBeNull();
});
it("matches across multiple system messages (any substring may live in any of them)", () => {
const fixture = makeFixture({ systemMessage: ["name=Atai", "default-activities"] });
const req = makeReq({
messages: [
{ role: "system", content: "Persona: helpful." },
{ role: "system", content: "Context: name=Atai" },
{ role: "system", content: "Context: default-activities" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when there are no system messages", () => {
const fixture = makeFixture({ systemMessage: ["anything"] });
const req = makeReq({ messages: [{ role: "user", content: "hi" }] });
expect(matchFixture([fixture], req)).toBeNull();
});
it("treats single-element array same as a string substring", () => {
const fixture = makeFixture({ systemMessage: ["Atai"] });
const req = makeReq({
messages: [
{ role: "system", content: "name=Atai" },
{ role: "user", content: "hi" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("combines with userMessage — both gate plus all substrings must match", () => {
const fixture = makeFixture({
userMessage: "Plan my morning",
systemMessage: ["name=Atai", "tz=PST"],
});
const matching = makeReq({
messages: [
{ role: "system", content: "name=Atai\ntz=PST" },
{ role: "user", content: "Plan my morning please" },
],
});
expect(matchFixture([fixture], matching)).toBe(fixture);
const partial = makeReq({
messages: [
{ role: "system", content: "name=Atai" }, // tz missing
{ role: "user", content: "Plan my morning please" },
],
});
expect(matchFixture([fixture], partial)).toBeNull();
});
it("falls through to the next fixture when one substring is missing", () => {
const specific = makeFixture(
{ userMessage: "hi", systemMessage: ["Atai", "PST"] },
{ content: "exact-defaults" },
);
const fallback = makeFixture({ userMessage: "hi" }, { content: "generic" });
const req = makeReq({
messages: [
{ role: "system", content: "name=Atai\ntz=EST" }, // tz mismatch
{ role: "user", content: "hi" },
],
});
expect(matchFixture([specific, fallback], req)).toBe(fallback);
});
});
describe("matchFixture — systemMessage (RegExp)", () => {
it("matches when the joined system text satisfies the regexp", () => {
const fixture = makeFixture({ systemMessage: /name=Atai/ });
const req = makeReq({
messages: [
{ role: "system", content: "ctx: name=Atai, tz=PST" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the regexp misses", () => {
const fixture = makeFixture({ systemMessage: /name=Atai/ });
const req = makeReq({
messages: [
{ role: "system", content: "ctx: name=Alem" },
{ role: "user", content: "Who am I?" },
],
});
expect(matchFixture([fixture], req)).toBeNull();
});
});
// ---------------------------------------------------------------------------
// matchFixture — toolCallId
// ---------------------------------------------------------------------------
describe("matchFixture — toolCallId", () => {
it("matches when the last tool message has the matching tool_call_id", () => {
const fixture = makeFixture({ toolCallId: "call_abc123" });
const req = makeReq({
messages: [
{ role: "user", content: "use a tool" },
{ role: "tool", content: "result", tool_call_id: "call_abc123" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the tool_call_id is different", () => {
const fixture = makeFixture({ toolCallId: "call_abc123" });
const req = makeReq({
messages: [{ role: "tool", content: "result", tool_call_id: "call_other" }],
});
expect(matchFixture([fixture], req)).toBeNull();
});
it("matches against the LAST tool message", () => {
const fixture = makeFixture({ toolCallId: "call_second" });
const req = makeReq({
messages: [
{ role: "tool", content: "first", tool_call_id: "call_first" },
{ role: "tool", content: "second", tool_call_id: "call_second" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when there is no tool message", () => {
const fixture = makeFixture({ toolCallId: "call_abc123" });
const req = makeReq({ messages: [{ role: "user", content: "hello" }] });
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match when a new user turn follows the tool message", () => {
// Regression: a toolCallId fixture is the response to a tool result, so it
// must only fire when the tool message is the LAST message in the request.
// If the user sends another turn after the tool result, the stale tool_call_id
// in history must not shadow userMessage matchers for the new turn.
const stale = makeFixture(
{ toolCallId: "call_pie_chart" },
{ content: "Pie chart rendered above" },
);
const fresh = makeFixture({ userMessage: "bar chart" }, { content: "bar chart response" });
const req = makeReq({
messages: [
{ role: "user", content: "show me a pie chart" },
{
role: "assistant",
content: null,
tool_calls: [
{
id: "call_pie_chart",
type: "function",
function: { name: "pieChart", arguments: "{}" },
},
],
},
{ role: "tool", content: "{}", tool_call_id: "call_pie_chart" },
{ role: "assistant", content: "Pie chart rendered above" },
{ role: "user", content: "now show me a bar chart" },
],
});
expect(matchFixture([stale, fresh], req)).toBe(fresh);
});
it("does not match when an assistant content message follows the tool message", () => {
// The assistant has already emitted its final content for the tool result;
// any follow-up LLM call that arrives in this state should not re-fire the
// toolCallId fixture (which would loop the same content back).
const stale = makeFixture({ toolCallId: "call_abc" }, { content: "tool answered" });
const req = makeReq({
messages: [
{ role: "user", content: "do thing" },
{
role: "assistant",
content: null,
tool_calls: [
{ id: "call_abc", type: "function", function: { name: "thing", arguments: "{}" } },
],
},
{ role: "tool", content: "{}", tool_call_id: "call_abc" },
{ role: "assistant", content: "tool answered" },
],
});
expect(matchFixture([stale], req)).toBeNull();
});
});
// ---------------------------------------------------------------------------
// matchFixture — toolResultContains
// ---------------------------------------------------------------------------
describe("matchFixture — toolResultContains", () => {
it("discriminates approve vs cancel legs that share a toolCallId", () => {
// The motivating case: a human-in-the-loop suspend tool resumes with the
// SAME tool_call_id for both outcomes; only the tool-result JSON differs.
const cancelled = makeFixture(
{ toolCallId: "call_schedule_001", toolResultContains: '"cancelled"' },
{ content: "No problem — nothing was booked." },
);
const confirmed = makeFixture(
{ toolCallId: "call_schedule_001", toolResultContains: '"chosen_' },
{ content: "Booked: Monday 9:00 AM confirmed." },
);
const base = [
{ role: "user" as const, content: "schedule a meeting" },
{
role: "assistant" as const,
content: null,
tool_calls: [
{
id: "call_schedule_001",
type: "function" as const,
function: { name: "schedule_meeting", arguments: "{}" },
},
],
},
];
const cancelReq = makeReq({
messages: [
...base,
{ role: "tool", content: '{"cancelled": true}', tool_call_id: "call_schedule_001" },
],
});
const pickReq = makeReq({
messages: [
...base,
{
role: "tool",
content: '{"chosen_time": "2026-07-20T09:00:00Z", "chosen_label": "Monday 9:00 AM"}',
tool_call_id: "call_schedule_001",
},
],
});
const fixtures = [cancelled, confirmed];
expect(matchFixture(fixtures, cancelReq)).toBe(cancelled);
expect(matchFixture(fixtures, pickReq)).toBe(confirmed);
});
it("matches when the last tool message contains the substring", () => {
const fixture = makeFixture({ toolResultContains: "cancelled" });
const req = makeReq({
messages: [
{ role: "user", content: "do thing" },
{ role: "tool", content: '{"cancelled": true}', tool_call_id: "call_x" },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the substring is absent", () => {
const fixture = makeFixture({ toolResultContains: "cancelled" });
const req = makeReq({
messages: [
{ role: "user", content: "do thing" },
{ role: "tool", content: '{"chosen_time": "9am"}', tool_call_id: "call_x" },
],
});
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match a request with no tool message", () => {
const fixture = makeFixture({ toolResultContains: "cancelled" });
const req = makeReq({ messages: [{ role: "user", content: "cancelled my plans" }] });
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match when a newer user turn follows the tool message", () => {
// Same last-message rule as toolCallId: a stale tool result in history
// must not shadow matchers for the new turn.
const fixture = makeFixture({ toolResultContains: "cancelled" });
const req = makeReq({
messages: [
{ role: "user", content: "do thing" },
{ role: "tool", content: '{"cancelled": true}', tool_call_id: "call_x" },
{ role: "assistant", content: "Cancelled." },
{ role: "user", content: "something else" },
],
});
expect(matchFixture([fixture], req)).toBeNull();
});
it("matches array-of-parts tool content", () => {
const fixture = makeFixture({ toolResultContains: "cancelled" });
const req = makeReq({
messages: [
{ role: "user", content: "do thing" },
{
role: "tool",
content: [{ type: "text", text: '{"cancelled": true}' }],
tool_call_id: "call_x",
},
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match tool content with no extractable text", () => {
const fixture = makeFixture({ toolResultContains: "cancelled" });
const req = makeReq({
messages: [
{ role: "user", content: "do thing" },
{ role: "tool", content: null, tool_call_id: "call_x" },
],
});
expect(matchFixture([fixture], req)).toBeNull();
});
});
// ---------------------------------------------------------------------------
// matchFixture — toolName
// ---------------------------------------------------------------------------
describe("matchFixture — toolName", () => {
it("matches when any tool definition has the matching function name", () => {
const fixture = makeFixture({ toolName: "get_weather" });
const req = makeReq({
tools: [
{ type: "function", function: { name: "get_time" } },
{ type: "function", function: { name: "get_weather" } },
],
});
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when no tool has the function name", () => {
const fixture = makeFixture({ toolName: "get_weather" });
const req = makeReq({
tools: [{ type: "function", function: { name: "get_time" } }],
});
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match when tools is undefined", () => {
const fixture = makeFixture({ toolName: "get_weather" });
const req = makeReq({ tools: undefined });
expect(matchFixture([fixture], req)).toBeNull();
});
});
// ---------------------------------------------------------------------------
// matchFixture — model
// ---------------------------------------------------------------------------
describe("matchFixture — model (string)", () => {
it("matches when the model is an exact string match", () => {
const fixture = makeFixture({ model: "gpt-4o" });
const req = makeReq({ model: "gpt-4o" });
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match gpt-4o fixture against gpt-4o-mini (dash + letter, not date suffix)", () => {
const fixture = makeFixture({ model: "gpt-4o" });
const req = makeReq({ model: "gpt-4o-mini" });
expect(matchFixture([fixture], req)).toBeNull();
});
it("matches when the request model has a date suffix (dash + digit)", () => {
const fixture = makeFixture({ model: "gpt-4o" });
const req = makeReq({ model: "gpt-4o-2024-08-06" });
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the request model does not start with the fixture model", () => {
const fixture = makeFixture({ model: "gpt-4o" });
const req = makeReq({ model: "gpt-3.5-turbo" });
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match gpt-4 fixture against gpt-4o request (no dash boundary)", () => {
const fixture = makeFixture({ model: "gpt-4" });
const req = makeReq({ model: "gpt-4o" });
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match when request model is undefined", () => {
const fixture = makeFixture({ model: "gpt-4o" });
const req = makeReq({ model: undefined });
expect(matchFixture([fixture], req)).toBeNull();
});
});
describe("matchFixture — model (startsWith)", () => {
it("matches when request model starts with fixture model", () => {
const fixture = makeFixture({ model: "claude-opus-4" });
const req = makeReq({ model: "claude-opus-4-20250514" });
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("matches exact model strings", () => {
const fixture = makeFixture({ model: "gpt-4o" });
const req = makeReq({ model: "gpt-4o" });
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when models diverge", () => {
const fixture = makeFixture({ model: "claude-opus-4" });
const req = makeReq({ model: "claude-haiku-4" });
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match when fixture model is longer than request model", () => {
const fixture = makeFixture({ model: "claude-opus-4-20250514" });
const req = makeReq({ model: "claude-opus-4" });
expect(matchFixture([fixture], req)).toBeNull();
});
it("still supports regexp model matching", () => {
const fixture = makeFixture({ model: /^claude-opus/ });
const req = makeReq({ model: "claude-opus-4-20250514" });
expect(matchFixture([fixture], req)).toBe(fixture);
});
});
describe("matchFixture — model (RegExp)", () => {
it("matches when the model satisfies the regexp", () => {
const fixture = makeFixture({ model: /^gpt-4/ });
const req = makeReq({ model: "gpt-4o-mini" });
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the regexp does not match the model", () => {
const fixture = makeFixture({ model: /^claude/ });
const req = makeReq({ model: "gpt-4o" });
expect(matchFixture([fixture], req)).toBeNull();
});
});
// ---------------------------------------------------------------------------
// matchFixture — predicate
// ---------------------------------------------------------------------------
describe("matchFixture — predicate", () => {
it("matches when the predicate returns true", () => {
const fixture = makeFixture({ predicate: (req) => req.model === "special-model" });
const req = makeReq({ model: "special-model" });
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the predicate returns false", () => {
const fixture = makeFixture({ predicate: () => false });
expect(matchFixture([fixture], makeReq())).toBeNull();
});
it("predicate receives the full request", () => {
let capturedReq: ChatCompletionRequest | null = null;
const req = makeReq({ model: "gpt-4o", temperature: 0.7 });
const fixture = makeFixture({
predicate: (r) => {
capturedReq = r;
return true;
},
});
matchFixture([fixture], req);
expect(capturedReq).toBe(req);
});
});
// ---------------------------------------------------------------------------
// matchFixture — AND logic (combined fields)
// ---------------------------------------------------------------------------
describe("matchFixture — AND logic", () => {
it("matches only when all specified fields are satisfied", () => {
const fixture = makeFixture({ userMessage: "hello", model: "gpt-4o" });
const matchingReq = makeReq({
model: "gpt-4o",
messages: [{ role: "user", content: "hello world" }],
});
const wrongModel = makeReq({
model: "gpt-3.5",
messages: [{ role: "user", content: "hello world" }],
});
const wrongMessage = makeReq({
model: "gpt-4o",
messages: [{ role: "user", content: "goodbye" }],
});
expect(matchFixture([fixture], matchingReq)).toBe(fixture);
expect(matchFixture([fixture], wrongModel)).toBeNull();
expect(matchFixture([fixture], wrongMessage)).toBeNull();
});
it("combines predicate with other fields using AND", () => {
const fixture = makeFixture({
model: "gpt-4o",
predicate: (req) => (req.temperature ?? 0) > 0.5,
});
const both = makeReq({ model: "gpt-4o", temperature: 0.9 });
const onlyModel = makeReq({ model: "gpt-4o", temperature: 0.1 });
const onlyPredicate = makeReq({ model: "gpt-3.5", temperature: 0.9 });
expect(matchFixture([fixture], both)).toBe(fixture);
expect(matchFixture([fixture], onlyModel)).toBeNull();
expect(matchFixture([fixture], onlyPredicate)).toBeNull();
});
it("empty match object matches any request", () => {
const fixture = makeFixture({});
expect(matchFixture([fixture], makeReq())).toBe(fixture);
});
});
// ---------------------------------------------------------------------------
// matchFixture — inputText (embedding matching)
// ---------------------------------------------------------------------------
describe("matchFixture — inputText (string)", () => {
it("matches when embeddingInput includes the string", () => {
const fixture = makeFixture({ inputText: "hello" });
const req = { ...makeReq(), embeddingInput: "say hello world" } as ChatCompletionRequest & {
embeddingInput: string;
};
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when embeddingInput does not include the string", () => {
const fixture = makeFixture({ inputText: "goodbye" });
const req = { ...makeReq(), embeddingInput: "hello" } as ChatCompletionRequest & {
embeddingInput: string;
};
expect(matchFixture([fixture], req)).toBeNull();
});
it("does not match when embeddingInput is not present", () => {
const fixture = makeFixture({ inputText: "hello" });
expect(matchFixture([fixture], makeReq())).toBeNull();
});
});
describe("matchFixture — inputText (RegExp)", () => {
it("matches when embeddingInput satisfies the regexp", () => {
const fixture = makeFixture({ inputText: /^hello/i });
const req = { ...makeReq(), embeddingInput: "Hello world" } as ChatCompletionRequest & {
embeddingInput: string;
};
expect(matchFixture([fixture], req)).toBe(fixture);
});
it("does not match when the regexp does not match", () => {
const fixture = makeFixture({ inputText: /^goodbye/i });
const req = { ...makeReq(), embeddingInput: "hello world" } as ChatCompletionRequest & {
embeddingInput: string;
};
expect(matchFixture([fixture], req)).toBeNull();
});
});
// ---------------------------------------------------------------------------
// matchFixture — responseFormat
// ---------------------------------------------------------------------------
describe("matchFixture — responseFormat", () => {
it("matches when response_format.type equals the fixture responseFormat", () => {
const fixture = makeFixture({ responseFormat: "json_object" });
const req = makeReq({ response_format: { type: "json_object" } });
expect(matchFixture([fixture], req)).toBe(fixture);
});