|
1 | | -import { describe, test, expect } from "bun:test"; |
| 1 | +import { describe, test, expect, beforeEach } from "bun:test"; |
2 | 2 | import { z } from "zod"; |
3 | 3 | import type { Tool } from "ai"; |
4 | | -import { generateMuxTypes } from "./typeGenerator"; |
| 4 | +import { generateMuxTypes, getCachedMuxTypes, clearTypeCache } from "./typeGenerator"; |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * Create a mock tool with the given schema and optional execute function. |
@@ -231,3 +231,53 @@ describe("generateMuxTypes", () => { |
231 | 231 | expect(types).toContain("declare var console"); |
232 | 232 | }); |
233 | 233 | }); |
| 234 | + |
| 235 | +describe("getCachedMuxTypes", () => { |
| 236 | + beforeEach(() => { |
| 237 | + clearTypeCache(); |
| 238 | + }); |
| 239 | + |
| 240 | + test("invalidates cache when tool schema changes", async () => { |
| 241 | + const toolV1 = createMockTool(z.object({ name: z.string() })); |
| 242 | + const toolV2 = createMockTool(z.object({ name: z.string(), age: z.number() })); |
| 243 | + |
| 244 | + const types1 = await getCachedMuxTypes({ my_tool: toolV1 }); |
| 245 | + expect(types1).toContain("name: string"); |
| 246 | + expect(types1).not.toContain("age"); |
| 247 | + |
| 248 | + // Same tool name, different schema - should regenerate |
| 249 | + const types2 = await getCachedMuxTypes({ my_tool: toolV2 }); |
| 250 | + expect(types2).toContain("name: string"); |
| 251 | + expect(types2).toContain("age: number"); |
| 252 | + }); |
| 253 | + |
| 254 | + test("invalidates cache when tool description changes", async () => { |
| 255 | + const tool1: Tool = { |
| 256 | + description: "Version 1", |
| 257 | + inputSchema: z.object({ x: z.string() }), |
| 258 | + execute: () => Promise.resolve({ success: true }), |
| 259 | + } as unknown as Tool; |
| 260 | + |
| 261 | + const tool2: Tool = { |
| 262 | + description: "Version 2", |
| 263 | + inputSchema: z.object({ x: z.string() }), |
| 264 | + execute: () => Promise.resolve({ success: true }), |
| 265 | + } as unknown as Tool; |
| 266 | + |
| 267 | + const types1 = await getCachedMuxTypes({ my_tool: tool1 }); |
| 268 | + expect(types1).toContain("Version 1"); |
| 269 | + |
| 270 | + const types2 = await getCachedMuxTypes({ my_tool: tool2 }); |
| 271 | + expect(types2).toContain("Version 2"); |
| 272 | + }); |
| 273 | + |
| 274 | + test("returns cached types when tools are identical", async () => { |
| 275 | + const tool = createMockTool(z.object({ value: z.string() })); |
| 276 | + |
| 277 | + const types1 = await getCachedMuxTypes({ my_tool: tool }); |
| 278 | + const types2 = await getCachedMuxTypes({ my_tool: tool }); |
| 279 | + |
| 280 | + // Should be the exact same object reference (cached) |
| 281 | + expect(types1).toBe(types2); |
| 282 | + }); |
| 283 | +}); |
0 commit comments