Files
webeditor3d/tests/domain/transform-session.command.test.js
Victor Giers 0b54fa14c9 auto-git:
[add] playwright.config.js
 [add] src/app/App.js
 [add] src/app/editor-store.js
 [add] src/app/use-editor-store.js
 [add] src/assets/audio-assets.js
 [add] src/assets/gltf-model-import.js
 [add] src/assets/image-assets.js
 [add] src/assets/model-instance-labels.js
 [add] src/assets/model-instance-rendering.js
 [add] src/assets/model-instances.js
 [add] src/assets/project-asset-storage.js
 [add] src/assets/project-assets.js
 [add] src/commands/brush-command-helpers.js
 [add] src/commands/command-history.js
 [add] src/commands/command.js
 [add] src/commands/commit-transform-session-command.js
 [add] src/commands/create-box-brush-command.js
 [add] src/commands/delete-box-brush-command.js
 [add] src/commands/delete-entity-command.js
 [add] src/commands/delete-interaction-link-command.js
 [add] src/commands/delete-model-instance-command.js
 [add] src/commands/import-audio-asset-command.js
 [add] src/commands/import-background-image-asset-command.js
 [add] src/commands/import-model-asset-command.js
 [add] src/commands/move-box-brush-command.js
 [add] src/commands/resize-box-brush-command.js
 [add] src/commands/rotate-box-brush-command.js
 [add] src/commands/set-box-brush-face-material-command.js
 [add] src/commands/set-box-brush-face-uv-state-command.js
 [add] src/commands/set-box-brush-name-command.js
 [add] src/commands/set-box-brush-transform-command.js
 [add] src/commands/set-entity-name-command.js
 [add] src/commands/set-model-instance-name-command.js
 [add] src/commands/set-player-start-command.js
 [add] src/commands/set-scene-name-command.js
 [add] src/commands/set-world-settings-command.js
 [add] src/commands/upsert-entity-command.js
 [add] src/commands/upsert-interaction-link-command.js
 [add] src/commands/upsert-model-instance-command.js
 [add] src/core/ids.js
 [add] src/core/selection.js
 [add] src/core/tool-mode.js
 [add] src/core/transform-session.js
 [add] src/core/vector.js
 [add] src/core/whitebox-selection-feedback.js
 [add] src/core/whitebox-selection-mode.js
 [add] src/document/brushes.js
 [add] src/document/migrate-scene-document.js
 [add] src/document/scene-document-validation.js
 [add] src/document/scene-document.js
 [add] src/document/world-settings.js
 [add] src/entities/entity-instances.js
 [add] src/entities/entity-labels.js
 [add] src/geometry/box-brush-components.js
 [add] src/geometry/box-brush-mesh.js
 [add] src/geometry/box-brush.js
 [add] src/geometry/box-face-uvs.js
 [add] src/geometry/grid-snapping.js
 [add] src/geometry/model-instance-collider-debug-mesh.js
 [add] src/geometry/model-instance-collider-generation.js
 [add] src/interactions/interaction-links.js
 [add] src/main.js
 [add] src/materials/starter-material-library.js
 [add] src/materials/starter-material-textures.js
 [add] src/rendering/advanced-rendering.js
 [add] src/runner-web/RunnerCanvas.js
 [add] src/runtime-three/first-person-navigation-controller.js
 [add] src/runtime-three/navigation-controller.js
 [add] src/runtime-three/orbit-visitor-navigation-controller.js
 [add] src/runtime-three/player-collision.js
 [add] src/runtime-three/rapier-collision-world.js
 [add] src/runtime-three/runtime-audio-system.js
 [add] src/runtime-three/runtime-host.js
 [add] src/runtime-three/runtime-interaction-system.js
 [add] src/runtime-three/runtime-scene-build.js
 [add] src/runtime-three/runtime-scene-validation.js
 [add] src/serialization/local-draft-storage.js
 [add] src/serialization/scene-document-json.js
 [add] src/shared-ui/HierarchicalMenu.js
 [add] src/shared-ui/Panel.js
 [add] src/shared-ui/world-background-style.js
 [add] src/viewport-three/ViewportCanvas.js
 [add] src/viewport-three/ViewportPanel.js
 [add] src/viewport-three/viewport-entity-markers.js
 [add] src/viewport-three/viewport-focus.js
 [add] src/viewport-three/viewport-host.js
 [add] src/viewport-three/viewport-layout.js
 [add] src/viewport-three/viewport-transient-state.js
 [add] src/viewport-three/viewport-view-modes.js
 [add] tests/domain/box-brush-face-editing.command.test.js
 [add] tests/domain/build-runtime-scene.test.js
 [add] tests/domain/create-box-brush.command.test.js
 [add] tests/domain/create-empty-scene-document.test.js
 [add] tests/domain/editor-store.test.js
 [add] tests/domain/entity.command.test.js
 [add] tests/domain/interaction-links.validation.test.js
 [add] tests/domain/model-import.test.js
 [add] tests/domain/model-instance.command.test.js
 [add] tests/domain/player-start.command.test.js
 [add] tests/domain/rapier-collision-world.test.js
 [add] tests/domain/runtime-audio-system.test.js
 [add] tests/domain/runtime-interaction-system.test.js
 [add] tests/domain/runtime-scene-validation.test.js
 [add] tests/domain/scene-document-validation.test.js
 [add] tests/domain/transform-session.command.test.js
 [add] tests/domain/world-settings.command.test.js
 [add] tests/domain/world-settings.test.js
 [add] tests/e2e/app-smoke.e2e.js
 [add] tests/e2e/box-brush-authoring.e2e.js
 [add] tests/e2e/entities-foundation.e2e.js
 [add] tests/e2e/face-material-authoring.e2e.js
 [add] tests/e2e/first-room-workflow.e2e.js
 [add] tests/e2e/import-draco-model-asset.e2e.js
 [add] tests/e2e/import-external-model-asset.e2e.js
 [add] tests/e2e/import-model-asset.e2e.js
 [add] tests/e2e/local-lights-and-background.e2e.js
 [add] tests/e2e/orthographic-views.e2e.js
 [add] tests/e2e/runner-v1.e2e.js
 [add] tests/e2e/runtime-click-interaction.e2e.js
 [add] tests/e2e/runtime-trigger-teleport.e2e.js
 [add] tests/e2e/viewport-quad-layout.e2e.js
 [add] tests/e2e/viewport-test-helpers.js
 [add] tests/e2e/whitebox-component-selection.e2e.js
 [add] tests/e2e/world-environment.e2e.js
 [add] tests/geometry/box-brush-geometry.test.js
 [add] tests/geometry/box-face-uvs.test.js
 [add] tests/geometry/model-instance-collider-generation.test.js
 [add] tests/helpers/model-collider-fixtures.js
 [add] tests/serialization/local-draft-storage.test.js
 [add] tests/serialization/project-asset-storage.test.js
 [add] tests/serialization/scene-document-json.test.js
 [add] tests/setup/vitest.setup.js
 [add] tests/unit/audio-assets.test.js
 [add] tests/unit/entity-instances.test.js
 [add] tests/unit/package-scripts.test.js
 [add] tests/unit/transform-foundation.integration.test.js
 [add] tests/unit/viewport-canvas.test.js
 [add] tests/unit/viewport-entity-markers.test.js
 [add] tests/unit/viewport-focus.test.js
 [add] tests/unit/viewport-layout.test.js
 [add] tests/unit/viewport-view-modes.test.js
 [add] vite.config.js
 [add] vitest.config.js
