Files
buzz-sheet/lib/auth/sudloh-redirect.test.ts
T
gunshiz 445865bf42
CI / Verify (push) Successful in 1m31s
CI / Build immutable images and deploy (push) Successful in 2m4s
fix(auth) : bind Sudloh sessions and stop OAuth redirect loops
2026-10-06 14:28:59 +07:00

43 lines
1.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { claimAutomaticSudlohSignIn, markSudlohSignInAttempt, sudlohStartErrorMessage } from "./sudloh-redirect";
const values = new Map<string, string>();
beforeEach(() => {
values.clear();
vi.stubGlobal("sessionStorage", {
getItem: (key: string) => values.get(key) ?? null,
setItem: (key: string, value: string) => { values.set(key, value); },
});
vi.spyOn(Date, "now").mockReturnValue(1_000_000);
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
describe("Sudloh redirect guard", () => {
it("starts once and stops automatic redirects when login is revisited", () => {
expect(claimAutomaticSudlohSignIn()).toBe(true);
expect(claimAutomaticSudlohSignIn()).toBe(false);
vi.spyOn(Date, "now").mockReturnValue(1_000_000 + 5 * 60_000);
expect(claimAutomaticSudlohSignIn()).toBe(true);
});
it("keeps logout from immediately starting another sign-in", () => {
markSudlohSignInAttempt();
expect(claimAutomaticSudlohSignIn()).toBe(false);
});
it("requires a manual start when browser storage is unavailable", () => {
vi.stubGlobal("sessionStorage", { getItem: () => { throw new Error("blocked"); } });
expect(claimAutomaticSudlohSignIn()).toBe(false);
});
it("explains a rate limit without leaking the provider response", () => {
expect(sudlohStartErrorMessage(429)).toContain("รอสักครู่");
expect(sudlohStartErrorMessage(503)).not.toContain("รอสักครู่");
});
});