|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { InMemoryIdempotencyKeyCatalog } from "./inMemoryIdempotencyKeyCatalog.js"; |
| 3 | + |
| 4 | +describe("InMemoryIdempotencyKeyCatalog", () => { |
| 5 | + it("stores and retrieves options", () => { |
| 6 | + const catalog = new InMemoryIdempotencyKeyCatalog(); |
| 7 | + const options = { key: "my-key", scope: "global" as const }; |
| 8 | + |
| 9 | + catalog.registerKeyOptions("hash1", options); |
| 10 | + |
| 11 | + expect(catalog.getKeyOptions("hash1")).toEqual(options); |
| 12 | + }); |
| 13 | + |
| 14 | + it("returns undefined for non-existent keys", () => { |
| 15 | + const catalog = new InMemoryIdempotencyKeyCatalog(); |
| 16 | + |
| 17 | + expect(catalog.getKeyOptions("non-existent")).toBeUndefined(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("updates options when registering the same hash twice", () => { |
| 21 | + const catalog = new InMemoryIdempotencyKeyCatalog(); |
| 22 | + |
| 23 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 24 | + catalog.registerKeyOptions("hash1", { key: "key1-updated", scope: "run" }); |
| 25 | + |
| 26 | + expect(catalog.getKeyOptions("hash1")).toEqual({ key: "key1-updated", scope: "run" }); |
| 27 | + }); |
| 28 | + |
| 29 | + it("retains every entry regardless of count (no eviction)", () => { |
| 30 | + const catalog = new InMemoryIdempotencyKeyCatalog(); |
| 31 | + const count = 5000; |
| 32 | + |
| 33 | + for (let i = 0; i < count; i++) { |
| 34 | + catalog.registerKeyOptions(`hash${i}`, { key: `key${i}`, scope: "global" }); |
| 35 | + } |
| 36 | + |
| 37 | + // The very first entry must still be present — nothing is silently evicted. |
| 38 | + expect(catalog.getKeyOptions("hash0")).toEqual({ key: "key0", scope: "global" }); |
| 39 | + expect(catalog.getKeyOptions(`hash${count - 1}`)).toEqual({ |
| 40 | + key: `key${count - 1}`, |
| 41 | + scope: "global", |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("clear() removes all entries", () => { |
| 46 | + const catalog = new InMemoryIdempotencyKeyCatalog(); |
| 47 | + |
| 48 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 49 | + catalog.registerKeyOptions("hash2", { key: "key2", scope: "run" }); |
| 50 | + |
| 51 | + catalog.clear(); |
| 52 | + |
| 53 | + expect(catalog.getKeyOptions("hash1")).toBeUndefined(); |
| 54 | + expect(catalog.getKeyOptions("hash2")).toBeUndefined(); |
| 55 | + }); |
| 56 | +}); |
0 commit comments