From 8e5931a1fb33b522562c9291e72f4623fcbc06dd Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Mon, 18 May 2026 12:59:37 +0200 Subject: [PATCH] Add unit tests for resolving viewport world state --- .../viewport-simulation-membership.test.ts | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/tests/unit/viewport-simulation-membership.test.ts b/tests/unit/viewport-simulation-membership.test.ts index d27d19a0..8624443b 100644 --- a/tests/unit/viewport-simulation-membership.test.ts +++ b/tests/unit/viewport-simulation-membership.test.ts @@ -1,7 +1,10 @@ import { describe, expect, it } from "vitest"; +import { createDefaultProjectTimeSettings } from "../../src/document/project-time-settings"; +import { createDefaultWorldSettings } from "../../src/document/world-settings"; import { - createViewportSimulationMembershipSignatures + createViewportSimulationMembershipSignatures, + resolveViewportWorldState } from "../../src/viewport-three/viewport-host"; import type { RuntimeSceneDefinition } from "../../src/runtime-three/runtime-scene-build"; @@ -98,3 +101,64 @@ describe("createViewportSimulationMembershipSignatures", () => { ).not.toEqual(createViewportSimulationMembershipSignatures(runtimeScene)); }); }); + +describe("resolveViewportWorldState", () => { + it("applies document project time when the runtime scene is unavailable", () => { + const world = createDefaultWorldSettings(); + const time = createDefaultProjectTimeSettings(); + time.startTimeOfDayHours = 0; + world.projectTimeLightingEnabled = true; + world.ambientLight = { + colorHex: "#ffffff", + intensity: 1 + }; + world.timeOfDay.night.ambientColorHex = "#101820"; + world.timeOfDay.night.ambientIntensityFactor = 0.25; + + const state = resolveViewportWorldState({ + currentWorld: world, + currentDocument: { time }, + currentSimulationScene: null, + currentSimulationClock: { + timeOfDayHours: 0, + dayCount: 0, + dayLengthMinutes: time.dayLengthMinutes + } + }); + + expect(state.resolvedTime?.dayPhase).toBe("night"); + expect(state.resolvedWorld?.ambientLight).toEqual({ + colorHex: "#101820", + intensity: 0.25 + }); + }); + + it("prefers runtime scene time settings when a runtime scene is available", () => { + const documentWorld = createDefaultWorldSettings(); + const runtimeWorld = createDefaultWorldSettings(); + const documentTime = createDefaultProjectTimeSettings(); + const runtimeTime = createDefaultProjectTimeSettings(); + documentTime.sunriseTimeOfDayHours = 6; + documentTime.sunsetTimeOfDayHours = 18; + runtimeTime.sunriseTimeOfDayHours = 10; + runtimeTime.sunsetTimeOfDayHours = 20; + + const state = resolveViewportWorldState({ + currentWorld: documentWorld, + currentDocument: { time: documentTime }, + currentSimulationScene: { + time: runtimeTime, + world: runtimeWorld + }, + currentSimulationClock: { + timeOfDayHours: 9, + dayCount: 0, + dayLengthMinutes: runtimeTime.dayLengthMinutes + } + }); + + expect(state.world).toBe(runtimeWorld); + expect(state.timeSettings).toBe(runtimeTime); + expect(state.resolvedTime?.dayPhase).toBe("night"); + }); +});