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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
test(lifecycle): renew foreground input in suspend comparisons
yhhd373852e8c79
diff --git a/tools/lib/suspendDrive.mjs b/tools/lib/suspendDrive.mjsnew file mode 100644index 000000000..6a1ce471b--- /dev/null+++ b/tools/lib/suspendDrive.mjs@@ -0,0 +1,14 @@+// SPDX-License-Identifier: Apache-2.0++/** The keyboard gesture used by the foreground/background package comparison. */+export function suspendDrive(spec, hidden = null) {+ const ranges = hidden+ ? [{from: 0, to: hidden.from}, {from: hidden.to + 1, to: spec.frames}]+ : [{from: 0, to: spec.frames}];+ return {+ frames: spec.frames,+ hidden: hidden ? [hidden] : [],+ holds: spec.keys.flatMap(key => ranges.filter(range => range.from < range.to)+ .map(range => ({key, ...range}))),+ };+}diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex d1aefe6c4..df09d2aac 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'test(lifecycle): renew foreground input in suspend comparisons':+ { internal: 'Suspend acceptance releases keyboard intent before hiding and sends a new key press after showing, preserving the engine cancellation contract and comparing actual movement with uninterrupted and held-background controls.' },'feat(nav): explain pending navigation surface replacements':{ note: 'Navigation replacements commit only from the latest request.' },'feat(nav): stage cancellable surface replacements and explain pending routes':diff --git a/tools/tests/suspend-drive.test.ts b/tools/tests/suspend-drive.test.tsnew file mode 100644index 000000000..d8bc66da1--- /dev/null+++ b/tools/tests/suspend-drive.test.ts@@ -0,0 +1,49 @@+// SPDX-License-Identifier: Apache-2.0+import {expect, it} from 'vitest';+import {runInNewContext} from 'node:vm';+import {InputState, inputEventCallbacks} from '../../sdk/src/input/input';+import {inputScript} from '../launchers/inputScript.mjs';+import {suspendDrive} from '../lib/suspendDrive.mjs';++async function sample(hidden: {from:number;to:number} | null) {+ const input=new InputState(), callbacks=inputEventCallbacks(input);+ const history: Array<{hidden:boolean;down:boolean}>=[], pending: Array<()=>void>=[];+ const document={hidden:false,querySelector:()=>null,dispatchEvent(event:{type:string;code:string}){+ if(event.type==='keydown')callbacks.onKeyDown?.(event.code);+ if(event.type==='keyup')callbacks.onKeyUp?.(event.code);+ if(event.type==='visibilitychange'&&document.hidden)callbacks.onInputCancel?.();+ }};+ class Event {constructor(public type:string, options={}){Object.assign(this,options);}}+ const result=runInNewContext(inputScript(suspendDrive({keys:['KeyD'],frames:10},hidden)),{+ document,window:{dispatchEvent(){}},navigator:{},Event,KeyboardEvent:Event,+ requestAnimationFrame:(callback:()=>void)=>pending.push(callback),+ });+ for(let frame=0;frame<12&&pending.length;frame++){+ history.push({hidden:document.hidden,down:input.isKeyDown('KeyD')});+ pending.splice(0).forEach(callback=>callback());+ await new Promise(resolve=>setImmediate(resolve));+ input.clearFrameState();+ }+ await result;+ expect(input.keysDown.size).toBe(0);+ return history;+}++it('renews foreground keyboard intent after cancellation without replaying a hidden hold',async()=>{+ const history=await sample({from:2,to:5});+ expect(history.slice(0,2).every(frame=>frame.down)).toBe(true);+ expect(history.slice(2,5).every(frame=>frame.hidden&&!frame.down)).toBe(true);+ expect(history[5]).toEqual({hidden:false,down:false});+ expect(history[6]).toEqual({hidden:false,down:true});+});++it('does not renew movement in the last frame of the held-background comparison',async()=>{+ const history=await sample({from:2,to:9});+ expect(history.slice(2).every(frame=>!frame.down)).toBe(true);+});++it('keeps the uninterrupted drive held for its requested frames and releases it afterwards',async()=>{+ const history=await sample(null);+ expect(history.slice(0,10).every(frame=>frame.down&&!frame.hidden)).toBe(true);+ expect(history[10].down).toBe(false);+});diff --git a/tools/verify-golden.mjs b/tools/verify-golden.mjsindex 83b004469..8c2f27733 100644--- a/tools/verify-golden.mjs+++ b/tools/verify-golden.mjs@@ -27,6 +27,7 @@ import { mkdirSync, rmSync, readFileSync, existsSync, writeFileSync } from 'nodeimport path from 'node:path';import { atTier, sharesOf, projectDir, parityFor, interactFor, audioFor, suspendFor, safeAreaFor, atlasFor, subpackageFor, firstScreenFor, packagePixels, launchTimeoutFor, ROOT } from './goldenProjects.mjs';import { frameDistance, frameCellMax, readPNG } from './frameCompare.mjs';+import { suspendDrive } from './lib/suspendDrive.mjs';import { retryOnDeadGpu, deadGpuVerdict, launchNeverHappenedVerdict, failureLines } from './lib/deadGpu.mjs';import { runElectron, ensureElectronBinary } from './lib/electronRun.mjs';import { requireCurrentEngine } from './lib/engineBuild.mjs';@@ -686,12 +687,12 @@ for (const { id, target } of pairs) {void allAnswered;// Backgrounded, the world stops; brought back, it carries on. Three runs of- // the same drive, read as how far the same entity got.+ // the same drive, with a new foreground key press after input cancellation.const suspend = suspendFor(golden);if (suspend) {const where = (hidden) => probePositions(target, out, editor.w, editor.h, [suspend.entity], [...(timeoutMs ? ['--timeout', String(timeoutMs)] : []),- '--input', JSON.stringify({ keys: suspend.keys, frames: suspend.frames, hidden }),+ '--input', JSON.stringify(suspendDrive(suspend, hidden[0] ?? null)),])?.[suspend.entity]?.x ?? null;const never = where([]);