import * as cookieHelper from "@/helpers/heritage-integration/cookie-helper";
import {
    isAnalyticsSessionStillActive,
    isSavedSessionStillActive,
    getDateForSavedSessionTimeout,
} from "@/helpers/heritage-integration/session-helper";
import { applicationConfig } from "@/constants/application-config";

describe("isAnalyticsSessionStillActive", () => {
    test("isAnalyticsSessionStillActive, should return true", () => {
        // Arrange
        const mockDate = new Date(new Date().toUTCString());
        mockDate.setDate(mockDate.getDate() + 1);

        cookieHelper.getFunnelCookie = jest
            .spyOn(cookieHelper, "getFunnelCookie")
            .mockReturnValue({ LastTouched: mockDate });

        // Act
        const result = isAnalyticsSessionStillActive();

        // Assert
        expect(result).toBe(true);
    });

    test("isAnalyticsSessionStillActive, should return false", () => {
        // Arrange
        const mockDate = new Date(new Date().toUTCString());
        mockDate.setDate(mockDate.getDate() - 1);

        cookieHelper.getFunnelCookie = jest
            .spyOn(cookieHelper, "getFunnelCookie")
            .mockReturnValue({ LastTouched: mockDate });

        // Act
        const result = isAnalyticsSessionStillActive();

        // Assert
        expect(result).toBe(false);
    });
});

describe("isSavedSessionStillActive", () => {
    test("isSavedSessionStillActive, should return true", () => {
        // Arrange
        const mockDate = new Date(new Date().toUTCString());
        mockDate.setDate(mockDate.getDate() + 1);

        cookieHelper.getFunnelCookie = jest
            .spyOn(cookieHelper, "getFunnelCookie")
            .mockReturnValue({ SavedSessionTimeoutDate: mockDate });

        // Act
        const result = isSavedSessionStillActive();

        // Assert
        expect(result).toBe(true);
    });

    test("isSavedSessionStillActive, should return false", () => {
        // Arrange
        const mockDate = new Date(new Date().toUTCString());
        mockDate.setDate(mockDate.getDate() - 1);

        cookieHelper.getFunnelCookie = jest
            .spyOn(cookieHelper, "getFunnelCookie")
            .mockReturnValue({ SavedSessionTimeoutDate: mockDate });

        // Act
        const result = isSavedSessionStillActive();

        // Assert
        expect(result).toBe(false);
    });
});

describe("getDateForSavedSessionTimeout", () => {
    test("getDateForSavedSessionTimeout, should equal application config setting", () => {
        // Arrange
        const currentDate = new Date(new Date().toUTCString());
        currentDate.setDate(currentDate.getDate() + applicationConfig.SAVED_SESSION_TIMEOUT_DAYS);

        // Act
        const result = new Date(getDateForSavedSessionTimeout()).toUTCString();

        // Assert
        expect(result).toEqual(currentDate.toUTCString());
    });
});
