|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createCookieProbeGate } from "../../features/session/session"; |
| 3 | + |
| 4 | +// UT re-sets the session cookie on every response, including the login probe's |
| 5 | +// own; the gate keeps the cookie watcher from probing in a loop. |
| 6 | +describe("cookie probe gate", () => { |
| 7 | + test("probes once for a new session id, then ignores identical re-sets", () => { |
| 8 | + const gate = createCookieProbeGate(60_000); |
| 9 | + expect(gate.shouldProbe("abc", 0)).toBe(true); |
| 10 | + expect(gate.shouldProbe("abc", 100)).toBe(false); |
| 11 | + expect(gate.shouldProbe("abc", 200)).toBe(false); |
| 12 | + }); |
| 13 | + |
| 14 | + test("throttles probes for changing ids to one per interval", () => { |
| 15 | + const gate = createCookieProbeGate(60_000); |
| 16 | + expect(gate.shouldProbe("a", 0)).toBe(true); |
| 17 | + expect(gate.shouldProbe("b", 1_000)).toBe(false); |
| 18 | + expect(gate.shouldProbe("c", 59_999)).toBe(false); |
| 19 | + expect(gate.shouldProbe("d", 60_000)).toBe(true); |
| 20 | + expect(gate.shouldProbe("e", 60_001)).toBe(false); |
| 21 | + }); |
| 22 | + |
| 23 | + test("a rotated id inside the interval is not re-probed later just for repeating", () => { |
| 24 | + const gate = createCookieProbeGate(60_000); |
| 25 | + expect(gate.shouldProbe("a", 0)).toBe(true); |
| 26 | + expect(gate.shouldProbe("b", 1_000)).toBe(false); |
| 27 | + // Same value again after the interval — nothing new to learn. |
| 28 | + expect(gate.shouldProbe("b", 70_000)).toBe(false); |
| 29 | + }); |
| 30 | + |
| 31 | + test("reset() treats the next set as a new session even with the same id", () => { |
| 32 | + const gate = createCookieProbeGate(60_000); |
| 33 | + expect(gate.shouldProbe("abc", 0)).toBe(true); |
| 34 | + gate.reset(); |
| 35 | + expect(gate.shouldProbe("abc", 60_000)).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + test("reset() does not bypass the interval throttle", () => { |
| 39 | + const gate = createCookieProbeGate(60_000); |
| 40 | + expect(gate.shouldProbe("abc", 0)).toBe(true); |
| 41 | + gate.reset(); |
| 42 | + expect(gate.shouldProbe("abc", 1_000)).toBe(false); |
| 43 | + }); |
| 44 | +}); |
0 commit comments