Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

9.1 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
import '../desktop/src/main';
import { captureUITextRows, captureUIFocusRows, captureUILayoutRows } from '../desktop/src/panels/UITextDebugPanel';
import { EngineHost } from '../desktop/src/engine/EngineHost';
import { SceneModel } from '../desktop/src/engine/SceneModel';
import { SceneCommands } from '../desktop/src/engine/SceneCommands';
import { ViewportController } from '../desktop/src/engine/ViewportController';
import { uiDebuggerTree, readUIElement } from '../desktop/src/panels/uiDebuggerData';
import { UIEventTrace } from '../desktop/src/engine/uiEventTrace';
import { useEditorStore } from '../desktop/src/store/editorStore';
import { inspectTextLayout, pseudoLocalize, MaskMode, EntityEventQueue } from 'esengine';

const status = document.createElement('pre');
status.style.cssText = 'position:fixed;left:20px;bottom:20px;z-index:1001;max-width:600px;background:#19191d;color:white;padding:12px';
document.body.append(status);
EngineHost.setSceneBootstrap(async () => { await EngineHost.loadScene('/__localization-scene.json'); });
useEditorStore.getState().enterEditor();
const deadline = Date.now() + 45000;
while (EngineHost.getSnapshot().status !== 'ready' && Date.now() < deadline) await new Promise(r => setTimeout(r, 100));
if (EngineHost.getSnapshot().status !== 'ready') throw new Error(JSON.stringify(EngineHost.getSnapshot()));
EngineHost.syncEditorViewToScene();
for (let i = 0; i < 12; i++) await new Promise(requestAnimationFrame);
const before = JSON.stringify(SceneModel.serialize());
const rows = captureUITextRows();
let passed = 0, failed = 0;
const check = (ok: boolean, label: string) => { ok ? passed++ : failed++; status.textContent += `${ok ? 'PASS' : 'FAIL'} ${label}\n`; };
check(rows.length === 12, `Actual Text + UINode rows: ${rows.length}`);
check(rows.every(row => row.width > 0 && row.height > 0), 'Computed Yoga boxes resolved');
const english = rows.find(row => row.name === 'English')!;
const options = { width: english.width, height: english.height, fontSize: english.text.fontSize, fontFamily: english.text.fontFamily };
check(inspectTextLayout(english.text.content, options).horizontalOverflow, 'Narrow English detects overflow before Ellipsis');
check(inspectTextLayout(pseudoLocalize(english.text.content, { expansion: 1 }), options).horizontalOverflow, '100% preview detects overflow');
for (const name of ['Chinese', 'Japanese', 'Korean']) {
  const row = rows.find(row => row.name === name)!;
  const result = inspectTextLayout(row.text.content, { ...options, width: row.width, height: row.height });
  check(result.unsupported === null && !result.horizontalOverflow, `${name} ordinary bounds fit`);
}
for (const name of ['Arabic — shaping unverified', 'Thai — shaping unverified']) {
  const row = rows.find(row => row.name === name)!;
  check(inspectTextLayout(row.text.content, options).unsupported === 'complex-script', `${name}: no false automatic pass`);
}
const wrapped = rows.find(row => row.name === 'Wrapped paragraph')!;
check(inspectTextLayout(wrapped.text.content, { ...options, width: wrapped.width, height: wrapped.height, wordWrap: true }).verticalOverflow, 'Wrapped paragraph detects vertical truncation');
check(JSON.stringify(SceneModel.serialize()) === before, 'Inspection / pseudolocalization leave authored scene unchanged');
const focusRows = captureUIFocusRows();
check(focusRows.visibilityResolved, 'Focus inspection uses host hierarchical visibility');
check(focusRows.entries.filter(e => e.order !== null).map(e => e.name).join(',') === 'LocaleButton,PseudoButton,WidthButton', 'Focus inspection reports the three authored buttons in runtime order');
check(JSON.stringify(SceneModel.serialize()) === before, 'Focus snapshot leaves authored scene unchanged');
const layoutRows = captureUILayoutRows();
check(layoutRows.length === 13, 'Layout inspector captures Canvas and all 12 UI children');
check(layoutRows.every(row => row.bounds !== null && row.width > 0 && row.height > 0), 'Layout inspector reports resolved world boundaries');
check(layoutRows.filter(row => row.gate === 'candidate').map(row => row.name).join(',') === 'LocaleButton,PseudoButton,WidthButton', 'Only the three buttons are input candidates');
check(layoutRows.filter(row => row.gate === 'no-interactable').length === 10, 'Decorative boxes are not advertised as input targets');
check(JSON.stringify(SceneModel.serialize()) === before, 'Layout/input snapshot does not change authored data');
check(MaskMode.Scissor === 0 && MaskMode.Stencil === 1, 'MaskMode is exported as a runtime enum');
// The fixture replaces its disposable scene through the normal authoring door.
SceneCommands.deleteEntities(SceneModel.serialize().entities.filter(entity => entity.parent === null).map(entity => entity.id) as never);
await EngineHost.loadScene('/__mask-scene.json');
EngineHost.syncEditorViewToScene();
for (let i = 0; i < 12; i++) await new Promise(requestAnimationFrame);
const widgetScene = SceneModel.serialize();
const inputSource = widgetScene.entities.find(entity => entity.name === 'Inside')!.id;
const dialogSource = widgetScene.entities.find(entity => entity.name === 'OuterMask')!.id;
SceneCommands.addComponent(inputSource, 'TextInput');
SceneCommands.setFieldValue(inputSource, 'TextInput', 'value', '中文 draft');
SceneCommands.setFieldValue(inputSource, 'TextInput', 'readOnly', true);
SceneCommands.addComponent(dialogSource, 'UIDialog');
SceneCommands.setFieldValue(dialogSource, 'UIDialog', 'closeOnEscape', false);
const maskBefore = JSON.stringify(SceneModel.serialize());
const maskedRows = captureUILayoutRows();
check(maskedRows.length === 6, 'Mask scene resolves Canvas, two masks and three buttons');
const tree = uiDebuggerTree(SceneModel.serialize());
check(tree.length === 6, 'Debugger hierarchy contains UI nodes and their ancestors');
check(tree.find(row => row.entity.name === 'Inside')?.path === 'Canvas / OuterMask / InnerMask / Inside', 'Debugger preserves the real nested hierarchy');
const inside = maskedRows.find(row => row.name === 'Inside')!;
const detail = readUIElement(EngineHost.world!, inside.entity);
check(detail?.width === 120 && detail.height === 32, 'Selected UI detail reads actual computed layout');
check(detail?.components.some(component => component.type === 'Interactable' && component.data.raycastTarget === true), 'Debugger reads actual input component values without verdicts');
const inputDetail = detail?.components.find(component => component.type === 'TextInput');
check(inputDetail?.data.value === '中文 draft' && inputDetail.data.readOnly === true,
  'Debugger exposes authored TextInput value and readOnly state');
