var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { describe, beforeAll, beforeEach, expect, it, vi } from "vitest";
import * as uuidFunctionality from "../uuid";
import { createNewSession, getSessionId } from "../session";
import { mockLocalStorage } from "./__mocks__/localStorage";
describe("utils.session.createNewSession", () => {
    it("creates a new session", () => __awaiter(void 0, void 0, void 0, function* () {
        const session = createNewSession();
        expect(session).toHaveProperty("sessionId");
        expect(session).toHaveProperty("startTime");
        expect(session).toHaveProperty("endTime");
        expect(session.sessionId).toBeTypeOf("string");
        expect(session.sessionId.replace("$session:", "")).toHaveLength(36);
        expect(session.startTime).toBeTypeOf("string");
        expect(session.endTime).toBeTypeOf("string");
    }));
    it("creates a new session id with the correct prefix", () => __awaiter(void 0, void 0, void 0, function* () {
        const session = createNewSession();
        expect(session.sessionId.startsWith("$session:")).toBe(true);
    }));
    it("creates a new session id with a valid UUID", () => __awaiter(void 0, void 0, void 0, function* () {
        const session = createNewSession();
        const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
        const uuid = session.sessionId.replace("$session:", "");
        expect(uuid).toMatch(uuidRegex);
    }));
    it("creates a new session id with a default value if UUID generation fails", () => __awaiter(void 0, void 0, void 0, function* () {
        vi.spyOn(uuidFunctionality, "generateUUID").mockImplementationOnce(() => {
            throw new Error("UUID generation failed");
        });
        const session = createNewSession();
        expect(session.sessionId.startsWith("$session:default-")).toBe(true);
    }));
});
describe("utils.session.getSessionId", () => {
    beforeAll(() => {
        mockLocalStorage();
    });
    beforeEach(() => {
        localStorage.clear();
    });
    it("creates a new session if no session exists", () => __awaiter(void 0, void 0, void 0, function* () {
        localStorage.removeItem("localLogicSession");
        const sessionId = getSessionId();
        expect(sessionId).toBeTypeOf("string");
        expect(sessionId).not.toBeNull();
    }));
    it("returns the existing session if it exists", () => __awaiter(void 0, void 0, void 0, function* () {
        const session = createNewSession();
        localStorage.setItem("localLogicSession", JSON.stringify(session));
        const sessionId = getSessionId();
        expect(sessionId).toEqual(session.sessionId);
    }));
    it("creates a new session if the existing session has expired", () => __awaiter(void 0, void 0, void 0, function* () {
        const session = createNewSession();
        localStorage.setItem("localLogicSession", JSON.stringify(session));
        // Simulate session expiration by setting the end time to 30 minutes ago
        const expiredSession = Object.assign(Object.assign({}, session), { endTime: new Date(Date.now() - 31 * 60 * 1000).toISOString() });
        localStorage.setItem("localLogicSession", JSON.stringify(expiredSession));
        const sessionId = getSessionId();
        expect(sessionId).not.toEqual(expiredSession.sessionId);
    }));
    it("updates the end time of the existing session", () => __awaiter(void 0, void 0, void 0, function* () {
        const session = createNewSession();
        localStorage.setItem("localLogicSession", JSON.stringify(session));
        yield new Promise((resolve) => {
            setTimeout(resolve, 50);
        });
        const initialEndTime = session.endTime;
        getSessionId();
        const updatedSession = JSON.parse(localStorage.getItem("localLogicSession") || "");
        expect(updatedSession.endTime).not.toEqual(initialEndTime);
    }));
    it("returns a valid session ID", () => __awaiter(void 0, void 0, void 0, function* () {
        const sessionId = getSessionId();
        expect(sessionId).toBeTypeOf("string");
        expect(sessionId).not.toBeNull();
    }));
    it("session ID is unique", () => __awaiter(void 0, void 0, void 0, function* () {
        const sessionId1 = getSessionId();
        localStorage.clear();
        const sessionId2 = getSessionId();
        expect(sessionId1).not.toEqual(sessionId2);
    }));
    it("session ID is consistent across calls", () => __awaiter(void 0, void 0, void 0, function* () {
        const sessionId1 = getSessionId();
        const sessionId2 = getSessionId();
        expect(sessionId1).toEqual(sessionId2);
    }));
});
//# sourceMappingURL=session.test.js.map