43 lines
1.5 KiB
TypeScript
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("รอสักครู่");
|
|
});
|
|
});
|