|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { ReasoningXmlMatcher } from "../reasoning-xml-matcher" |
| 3 | + |
| 4 | +describe("ReasoningXmlMatcher", () => { |
| 5 | + it("should match <think> tags", () => { |
| 6 | + const matcher = new ReasoningXmlMatcher() |
| 7 | + const input = "Some text <think>This is reasoning content</think> more text" |
| 8 | + const results = matcher.final(input) |
| 9 | + |
| 10 | + expect(results).toHaveLength(3) |
| 11 | + expect(results[0]).toEqual({ matched: false, data: "Some text " }) |
| 12 | + expect(results[1]).toEqual({ matched: true, data: "<think>This is reasoning content</think>" }) |
| 13 | + expect(results[2]).toEqual({ matched: false, data: " more text" }) |
| 14 | + }) |
| 15 | + |
| 16 | + it("should match <thinking> tags", () => { |
| 17 | + const matcher = new ReasoningXmlMatcher() |
| 18 | + const input = "Some text <thinking>This is reasoning content</thinking> more text" |
| 19 | + const results = matcher.final(input) |
| 20 | + |
| 21 | + expect(results).toHaveLength(3) |
| 22 | + expect(results[0]).toEqual({ matched: false, data: "Some text " }) |
| 23 | + expect(results[1]).toEqual({ matched: true, data: "<thinking>This is reasoning content</thinking>" }) |
| 24 | + expect(results[2]).toEqual({ matched: false, data: " more text" }) |
| 25 | + }) |
| 26 | + |
| 27 | + it("should match <reasoning> tags", () => { |
| 28 | + const matcher = new ReasoningXmlMatcher() |
| 29 | + const input = "Some text <reasoning>This is reasoning content</reasoning> more text" |
| 30 | + const results = matcher.final(input) |
| 31 | + |
| 32 | + expect(results).toHaveLength(3) |
| 33 | + expect(results[0]).toEqual({ matched: false, data: "Some text " }) |
| 34 | + expect(results[1]).toEqual({ matched: true, data: "<reasoning>This is reasoning content</reasoning>" }) |
| 35 | + expect(results[2]).toEqual({ matched: false, data: " more text" }) |
| 36 | + }) |
| 37 | + |
| 38 | + it("should match <thought> tags", () => { |
| 39 | + const matcher = new ReasoningXmlMatcher() |
| 40 | + const input = "Some text <thought>This is reasoning content</thought> more text" |
| 41 | + const results = matcher.final(input) |
| 42 | + |
| 43 | + expect(results).toHaveLength(3) |
| 44 | + expect(results[0]).toEqual({ matched: false, data: "Some text " }) |
| 45 | + expect(results[1]).toEqual({ matched: true, data: "<thought>This is reasoning content</thought>" }) |
| 46 | + expect(results[2]).toEqual({ matched: false, data: " more text" }) |
| 47 | + }) |
| 48 | + |
| 49 | + it("should handle streaming updates for all tag variants", () => { |
| 50 | + const testCases = [ |
| 51 | + { tag: "think", content: "Thinking about the problem" }, |
| 52 | + { tag: "thinking", content: "Processing the request" }, |
| 53 | + { tag: "reasoning", content: "Analyzing the situation" }, |
| 54 | + { tag: "thought", content: "Considering options" }, |
| 55 | + ] |
| 56 | + |
| 57 | + testCases.forEach(({ tag, content }) => { |
| 58 | + const matcher = new ReasoningXmlMatcher() |
| 59 | + |
| 60 | + // Simulate streaming |
| 61 | + const chunks = [ |
| 62 | + "Initial text ", |
| 63 | + `<${tag}>`, |
| 64 | + content.slice(0, 10), |
| 65 | + content.slice(10), |
| 66 | + `</${tag}>`, |
| 67 | + " final text", |
| 68 | + ] |
| 69 | + |
| 70 | + let allResults: any[] = [] |
| 71 | + chunks.forEach((chunk) => { |
| 72 | + const results = matcher.update(chunk) |
| 73 | + allResults.push(...results) |
| 74 | + }) |
| 75 | + |
| 76 | + // Get final results |
| 77 | + const finalResults = matcher.final() |
| 78 | + allResults.push(...finalResults) |
| 79 | + |
| 80 | + // Verify we got the expected matched content |
| 81 | + const matchedResults = allResults.filter((r) => r.matched) |
| 82 | + const unmatchedResults = allResults.filter((r) => !r.matched) |
| 83 | + |
| 84 | + expect(matchedResults.length).toBeGreaterThan(0) |
| 85 | + const fullMatchedContent = matchedResults.map((r) => r.data).join("") |
| 86 | + expect(fullMatchedContent).toContain(content) |
| 87 | + |
| 88 | + const fullUnmatchedContent = unmatchedResults.map((r) => r.data).join("") |
| 89 | + expect(fullUnmatchedContent).toContain("Initial text") |
| 90 | + expect(fullUnmatchedContent).toContain("final text") |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + it("should handle nested tags correctly", () => { |
| 95 | + const matcher = new ReasoningXmlMatcher() |
| 96 | + const input = "<think>Outer <think>Inner</think> content</think>" |
| 97 | + const results = matcher.final(input) |
| 98 | + |
| 99 | + // Should match the entire nested structure |
| 100 | + expect(results).toHaveLength(1) |
| 101 | + expect(results[0]).toEqual({ |
| 102 | + matched: true, |
| 103 | + data: "<think>Outer <think>Inner</think> content</think>", |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + it("should handle multiple different reasoning tags in sequence", () => { |
| 108 | + const matcher = new ReasoningXmlMatcher() |
| 109 | + const input = "Text <think>Think content</think> middle <thinking>Thinking content</thinking> end" |
| 110 | + const results = matcher.final(input) |
| 111 | + |
| 112 | + // Should match only the first tag type encountered |
| 113 | + expect(results.filter((r) => r.matched).length).toBeGreaterThan(0) |
| 114 | + expect(results.some((r) => r.data.includes("Think content"))).toBe(true) |
| 115 | + }) |
| 116 | + |
| 117 | + it("should apply custom transform function", () => { |
| 118 | + const transform = (chunk: { matched: boolean; data: string }) => ({ |
| 119 | + type: chunk.matched ? "reasoning" : "text", |
| 120 | + text: chunk.data, |
| 121 | + }) |
| 122 | + |
| 123 | + const matcher = new ReasoningXmlMatcher(transform) |
| 124 | + const input = "Normal text <think>Reasoning here</think> more text" |
| 125 | + const results = matcher.final(input) |
| 126 | + |
| 127 | + expect(results[0]).toEqual({ type: "text", text: "Normal text " }) |
| 128 | + expect(results[1]).toEqual({ type: "reasoning", text: "<think>Reasoning here</think>" }) |
| 129 | + expect(results[2]).toEqual({ type: "text", text: " more text" }) |
| 130 | + }) |
| 131 | +}) |
0 commit comments