const dialogRuntime = SceneModel.runtimeFor(dialogSource)!;
const dialogDetail = readUIElement(EngineHost.world!, dialogRuntime as never)?.components.find(component => component.type === 'UIDialog');
check(dialogDetail?.data.closeOnEscape === false && dialogDetail.data.closeOnBackdrop === true,
  'Debugger exposes authored UIDialog dismissal settings');
if (inputDetail) inputDetail.data.value = 'snapshot only';
check(readUIElement(EngineHost.world!, inside.entity)?.components.find(component => component.type === 'TextInput')?.data.value === '中文 draft',
  'Detached widget inspection cannot change the editor world');
const screenRect = ViewportController.getEntityScreenRect(inside.entity)!;
const canvasRect = EngineHost.canvas!.getBoundingClientRect();
const point = { x: canvasRect.left + screenRect.x + screenRect.w / 2, y: canvasRect.top + screenRect.y + screenRect.h / 2 };
check(ViewportController.pickUIEntity(point.x, point.y) === inside.entity, 'Debugger selection uses the real engine editor picker');
check(JSON.stringify(SceneModel.serialize()) === maskBefore, 'Debugger reads and editor picking preserve scene data');
// Actual SDK dispatch/bubbling through the recorder; not a pointer-delivery assertion.
const queue = new EntityEventQueue();
const eventTrace = new UIEventTrace(id => `Node${id}`);
queue.on(2 as never, 'click', event => { event.stopPropagation(); event.preventDefault(); });
eventTrace.control(queue, 'start');
const click = queue.emit(1 as never, 'click', { text: 'payload excluded' });
queue.emitBubbled(2 as never, click); queue.emitBubbled(3 as never, click);
const trace = eventTrace.control(queue, 'read');
check(trace.rows.map(row => row.currentTarget).join(',') === '1,2', 'Trace records only actual dispatch nodes');
check(trace.rows[0].eventId === trace.rows[1].eventId, 'Target and bubble share one event identity');
check(trace.rows[1].propagationStopped && trace.rows[1].defaultPrevented, 'Trace captures flags after real handlers');
check(!('data' in trace.rows[0]) && queue.drain().length === 2, 'Observer excludes payload and does not consume game events');
eventTrace.control(queue, 'stop'); queue.emit(1 as never, 'release');
check(eventTrace.control(queue, 'read').rows.length === 2, 'Stopped recorder receives no new events');
eventTrace.dispose();
status.textContent += `RESULT ${passed} passed, ${failed} failed · ${EngineHost.activeBackend}`;
const dismiss = document.createElement('button');
dismiss.textContent = 'Dismiss verification report';
dismiss.onclick = () => { status.style.display = 'none'; };
status.append(document.createElement('br'), dismiss);