import { expect } from "@jest/globals"; import { ICacheClient, CacheDelete, CacheGet, CacheIncrement, CacheSet, CacheSetIfNotExists, CacheSetFetch, CacheSetAddElements, CacheSetAddElement, CacheSetRemoveElements, CacheSetRemoveElement, CacheListFetch, CacheListLength, CacheListPushFront, CacheListPushBack, CacheListConcatenateBack, CacheListConcatenateFront, CacheListPopBack, CacheListPopFront, CacheListRemoveValue, CacheListRetain, CacheDictionarySetField, CacheDictionarySetFields, CacheDictionaryGetField, CacheDictionaryFetch, CacheDictionaryIncrement, CacheDictionaryRemoveField, CacheDictionaryRemoveFields, CacheSortedSetFetch, CacheSortedSetPutElement, CacheSortedSetPutElements, CacheSortedSetGetRank, CacheSortedSetGetScore, CacheSortedSetGetScores, CacheSortedSetIncrementScore, CacheSortedSetRemoveElement, ItemGetType, CreateCache, ListCaches, DeleteCache, CacheFlush, } from "@gomomento/sdk"; import { MomentoCache } from "../momento.js"; import { Generation } from "../../schema/index.js"; class MockClient implements ICacheClient { private cache: Map; constructor() { this.cache = new Map(); } public async get(_: string, key: string): Promise { if (this.cache.has(key)) { return new CacheGet.Hit(new TextEncoder().encode(this.cache.get(key))); } else { return new CacheGet.Miss(); } } public async set( _: string, key: string, value: string ): Promise { this.cache.set(key, value); return new CacheSet.Success(); } public async createCache(): Promise { return new CreateCache.Success(); } deleteCache(): Promise { throw new Error("Method not implemented."); } listCaches(): Promise { throw new Error("Method not implemented."); } flushCache(): Promise { throw new Error("Method not implemented."); } ping(): Promise { throw new Error("Method not implemented."); } delete(): Promise { throw new Error("Method not implemented."); } increment(): Promise { throw new Error("Method not implemented."); } setIfNotExists(): Promise { throw new Error("Method not implemented."); } setFetch(): Promise { throw new Error("Method not implemented."); } setAddElement(): Promise { throw new Error("Method not implemented."); } setAddElements(): Promise { throw new Error("Method not implemented."); } setRemoveElement(): Promise { throw new Error("Method not implemented."); } setRemoveElements(): Promise { throw new Error("Method not implemented."); } listFetch(): Promise { throw new Error("Method not implemented."); } listLength(): Promise { throw new Error("Method not implemented."); } listPushFront(): Promise { throw new Error("Method not implemented."); } listPushBack(): Promise { throw new Error("Method not implemented."); } listConcatenateBack(): Promise { throw new Error("Method not implemented."); } listConcatenateFront(): Promise { throw new Error("Method not implemented."); } listPopBack(): Promise { throw new Error("Method not implemented."); } listPopFront(): Promise { throw new Error("Method not implemented."); } listRemoveValue(): Promise { throw new Error("Method not implemented."); } listRetain(): Promise { throw new Error("Method not implemented."); } dictionarySetField(): Promise { throw new Error("Method not implemented."); } dictionarySetFields(): Promise { throw new Error("Method not implemented."); } dictionaryGetField(): Promise { throw new Error("Method not implemented."); } dictionaryGetFields(): Promise { throw new Error("Method not implemented."); } dictionaryFetch(): Promise { throw new Error("Method not implemented."); } dictionaryIncrement(): Promise { throw new Error("Method not implemented."); } dictionaryRemoveField(): Promise { throw new Error("Method not implemented."); } dictionaryRemoveFields(): Promise { throw new Error("Method not implemented."); } sortedSetFetchByRank(): Promise { throw new Error("Method not implemented."); } sortedSetFetchByScore(): Promise { throw new Error("Method not implemented."); } sortedSetPutElement(): Promise { throw new Error("Method not implemented."); } sortedSetPutElements(): Promise { throw new Error("Method not implemented."); } sortedSetGetRank(): Promise { throw new Error("Method not implemented."); } sortedSetGetScore(): Promise { throw new Error("Method not implemented."); } sortedSetGetScores(): Promise { throw new Error("Method not implemented."); } sortedSetIncrementScore(): Promise { throw new Error("Method not implemented."); } sortedSetRemoveElement(): Promise { throw new Error("Method not implemented."); } sortedSetRemoveElements(): Promise { throw new Error("Method not implemented."); } itemGetType(): Promise { throw new Error("Method not implemented."); } } describe("MomentoCache", () => { it("should return null on a cache miss", async () => { const client = new MockClient(); const cache = await MomentoCache.fromProps({ client, cacheName: "test-cache", }); expect(await cache.lookup("prompt", "llm-key")).toBeNull(); }); it("should get a stored value", async () => { const client = new MockClient(); const cache = await MomentoCache.fromProps({ client, cacheName: "test-cache", }); const generations: Generation[] = [{ text: "foo" }]; await cache.update("prompt", "llm-key", generations); expect(await cache.lookup("prompt", "llm-key")).toStrictEqual(generations); }); it("should work with multiple generations", async () => { const client = new MockClient(); const cache = await MomentoCache.fromProps({ client, cacheName: "test-cache", }); const generations: Generation[] = [ { text: "foo" }, { text: "bar" }, { text: "baz" }, ]; await cache.update("prompt", "llm-key", generations); expect(await cache.lookup("prompt", "llm-key")).toStrictEqual(generations); }); });