2026-04-05 02:25:29 +02:00

582 lines
20 KiB
JavaScript

import { describe, expect, it } from "vitest";
import { createEditorStore } from "../../src/app/editor-store";
import { createModelInstance } from "../../src/assets/model-instances";
import { createProjectAssetStorageKey } from "../../src/assets/project-assets";
import { createCommitTransformSessionCommand } from "../../src/commands/commit-transform-session-command";
import { createTransformSession, resolveTransformTarget, supportsTransformAxisConstraint, supportsTransformOperation } from "../../src/core/transform-session";
import { createBoxBrush } from "../../src/document/brushes";
import { createEmptySceneDocument } from "../../src/document/scene-document";
import { createPlayerStartEntity } from "../../src/entities/entity-instances";
const modelAsset = {
id: "asset-model-transform-fixture",
kind: "model",
sourceName: "transform-fixture.glb",
mimeType: "model/gltf-binary",
storageKey: createProjectAssetStorageKey("asset-model-transform-fixture"),
byteLength: 64,
metadata: {
kind: "model",
format: "glb",
sceneName: "Transform Fixture",
nodeCount: 1,
meshCount: 1,
materialNames: [],
textureNames: [],
animationNames: [],
boundingBox: {
min: {
x: -0.5,
y: 0,
z: -0.5
},
max: {
x: 0.5,
y: 1,
z: 0.5
},
size: {
x: 1,
y: 1,
z: 1
}
},
warnings: []
}
};
describe("transform session commit commands", () => {
it("resolves component transform targets in matching mode and enforces operation support", () => {
const brush = createBoxBrush({
id: "brush-main"
});
const document = {
...createEmptySceneDocument(),
brushes: {
[brush.id]: brush
}
};
const faceWrongModeResolved = resolveTransformTarget(document, {
kind: "brushFace",
brushId: brush.id,
faceId: "posZ"
});
const faceResolved = resolveTransformTarget(document, {
kind: "brushFace",
brushId: brush.id,
faceId: "posZ"
}, "face");
const edgeResolved = resolveTransformTarget(document, {
kind: "brushEdge",
brushId: brush.id,
edgeId: "edgeX_posY_negZ"
}, "edge");
const vertexResolved = resolveTransformTarget(document, {
kind: "brushVertex",
brushId: brush.id,
vertexId: "posX_posY_negZ"
}, "vertex");
const faceModeBrushResolved = resolveTransformTarget(document, {
kind: "brushes",
ids: [brush.id]
}, "face");
const objectResolved = resolveTransformTarget(document, {
kind: "brushes",
ids: [brush.id]
});
expect(faceWrongModeResolved.target).toBeNull();
expect(faceWrongModeResolved.message).toContain("Face mode");
expect(faceResolved.target).toMatchObject({
kind: "brushFace",
brushId: brush.id,
faceId: "posZ"
});
expect(edgeResolved.target).toMatchObject({
kind: "brushEdge",
brushId: brush.id,
edgeId: "edgeX_posY_negZ"
});
expect(vertexResolved.target).toMatchObject({
kind: "brushVertex",
brushId: brush.id,
vertexId: "posX_posY_negZ"
});
expect(faceModeBrushResolved.target).toBeNull();
expect(faceModeBrushResolved.message).toContain("Object mode");
expect(objectResolved.target).toMatchObject({
kind: "brush",
brushId: brush.id,
initialCenter: brush.center,
initialRotationDegrees: brush.rotationDegrees,
initialSize: brush.size
});
expect(objectResolved.target).not.toBeNull();
expect(supportsTransformOperation(objectResolved.target, "translate")).toBe(true);
expect(supportsTransformOperation(objectResolved.target, "rotate")).toBe(true);
expect(supportsTransformOperation(objectResolved.target, "scale")).toBe(true);
expect(supportsTransformOperation(faceResolved.target, "translate")).toBe(true);
expect(supportsTransformOperation(faceResolved.target, "rotate")).toBe(true);
expect(supportsTransformOperation(faceResolved.target, "scale")).toBe(true);
expect(supportsTransformOperation(vertexResolved.target, "translate")).toBe(true);
expect(supportsTransformOperation(vertexResolved.target, "rotate")).toBe(false);
expect(supportsTransformOperation(vertexResolved.target, "scale")).toBe(false);
});
it("applies axis-constraint rules across object and component transform sessions", () => {
const brush = createBoxBrush({
id: "brush-axis-rules"
});
const document = {
...createEmptySceneDocument(),
brushes: {
[brush.id]: brush
}
};
const faceTarget = resolveTransformTarget(document, {
kind: "brushFace",
brushId: brush.id,
faceId: "posX"
}, "face").target;
const edgeTarget = resolveTransformTarget(document, {
kind: "brushEdge",
brushId: brush.id,
edgeId: "edgeY_posX_posZ"
}, "edge").target;
const vertexTarget = resolveTransformTarget(document, {
kind: "brushVertex",
brushId: brush.id,
vertexId: "posX_posY_posZ"
}, "vertex").target;
if (faceTarget === null || faceTarget.kind !== "brushFace") {
throw new Error("Expected a face transform target.");
}
if (edgeTarget === null || edgeTarget.kind !== "brushEdge") {
throw new Error("Expected an edge transform target.");
}
if (vertexTarget === null || vertexTarget.kind !== "brushVertex") {
throw new Error("Expected a vertex transform target.");
}
const faceRotateSession = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "rotate",
target: faceTarget
});
const edgeScaleSession = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "scale",
target: edgeTarget
});
const vertexTranslateSession = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "translate",
target: vertexTarget
});
expect(supportsTransformAxisConstraint(faceRotateSession, "x")).toBe(true);
expect(supportsTransformAxisConstraint(faceRotateSession, "y")).toBe(false);
expect(supportsTransformAxisConstraint(faceRotateSession, "z")).toBe(false);
expect(supportsTransformAxisConstraint(edgeScaleSession, "x")).toBe(true);
expect(supportsTransformAxisConstraint(edgeScaleSession, "y")).toBe(false);
expect(supportsTransformAxisConstraint(edgeScaleSession, "z")).toBe(true);
expect(supportsTransformAxisConstraint(vertexTranslateSession, "x")).toBe(true);
expect(supportsTransformAxisConstraint(vertexTranslateSession, "y")).toBe(true);
expect(supportsTransformAxisConstraint(vertexTranslateSession, "z")).toBe(true);
});
it("commits whitebox box rotate and scale transforms with undo and redo", () => {
const brush = createBoxBrush({
id: "brush-transform-main",
center: {
x: 1.25,
y: 1.5,
z: -0.75
},
size: {
x: 2.5,
y: 2,
z: 4
}
});
const store = createEditorStore({
initialDocument: {
...createEmptySceneDocument({ name: "Brush Transform Fixture" }),
brushes: {
[brush.id]: brush
}
}
});
const target = resolveTransformTarget(store.getState().document, {
kind: "brushes",
ids: [brush.id]
}).target;
if (target === null || target.kind !== "brush") {
throw new Error("Expected a whitebox box transform target.");
}
const rotateSession = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "rotate",
target
});
rotateSession.preview = {
kind: "brush",
center: {
...brush.center
},
rotationDegrees: {
x: 0,
y: 37.5,
z: 12.5
},
size: {
...brush.size
}
};
store.executeCommand(createCommitTransformSessionCommand(store.getState().document, rotateSession));
expect(store.getState().document.brushes[brush.id].rotationDegrees).toEqual({
x: 0,
y: 37.5,
z: 12.5
});
const scaleTarget = resolveTransformTarget(store.getState().document, {
kind: "brushes",
ids: [brush.id]
}).target;
if (scaleTarget === null || scaleTarget.kind !== "brush") {
throw new Error("Expected a whitebox box transform target after rotation.");
}
const scaleSession = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "scale",
target: scaleTarget
});
scaleSession.preview = {
kind: "brush",
center: {
...brush.center
},
rotationDegrees: {
x: 0,
y: 37.5,
z: 12.5
},
size: {
x: 3.25,
y: 1.75,
z: 5.5
}
};
store.executeCommand(createCommitTransformSessionCommand(store.getState().document, scaleSession));
expect(store.getState().document.brushes[brush.id]).toMatchObject({
rotationDegrees: {
x: 0,
y: 37.5,
z: 12.5
},
size: {
x: 3.25,
y: 1.75,
z: 5.5
}
});
expect(store.undo()).toBe(true);
expect(store.getState().document.brushes[brush.id]).toMatchObject({
rotationDegrees: {
x: 0,
y: 37.5,
z: 12.5
},
size: {
x: 2.5,
y: 2,
z: 4
}
});
expect(store.undo()).toBe(true);
expect(store.getState().document.brushes[brush.id]).toEqual(brush);
expect(store.redo()).toBe(true);
expect(store.redo()).toBe(true);
expect(store.getState().document.brushes[brush.id]).toMatchObject({
rotationDegrees: {
x: 0,
y: 37.5,
z: 12.5
},
size: {
x: 3.25,
y: 1.75,
z: 5.5
}
});
});
it("commits a face transform preview and restores it through undo/redo", () => {
const brush = createBoxBrush({
id: "brush-face-transform",
center: { x: 0, y: 1, z: 0 },
size: { x: 2, y: 2, z: 2 }
});
const store = createEditorStore({
initialDocument: {
...createEmptySceneDocument({ name: "Face Transform Fixture" }),
brushes: {
[brush.id]: brush
}
}
});
const target = resolveTransformTarget(store.getState().document, {
kind: "brushFace",
brushId: brush.id,
faceId: "posX"
}, "face").target;
if (target === null || target.kind !== "brushFace") {
throw new Error("Expected a whitebox face transform target.");
}
const session = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "translate",
target
});
session.preview = {
kind: "brush",
center: { x: 0.5, y: 1, z: 0 },
rotationDegrees: { x: 0, y: 0, z: 0 },
size: { x: 3, y: 2, z: 2 }
};
store.executeCommand(createCommitTransformSessionCommand(store.getState().document, session));
expect(store.getState().document.brushes[brush.id]).toMatchObject({
center: { x: 0.5, y: 1, z: 0 },
size: { x: 3, y: 2, z: 2 }
});
expect(store.getState().selection).toEqual({
kind: "brushFace",
brushId: brush.id,
faceId: "posX"
});
expect(store.undo()).toBe(true);
expect(store.getState().document.brushes[brush.id]).toEqual(brush);
expect(store.redo()).toBe(true);
expect(store.getState().document.brushes[brush.id]).toMatchObject({
center: { x: 0.5, y: 1, z: 0 },
size: { x: 3, y: 2, z: 2 }
});
});
it("commits a vertex transform preview and preserves vertex selection through undo/redo", () => {
const brush = createBoxBrush({
id: "brush-vertex-transform",
center: { x: 0, y: 1, z: 0 },
size: { x: 2, y: 2, z: 2 }
});
const store = createEditorStore({
initialDocument: {
...createEmptySceneDocument({ name: "Vertex Transform Fixture" }),
brushes: {
[brush.id]: brush
}
}
});
const target = resolveTransformTarget(store.getState().document, {
kind: "brushVertex",
brushId: brush.id,
vertexId: "posX_posY_posZ"
}, "vertex").target;
if (target === null || target.kind !== "brushVertex") {
throw new Error("Expected a whitebox vertex transform target.");
}
const session = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "translate",
target
});
session.preview = {
kind: "brush",
center: { x: 0.5, y: 1.5, z: 0.5 },
rotationDegrees: { x: 0, y: 0, z: 0 },
size: { x: 3, y: 3, z: 3 }
};
store.executeCommand(createCommitTransformSessionCommand(store.getState().document, session));
expect(store.getState().document.brushes[brush.id]).toMatchObject({
center: { x: 0.5, y: 1.5, z: 0.5 },
size: { x: 3, y: 3, z: 3 }
});
expect(store.getState().selection).toEqual({
kind: "brushVertex",
brushId: brush.id,
vertexId: "posX_posY_posZ"
});
expect(store.undo()).toBe(true);
expect(store.getState().document.brushes[brush.id]).toEqual(brush);
expect(store.redo()).toBe(true);
expect(store.getState().selection).toEqual({
kind: "brushVertex",
brushId: brush.id,
vertexId: "posX_posY_posZ"
});
});
it("commits a model instance translate/rotate/scale transform with undo and redo", () => {
const modelInstance = createModelInstance({
id: "model-instance-main",
assetId: modelAsset.id,
position: {
x: 0,
y: 1,
z: 0
},
rotationDegrees: {
x: 0,
y: 0,
z: 0
},
scale: {
x: 1,
y: 1,
z: 1
}
});
const store = createEditorStore({
initialDocument: {
...createEmptySceneDocument({ name: "Transform Fixture" }),
assets: {
[modelAsset.id]: modelAsset
},
modelInstances: {
[modelInstance.id]: modelInstance
}
}
});
const target = resolveTransformTarget(store.getState().document, {
kind: "modelInstances",
ids: [modelInstance.id]
}).target;
if (target === null || target.kind !== "modelInstance") {
throw new Error("Expected a model instance transform target.");
}
const session = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "scale",
target
});
session.preview = {
kind: "modelInstance",
position: {
x: 4,
y: 1,
z: -2
},
rotationDegrees: {
x: 0,
y: 90,
z: 0
},
scale: {
x: 1.5,
y: 2,
z: 1.5
}
};
store.executeCommand(createCommitTransformSessionCommand(store.getState().document, session));
expect(store.getState().document.modelInstances[modelInstance.id]).toMatchObject({
position: {
x: 4,
y: 1,
z: -2
},
rotationDegrees: {
x: 0,
y: 90,
z: 0
},
scale: {
x: 1.5,
y: 2,
z: 1.5
}
});
expect(store.undo()).toBe(true);
expect(store.getState().document.modelInstances[modelInstance.id]).toEqual(modelInstance);
expect(store.redo()).toBe(true);
expect(store.getState().document.modelInstances[modelInstance.id]).toMatchObject({
position: {
x: 4,
y: 1,
z: -2
},
rotationDegrees: {
x: 0,
y: 90,
z: 0
},
scale: {
x: 1.5,
y: 2,
z: 1.5
}
});
});
it("commits a rotatable entity transform with undo and redo", () => {
const playerStart = createPlayerStartEntity({
id: "entity-player-start-main",
position: {
x: 0,
y: 0,
z: 0
},
yawDegrees: 0
});
const store = createEditorStore({
initialDocument: {
...createEmptySceneDocument({ name: "Entity Transform Fixture" }),
entities: {
[playerStart.id]: playerStart
}
}
});
const target = resolveTransformTarget(store.getState().document, {
kind: "entities",
ids: [playerStart.id]
}).target;
if (target === null || target.kind !== "entity") {
throw new Error("Expected an entity transform target.");
}
const session = createTransformSession({
source: "keyboard",
sourcePanelId: "topLeft",
operation: "rotate",
target
});
session.preview = {
kind: "entity",
position: {
x: 6,
y: 0,
z: -4
},
rotation: {
kind: "yaw",
yawDegrees: 90
}
};
store.executeCommand(createCommitTransformSessionCommand(store.getState().document, session));
expect(store.getState().document.entities[playerStart.id]).toMatchObject({
position: {
x: 6,
y: 0,
z: -4
},
yawDegrees: 90
});
expect(store.undo()).toBe(true);
expect(store.getState().document.entities[playerStart.id]).toEqual(playerStart);
expect(store.redo()).toBe(true);
expect(store.getState().document.entities[playerStart.id]).toMatchObject({
position: {
x: 6,
y: 0,
z: -4
},
yawDegrees: 90
});
});
});