2026-04-11 04:18:13 +02:00
|
|
|
import { waitFor } from "@testing-library/react";
|
|
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
2026-04-14 01:37:48 +02:00
|
|
|
import {
|
|
|
|
|
createLightControlTargetRef,
|
2026-04-14 01:38:26 +02:00
|
|
|
type ControlEffect,
|
2026-04-14 01:37:48 +02:00
|
|
|
createSetLightEnabledControlEffect,
|
|
|
|
|
createSetLightIntensityControlEffect
|
|
|
|
|
} from "../../src/controls/control-surface";
|
2026-04-11 04:18:13 +02:00
|
|
|
import { createEmptySceneDocument } from "../../src/document/scene-document";
|
2026-04-14 01:37:48 +02:00
|
|
|
import { createPointLightEntity } from "../../src/entities/entity-instances";
|
2026-04-14 01:38:26 +02:00
|
|
|
import {
|
|
|
|
|
createControlInteractionLink,
|
|
|
|
|
type InteractionLink
|
|
|
|
|
} from "../../src/interactions/interaction-links";
|
2026-04-11 04:18:13 +02:00
|
|
|
import { RapierCollisionWorld } from "../../src/runtime-three/rapier-collision-world";
|
|
|
|
|
import {
|
|
|
|
|
RuntimeHost,
|
|
|
|
|
type RuntimeSceneLoadState
|
|
|
|
|
} from "../../src/runtime-three/runtime-host";
|
|
|
|
|
import { buildRuntimeSceneFromDocument } from "../../src/runtime-three/runtime-scene-build";
|
|
|
|
|
|
|
|
|
|
function createDeferred<T>() {
|
|
|
|
|
let resolve: ((value: T) => void) | null = null;
|
|
|
|
|
let reject: ((error: unknown) => void) | null = null;
|
|
|
|
|
const promise = new Promise<T>((innerResolve, innerReject) => {
|
|
|
|
|
resolve = innerResolve;
|
|
|
|
|
reject = innerReject;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
promise,
|
|
|
|
|
resolve(value: T) {
|
|
|
|
|
resolve?.(value);
|
|
|
|
|
},
|
|
|
|
|
reject(error: unknown) {
|
|
|
|
|
reject?.(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("RuntimeHost", () => {
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("delays controller activation until collision setup reports the scene as ready", async () => {
|
2026-04-11 04:19:50 +02:00
|
|
|
const runtimeScene = buildRuntimeSceneFromDocument(
|
|
|
|
|
createEmptySceneDocument()
|
|
|
|
|
);
|
2026-04-11 04:19:15 +02:00
|
|
|
vi.spyOn(console, "warn").mockImplementation(() => undefined);
|
2026-04-11 04:18:13 +02:00
|
|
|
const collisionWorld = {
|
2026-04-11 11:22:17 +02:00
|
|
|
dispose: vi.fn(),
|
|
|
|
|
resolveThirdPersonCameraCollision: vi.fn(
|
|
|
|
|
(_pivot, desiredCameraPosition) => desiredCameraPosition
|
|
|
|
|
)
|
2026-04-11 04:18:13 +02:00
|
|
|
} as unknown as RapierCollisionWorld;
|
|
|
|
|
const deferredCollisionWorld = createDeferred<RapierCollisionWorld>();
|
|
|
|
|
vi.spyOn(RapierCollisionWorld, "create").mockReturnValue(
|
|
|
|
|
deferredCollisionWorld.promise
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const runtimeMessages: Array<string | null> = [];
|
|
|
|
|
const sceneLoadStates: RuntimeSceneLoadState[] = [];
|
|
|
|
|
const host = new RuntimeHost({
|
|
|
|
|
enableRendering: false
|
|
|
|
|
});
|
|
|
|
|
host.setRuntimeMessageHandler((message) => {
|
|
|
|
|
runtimeMessages.push(message);
|
|
|
|
|
});
|
|
|
|
|
host.setSceneLoadStateHandler((state) => {
|
|
|
|
|
sceneLoadStates.push(state);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
host.loadScene(runtimeScene);
|
2026-04-11 11:19:00 +02:00
|
|
|
host.setNavigationMode("thirdPerson");
|
2026-04-11 04:18:13 +02:00
|
|
|
|
|
|
|
|
expect(sceneLoadStates).toEqual([
|
|
|
|
|
{
|
|
|
|
|
status: "loading",
|
|
|
|
|
message: null
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
expect(runtimeMessages).toEqual([null]);
|
|
|
|
|
|
|
|
|
|
deferredCollisionWorld.resolve(collisionWorld);
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
expect(sceneLoadStates).toContainEqual({
|
|
|
|
|
status: "ready",
|
|
|
|
|
message: null
|
|
|
|
|
});
|
|
|
|
|
expect(runtimeMessages).toContain(
|
2026-04-12 03:59:29 +02:00
|
|
|
"Third Person active. Drag to orbit the camera, use the right stick for gamepad camera look, move with your authored bindings, and scroll to zoom."
|
2026-04-11 04:18:13 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
host.dispose();
|
|
|
|
|
expect(collisionWorld.dispose).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
2026-04-14 01:37:48 +02:00
|
|
|
|
|
|
|
|
it("applies typed light control effects through the runtime dispatcher", () => {
|
|
|
|
|
vi.spyOn(console, "warn").mockImplementation(() => undefined);
|
|
|
|
|
vi.spyOn(RapierCollisionWorld, "create").mockResolvedValue({
|
|
|
|
|
dispose: vi.fn(),
|
|
|
|
|
resolveThirdPersonCameraCollision: vi.fn(
|
|
|
|
|
(_pivot, desiredCameraPosition) => desiredCameraPosition
|
|
|
|
|
)
|
|
|
|
|
} as unknown as RapierCollisionWorld);
|
|
|
|
|
|
|
|
|
|
const pointLight = createPointLightEntity({
|
|
|
|
|
id: "entity-point-light-main",
|
|
|
|
|
intensity: 1.25
|
|
|
|
|
});
|
|
|
|
|
const runtimeScene = buildRuntimeSceneFromDocument({
|
|
|
|
|
...createEmptySceneDocument(),
|
|
|
|
|
entities: {
|
|
|
|
|
[pointLight.id]: pointLight
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const host = new RuntimeHost({
|
|
|
|
|
enableRendering: false
|
|
|
|
|
});
|
|
|
|
|
host.loadScene(runtimeScene);
|
|
|
|
|
|
|
|
|
|
const disableEffect = createSetLightEnabledControlEffect({
|
|
|
|
|
target: createLightControlTargetRef("pointLight", pointLight.id),
|
|
|
|
|
enabled: false
|
|
|
|
|
});
|
|
|
|
|
const intensityEffect = createSetLightIntensityControlEffect({
|
|
|
|
|
target: createLightControlTargetRef("pointLight", pointLight.id),
|
|
|
|
|
intensity: 3.5
|
|
|
|
|
});
|
|
|
|
|
const disableLink = createControlInteractionLink({
|
|
|
|
|
id: "link-light-disable",
|
|
|
|
|
sourceEntityId: "entity-trigger-main",
|
|
|
|
|
effect: disableEffect
|
|
|
|
|
});
|
|
|
|
|
const intensityLink = createControlInteractionLink({
|
|
|
|
|
id: "link-light-intensity",
|
|
|
|
|
sourceEntityId: "entity-trigger-main",
|
|
|
|
|
effect: intensityEffect
|
|
|
|
|
});
|
|
|
|
|
const hostInternals = host as unknown as {
|
|
|
|
|
createInteractionDispatcher(): {
|
2026-04-14 01:40:15 +02:00
|
|
|
dispatchControlEffect(
|
|
|
|
|
effect: ControlEffect,
|
|
|
|
|
link: InteractionLink
|
|
|
|
|
): void;
|
2026-04-14 01:37:48 +02:00
|
|
|
};
|
|
|
|
|
localLightObjects: Map<
|
|
|
|
|
string,
|
|
|
|
|
{
|
|
|
|
|
group: { visible: boolean };
|
|
|
|
|
light: { intensity: number };
|
|
|
|
|
}
|
|
|
|
|
>;
|
|
|
|
|
};
|
|
|
|
|
const dispatcher = hostInternals.createInteractionDispatcher();
|
|
|
|
|
const renderObjects = hostInternals.localLightObjects.get(pointLight.id);
|
|
|
|
|
|
|
|
|
|
expect(renderObjects).toBeDefined();
|
|
|
|
|
expect(renderObjects?.group.visible).toBe(true);
|
|
|
|
|
expect(renderObjects?.light.intensity).toBe(1.25);
|
|
|
|
|
|
|
|
|
|
dispatcher.dispatchControlEffect(disableEffect, disableLink);
|
|
|
|
|
dispatcher.dispatchControlEffect(intensityEffect, intensityLink);
|
|
|
|
|
|
|
|
|
|
expect(renderObjects?.group.visible).toBe(false);
|
|
|
|
|
expect(renderObjects?.light.intensity).toBe(3.5);
|
|
|
|
|
expect(runtimeScene.control.resolved.discrete).toEqual(
|
|
|
|
|
expect.arrayContaining([
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
type: "lightEnabled",
|
|
|
|
|
value: false,
|
|
|
|
|
source: {
|
|
|
|
|
kind: "interactionLink",
|
|
|
|
|
linkId: disableLink.id
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
expect(runtimeScene.control.resolved.channels).toEqual(
|
|
|
|
|
expect.arrayContaining([
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
type: "lightIntensity",
|
|
|
|
|
value: 3.5,
|
|
|
|
|
source: {
|
|
|
|
|
kind: "interactionLink",
|
|
|
|
|
linkId: intensityLink.id
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
host.dispose();
|
|
|
|
|
});
|
2026-04-11 04:18:13 +02:00
|
|
|
});
|