import { MemoryAdapter, createSharedStore } from "@hivegrid/shared-store"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { __resetLoginCaptchaGateStoreForTests, __setLoginCaptchaGateStoreForTests, clearFailedLogins, getFailedLoginCount, LOGIN_CAPTCHA_AFTER_FAILURES, loginRequiresCaptcha, recordFailedLogin, } from "./login-captcha-gate.server"; function req() { return new Request("https://example.test/login", { headers: { "cf-connecting-ip": "203.0.113.10" } }); } describe("login-captcha-gate", () => { let adapter: MemoryAdapter; beforeEach(() => { adapter = new MemoryAdapter(); __setLoginCaptchaGateStoreForTests(createSharedStore({ namespace: "login-captcha-test", adapter })); }); afterEach(() => { __resetLoginCaptchaGateStoreForTests(); vi.restoreAllMocks(); }); it("starts at zero and atomically increments each recorded failure", async () => { const request = req(); await expect(getFailedLoginCount({ request, identifier: "ada@example.test" })).resolves.toBe(0); await expect(recordFailedLogin({ request, identifier: "ada@example.test" })).resolves.toBe(1); await expect(recordFailedLogin({ request, identifier: "ada@example.test" })).resolves.toBe(2); await expect(getFailedLoginCount({ request, identifier: "ada@example.test" })).resolves.toBe(2); }); it("serializes concurrent failure reservations without losing increments", async () => { const args = { request: req(), identifier: "ada@example.test" }; const counts = await Promise.all(Array.from({ length: 12 }, () => recordFailedLogin(args))); expect([...counts].sort((a, b) => a - b)).toEqual(Array.from({ length: 12 }, (_, index) => index + 1)); await expect(getFailedLoginCount(args)).resolves.toBe(12); }); it("requires a captcha only from the configured failure threshold", () => { expect(loginRequiresCaptcha(0)).toBe(false); expect(loginRequiresCaptcha(LOGIN_CAPTCHA_AFTER_FAILURES - 1)).toBe(false); expect(loginRequiresCaptcha(LOGIN_CAPTCHA_AFTER_FAILURES)).toBe(true); expect(loginRequiresCaptcha(LOGIN_CAPTCHA_AFTER_FAILURES + 2)).toBe(true); }); it("shares failures and successful reset across replicas", async () => { const args = { request: req(), identifier: "ada@example.test" }; const replicaA = createSharedStore({ namespace: "login-captcha-replicas", adapter }); const replicaB = createSharedStore({ namespace: "login-captcha-replicas", adapter }); __setLoginCaptchaGateStoreForTests(replicaA); await recordFailedLogin(args); await recordFailedLogin(args); await recordFailedLogin(args); __setLoginCaptchaGateStoreForTests(replicaB); await expect(getFailedLoginCount(args)).resolves.toBe(3); await clearFailedLogins(args); __setLoginCaptchaGateStoreForTests(replicaA); await expect(getFailedLoginCount(args)).resolves.toBe(0); }); it("expires the bounded failure state after its fixed window", async () => { const startedAt = new Date("2026-05-25T12:00:00.000Z").getTime(); vi.spyOn(Date, "now").mockReturnValue(startedAt); adapter = new MemoryAdapter(); __setLoginCaptchaGateStoreForTests(createSharedStore({ namespace: "login-captcha-expiry", adapter })); const args = { request: req(), identifier: "ada@example.test" }; await recordFailedLogin(args); vi.mocked(Date.now).mockReturnValue(startedAt + 15 * 60 * 1000 + 1); await expect(getFailedLoginCount(args)).resolves.toBe(0); }); it("keys per identifier so a different email does not inherit the captcha gate", async () => { const request = req(); await recordFailedLogin({ request, identifier: "ada@example.test" }); await recordFailedLogin({ request, identifier: "ada@example.test" }); await recordFailedLogin({ request, identifier: "ada@example.test" }); expect(loginRequiresCaptcha(await getFailedLoginCount({ request, identifier: "ada@example.test" }))).toBe(true); await expect(getFailedLoginCount({ request, identifier: "mallory@example.test" })).resolves.toBe(0); }); it("fails closed by requiring captcha when the shared count cannot be read", async () => { vi.spyOn(adapter, "get").mockRejectedValueOnce(new Error("redis unavailable")); const count = await getFailedLoginCount({ request: req(), identifier: "ada@example.test" }); expect(loginRequiresCaptcha(count)).toBe(true); }); it("fails closed when the persisted counter is malformed or negative", async () => { vi.spyOn(adapter, "get").mockResolvedValueOnce("not-a-number").mockResolvedValueOnce("-1"); await expect(getFailedLoginCount({ request: req(), identifier: "ada@example.test" })).resolves.toBe(LOGIN_CAPTCHA_AFTER_FAILURES); await expect(getFailedLoginCount({ request: req(), identifier: "ada@example.test" })).resolves.toBe(LOGIN_CAPTCHA_AFTER_FAILURES); }); it("handles non-Error read failures without leaking the store payload", async () => { vi.spyOn(adapter, "get").mockRejectedValueOnce("redis-secret"); await expect(getFailedLoginCount({ request: req(), identifier: "ada@example.test" })).resolves.toBe(LOGIN_CAPTCHA_AFTER_FAILURES); }); it("propagates failed atomic writes so login cannot continue unthrottled", async () => { vi.spyOn(adapter, "eval").mockRejectedValueOnce("write-secret"); await expect(recordFailedLogin({ request: req(), identifier: "ada@example.test" })).rejects.toBe("write-secret"); }); it("propagates failed resets so a successful login cannot claim the gate was cleared", async () => { vi.spyOn(adapter, "del").mockRejectedValueOnce("reset-secret"); await expect(clearFailedLogins({ request: req(), identifier: "ada@example.test" })).rejects.toBe("reset-secret"); }); });