cultcache-ts: pushAll keeps the last of same-type entries sharing a key

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-11 15:00:40 +00:00
commit e260b72697
2 changed files with 13 additions and 1 deletions

View file

@ -103,7 +103,8 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore {
}
requireOneTypePerKey(this.filePath, entries, []);
await this.#writeAll(entries, []);
// A key is held once: of same-type entries sharing a key in one batch, the last is kept.
await this.#writeAll([...new Map(entries.map((entry) => [entry.key, entry])).values()], []);
});
}

View file

@ -623,6 +623,17 @@ test("a pushAll batch that carries one key under two types is refused and writes
}
});
test("a pushAll batch that carries one key twice under one type keeps the last and the file reads back with one record", async () => {
const directory = await scratch();
try {
const path = join(directory, "store.cc");
await new SingleFileMessagePackBackingStore(path).pushAll([entryOf("k", "t", 1), entryOf("a", "t", 3), entryOf("k", "t", 2)]);
assert.deepEqual(await snapshotOf(new SingleFileMessagePackBackingStore(path)), ["t/a:3", "t/k:2"]);
} finally {
await rm(directory, { recursive: true, force: true });
}
});
test("a legacy envelope array that holds a key under two types is refused on open, as a v1 store is", async () => {
const directory = await scratch();
try {