Refactor App.tsx and add scene document validation tests
This commit is contained in:
@@ -942,7 +942,12 @@ export function App({ store, initialStatusMessage }: AppProps) {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="toolbar__group">
|
<div className="toolbar__group">
|
||||||
<button className="toolbar__button toolbar__button--accent" type="button" data-testid="create-box-brush" onClick={handleCreateBoxBrush}>
|
<button
|
||||||
|
className="toolbar__button toolbar__button--accent"
|
||||||
|
type="button"
|
||||||
|
data-testid="create-box-brush"
|
||||||
|
onClick={() => handleCreateBoxBrush()}
|
||||||
|
>
|
||||||
Create Box
|
Create Box
|
||||||
</button>
|
</button>
|
||||||
<button className="toolbar__button" type="button" data-testid="place-player-start-toolbar" onClick={handleSelectOrPlacePlayerStart}>
|
<button className="toolbar__button" type="button" data-testid="place-player-start-toolbar" onClick={handleSelectOrPlacePlayerStart}>
|
||||||
@@ -958,21 +963,6 @@ export function App({ store, initialStatusMessage }: AppProps) {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="toolbar__group">
|
|
||||||
<button className="toolbar__button" type="button" disabled={!editorState.storageAvailable} onClick={handleSaveDraft}>
|
|
||||||
Save Draft
|
|
||||||
</button>
|
|
||||||
<button className="toolbar__button" type="button" disabled={!editorState.storageAvailable} onClick={handleLoadDraft}>
|
|
||||||
Load Draft
|
|
||||||
</button>
|
|
||||||
<button className="toolbar__button" type="button" onClick={handleExportJson}>
|
|
||||||
Export JSON
|
|
||||||
</button>
|
|
||||||
<button className="toolbar__button" type="button" onClick={handleImportButtonClick}>
|
|
||||||
Import JSON
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="toolbar__group">
|
<div className="toolbar__group">
|
||||||
<button className="toolbar__button" type="button" disabled={!editorState.canUndo} onClick={() => store.undo()}>
|
<button className="toolbar__button" type="button" disabled={!editorState.canUndo} onClick={() => store.undo()}>
|
||||||
Undo
|
Undo
|
||||||
|
|||||||
@@ -139,4 +139,12 @@ describe("buildRuntimeSceneFromDocument", () => {
|
|||||||
yawDegrees: 180
|
yawDegrees: 180
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("blocks first-person runtime builds when PlayerStart is missing", () => {
|
||||||
|
expect(() =>
|
||||||
|
buildRuntimeSceneFromDocument(createEmptySceneDocument({ name: "Missing Player Start" }), {
|
||||||
|
navigationMode: "firstPerson"
|
||||||
|
})
|
||||||
|
).toThrow("First-person run requires an authored Player Start");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
119
tests/domain/scene-document-validation.test.ts
Normal file
119
tests/domain/scene-document-validation.test.ts
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { createBoxBrush } from "../../src/document/brushes";
|
||||||
|
import { createEmptySceneDocument } from "../../src/document/scene-document";
|
||||||
|
import { validateSceneDocument } from "../../src/document/scene-document-validation";
|
||||||
|
import { createPlayerStartEntity } from "../../src/entities/entity-instances";
|
||||||
|
|
||||||
|
describe("validateSceneDocument", () => {
|
||||||
|
it("accepts a valid first-room document", () => {
|
||||||
|
const brush = createBoxBrush({
|
||||||
|
id: "brush-room-shell"
|
||||||
|
});
|
||||||
|
const playerStart = createPlayerStartEntity({
|
||||||
|
id: "entity-player-start-main"
|
||||||
|
});
|
||||||
|
const document = {
|
||||||
|
...createEmptySceneDocument({ name: "First Room" }),
|
||||||
|
brushes: {
|
||||||
|
[brush.id]: brush
|
||||||
|
},
|
||||||
|
entities: {
|
||||||
|
[playerStart.id]: playerStart
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const validation = validateSceneDocument(document);
|
||||||
|
|
||||||
|
expect(validation.errors).toEqual([]);
|
||||||
|
expect(validation.warnings).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("detects duplicate authored ids across collections", () => {
|
||||||
|
const brush = createBoxBrush({
|
||||||
|
id: "shared-room-id"
|
||||||
|
});
|
||||||
|
const playerStart = createPlayerStartEntity({
|
||||||
|
id: "shared-room-id"
|
||||||
|
});
|
||||||
|
const document = {
|
||||||
|
...createEmptySceneDocument(),
|
||||||
|
brushes: {
|
||||||
|
[brush.id]: brush
|
||||||
|
},
|
||||||
|
entities: {
|
||||||
|
"entity-player-start-main": playerStart
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const validation = validateSceneDocument(document);
|
||||||
|
|
||||||
|
expect(validation.errors).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
code: "entity-id-mismatch"
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
code: "duplicate-authored-id"
|
||||||
|
})
|
||||||
|
])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("detects invalid box sizes and missing material references", () => {
|
||||||
|
const brush = createBoxBrush({
|
||||||
|
id: "brush-invalid"
|
||||||
|
});
|
||||||
|
brush.size.x = 0;
|
||||||
|
brush.faces.posZ.materialId = "material-that-does-not-exist";
|
||||||
|
|
||||||
|
const validation = validateSceneDocument({
|
||||||
|
...createEmptySceneDocument(),
|
||||||
|
brushes: {
|
||||||
|
[brush.id]: brush
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(validation.errors).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
code: "invalid-box-size",
|
||||||
|
path: "brushes.brush-invalid.size"
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
code: "missing-material-ref",
|
||||||
|
path: "brushes.brush-invalid.faces.posZ.materialId"
|
||||||
|
})
|
||||||
|
])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("detects invalid Player Start values", () => {
|
||||||
|
const validation = validateSceneDocument({
|
||||||
|
...createEmptySceneDocument(),
|
||||||
|
entities: {
|
||||||
|
"entity-player-start-main": {
|
||||||
|
id: "entity-player-start-main",
|
||||||
|
kind: "playerStart",
|
||||||
|
position: {
|
||||||
|
x: 0,
|
||||||
|
y: Number.NaN,
|
||||||
|
z: 0
|
||||||
|
},
|
||||||
|
yawDegrees: Number.NaN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(validation.errors).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
code: "invalid-player-start-position"
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
code: "invalid-player-start-yaw"
|
||||||
|
})
|
||||||
|
])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -206,4 +206,57 @@ describe("scene document JSON", () => {
|
|||||||
})
|
})
|
||||||
).toThrow("Unsupported scene document version");
|
).toThrow("Unsupported scene document version");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("rejects duplicate authored ids after migration and validation", () => {
|
||||||
|
expect(() =>
|
||||||
|
parseSceneDocumentJson(
|
||||||
|
JSON.stringify({
|
||||||
|
version: 4,
|
||||||
|
name: "Duplicate Id Scene",
|
||||||
|
world: createEmptySceneDocument().world,
|
||||||
|
materials: createEmptySceneDocument().materials,
|
||||||
|
textures: {},
|
||||||
|
assets: {},
|
||||||
|
brushes: {
|
||||||
|
"brush-room-shell": {
|
||||||
|
id: "shared-id",
|
||||||
|
kind: "box",
|
||||||
|
center: {
|
||||||
|
x: 0,
|
||||||
|
y: 1,
|
||||||
|
z: 0
|
||||||
|
},
|
||||||
|
size: {
|
||||||
|
x: 2,
|
||||||
|
y: 2,
|
||||||
|
z: 2
|
||||||
|
},
|
||||||
|
faces: {
|
||||||
|
posX: { materialId: null, uv: createBoxBrush().faces.posX.uv },
|
||||||
|
negX: { materialId: null, uv: createBoxBrush().faces.negX.uv },
|
||||||
|
posY: { materialId: null, uv: createBoxBrush().faces.posY.uv },
|
||||||
|
negY: { materialId: null, uv: createBoxBrush().faces.negY.uv },
|
||||||
|
posZ: { materialId: null, uv: createBoxBrush().faces.posZ.uv },
|
||||||
|
negZ: { materialId: null, uv: createBoxBrush().faces.negZ.uv }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
modelInstances: {},
|
||||||
|
entities: {
|
||||||
|
"entity-player-start-main": {
|
||||||
|
id: "shared-id",
|
||||||
|
kind: "playerStart",
|
||||||
|
position: {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
z: 0
|
||||||
|
},
|
||||||
|
yawDegrees: 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
interactionLinks: {}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
).toThrow("Duplicate authored id shared-id");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user