Skip to content

Commit e3194fb

Browse files
committed
fix a few AI review notes
1 parent 77cd398 commit e3194fb

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

packages/core/src/v3/idempotency-key-catalog/lruIdempotencyKeyCatalog.test.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,16 @@ describe("LRUIdempotencyKeyCatalog", () => {
5858
expect(catalog.getKeyOptions("hash2")).toBeDefined();
5959
expect(catalog.getKeyOptions("hash3")).toBeDefined();
6060

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
61+
// After the gets above, the LRU order from oldest to newest is: hash1, hash2, hash3
62+
// (each get moves the key to the most recent position)
63+
64+
// Add a fourth - hash1 should be evicted (it was accessed first, so it's the oldest)
65+
catalog.registerKeyOptions("hash4", { key: "key4", scope: "global" });
66+
67+
expect(catalog.getKeyOptions("hash1")).toBeUndefined();
68+
expect(catalog.getKeyOptions("hash2")).toBeDefined();
69+
expect(catalog.getKeyOptions("hash3")).toBeDefined();
70+
expect(catalog.getKeyOptions("hash4")).toBeDefined();
6471
});
6572

6673
it("should evict least recently registered entry when capacity exceeded", () => {
@@ -178,4 +185,25 @@ describe("LRUIdempotencyKeyCatalog", () => {
178185
expect(catalog.getKeyOptions("hash1000")).toBeDefined();
179186
});
180187
});
188+
189+
describe("edge cases", () => {
190+
it("should handle negative maxSize by clamping to 0", () => {
191+
const catalog = new LRUIdempotencyKeyCatalog(-5);
192+
193+
// With maxSize clamped to 0, nothing should be stored
194+
catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" });
195+
196+
// Should be immediately evicted since maxSize is 0
197+
expect(catalog.getKeyOptions("hash1")).toBeUndefined();
198+
});
199+
200+
it("should handle maxSize of 0", () => {
201+
const catalog = new LRUIdempotencyKeyCatalog(0);
202+
203+
catalog.registerKeyOptions("hash1", { key: "key1", scope: "global" });
204+
205+
// Should be immediately evicted since maxSize is 0
206+
expect(catalog.getKeyOptions("hash1")).toBeUndefined();
207+
});
208+
});
181209
});

packages/core/src/v3/idempotency-key-catalog/lruIdempotencyKeyCatalog.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export class LRUIdempotencyKeyCatalog implements IdempotencyKeyCatalog {
66

77
constructor(maxSize: number = 1_000) {
88
this.cache = new Map();
9-
this.maxSize = maxSize;
9+
// Clamp to non-negative to prevent infinite loop in eviction
10+
this.maxSize = Math.max(0, maxSize);
1011
}
1112

1213
registerKeyOptions(hash: string, options: IdempotencyKeyOptions): void {

0 commit comments

Comments
 (0)