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.

test(golden): align mini-game captures with the web frame clock

yhhf6e1df4d15e6
diff --git a/tools/launchers/frameClock.mjs b/tools/launchers/frameClock.mjs
new file mode 100644
index 000000000..23b8e5fd8
--- /dev/null
+++ b/tools/launchers/frameClock.mjs
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/** Advance package simulation once per rendered frame, independent of runner speed. */
+export function frameClockScript(ms) {
+ if (!(Number.isFinite(ms) && ms > 0)) return '';
+ return `<script>(() => {
+ let now = performance.now(); let frame = -1;
+ const raf = window.requestAnimationFrame.bind(window);
+ window.requestAnimationFrame = (cb) => raf((real) => {
+ if (real !== frame) { frame = real; now += ${ms}; }
+ cb(now);
+ });
+ })();</script>`;
+}
diff --git a/tools/launchers/launch-export.mjs b/tools/launchers/launch-export.mjs
index 2d09ba77f..22b85fd05 100644
--- a/tools/launchers/launch-export.mjs
+++ b/tools/launchers/launch-export.mjs
@@ -20,6 +20,7 @@
* to distinguish a renderer failure from compositor capture
* --w / --h surface size (default 640x360)
* --settle <n> frames to let run before capturing (default 30)
+ * --frame-ms <n> simulated time per render frame (default 1000/60; 0 = real time)
* --timeout <ms> how long to wait for the first frame (default 30000)
* --scene <name> boot this named scene instead of the package's entry
* --allow-flat accept a single-colour frame (a deliberately blank scene)
@@ -62,6 +63,7 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { onRendererConsole } from '../lib/rendererConsole.mjs';
import { inputScript } from './inputScript.mjs';
+import { frameClockScript } from './frameClock.mjs';
import { networkProfile } from './networkProfiles.mjs';
// Headless / GPU-less (CI) WebGL2 falls back to SwiftShader; harmless with a GPU.
@@ -153,17 +155,6 @@ const MIME = {
/** Serve the export directory. Range requests are not implemented: a build that
* needs them (video seek) would read as a broken asset here, not a broken server. */
-/** Advances the page's clock once per rendered frame, by `ms`: the loop reads its
- * delta off the rAF timestamp, and a slow runner's frame would otherwise hand a
- * game a quarter of a second at a time. */
-const clockScript = (ms) => `<script>(() => {
- let now = performance.now(); let frame = -1;
- const raf = window.requestAnimationFrame.bind(window);
- window.requestAnimationFrame = (cb) => raf((real) => {
- if (real !== frame) { frame = real; now += ${ms}; }
- cb(now);
- });
-})();</script>`;
function serve(root, safeArea, frameMs, compress) {
const server = http.createServer(async (req, res) => {
@@ -178,7 +169,7 @@ function serve(root, safeArea, frameMs, compress) {
// outside after that is a variable nobody ever asks about again.
if ((safeArea || frameMs) && rel === 'index.html') {
const [t = 0, r = 0, b = 0, l = 0] = safeArea.split(',').map(Number);
- const head = (frameMs ? clockScript(frameMs) : '')
+ const head = frameClockScript(frameMs)
+ (safeArea ? `<style>:root{--sat:${t}px;--sar:${r}px;--sab:${b}px;--sal:${l}px}</style>` : '');
const html = String(bytes);
const injected = html.replace('<head>', `<head>${head}`);
diff --git a/tools/launchers/launch-minigame.mjs b/tools/launchers/launch-minigame.mjs
index e93f7d383..f47761182 100644
--- a/tools/launchers/launch-minigame.mjs
+++ b/tools/launchers/launch-minigame.mjs
@@ -14,6 +14,7 @@
* --out <file.png> write the captured frame
* --w / --h surface size (default 640x360)
* --settle <n> frames to let run before capturing (default 30)
+ * --frame-ms <n> simulated time per render frame (default 1000/60; 0 = real time)
* --timeout <ms> how long to wait for the first frame (default 30000)
* --input <json> drive it, same spec as launch-export
* --fail-subpackage <name> the host refuses that 分包, to see what it does
@@ -27,6 +28,7 @@ import { fileURLToPath } from 'node:url';
import { onRendererConsole } from '../lib/rendererConsole.mjs';
import { HOST_PAGE } from './minigameHost.mjs';
import { inputScript } from './inputScript.mjs';
+import { frameClockScript } from './frameClock.mjs';
// Headless / GPU-less (CI) WebGL2 falls back to SwiftShader; harmless with a GPU.
app.commandLine.appendSwitch('enable-unsafe-swiftshader');
@@ -52,6 +54,7 @@ const OUT = flag('out', '');
const W = Number(flag('w', '640'));
const H = Number(flag('h', '360'));
const SETTLE = Number(flag('settle', '30'));
+const FRAME_MS = Number(flag('frame-ms', String(1000 / 60)));
const TIMEOUT = Number(flag('timeout', '30000'));
const INPUT = flag('input', '');
/**
@@ -85,10 +88,11 @@ function serve(root, page) {
// brotli), so the transport does — the stand-in host sees raw wasm either way.
const brotli = abs.endsWith('.br');
const ext = path.extname(brotli ? abs.slice(0, -3) : abs).toLowerCase();
+ const body = await readFile(abs);
res.writeHead(200, {
'content-type': MIME[ext] ?? 'application/octet-stream',
...(brotli ? { 'content-encoding': 'br' } : {}),
- }).end(await readFile(abs));
+ }).end(body);
} catch {
res.writeHead(404).end();
}
@@ -127,7 +131,8 @@ async function main() {
}
JSON.parse(readFileSync(manifest, 'utf8')); // a manifest the vendor cannot parse is a dead package
- const server = await serve(DIR, HOST_PAGE(entry, FAIL_SUBPACKAGE || null));
+ const page = HOST_PAGE(entry, FAIL_SUBPACKAGE || null).replace('<head>', '<head>' + frameClockScript(FRAME_MS));
+ const server = await serve(DIR, page);
const base = `http://127.0.0.1:${server.address().port}/`;
const win = new BrowserWindow({
diff --git a/tools/tests/frame-clock.test.ts b/tools/tests/frame-clock.test.ts
new file mode 100644
index 000000000..6c740a550
--- /dev/null
+++ b/tools/tests/frame-clock.test.ts
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { expect, it } from 'vitest';
+import { runInNewContext } from 'node:vm';
+import { frameClockScript } from '../launchers/frameClock.mjs';
+
+it('keeps simulation cadence fixed when the renderer stalls, without counting each callback as a frame', () => {
+ const queued: Array<(time: number) => void> = [], observed: number[] = [];
+ const window = { requestAnimationFrame: (cb: (time: number) => void) => { queued.push(cb); return queued.length; } };
+ const script = frameClockScript(20).slice('<script>'.length, -'</script>'.length);
+ runInNewContext(script, { window, performance: { now: () => 100 } });
+ window.requestAnimationFrame(time => observed.push(time));
+ window.requestAnimationFrame(time => observed.push(time));
+ queued.splice(0).forEach(cb => cb(1000));
+ window.requestAnimationFrame(time => observed.push(time));
+ queued.splice(0).forEach(cb => cb(12000));
+ expect(observed).toEqual([120, 120, 140]);
+});
+it('can leave the host clock untouched for real-time verification', () => {
+ expect(frameClockScript(0)).toBe('');
+ expect(frameClockScript(NaN)).toBe('');
+});