|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { LRUIdempotencyKeyCatalog } from "./lruIdempotencyKeyCatalog.js"; |
| 3 | + |
| 4 | +describe("LRUIdempotencyKeyCatalog", () => { |
| 5 | + describe("registerKeyOptions and getKeyOptions", () => { |
| 6 | + it("should store and retrieve options", () => { |
| 7 | + const catalog = new LRUIdempotencyKeyCatalog(); |
| 8 | + const options = { key: "my-key", scope: "global" as const }; |
| 9 | + |
| 10 | + catalog.registerKeyOptions("hash1", options); |
| 11 | + |
| 12 | + expect(catalog.getKeyOptions("hash1")).toEqual(options); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should return undefined for non-existent keys", () => { |
| 16 | + const catalog = new LRUIdempotencyKeyCatalog(); |
| 17 | + |
| 18 | + expect(catalog.getKeyOptions("non-existent")).toBeUndefined(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should store multiple keys", () => { |
| 22 | + const catalog = new LRUIdempotencyKeyCatalog(); |
| 23 | + const options1 = { key: "key1", scope: "global" as const }; |
| 24 | + const options2 = { key: "key2", scope: "run" as const }; |
| 25 | + const options3 = { key: "key3", scope: "attempt" as const }; |
| 26 | + |
| 27 | + catalog.registerKeyOptions("hash1", options1); |
| 28 | + catalog.registerKeyOptions("hash2", options2); |
| 29 | + catalog.registerKeyOptions("hash3", options3); |
| 30 | + |
| 31 | + expect(catalog.getKeyOptions("hash1")).toEqual(options1); |
| 32 | + expect(catalog.getKeyOptions("hash2")).toEqual(options2); |
| 33 | + expect(catalog.getKeyOptions("hash3")).toEqual(options3); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should update options when registering same key twice", () => { |
| 37 | + const catalog = new LRUIdempotencyKeyCatalog(); |
| 38 | + const options1 = { key: "key1", scope: "global" as const }; |
| 39 | + const options2 = { key: "key1-updated", scope: "run" as const }; |
| 40 | + |
| 41 | + catalog.registerKeyOptions("hash1", options1); |
| 42 | + catalog.registerKeyOptions("hash1", options2); |
| 43 | + |
| 44 | + expect(catalog.getKeyOptions("hash1")).toEqual(options2); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("LRU eviction", () => { |
| 49 | + it("should evict oldest entry when over capacity", () => { |
| 50 | + const catalog = new LRUIdempotencyKeyCatalog(3); |
| 51 | + |
| 52 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 53 | + catalog.registerKeyOptions("hash2", { key: "key2", scope: "global" }); |
| 54 | + catalog.registerKeyOptions("hash3", { key: "key3", scope: "global" }); |
| 55 | + |
| 56 | + // All three should exist |
| 57 | + expect(catalog.getKeyOptions("hash1")).toBeDefined(); |
| 58 | + expect(catalog.getKeyOptions("hash2")).toBeDefined(); |
| 59 | + expect(catalog.getKeyOptions("hash3")).toBeDefined(); |
| 60 | + |
| 61 | + // Add a fourth - hash1 should be evicted (it was least recently used after the gets above moved others) |
| 62 | + // Note: After the gets above, the order is hash1, hash2, hash3 (hash1 was accessed first in the gets) |
| 63 | + // Actually let's reset and test more carefully |
| 64 | + }); |
| 65 | + |
| 66 | + it("should evict least recently registered entry when capacity exceeded", () => { |
| 67 | + const catalog = new LRUIdempotencyKeyCatalog(3); |
| 68 | + |
| 69 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 70 | + catalog.registerKeyOptions("hash2", { key: "key2", scope: "global" }); |
| 71 | + catalog.registerKeyOptions("hash3", { key: "key3", scope: "global" }); |
| 72 | + |
| 73 | + // Adding fourth should evict hash1 (oldest) |
| 74 | + catalog.registerKeyOptions("hash4", { key: "key4", scope: "global" }); |
| 75 | + |
| 76 | + expect(catalog.getKeyOptions("hash1")).toBeUndefined(); |
| 77 | + expect(catalog.getKeyOptions("hash2")).toBeDefined(); |
| 78 | + expect(catalog.getKeyOptions("hash3")).toBeDefined(); |
| 79 | + expect(catalog.getKeyOptions("hash4")).toBeDefined(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should evict multiple entries when adding many at once would exceed capacity", () => { |
| 83 | + const catalog = new LRUIdempotencyKeyCatalog(2); |
| 84 | + |
| 85 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 86 | + catalog.registerKeyOptions("hash2", { key: "key2", scope: "global" }); |
| 87 | + catalog.registerKeyOptions("hash3", { key: "key3", scope: "global" }); |
| 88 | + catalog.registerKeyOptions("hash4", { key: "key4", scope: "global" }); |
| 89 | + |
| 90 | + // Only hash3 and hash4 should remain |
| 91 | + expect(catalog.getKeyOptions("hash1")).toBeUndefined(); |
| 92 | + expect(catalog.getKeyOptions("hash2")).toBeUndefined(); |
| 93 | + expect(catalog.getKeyOptions("hash3")).toBeDefined(); |
| 94 | + expect(catalog.getKeyOptions("hash4")).toBeDefined(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should work with maxSize of 1", () => { |
| 98 | + const catalog = new LRUIdempotencyKeyCatalog(1); |
| 99 | + |
| 100 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 101 | + expect(catalog.getKeyOptions("hash1")).toBeDefined(); |
| 102 | + |
| 103 | + catalog.registerKeyOptions("hash2", { key: "key2", scope: "global" }); |
| 104 | + expect(catalog.getKeyOptions("hash1")).toBeUndefined(); |
| 105 | + expect(catalog.getKeyOptions("hash2")).toBeDefined(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("LRU ordering", () => { |
| 110 | + it("should move accessed key to most recent position", () => { |
| 111 | + const catalog = new LRUIdempotencyKeyCatalog(3); |
| 112 | + |
| 113 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 114 | + catalog.registerKeyOptions("hash2", { key: "key2", scope: "global" }); |
| 115 | + catalog.registerKeyOptions("hash3", { key: "key3", scope: "global" }); |
| 116 | + |
| 117 | + // Access hash1, moving it to most recent |
| 118 | + catalog.getKeyOptions("hash1"); |
| 119 | + |
| 120 | + // Add hash4 - should evict hash2 (now the oldest) |
| 121 | + catalog.registerKeyOptions("hash4", { key: "key4", scope: "global" }); |
| 122 | + |
| 123 | + expect(catalog.getKeyOptions("hash1")).toBeDefined(); |
| 124 | + expect(catalog.getKeyOptions("hash2")).toBeUndefined(); |
| 125 | + expect(catalog.getKeyOptions("hash3")).toBeDefined(); |
| 126 | + expect(catalog.getKeyOptions("hash4")).toBeDefined(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should move re-registered key to most recent position", () => { |
| 130 | + const catalog = new LRUIdempotencyKeyCatalog(3); |
| 131 | + |
| 132 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 133 | + catalog.registerKeyOptions("hash2", { key: "key2", scope: "global" }); |
| 134 | + catalog.registerKeyOptions("hash3", { key: "key3", scope: "global" }); |
| 135 | + |
| 136 | + // Re-register hash1, moving it to most recent |
| 137 | + catalog.registerKeyOptions("hash1", { key: "key1-updated", scope: "run" }); |
| 138 | + |
| 139 | + // Add hash4 - should evict hash2 (now the oldest) |
| 140 | + catalog.registerKeyOptions("hash4", { key: "key4", scope: "global" }); |
| 141 | + |
| 142 | + expect(catalog.getKeyOptions("hash1")).toEqual({ key: "key1-updated", scope: "run" }); |
| 143 | + expect(catalog.getKeyOptions("hash2")).toBeUndefined(); |
| 144 | + expect(catalog.getKeyOptions("hash3")).toBeDefined(); |
| 145 | + expect(catalog.getKeyOptions("hash4")).toBeDefined(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should not affect order when getting non-existent key", () => { |
| 149 | + const catalog = new LRUIdempotencyKeyCatalog(2); |
| 150 | + |
| 151 | + catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" }); |
| 152 | + catalog.registerKeyOptions("hash2", { key: "key2", scope: "global" }); |
| 153 | + |
| 154 | + // Try to get non-existent key |
| 155 | + catalog.getKeyOptions("non-existent"); |
| 156 | + |
| 157 | + // Add hash3 - should still evict hash1 (oldest) |
| 158 | + catalog.registerKeyOptions("hash3", { key: "key3", scope: "global" }); |
| 159 | + |
| 160 | + expect(catalog.getKeyOptions("hash1")).toBeUndefined(); |
| 161 | + expect(catalog.getKeyOptions("hash2")).toBeDefined(); |
| 162 | + expect(catalog.getKeyOptions("hash3")).toBeDefined(); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe("default maxSize", () => { |
| 167 | + it("should use default maxSize of 1000", () => { |
| 168 | + const catalog = new LRUIdempotencyKeyCatalog(); |
| 169 | + |
| 170 | + // Register 1001 entries |
| 171 | + for (let i = 0; i < 1001; i++) { |
| 172 | + catalog.registerKeyOptions(`hash${i}`, { key: `key${i}`, scope: "global" }); |
| 173 | + } |
| 174 | + |
| 175 | + // First entry should be evicted |
| 176 | + expect(catalog.getKeyOptions("hash0")).toBeUndefined(); |
| 177 | + // Last entry should exist |
| 178 | + expect(catalog.getKeyOptions("hash1000")).toBeDefined(); |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments