|
| 1 | +// npx vitest core/prompts/utils/__tests__/schemaCompressor.spec.ts |
| 2 | + |
| 3 | +import { describe, it, expect } from "vitest" |
| 4 | +import { jsonSchemaToXml, compressSchemaWithMetrics, compressToolSchemas } from "../schemaCompressor" |
| 5 | + |
| 6 | +describe("schemaCompressor", () => { |
| 7 | + describe("jsonSchemaToXml", () => { |
| 8 | + it("should handle empty schema", () => { |
| 9 | + expect(jsonSchemaToXml(null)).toBe("<schema></schema>") |
| 10 | + expect(jsonSchemaToXml({})).toBe("<schema></schema>") |
| 11 | + expect(jsonSchemaToXml({ properties: {} })).toBe("<schema></schema>") |
| 12 | + }) |
| 13 | + |
| 14 | + it("should compress simple string parameter", () => { |
| 15 | + const schema = { |
| 16 | + type: "object", |
| 17 | + properties: { |
| 18 | + query: { |
| 19 | + type: "string", |
| 20 | + description: "The search query", |
| 21 | + }, |
| 22 | + }, |
| 23 | + required: ["query"], |
| 24 | + } |
| 25 | + |
| 26 | + expect(jsonSchemaToXml(schema)).toBe("<schema>query*:string</schema>") |
| 27 | + }) |
| 28 | + |
| 29 | + it("should handle multiple parameters with different types", () => { |
| 30 | + const schema = { |
| 31 | + type: "object", |
| 32 | + properties: { |
| 33 | + topic: { |
| 34 | + type: "string", |
| 35 | + description: "The topic to search", |
| 36 | + }, |
| 37 | + count: { |
| 38 | + type: "number", |
| 39 | + description: "Number of results", |
| 40 | + }, |
| 41 | + include_examples: { |
| 42 | + type: "boolean", |
| 43 | + description: "Whether to include examples", |
| 44 | + }, |
| 45 | + }, |
| 46 | + required: ["topic", "count"], |
| 47 | + } |
| 48 | + |
| 49 | + expect(jsonSchemaToXml(schema)).toBe( |
| 50 | + "<schema>topic*:string, count*:number, include_examples?:boolean</schema>", |
| 51 | + ) |
| 52 | + }) |
| 53 | + |
| 54 | + it("should handle array types", () => { |
| 55 | + const schema = { |
| 56 | + type: "object", |
| 57 | + properties: { |
| 58 | + tech_stack: { |
| 59 | + type: "array", |
| 60 | + items: { |
| 61 | + type: "string", |
| 62 | + }, |
| 63 | + }, |
| 64 | + numbers: { |
| 65 | + type: "array", |
| 66 | + items: { |
| 67 | + type: "number", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + required: ["tech_stack"], |
| 72 | + } |
| 73 | + |
| 74 | + expect(jsonSchemaToXml(schema)).toBe("<schema>tech_stack*:array[string], numbers?:array[number]</schema>") |
| 75 | + }) |
| 76 | + |
| 77 | + it("should handle enum types", () => { |
| 78 | + const schema = { |
| 79 | + type: "object", |
| 80 | + properties: { |
| 81 | + format: { |
| 82 | + type: "string", |
| 83 | + enum: ["json", "xml", "csv"], |
| 84 | + }, |
| 85 | + size: { |
| 86 | + type: "string", |
| 87 | + enum: ["small", "medium", "large", "xlarge", "xxlarge"], |
| 88 | + }, |
| 89 | + }, |
| 90 | + required: ["format"], |
| 91 | + } |
| 92 | + |
| 93 | + expect(jsonSchemaToXml(schema)).toBe("<schema>format*:enum(json|xml|csv), size?:enum</schema>") |
| 94 | + }) |
| 95 | + |
| 96 | + it("should handle object types", () => { |
| 97 | + const schema = { |
| 98 | + type: "object", |
| 99 | + properties: { |
| 100 | + requirements: { |
| 101 | + type: "object", |
| 102 | + properties: { |
| 103 | + description: { type: "string" }, |
| 104 | + scale: { type: "string" }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + required: ["requirements"], |
| 109 | + } |
| 110 | + |
| 111 | + expect(jsonSchemaToXml(schema)).toBe("<schema>requirements*:object</schema>") |
| 112 | + }) |
| 113 | + |
| 114 | + it("should handle date and url formats", () => { |
| 115 | + const schema = { |
| 116 | + type: "object", |
| 117 | + properties: { |
| 118 | + created_date: { |
| 119 | + type: "string", |
| 120 | + format: "date", |
| 121 | + }, |
| 122 | + website: { |
| 123 | + type: "string", |
| 124 | + format: "uri", |
| 125 | + }, |
| 126 | + email: { |
| 127 | + type: "string", |
| 128 | + format: "email", |
| 129 | + }, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + expect(jsonSchemaToXml(schema)).toBe("<schema>created_date?:date, website?:url, email?:email</schema>") |
| 134 | + }) |
| 135 | + }) |
| 136 | + |
| 137 | + describe("compressSchemaWithMetrics", () => { |
| 138 | + it("should calculate compression metrics", () => { |
| 139 | + const schema = { |
| 140 | + type: "object", |
| 141 | + properties: { |
| 142 | + query: { |
| 143 | + type: "string", |
| 144 | + description: "The natural language question to answer using web search.", |
| 145 | + }, |
| 146 | + }, |
| 147 | + required: ["query"], |
| 148 | + } |
| 149 | + |
| 150 | + const result = compressSchemaWithMetrics(schema) |
| 151 | + |
| 152 | + expect(result.compressed).toBe("<schema>query*:string</schema>") |
| 153 | + expect(result.originalTokens).toBeGreaterThan(result.compressedTokens) |
| 154 | + expect(result.reduction).toBeGreaterThan(50) // Should be significant reduction |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + describe("compressToolSchemas", () => { |
| 159 | + it("should compress multiple tools and calculate total metrics", () => { |
| 160 | + const tools = [ |
| 161 | + { |
| 162 | + name: "search", |
| 163 | + inputSchema: { |
| 164 | + type: "object", |
| 165 | + properties: { |
| 166 | + query: { type: "string" }, |
| 167 | + }, |
| 168 | + required: ["query"], |
| 169 | + }, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "translate", |
| 173 | + inputSchema: { |
| 174 | + type: "object", |
| 175 | + properties: { |
| 176 | + text: { type: "string" }, |
| 177 | + target_lang: { type: "string" }, |
| 178 | + source_lang: { type: "string" }, |
| 179 | + }, |
| 180 | + required: ["text", "target_lang"], |
| 181 | + }, |
| 182 | + }, |
| 183 | + ] |
| 184 | + |
| 185 | + const result = compressToolSchemas(tools) |
| 186 | + |
| 187 | + expect(result.compressedTools).toHaveLength(2) |
| 188 | + expect(result.compressedTools[0].compressedSchema).toBe("<schema>query*:string</schema>") |
| 189 | + expect(result.compressedTools[1].compressedSchema).toBe( |
| 190 | + "<schema>text*:string, target_lang*:string, source_lang?:string</schema>", |
| 191 | + ) |
| 192 | + expect(result.totalReduction).toBeGreaterThan(0) |
| 193 | + expect(result.originalTokens).toBeGreaterThan(result.compressedTokens) |
| 194 | + }) |
| 195 | + }) |
| 196 | + |
| 197 | + describe("real MCP server examples", () => { |
| 198 | + it("should compress google-ai-search-mcp tool schema", () => { |
| 199 | + const schema = { |
| 200 | + type: "object", |
| 201 | + properties: { |
| 202 | + topic: { |
| 203 | + type: "string", |
| 204 | + description: "The software/library/framework topic (e.g., 'React Router', 'Python requests').", |
| 205 | + }, |
| 206 | + query: { |
| 207 | + type: "string", |
| 208 | + description: "The specific question to answer based on the documentation.", |
| 209 | + }, |
| 210 | + }, |
| 211 | + required: ["topic", "query"], |
| 212 | + } |
| 213 | + |
| 214 | + expect(jsonSchemaToXml(schema)).toBe("<schema>topic*:string, query*:string</schema>") |
| 215 | + }) |
| 216 | + |
| 217 | + it("should compress context7-enhanced tool schema", () => { |
| 218 | + const schema = { |
| 219 | + type: "object", |
| 220 | + properties: { |
| 221 | + libraryName: { |
| 222 | + type: "string", |
| 223 | + description: "Library name to search for and retrieve a Context7-compatible library ID.", |
| 224 | + }, |
| 225 | + sort: { |
| 226 | + type: "string", |
| 227 | + enum: ["trust_score", "last_updated", "snippets"], |
| 228 | + description: "Sort results by trust score, last updated date, or snippet count", |
| 229 | + }, |
| 230 | + minTrustScore: { |
| 231 | + type: "number", |
| 232 | + description: "Filter results to only show libraries with trust score >= this value", |
| 233 | + }, |
| 234 | + maxTrustScore: { |
| 235 | + type: "number", |
| 236 | + description: "Filter results to only show libraries with trust score <= this value", |
| 237 | + }, |
| 238 | + }, |
| 239 | + required: ["libraryName"], |
| 240 | + } |
| 241 | + |
| 242 | + expect(jsonSchemaToXml(schema)).toBe( |
| 243 | + "<schema>libraryName*:string, sort?:enum(trust_score|last_updated|snippets), minTrustScore?:number, maxTrustScore?:number</schema>", |
| 244 | + ) |
| 245 | + }) |
| 246 | + }) |
| 247 | +}) |
0 commit comments