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(minigame): the gate that sees the rewrite has to be on the runtime side
esengine5a6c3e3548d5
diff --git a/pipeline/tests/export-wechat-read-paths.test.ts b/pipeline/tests/export-wechat-read-paths.test.tsindex c3f0e3530..8c0071b78 100644--- a/pipeline/tests/export-wechat-read-paths.test.ts+++ b/pipeline/tests/export-wechat-read-paths.test.ts@@ -11,20 +11,19 @@* found" — which is how a build shipped every prefab and material under* a name no read would ever produce.*- * So this drives the REAL read the packaged runtime performs — manifest- * lookup (`indexPackagedManifest`) then the shipped adapter over the- * exported directory — and asserts every spelling of every asset lands- * on a file the package carries.+ * This is the exporter's half: every spelling of every asset the package+ * carries resolves, through the SAME `ManifestModel` the shipped runtime+ * resolves with, to a file that is there. The runtime's half — that the+ * name reaching the host filesystem is the one the manifest gave — is+ * `sdk/tests/minigame-read-path.test.ts`, which is where the rewrite+ * lived. Neither half alone sees the bug; the pair does.*/import { describe, it, expect, afterAll } from 'vitest';import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import path from 'node:path';import { exportGame } from '../src/export/exportGame';-import { indexPackagedManifest } from '../../sdk/src/runtime/packagedRuntime';-import { MiniGamePlatformAdapter } from '../../sdk/src/platform/minigame/adapter';-import type { MiniGameGlobal } from '../../sdk/src/platform/minigame/api';-import type { AddressableManifest } from '../../sdk/src/asset/AddressableManifest';+import { ManifestModel, type AddressableManifest } from '../../sdk/src/asset/AddressableManifest';const TEX = 'dddddddd-dddd-dddd-dddd-dddddddddddd';const MAT = 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee';@@ -80,23 +79,6 @@ function scaffold(): { root: string; out: string } {return { root, out: path.join(root, 'dist-wechat') };}-/** The shipped adapter over the exported package — the host filesystem is the- * staged directory, so a read asks for exactly what a device would ask for. */-function adapterOver(out: string): MiniGamePlatformAdapter {- const fs = {- readFileSync: (p: string, encoding?: string) => {- const abs = path.join(out, p);- if (!existsSync(abs)) throw new Error(`no such file or directory ${p}`);- return encoding ? readFileSync(abs, 'utf8') : readFileSync(abs).buffer;- },- accessSync: (p: string) => {- if (!existsSync(path.join(out, p))) throw new Error(`no such file or directory ${p}`);- },- };- const global = { getFileSystemManager: () => fs } as unknown as MiniGameGlobal;- return new MiniGamePlatformAdapter({ id: 'wechat', hostLabel: 'WeChat', global });-}-describe('exportGame (wechat) — every asset resolves to a file the package carries', () => {it('resolves each manifest asset to a staged file', async () => {const { root, out } = scaffold();@@ -113,7 +95,7 @@ describe('exportGame (wechat) — every asset resolves to a file the package carexpect(res.ok).toBe(true);const manifest = JSON.parse(readFileSync(path.join(out, 'asset-manifest.json'), 'utf8')) as AddressableManifest;- const index = indexPackagedManifest(manifest);+ const model = ManifestModel.fromJson(manifest);// Both the prefab and the material ship — the scene names them — and the// packer is told to carry those suffixes, else the file never lands.@@ -124,20 +106,20 @@ describe('exportGame (wechat) — every asset resolves to a file the package carexpect(pcfg.packOptions.include).toContainEqual({ type: 'suffix', value: '.esprefab' });expect(pcfg.packOptions.include).toContainEqual({ type: 'suffix', value: '.esmaterial' });- // Every spelling the game can hold: the uuid a scene carries, and the- // project-relative path code writes by hand.- const adapter = adapterOver(out);+ // Every spelling the game can hold: the uuid a scene carries (the runtime+ // strips the `@uuid:` before the lookup) and the project-relative path code+ // writes by hand.const missing: string[] = [];for (const [uuid, asset] of Object.entries(manifest.groups.main.assets)) {- for (const ref of [uuid, `@uuid:${uuid}`, asset.address ?? asset.path]) {- const read = index.resolvePath(ref);- if (!(await adapter.fileExists(read))) missing.push(`${ref} → ${read}`);+ for (const ref of [uuid, asset.address ?? asset.path]) {+ const read = model.resolvePath(ref);+ if (!existsSync(path.join(out, read))) missing.push(`${ref} → ${read}`);}}expect(missing).toEqual([]);- // …and the bytes come back, which is what the game was after.- const prefab = JSON.parse(await adapter.readTextFile(index.resolvePath(PREFAB)));+ // …and the bytes are the asset's, not some neighbour that happens to exist.+ const prefab = JSON.parse(readFileSync(path.join(out, model.resolvePath(PREFAB)), 'utf8'));expect(prefab.name).toBe('Hero');}, 60_000);});diff --git a/sdk/tests/minigame-read-path.test.ts b/sdk/tests/minigame-read-path.test.tsnew file mode 100644index 000000000..3ae9f88d8--- /dev/null+++ b/sdk/tests/minigame-read-path.test.ts@@ -0,0 +1,65 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file The name a mini-game hands its host filesystem is the name it was given.+ *+ * A build path is whatever the cook staged the file as, and the manifest+ * has already said what that is by the time a read starts. So the adapter+ * rewrites nothing: an extension it "corrects" on the way out is a name+ * the package never wrote, and the device reports a missing asset.+ *+ * The exporter side cannot see a rewrite here — it ships the right bytes+ * under the right name either way — so this is the half that must hold it.+ */+import { describe, it, expect } from 'vitest';+import { MiniGamePlatformAdapter } from '../src/platform/minigame/adapter';+import type { MiniGameGlobal } from '../src/platform/minigame/api';++/** An adapter over a host whose filesystem records what it was asked for. */+function adapterAsking(asked: string[]): MiniGamePlatformAdapter {+ const fs = {+ readFileSync: (p: string, encoding?: string) => {+ asked.push(p);+ return encoding ? '{}' : new ArrayBuffer(0);+ },+ accessSync: (p: string) => { asked.push(p); },+ };+ const global = { getFileSystemManager: () => fs } as unknown as MiniGameGlobal;+ return new MiniGamePlatformAdapter({ id: 'wechat', hostLabel: 'WeChat', global });+}++// Every shape a staged path takes: the authored custom extensions, the scene the+// export restages as .json, the suffix it restages as .bin, and a plain texture.+const STAGED = [+ 'assets/ui/Root.esprefab',+ 'assets/ui/Root.esmaterial',+ 'assets/i18n/zh.eslocale',+ 'assets/tiles/town.estileset',+ 'assets/hero.esanimator',+ 'scenes/main.json',+ 'assets/hero.ktx2.bin',+ 'assets/hero.png',+];++describe('MiniGamePlatformAdapter — the path reaches the host verbatim', () => {+ it('reads text under the name it was given', async () => {+ const asked: string[] = [];+ const adapter = adapterAsking(asked);+ for (const p of STAGED) await adapter.readTextFile(p);+ expect(asked).toEqual(STAGED);+ });++ it('reads bytes under the name it was given', async () => {+ const asked: string[] = [];+ const adapter = adapterAsking(asked);+ for (const p of STAGED) await adapter.readFile(p);+ expect(asked).toEqual(STAGED);+ });++ it('asks whether THAT file exists', async () => {+ const asked: string[] = [];+ const adapter = adapterAsking(asked);+ for (const p of STAGED) await adapter.fileExists(p);+ expect(asked).toEqual(STAGED);+ });+});