import assert from "node:assert/strict"; import { mkdtemp } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { test } from "node:test"; import { encode } from "@msgpack/msgpack"; import { z } from "zod"; import { CultCache } from "../src/cult-cache"; import { defineDocumentRegistry, defineDocumentType } from "../src/document"; import { SingleFileMessagePackBackingStore } from "../src/single-file-messagepack-backing-store"; import type { CacheBackingStore, CultCacheEnvelope } from "../src/types"; test("CultCache supports registry bootstrap, global documents, and lookup by name and index", async () => { const itemDocument = defineDocumentType({ type: "item", schema: z.object({ name: z.string(), category: z.string(), value: z.number().int(), }), name: "name", indexes: { category: "category", }, }); const settingsDocument = defineDocumentType({ type: "settings", schema: z.object({ theme: z.string(), retries: z.number().int().nonnegative(), }), global: true, }); const storePath = join(await mkdtemp(join(tmpdir(), "cultcache-ts-")), "cache.msgpack"); const registry = defineDocumentRegistry(itemDocument, settingsDocument); const cache = CultCache.builder() .withRegistry(registry) .withGenericStore(new SingleFileMessagePackBackingStore(storePath)) .build(); await cache.put(itemDocument, "item:potion", { name: "Potion", category: "Consumable", value: 50, }); await cache.putGlobal(settingsDocument, { theme: "ash", retries: 3, }); assert.deepEqual(cache.getByName(itemDocument, "Potion"), { name: "Potion", category: "Consumable", value: 50, }); assert.equal(cache.getKeyByIndex(itemDocument, "category", "Consumable"), "item:potion"); assert.deepEqual(cache.getGlobal(settingsDocument), { theme: "ash", retries: 3, }); const snapshot = cache.snapshot(); assert.equal(snapshot.length, 2); assert.ok(snapshot.every((entry) => entry.payload instanceof Uint8Array && entry.payload.length > 0)); const reloaded = CultCache.builder() .withRegistry(registry) .withGenericStore(new SingleFileMessagePackBackingStore(storePath)) .build(); await reloaded.pullAllBackingStores(); assert.deepEqual(reloaded.getByName(itemDocument, "Potion"), { name: "Potion", category: "Consumable", value: 50, }); assert.deepEqual(reloaded.getRequiredGlobal(settingsDocument), { theme: "ash", retries: 3, }); }); test("CultCache can register name and index lookups after entries already exist", async () => { const noteDocument = defineDocumentType({ type: "note", schema: z.object({ title: z.string(), author: z.string(), body: z.string(), }), }); const storePath = join(await mkdtemp(join(tmpdir(), "cultcache-ts-")), "cache.msgpack"); const cache = CultCache.builder() .withDocumentType(noteDocument) .withGenericStore(new SingleFileMessagePackBackingStore(storePath)) .build(); await cache.put(noteDocument, "note:hello", { title: "Hello", author: "ari", body: "world", }); cache.registerNameLookup(noteDocument, "title"); cache.registerIndex(noteDocument, "author", "author"); assert.equal(cache.getIdByName(noteDocument, "Hello"), "note:hello"); assert.equal(cache.getIdByIndex(noteDocument, "author", "ari"), "note:hello"); }); test("CultCache fails closed on unknown or illegal persisted polymorphic state", async () => { const settingsDocument = defineDocumentType({ type: "settings", schema: z.object({ theme: z.string(), }), global: true, }); class InMemoryStore implements CacheBackingStore { constructor(private readonly entries: CultCacheEnvelope[]) {} async pullAll(): Promise { return this.entries; } async push(): Promise { throw new Error("not used"); } async delete(): Promise { throw new Error("not used"); } } const cacheWithUnknownType = CultCache.builder() .withDocumentType(settingsDocument) .withGenericStore(new InMemoryStore([ { key: "mystery", type: "forbidden", payload: encode({ theme: "ash" }), storedAt: new Date().toISOString(), }, ])) .build(); await assert.rejects( async () => cacheWithUnknownType.pullAllBackingStores(), /No schema is registered for persisted document type "forbidden"\./, ); const cacheWithDuplicateGlobal = CultCache.builder() .withDocumentType(settingsDocument) .withGenericStore(new InMemoryStore([ { key: "one", type: "settings", payload: encode({ theme: "ash" }), storedAt: new Date().toISOString(), }, { key: "two", type: "settings", payload: encode({ theme: "ember" }), storedAt: new Date().toISOString(), }, ])) .build(); await assert.rejects( async () => cacheWithDuplicateGlobal.pullAllBackingStores(), /has multiple persisted entries/, ); });