|
| 1 | +import { TagMatcher } from "../tag-matcher" |
| 2 | + |
| 3 | +describe("TagMatcher", () => { |
| 4 | + describe("single tag name (backward compatibility)", () => { |
| 5 | + it("should extract content inside <think> tags", () => { |
| 6 | + const matcher = new TagMatcher("think") |
| 7 | + const result = matcher.final("<think>reasoning here</think> output text") |
| 8 | + expect(result).toEqual([ |
| 9 | + { matched: true, data: "reasoning here" }, |
| 10 | + { matched: false, data: " output text" }, |
| 11 | + ]) |
| 12 | + }) |
| 13 | + |
| 14 | + it("should handle streamed chunks", () => { |
| 15 | + const matcher = new TagMatcher("think") |
| 16 | + const chunks = [] |
| 17 | + chunks.push(...matcher.update("<thi")) |
| 18 | + chunks.push(...matcher.update("nk>reason")) |
| 19 | + chunks.push(...matcher.update("ing</think>")) |
| 20 | + chunks.push(...matcher.final(" done")) |
| 21 | + const allData = chunks.reduce( |
| 22 | + (acc, c) => { |
| 23 | + const key = c.matched ? "matched" : "unmatched" |
| 24 | + acc[key] += c.data |
| 25 | + return acc |
| 26 | + }, |
| 27 | + { matched: "", unmatched: "" }, |
| 28 | + ) |
| 29 | + expect(allData.matched).toBe("reasoning") |
| 30 | + expect(allData.unmatched).toBe(" done") |
| 31 | + }) |
| 32 | + |
| 33 | + it("should pass through text with no tags", () => { |
| 34 | + const matcher = new TagMatcher("think") |
| 35 | + const result = matcher.final("just some text") |
| 36 | + expect(result).toEqual([{ matched: false, data: "just some text" }]) |
| 37 | + }) |
| 38 | + |
| 39 | + it("tagName getter returns first tag name", () => { |
| 40 | + const matcher = new TagMatcher("think") |
| 41 | + expect(matcher.tagName).toBe("think") |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + describe("multiple tag names", () => { |
| 46 | + it("should extract content inside <thought> tags", () => { |
| 47 | + const matcher = new TagMatcher(["think", "thought"]) |
| 48 | + const result = matcher.final("<thought>reasoning here</thought> output text") |
| 49 | + expect(result).toEqual([ |
| 50 | + { matched: true, data: "reasoning here" }, |
| 51 | + { matched: false, data: " output text" }, |
| 52 | + ]) |
| 53 | + }) |
| 54 | + |
| 55 | + it("should still extract content inside <think> tags", () => { |
| 56 | + const matcher = new TagMatcher(["think", "thought"]) |
| 57 | + const result = matcher.final("<think>reasoning here</think> output text") |
| 58 | + expect(result).toEqual([ |
| 59 | + { matched: true, data: "reasoning here" }, |
| 60 | + { matched: false, data: " output text" }, |
| 61 | + ]) |
| 62 | + }) |
| 63 | + |
| 64 | + it("should handle streamed <thought> tags across chunks", () => { |
| 65 | + const matcher = new TagMatcher(["think", "thought"]) |
| 66 | + const chunks = [] |
| 67 | + chunks.push(...matcher.update("<thou")) |
| 68 | + chunks.push(...matcher.update("ght>my rea")) |
| 69 | + chunks.push(...matcher.update("soning</thought>")) |
| 70 | + chunks.push(...matcher.final(" answer")) |
| 71 | + const allData = chunks.reduce( |
| 72 | + (acc, c) => { |
| 73 | + const key = c.matched ? "matched" : "unmatched" |
| 74 | + acc[key] += c.data |
| 75 | + return acc |
| 76 | + }, |
| 77 | + { matched: "", unmatched: "" }, |
| 78 | + ) |
| 79 | + expect(allData.matched).toBe("my reasoning") |
| 80 | + expect(allData.unmatched).toBe(" answer") |
| 81 | + }) |
| 82 | + |
| 83 | + it("should not match mismatched open/close tags", () => { |
| 84 | + // <think> opened but </thought> close - should not match as valid close |
| 85 | + const matcher = new TagMatcher(["think", "thought"]) |
| 86 | + const result = matcher.final("<think>content</thought>more") |
| 87 | + // The close tag won't match because activeTagName is "think" |
| 88 | + // so </thought> is not recognized as closing it |
| 89 | + const matchedData = result.filter((c) => c.matched).map((c) => c.data) |
| 90 | + const unmatchedData = result.filter((c) => !c.matched).map((c) => c.data) |
| 91 | + // Content stays matched because the tag was never properly closed |
| 92 | + expect(matchedData.join("")).toContain("content") |
| 93 | + expect(unmatchedData.join("")).not.toContain("content") |
| 94 | + }) |
| 95 | + |
| 96 | + it("should handle text before thought tag", () => { |
| 97 | + const matcher = new TagMatcher(["think", "thought"], undefined, 0) |
| 98 | + const result = matcher.final("<thought>reasoning</thought>answer") |
| 99 | + expect(result).toEqual([ |
| 100 | + { matched: true, data: "reasoning" }, |
| 101 | + { matched: false, data: "answer" }, |
| 102 | + ]) |
| 103 | + }) |
| 104 | + |
| 105 | + it("should ignore non-matching tags", () => { |
| 106 | + const matcher = new TagMatcher(["think", "thought"]) |
| 107 | + const result = matcher.final("<div>not a match</div>") |
| 108 | + expect(result).toEqual([{ matched: false, data: "<div>not a match</div>" }]) |
| 109 | + }) |
| 110 | + |
| 111 | + it("tagName getter returns first tag name from array", () => { |
| 112 | + const matcher = new TagMatcher(["think", "thought"]) |
| 113 | + expect(matcher.tagName).toBe("think") |
| 114 | + }) |
| 115 | + |
| 116 | + it("tagNames contains all provided tag names", () => { |
| 117 | + const matcher = new TagMatcher(["think", "thought"]) |
| 118 | + expect(matcher.tagNames).toEqual(["think", "thought"]) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe("with transform", () => { |
| 123 | + it("should apply transform to thought tag results", () => { |
| 124 | + const matcher = new TagMatcher(["think", "thought"], (chunk) => ({ |
| 125 | + type: chunk.matched ? "reasoning" : "text", |
| 126 | + text: chunk.data, |
| 127 | + })) |
| 128 | + const result = matcher.final("<thought>my reasoning</thought>my answer") |
| 129 | + expect(result).toEqual([ |
| 130 | + { type: "reasoning", text: "my reasoning" }, |
| 131 | + { type: "text", text: "my answer" }, |
| 132 | + ]) |
| 133 | + }) |
| 134 | + }) |
| 135 | +}) |
0 commit comments