import { makeAppObjectRepo } from "@vived/core";
import { MockAppHandlerEntity } from "../Mocks/MockAppHandlerEntity";
import { makeStartZSpaceUC } from "./StartZSpaceUC";
import { MockCheckZSpaceDisplaySizeUC } from "../../zSpace/Mocks/MockCheckZSpaceDisplaySizeUC";
import { makeZSpaceEntity, ZSpaceDevice } from "../../zSpace";
import { MockRenderToXR } from "../../XR/Mocks/MockRenderToXR";
import { MockXRSession } from "../../XR/Mocks/MockXRSession";
import { makeMockEmulateStylusUC } from "../../Stylus";
function makeTestRig() {
    const appObjectRepo = makeAppObjectRepo();
    const appObj = appObjectRepo.getOrCreate("Handler");
    const mockAppHandler = new MockAppHandlerEntity(appObj);
    const zSpaceEntity = makeZSpaceEntity(appObj);
    const mockRenderUC = new MockRenderToXR(appObj);
    const mockCheckZSpaceDisplaySize = new MockCheckZSpaceDisplaySizeUC(appObj);
    const mockEmulate = makeMockEmulateStylusUC(appObjectRepo);
    const startZSpace = makeStartZSpaceUC(appObj);
    return {
        mockAppHandler,
        startZSpace,
        mockCheckZSpaceDisplaySize,
        zSpaceEntity,
        mockRenderUC,
        mockEmulate,
    };
}
describe("Set is authoring Base Handler", () => {
    it("Registers as a handler when constructed", () => {
        const { mockAppHandler, startZSpace } = makeTestRig();
        expect(mockAppHandler.registerRequestHandler).toHaveBeenCalledWith(startZSpace);
    });
    it("Triggers the action for v3", () => {
        const { startZSpace } = makeTestRig();
        startZSpace.action = jest.fn();
        startZSpace.handleRequest(3, {
            session: { foo: "bar" },
            emulate: true,
        });
        expect(startZSpace.action).toHaveBeenCalledWith({ foo: "bar" }, true);
    });
    it("Throws for an bungled payload", () => {
        const { startZSpace } = makeTestRig();
        expect(() => startZSpace.handleRequest(3, { foo: "bar" })).toThrowError();
    });
    it("Throws for an unsupported version", () => {
        const { startZSpace } = makeTestRig();
        expect(() => startZSpace.handleRequest(-1, {
            session: { foo: "bar" },
        })).toThrowError();
    });
    it("Warns if it cannot parse the xr session", () => {
        const { startZSpace } = makeTestRig();
        startZSpace.warn = jest.fn();
        startZSpace.action(null, false);
        expect(startZSpace.warn).toHaveBeenCalled();
    });
    it("Warns if it can't find the zSpace entity session", () => {
        const { startZSpace, zSpaceEntity } = makeTestRig();
        startZSpace.warn = jest.fn();
        zSpaceEntity.dispose();
        console.warn = jest.fn();
        startZSpace.action(new MockXRSession(), false);
        expect(startZSpace.warn).toHaveBeenCalled();
    });
    it("Sets the zSpace Device to Inspire by default", () => {
        const { startZSpace, zSpaceEntity } = makeTestRig();
        const spy = jest.spyOn(zSpaceEntity.display, "setForDevice");
        startZSpace.action(new MockXRSession(), false);
        expect(spy).toHaveBeenCalledWith(ZSpaceDevice.INSPIRE);
    });
    it("Starts the rendering", () => {
        const { startZSpace, mockRenderUC } = makeTestRig();
        startZSpace.action(new MockXRSession(), false);
        expect(mockRenderUC.startRenderingXR).toHaveBeenCalled();
    });
    it("Set enabled to true", () => {
        const { startZSpace, zSpaceEntity } = makeTestRig();
        const spy = jest.spyOn(zSpaceEntity, "enabled", "set");
        startZSpace.action(new MockXRSession(), false);
        expect(spy).toHaveBeenCalledWith(true);
    });
    it("Sets emulation to true if is passed in", () => {
        const { startZSpace, zSpaceEntity } = makeTestRig();
        expect(zSpaceEntity.emulate).toEqual(false);
        startZSpace.action({}, true);
        expect(zSpaceEntity.emulate).toEqual(true);
    });
    it("Checks the zspace display size if not emulating", () => {
        const { startZSpace, mockCheckZSpaceDisplaySize } = makeTestRig();
        const mockSession = new MockXRSession();
        startZSpace.action(mockSession, false);
        expect(mockCheckZSpaceDisplaySize.checkDisplayFromXRSession).toHaveBeenCalledWith(mockSession);
    });
    it("Does not check the zspace display size if emulating", () => {
        const { startZSpace, mockCheckZSpaceDisplaySize } = makeTestRig();
        startZSpace.action(new MockXRSession(), true);
        expect(mockCheckZSpaceDisplaySize.checkDisplayFromXRSession).not.toHaveBeenCalled();
    });
    it("Kicks off the stylus emulator", () => {
        const { startZSpace, mockEmulate } = makeTestRig();
        startZSpace.action(new MockXRSession(), true);
        expect(mockEmulate.startEmulating).toHaveBeenCalled();
    });
});
//# sourceMappingURL=StartZSpaceUC.test.js.map