diff options
Diffstat (limited to 'packages/ai/tests/cache.test.ts')
| -rw-r--r-- | packages/ai/tests/cache.test.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/packages/ai/tests/cache.test.ts b/packages/ai/tests/cache.test.ts new file mode 100644 index 0000000..8b78b99 --- /dev/null +++ b/packages/ai/tests/cache.test.ts @@ -0,0 +1,81 @@ +import { describe, test, expect, beforeEach, afterEach } from "bun:test"; + +// const url = "https://urbit.org"; + +import models, { type AIModelAPI, type LLMChoice } from "../index"; +import { memoize } from "../src/cache"; + +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); +async function thingy(arg1: string, arg2?: string): Promise<string> { + const res = `lolololol\n${arg1}\n${arg2};`; + await sleep(3000); + return res; +} + +describe("HTTP cache", () => { + test("should cache a function call", async () => { + const ts = Date.now(); + // const choice: LLMChoice = { deepseek: "deepseek-chat" }; + // const api = models(choice); + // + const cachedCall = memoize(thingy, { + ttlMs: 7 * 24 * 60 * 60 * 1000, + maxEntries: 5000, + persistDir: "./cache/memo", + }); + const res = await cachedCall("LOLAZO", "bar"); + const elapsed = Date.now() - ts; + console.log(elapsed); + const randomRes = await cachedCall(`${Math.random()}`); + const elapsedRandom = Date.now() - ts; + console.log("uncachable", elapsedRandom); + console.log("cached res", res); + + expect(elapsed).toBeLessThan(3000); + }); + test("should cache a request", async () => { + const testMessage = "oh hi"; + + const choice3: LLMChoice = { chatgpt: "gpt-5-nano" }; + const choice4: LLMChoice = { deepseek: "deepseek-chat" }; + const choice5: LLMChoice = { kimi: "kimi-k2-0905-preview" }; + + const api3 = models(choice3); + const api4 = models(choice4); + const api5 = models(choice5); + + const ts = Date.now(); + const r3 = api3.send(testMessage); + const r4 = api4.send(testMessage); + const r5 = api5.send(testMessage); + // Check ChatGPT response + const res3 = await r3; + console.log("elapsed r3", Date.now() - ts); + if ("ok" in res3) { + console.log(`✅ ChatGPT Response: ${res3.ok}`); + expect(res3.ok).toBeString(); + } else { + console.log(`❌ ChatGPT Error: ${res3.error}`); + } + + // // Check DeepSeek response + const res4 = await r4; + console.log("elapsed r4", Date.now() - ts); + if ("ok" in res4) { + console.log(`✅ DeepSeek Response: ${res4.ok}`); + expect(res4.ok).toBeString(); + } else { + console.log(`❌ DeepSeek Error: ${res4.error}`); + } + + // // Check Kimi response + const res5 = await r5; + console.log("elapsed r5", Date.now() - ts); + if ("ok" in res5) { + console.log(`✅ Kimi Response: ${res5.ok}`); + expect(res5.ok).toBeString(); + } else { + console.log(`❌ Kimi Error: ${res5.error}`); + } + }); +}); |
