You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.6 KiB
128 lines
3.6 KiB
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getClientCookie: vi.fn(),
|
|
setCachedEntryPath: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("./cookies", () => ({
|
|
getClientCookie: mocks.getClientCookie,
|
|
}));
|
|
|
|
vi.mock("./entry-route-cache", () => ({
|
|
setCachedMarriageEntryPath: mocks.setCachedEntryPath,
|
|
}));
|
|
|
|
describe("authBridge", () => {
|
|
let listeners: Array<(e: any) => void> = [];
|
|
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
vi.clearAllMocks();
|
|
listeners = [];
|
|
mocks.getClientCookie.mockReturnValue(null);
|
|
window.HABIB_TOKEN = undefined;
|
|
window.addFlutterResponseListener = vi.fn((cb) => {
|
|
listeners.push(cb);
|
|
return () => {
|
|
listeners = listeners.filter((l) => l !== cb);
|
|
};
|
|
});
|
|
window.sessionStorage.clear();
|
|
delete (window as any).HABIB_MARRIAGE;
|
|
delete (window as any).__HABIB_MARRIAGE_INITIAL_DATA__;
|
|
delete (window as any).__HABIB_BOOTSTRAP__;
|
|
});
|
|
|
|
it("treats a missing token as anonymous and clears the entry cache", async () => {
|
|
const { authBridge } = await import("./auth-bridge");
|
|
|
|
expect(authBridge.isAuthenticated()).toBe(false);
|
|
expect(mocks.setCachedEntryPath).toHaveBeenCalledWith(null);
|
|
});
|
|
|
|
it("treats an empty token as anonymous", async () => {
|
|
window.HABIB_TOKEN = "";
|
|
const { authBridge } = await import("./auth-bridge");
|
|
|
|
expect(authBridge.isAuthenticated()).toBe(false);
|
|
});
|
|
|
|
it("captures marriage initial data from Flutter initial_config event", async () => {
|
|
const { authBridge } = await import("./auth-bridge");
|
|
|
|
const marriagePayload = {
|
|
id: 123,
|
|
status: "match_found",
|
|
match_summary: { id: 456, gender: "female", public_info: [] },
|
|
};
|
|
|
|
// Simulate Flutter firing INITIAL_CONFIG event
|
|
listeners.forEach((listener) => {
|
|
listener({
|
|
action: "INITIAL_CONFIG",
|
|
success: true,
|
|
data: { marriage: marriagePayload },
|
|
});
|
|
});
|
|
|
|
expect(authBridge.getMarriageData()).toEqual(marriagePayload);
|
|
expect((window as any).HABIB_MARRIAGE).toEqual(marriagePayload);
|
|
});
|
|
|
|
it("captures marriage data when passed via payload property instead of data", async () => {
|
|
const { authBridge } = await import("./auth-bridge");
|
|
|
|
const marriagePayload = {
|
|
id: 789,
|
|
status: "match_found",
|
|
};
|
|
|
|
listeners.forEach((listener) => {
|
|
listener({
|
|
action: "initial_config",
|
|
success: true,
|
|
payload: { marriage: marriagePayload },
|
|
});
|
|
});
|
|
|
|
expect(authBridge.getMarriageData()).toEqual(marriagePayload);
|
|
});
|
|
|
|
it("extracts and parses URI-encoded HABIB_MARRIAGE_DATA cookie with Persian text", async () => {
|
|
const marriagePayload = {
|
|
id: 999,
|
|
status: "match_found",
|
|
city: "اصفهان",
|
|
name: "سارا",
|
|
};
|
|
|
|
mocks.getClientCookie.mockImplementation((name: string) => {
|
|
if (name === "HABIB_MARRIAGE_DATA") {
|
|
return encodeURIComponent(JSON.stringify(marriagePayload));
|
|
}
|
|
return null;
|
|
});
|
|
|
|
const { authBridge } = await import("./auth-bridge");
|
|
expect(authBridge.getMarriageData()).toEqual(marriagePayload);
|
|
});
|
|
|
|
it("falls back to parsing unencoded HABIB_MARRIAGE_DATA cookie", async () => {
|
|
const marriagePayload = {
|
|
id: 888,
|
|
status: "intro",
|
|
city: "تبریز",
|
|
};
|
|
|
|
mocks.getClientCookie.mockImplementation((name: string) => {
|
|
if (name === "HABIB_MARRIAGE_DATA") {
|
|
return JSON.stringify(marriagePayload);
|
|
}
|
|
return null;
|
|
});
|
|
|
|
const { authBridge } = await import("./auth-bridge");
|
|
expect(authBridge.getMarriageData()).toEqual(marriagePayload);
|
|
});
|
|
});
|