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 { createNewDeviceId, getDeviceId } from "../device";
import { mockLocalStorage } from "./__mocks__/localStorage";
describe("utils.device.createNewDeviceId", () => {
    it("creates a new device id", () => __awaiter(void 0, void 0, void 0, function* () {
        const deviceId = createNewDeviceId();
        expect(deviceId).toBeTypeOf("string");
        expect(deviceId.replace("$device:", "")).toHaveLength(36);
    }));
    it("creates a new device id with the correct prefix", () => __awaiter(void 0, void 0, void 0, function* () {
        const deviceId = createNewDeviceId();
        expect(deviceId.startsWith("$device:")).toBe(true);
    }));
    it("creates a new device id with a valid UUID", () => __awaiter(void 0, void 0, void 0, function* () {
        const deviceId = createNewDeviceId();
        const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
        const uuid = deviceId.replace("$device:", "");
        expect(uuid).toMatch(uuidRegex);
    }));
    it("creates a new device 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 deviceId = createNewDeviceId();
        expect(deviceId.startsWith("$device:default-")).toBe(true);
    }));
});
describe("utils.device.getDeviceId", () => {
    beforeAll(() => {
        mockLocalStorage();
    });
    beforeEach(() => {
        localStorage.clear();
    });
    it("creates a new device id if no device id exists", () => __awaiter(void 0, void 0, void 0, function* () {
        localStorage.removeItem("localLogicDevice");
        const deviceId = getDeviceId();
        expect(deviceId).toBeTypeOf("string");
        expect(deviceId).not.toBeNull();
    }));
    it("returns the existing device id if it exists", () => __awaiter(void 0, void 0, void 0, function* () {
        const deviceId = createNewDeviceId();
        localStorage.setItem("localLogicDevice", deviceId);
        const returnedDeviceId = getDeviceId();
        expect(returnedDeviceId).toEqual(deviceId);
    }));
    it("returns a new device id if the existing one is empty", () => __awaiter(void 0, void 0, void 0, function* () {
        localStorage.setItem("localLogicDevice", "");
        const deviceId = getDeviceId();
        expect(deviceId).toBeTypeOf("string");
        expect(deviceId).not.toEqual("");
        expect(deviceId.replace("$device:", "")).toHaveLength(36);
    }));
    it("returns a valid device ID", () => __awaiter(void 0, void 0, void 0, function* () {
        const deviceId = getDeviceId();
        expect(deviceId).toBeTypeOf("string");
        expect(deviceId).not.toBeNull();
    }));
    it("device ID is unique", () => __awaiter(void 0, void 0, void 0, function* () {
        const deviceId1 = getDeviceId();
        localStorage.clear();
        const deviceId2 = getDeviceId();
        expect(deviceId1).not.toEqual(deviceId2);
    }));
    it("device ID is consistent across calls", () => __awaiter(void 0, void 0, void 0, function* () {
        const deviceId1 = getDeviceId();
        const deviceId2 = getDeviceId();
        expect(deviceId1).toEqual(deviceId2);
    }));
});
//# sourceMappingURL=device.test.js.map