|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | | -import { matchFixture, getLastMessageByRole } from "../router.js"; |
3 | | -import type { ChatCompletionRequest, ChatMessage, Fixture } from "../types.js"; |
| 2 | +import { matchFixture, getLastMessageByRole, getTextContent } from "../router.js"; |
| 3 | +import type { ChatCompletionRequest, ChatMessage, ContentPart, Fixture } from "../types.js"; |
4 | 4 |
|
5 | 5 | // --------------------------------------------------------------------------- |
6 | 6 | // Helpers |
@@ -54,6 +54,57 @@ describe("getLastMessageByRole", () => { |
54 | 54 | }); |
55 | 55 | }); |
56 | 56 |
|
| 57 | +// --------------------------------------------------------------------------- |
| 58 | +// getTextContent |
| 59 | +// --------------------------------------------------------------------------- |
| 60 | + |
| 61 | +describe("getTextContent", () => { |
| 62 | + it("returns the string as-is for string content", () => { |
| 63 | + expect(getTextContent("hello world")).toBe("hello world"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns null for null content", () => { |
| 67 | + expect(getTextContent(null)).toBeNull(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("extracts text from array-of-parts content", () => { |
| 71 | + const parts: ContentPart[] = [{ type: "text", text: "hello world" }]; |
| 72 | + expect(getTextContent(parts)).toBe("hello world"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("concatenates multiple text parts", () => { |
| 76 | + const parts: ContentPart[] = [ |
| 77 | + { type: "text", text: "hello " }, |
| 78 | + { type: "text", text: "world" }, |
| 79 | + ]; |
| 80 | + expect(getTextContent(parts)).toBe("hello world"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("ignores non-text parts in array content", () => { |
| 84 | + const parts: ContentPart[] = [ |
| 85 | + { type: "image_url", image_url: { url: "https://example.com/img.png" } }, |
| 86 | + { type: "text", text: "describe this" }, |
| 87 | + ]; |
| 88 | + expect(getTextContent(parts)).toBe("describe this"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("returns null for array with no text parts", () => { |
| 92 | + const parts: ContentPart[] = [ |
| 93 | + { type: "image_url", image_url: { url: "https://example.com/img.png" } }, |
| 94 | + ]; |
| 95 | + expect(getTextContent(parts)).toBeNull(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("returns null for empty array", () => { |
| 99 | + expect(getTextContent([])).toBeNull(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("returns null for array with only empty-string text parts", () => { |
| 103 | + const parts: ContentPart[] = [{ type: "text", text: "" }]; |
| 104 | + expect(getTextContent(parts)).toBeNull(); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
57 | 108 | // --------------------------------------------------------------------------- |
58 | 109 | // matchFixture — empty / null cases |
59 | 110 | // --------------------------------------------------------------------------- |
@@ -106,6 +157,61 @@ describe("matchFixture — userMessage (string)", () => { |
106 | 157 | }); |
107 | 158 | }); |
108 | 159 |
|
| 160 | +describe("matchFixture — userMessage (array content)", () => { |
| 161 | + it("matches when user content is array-of-parts with matching text", () => { |
| 162 | + const fixture = makeFixture({ userMessage: "hello" }); |
| 163 | + const req = makeReq({ |
| 164 | + messages: [{ role: "user", content: [{ type: "text", text: "say hello world" }] }], |
| 165 | + }); |
| 166 | + expect(matchFixture([fixture], req)).toBe(fixture); |
| 167 | + }); |
| 168 | + |
| 169 | + it("does not match when array-of-parts text does not include the string", () => { |
| 170 | + const fixture = makeFixture({ userMessage: "goodbye" }); |
| 171 | + const req = makeReq({ |
| 172 | + messages: [{ role: "user", content: [{ type: "text", text: "hello" }] }], |
| 173 | + }); |
| 174 | + expect(matchFixture([fixture], req)).toBeNull(); |
| 175 | + }); |
| 176 | + |
| 177 | + it("matches regexp against array-of-parts text", () => { |
| 178 | + const fixture = makeFixture({ userMessage: /^hello/i }); |
| 179 | + const req = makeReq({ |
| 180 | + messages: [{ role: "user", content: [{ type: "text", text: "Hello world" }] }], |
| 181 | + }); |
| 182 | + expect(matchFixture([fixture], req)).toBe(fixture); |
| 183 | + }); |
| 184 | + |
| 185 | + it("concatenates multiple text parts for matching", () => { |
| 186 | + const fixture = makeFixture({ userMessage: "hello world" }); |
| 187 | + const req = makeReq({ |
| 188 | + messages: [ |
| 189 | + { |
| 190 | + role: "user", |
| 191 | + content: [ |
| 192 | + { type: "text", text: "hello " }, |
| 193 | + { type: "text", text: "world" }, |
| 194 | + ], |
| 195 | + }, |
| 196 | + ], |
| 197 | + }); |
| 198 | + expect(matchFixture([fixture], req)).toBe(fixture); |
| 199 | + }); |
| 200 | + |
| 201 | + it("skips array content with no text parts", () => { |
| 202 | + const fixture = makeFixture({ userMessage: "hello" }); |
| 203 | + const req = makeReq({ |
| 204 | + messages: [ |
| 205 | + { |
| 206 | + role: "user", |
| 207 | + content: [{ type: "image_url", image_url: { url: "https://example.com" } }], |
| 208 | + }, |
| 209 | + ], |
| 210 | + }); |
| 211 | + expect(matchFixture([fixture], req)).toBeNull(); |
| 212 | + }); |
| 213 | +}); |
| 214 | + |
109 | 215 | describe("matchFixture — userMessage (RegExp)", () => { |
110 | 216 | it("matches when the last user message satisfies the regexp", () => { |
111 | 217 | const fixture = makeFixture({ userMessage: /^hello/i }); |
|
0 commit comments