|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { withMockedModule } from "../../testkit/mock-module.js"; |
| 3 | + |
| 4 | +let hashCalls = 0; |
| 5 | +await withMockedModule<typeof import("node:crypto")>( |
| 6 | + import.meta.resolve("node:crypto"), |
| 7 | + (real) => ({ |
| 8 | + ...real, |
| 9 | + createHash: (algorithm: string) => { |
| 10 | + hashCalls += 1; |
| 11 | + return real.createHash(algorithm); |
| 12 | + }, |
| 13 | + }), |
| 14 | +); |
| 15 | + |
| 16 | +const { createChatDirector } = await import("./director.js"); |
| 17 | + |
| 18 | +const base = { |
| 19 | + name: "read_file", |
| 20 | + description: "read a file", |
| 21 | + inputSchema: { type: "object" }, |
| 22 | +}; |
| 23 | + |
| 24 | +describe("toolSetDigest memoization", () => { |
| 25 | + test("hashes each installed tool set once per director", () => { |
| 26 | + const director = createChatDirector("system", [], {}); |
| 27 | + const tools = [{ ...base }]; |
| 28 | + const changed = [{ ...base, description: "changed" }]; |
| 29 | + |
| 30 | + director.updateToolDefinitions(tools); |
| 31 | + hashCalls = 0; |
| 32 | + director.updateToolDefinitions(changed); |
| 33 | + expect(hashCalls).toBe(1); |
| 34 | + }); |
| 35 | + |
| 36 | + test("recomputes when a new tool set arrives", () => { |
| 37 | + const director = createChatDirector("system", [], {}); |
| 38 | + const tools = [{ ...base }]; |
| 39 | + const changed = [{ ...base, description: "changed" }]; |
| 40 | + |
| 41 | + director.updateToolDefinitions(tools); |
| 42 | + director.updateToolDefinitions(changed); |
| 43 | + hashCalls = 0; |
| 44 | + director.updateToolDefinitions([{ ...base }]); |
| 45 | + expect(hashCalls).toBe(1); |
| 46 | + }); |
| 47 | +}); |
0 commit comments