import {
    getFunnelCookie,
    getDeviceIdValue,
    getSessionKeyValue,
    getSessionIdValue,
} from "@/helpers/heritage-integration/cookie-helper.js";
import { removeAllTestCookies, setupCookies } from "@/helpers/unit-test-helper";

describe("cookies", () => {
    afterEach(() => {
        removeAllTestCookies();
    });

    describe("getFunnelCookie method", () => {
        test("gets correct value when cookie is present", () => {
            // Arrange
            const testReferralNumber = 1566818;
            const testReferralDate = "2022-03-15T10:56:24.597";
            const testReferralCorrelationId = "404d2b04-f86e-45c3-b373-127b6217b060";
            const testShouldResetState = false;
            const testDidHeritageFunnelUpdateLast = true;

            const testCookieValue = {
                ReferralNumber: testReferralNumber,
                ReferralDate: testReferralDate,
                ReferralCorrelationId: testReferralCorrelationId,
                ShouldResetState: testShouldResetState,
                DidHeritageFunnelUpdateLast: testDidHeritageFunnelUpdateLast,
                SuppressConceptFunnel: true,
            };

            setupCookies({ funnelCookieValue: JSON.stringify(testCookieValue) });

            // Act
            var result = getFunnelCookie();

            // Assert
            expect(result).toEqual(testCookieValue);
            expect(typeof result).toEqual("object");
            expect(result.ReferralNumber).toEqual(testReferralNumber);
            expect(result.ReferralDate).toEqual(testReferralDate);
            expect(result.ReferralCorrelationId).toEqual(testReferralCorrelationId);
            expect(result.ShouldResetState).toEqual(testShouldResetState);
            expect(result.DidHeritageFunnelUpdateLast).toEqual(testDidHeritageFunnelUpdateLast);
        });

        test("returns empty object when value is empty object", () => {
            // Arrange
            const testCookieValue = {};
            setupCookies({ funnelCookieValue: JSON.stringify(testCookieValue) });

            // Act
            var result = getFunnelCookie();

            // Assert
            expect(result).toEqual(testCookieValue);
            expect(typeof result).toEqual("object");
            expect(Object.keys(result)).toHaveLength(0);
        });

        test("returns null when value is empty string", () => {
            // Arrange
            const testCookieValue = "";
            setupCookies({ funnelCookieValue: testCookieValue });

            // Act
            var result = getFunnelCookie();

            // Assert
            expect(result).toEqual(null);
        });

        test("returns null when funnel cookie doesn't exist", () => {
            // Arrange
            setupCookies({ includeHeritageCookie: false });

            // Act
            var result = getFunnelCookie();

            // Assert
            expect(result).toEqual(null);
        });

        test("gets correct cookie value", () => {
            // Arrange
            const testCookieValue = { test: "testValue" };

            setupCookies({ funnelCookieValue: JSON.stringify(testCookieValue) });

            // Act
            const actualCookieValue = getFunnelCookie();

            // Assert
            expect(actualCookieValue).toEqual(testCookieValue);
        });

        test("getCookieValue: Gets null cookie value", () => {
            // Arrange
            setupCookies({ includeHeritageCookie: false });

            // Act
            const actualCookieValue = getFunnelCookie();

            // Assert
            expect(actualCookieValue).toBeNull();
        });
    });

    describe("getDeviceIdValue", () => {
        test("getDeviceIdValue, should return GUID", () => {
            // Arrange
            setupCookies({});

            // Act
            const result = getDeviceIdValue();

            //Assert
            expect(result).toBe("21b9b94a-ec23-42c1-aaac-e2ae4e4dbffe");
        });

        test("getSessionKeyValue, should return session key int", () => {
            // Arrange
            setupCookies({});

            // Act
            const result = getSessionKeyValue();

            //Assert
            expect(result).toBe("12345");
        });
    });

    describe("getSessionIdValue", () => {
        test("getSessionIdValue, should return GUID", () => {
            // Arrange
            setupCookies({});

            // Act
            const result = getSessionIdValue();

            //Assert
            expect(result).toBe("cba0c3d1-3c1b-4305-bb56-31aa50f58e27");
        });
    });
});
