From 6fbce23f9216b96f95f0803de102ac8126abe597 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 11 Oct 2026 21:05:09 +0200 Subject: [PATCH] cultcache-ts test: the cache keeps its own copy of an admitted envelope's bytes and of the ones it hands out Co-Authored-By: Claude Sonnet 5.5 --- packages/cultcache-ts/test/cult-cache.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/cultcache-ts/test/cult-cache.test.ts b/packages/cultcache-ts/test/cult-cache.test.ts index dda2aa79..6dc59451 100644 --- a/packages/cultcache-ts/test/cult-cache.test.ts +++ b/packages/cultcache-ts/test/cult-cache.test.ts @@ -328,6 +328,26 @@ test("CultCache can ingest raw envelopes without re-encoding the payload", async assert.deepEqual(target.getRequiredEnvelope(noteDocument, "note:hello").payload, envelope.payload); }); +test("the cache keeps its own copy of an envelope's bytes: a caller mutating them afterwards changes nothing", async () => { + const noteDocument = defineDocumentType({ type: "note", schema: z.object({ title: z.string(), body: z.string() }) }); + const origin = CultCache.builder().withDocumentType(noteDocument) + .withGenericStore(new SingleFileMessagePackBackingStore(join(await mkdtemp(join(tmpdir(), "cultcache-ts-")), "origin.msgpack"))).build(); + const target = CultCache.builder().withDocumentType(noteDocument) + .withGenericStore(new SingleFileMessagePackBackingStore(join(await mkdtemp(join(tmpdir(), "cultcache-ts-")), "target.msgpack"))).build(); + await origin.put(noteDocument, "note:hello", { title: "Hello", body: "world" }); + + const envelope = origin.getRequiredEnvelope(noteDocument, "note:hello"); + const before = Buffer.from(documentPayloadBytes(envelope.payload)); + await target.putEnvelope(noteDocument, envelope); + documentPayloadBytes(envelope.payload).fill(0); + + assert.deepEqual(Buffer.from(documentPayloadBytes(target.getRequiredEnvelope(noteDocument, "note:hello").payload)), before); + const read = target.getRequiredEnvelope(noteDocument, "note:hello"); + documentPayloadBytes(read.payload).fill(0); + assert.deepEqual(Buffer.from(documentPayloadBytes(target.getRequiredEnvelope(noteDocument, "note:hello").payload)), before); + assert.deepEqual(target.getRequired(noteDocument, "note:hello"), { title: "Hello", body: "world" }); +}); + test("SingleFileMessagePackBackingStore writes the CultCache v1 snapshot shape", async () => { const namedDocument = defineDocumentType({ type: "tests.named_entry",