|
| 1 | +import { describe, it, expect, afterEach } from "vitest"; |
| 2 | +import { SessionAffinityMap } from "../session-affinity.js"; |
| 3 | + |
| 4 | +describe("SessionAffinityMap", () => { |
| 5 | + let map: SessionAffinityMap; |
| 6 | + |
| 7 | + afterEach(() => { |
| 8 | + map?.dispose(); |
| 9 | + }); |
| 10 | + |
| 11 | + it("records and looks up a mapping", () => { |
| 12 | + map = new SessionAffinityMap(); |
| 13 | + map.record("resp_abc", "entry_123", "conv_1"); |
| 14 | + expect(map.lookup("resp_abc")).toBe("entry_123"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("returns null for unknown response IDs", () => { |
| 18 | + map = new SessionAffinityMap(); |
| 19 | + expect(map.lookup("resp_unknown")).toBeNull(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("overwrites previous mapping for same response ID", () => { |
| 23 | + map = new SessionAffinityMap(); |
| 24 | + map.record("resp_abc", "entry_1", "conv_1"); |
| 25 | + map.record("resp_abc", "entry_2", "conv_2"); |
| 26 | + expect(map.lookup("resp_abc")).toBe("entry_2"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("expires entries after TTL", () => { |
| 30 | + map = new SessionAffinityMap(50); // 50ms TTL |
| 31 | + map.record("resp_abc", "entry_123", "conv_1"); |
| 32 | + expect(map.lookup("resp_abc")).toBe("entry_123"); |
| 33 | + |
| 34 | + const start = Date.now(); |
| 35 | + while (Date.now() - start < 60) { |
| 36 | + // busy wait |
| 37 | + } |
| 38 | + expect(map.lookup("resp_abc")).toBeNull(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("tracks size correctly", () => { |
| 42 | + map = new SessionAffinityMap(); |
| 43 | + expect(map.size).toBe(0); |
| 44 | + map.record("resp_1", "entry_1", "conv_1"); |
| 45 | + map.record("resp_2", "entry_2", "conv_2"); |
| 46 | + expect(map.size).toBe(2); |
| 47 | + }); |
| 48 | + |
| 49 | + it("cleans up on dispose", () => { |
| 50 | + map = new SessionAffinityMap(); |
| 51 | + map.record("resp_1", "entry_1", "conv_1"); |
| 52 | + map.dispose(); |
| 53 | + expect(map.size).toBe(0); |
| 54 | + }); |
| 55 | + |
| 56 | + // Conversation ID tracking |
| 57 | + it("looks up conversation ID for a response", () => { |
| 58 | + map = new SessionAffinityMap(); |
| 59 | + map.record("resp_abc", "entry_1", "conv_xyz"); |
| 60 | + expect(map.lookupConversationId("resp_abc")).toBe("conv_xyz"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("returns null conversation ID for unknown response", () => { |
| 64 | + map = new SessionAffinityMap(); |
| 65 | + expect(map.lookupConversationId("resp_unknown")).toBeNull(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("conversation ID is inherited across response chain", () => { |
| 69 | + map = new SessionAffinityMap(); |
| 70 | + // Turn 1: new conversation |
| 71 | + map.record("resp_1", "entry_1", "conv_abc"); |
| 72 | + // Turn 2: inherit conv ID from turn 1 |
| 73 | + const convId = map.lookupConversationId("resp_1"); |
| 74 | + expect(convId).toBe("conv_abc"); |
| 75 | + map.record("resp_2", "entry_1", convId!); |
| 76 | + // Turn 3: inherit from turn 2 |
| 77 | + expect(map.lookupConversationId("resp_2")).toBe("conv_abc"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("expires conversation ID along with entry", () => { |
| 81 | + map = new SessionAffinityMap(50); |
| 82 | + map.record("resp_abc", "entry_1", "conv_1"); |
| 83 | + |
| 84 | + const start = Date.now(); |
| 85 | + while (Date.now() - start < 60) { |
| 86 | + // busy wait |
| 87 | + } |
| 88 | + expect(map.lookupConversationId("resp_abc")).toBeNull(); |
| 89 | + }); |
| 90 | +}); |
0 commit comments