"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const StateMachineEntity_1 = require("../../StateMachine/Entities/StateMachineEntity");
const StoredView_1 = require("../../StoredView");
const Entities_1 = require("../Entities");
const SerializeSceneRoot_1 = require("./SerializeSceneRoot");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const appObj = appObjects.getOrCreate("Scene Root");
    const storedView = (0, StoredView_1.makeStoredView)(appObj);
    const sceneRoot = (0, Entities_1.makeSceneRootEntity)(appObj);
    const stateMachine = (0, StateMachineEntity_1.makeStateMachineEntity)(appObj);
    const mockRegister = jest.fn();
    stateMachine.registerSerializedSystem = mockRegister;
    const serializer = (0, SerializeSceneRoot_1.makeSerializeSceneRoot)(appObj);
    return {
        mockRegister,
        sceneRoot,
        storedView,
        stateMachine,
        serializer,
        appObj,
        appObjects,
    };
}
describe("Serializing the Scene Root", () => {
    it("Registers with the state machine on construction", () => {
        const { mockRegister } = makeTestRig();
        expect(mockRegister).toHaveBeenCalled();
    });
    it("Serializes the Scene Root as expected", () => {
        const { sceneRoot, serializer } = makeTestRig();
        sceneRoot.position = new core_2.Vector3(1, 2, 3);
        const rot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        sceneRoot.rotation = rot;
        sceneRoot.scale = 5;
        const state = serializer.getSystemState();
        expect(state.position).toEqual(sceneRoot.position.dto);
        expect(state.scale).toEqual(5);
        expect(state.rotation).toEqual(sceneRoot.rotation.dto);
    });
    it("Applies V1 state", () => {
        const { sceneRoot, serializer, stateMachine } = makeTestRig();
        const desiredPos = new core_2.Vector3(1, 2, 3);
        const desiredRot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        const desiredScale = new core_2.Vector3(5, 5, 5);
        const state = {
            position: desiredPos.array,
            rotation: desiredRot.toArray(),
            scale: desiredScale.array,
        };
        const lerpMock = jest.fn();
        sceneRoot.transform.lerp = lerpMock;
        serializer.applySystemState(state, 1, 1);
        const expectedDTO = {
            durationMS: 1000,
            ease: stateMachine.transitionEaseFunction,
            desiredPosition: desiredPos,
            desiredRotation: desiredRot,
            desiredScale,
        };
        expect(lerpMock).toHaveBeenCalledWith(expectedDTO);
    });
    it("Applies V2 state", () => {
        const { sceneRoot, serializer, stateMachine } = makeTestRig();
        const desiredPos = new core_2.Vector3(1, 2, 3);
        const desiredRot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        const desiredScale = 5;
        const state = {
            position: desiredPos.dto,
            rotation: desiredRot.dto,
            scale: desiredScale,
        };
        const lerpMock = jest.fn();
        sceneRoot.transform.lerp = lerpMock;
        serializer.applySystemState(state, 2, 1);
        const expectedDTO = {
            durationMS: 1000,
            ease: stateMachine.transitionEaseFunction,
            desiredPosition: desiredPos,
            desiredRotation: desiredRot,
            desiredScale: new core_2.Vector3(5, 5, 5),
        };
        expect(lerpMock).toHaveBeenCalledWith(expectedDTO);
    });
    it("Applies the default state", () => {
        const { sceneRoot, serializer, stateMachine } = makeTestRig();
        sceneRoot.position = new core_2.Vector3(1, 2, 3);
        sceneRoot.rotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        sceneRoot.scale = 5;
        sceneRoot.lerpToDefault = jest.fn();
        serializer.applyDefaultState(2);
        expect(sceneRoot.lerpToDefault).toHaveBeenCalledWith(2000, stateMachine.transitionEaseFunction);
    });
    it("Warns and applies default if the version is not supported", () => {
        const { sceneRoot, serializer } = makeTestRig();
        sceneRoot.position = new core_2.Vector3(1, 2, 3);
        sceneRoot.rotation = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        sceneRoot.scale = 5;
        serializer.applyDefaultState = jest.fn();
        console.warn = jest.fn();
        serializer.applySystemState({ foo: "bar" }, -1, 1);
        expect(console.warn).toHaveBeenCalled();
        expect(serializer.applyDefaultState).toHaveBeenCalled();
    });
    it("Adds itself as a UC to the App Object", () => {
        const { appObj, serializer } = makeTestRig();
        expect(appObj.hasComponent(SerializeSceneRoot_1.SerializeSceneRoot.type)).toEqual(true);
        expect(appObj.getComponent(SerializeSceneRoot_1.SerializeSceneRoot.type)).toEqual(serializer);
    });
    it("Warns if it is added to a App Object that does not have transform", () => {
        const { appObjects } = makeTestRig();
        const appObj = (0, core_1.makeAppObject)("anotherObj", appObjects);
        console.warn = jest.fn();
        (0, SerializeSceneRoot_1.makeSerializeSceneRoot)(appObj);
        expect(console.warn).toHaveBeenCalled();
    });
    it("Does not add itself to an App Object that does not have transform", () => {
        const { appObjects } = makeTestRig();
        const appObj = (0, core_1.makeAppObject)("anotherObj", appObjects);
        console.warn = jest.fn();
        (0, SerializeSceneRoot_1.makeSerializeSceneRoot)(appObj);
        expect(appObj.hasComponent(SerializeSceneRoot_1.SerializeSceneRoot.type)).toEqual(false);
    });
    it("Serializes the Stored View if it exists", () => {
        const { sceneRoot, storedView, serializer } = makeTestRig();
        storedView.position = new core_2.Vector3(4, 5, 6);
        const rot1 = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(7, 8, 9), core_2.Angle.FromDegrees(10));
        storedView.rotation = rot1;
        storedView.scale = 11;
        storedView.hasStoredView = true;
        sceneRoot.position = new core_2.Vector3(1, 2, 3);
        const rot2 = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        sceneRoot.rotation = rot2;
        sceneRoot.scale = 5;
        const state = serializer.getSystemState();
        expect(state.position).toEqual(storedView.position.dto);
        expect(state.scale).toEqual(11);
        expect(state.rotation).toEqual(storedView.rotation.dto);
    });
    it("Applies V1 state even when there is no stored view", () => {
        const appObjects = (0, core_1.makeAppObjectRepo)();
        const appObj = appObjects.getOrCreate("Scene Root");
        const sceneRoot = (0, Entities_1.makeSceneRootEntity)(appObj);
        const stateMachine = (0, StateMachineEntity_1.makeStateMachineEntity)(appObj);
        const mockRegister = jest.fn();
        stateMachine.registerSerializedSystem = mockRegister;
        const serializer = (0, SerializeSceneRoot_1.makeSerializeSceneRoot)(appObj);
        const desiredPos = new core_2.Vector3(1, 2, 3);
        const desiredRot = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(4, 5, 6), core_2.Angle.FromDegrees(35));
        const desiredScale = new core_2.Vector3(5, 5, 5);
        const state = {
            position: desiredPos.array,
            rotation: desiredRot.toArray(),
            scale: desiredScale.array,
        };
        const lerpMock = jest.fn();
        sceneRoot.transform.lerp = lerpMock;
        serializer.applySystemState(state, 1, 1);
        const expectedDTO = {
            durationMS: 1000,
            ease: stateMachine.transitionEaseFunction,
            desiredPosition: desiredPos,
            desiredRotation: desiredRot,
            desiredScale,
        };
        expect(lerpMock).toHaveBeenCalledWith(expectedDTO);
    });
});
//# sourceMappingURL=SerializeSceneRoot.test.js.map