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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* Unit tests for Phase 1.6 — asset load error propagation.
* Verifies preloadSceneAssets populates `missing` and that
* loadSceneWithAssets surfaces it via onMissingAssets / abortOnMissingAssets.
*
* Integration with the real loader path is covered by higher-level
* scene tests; here we short-circuit by handing preloadSceneAssets
* a stub Assets that only exercises the unresolved-ref branch.
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { defineComponent, clearUserComponents } from '../src/ecs/component';
import { discoverSceneAssets } from '../src/asset/discoverAssets';
import { AssetRegistry, makeUuidRef } from '../src/asset/AssetRegistry';
import type { SceneData } from '../src/scene/scene';
import { MissingAssetsError } from '../src/scene/scene';
const SPRITE_NAME = 'MissingTest_Sprite';
const UUID_KNOWN = 'aaaaaaaa-1111-4111-8111-aaaaaaaaaaaa';
const UUID_MISSING = 'bbbbbbbb-2222-4222-8222-bbbbbbbbbbbb';
beforeEach(() => {
clearUserComponents();
defineComponent(SPRITE_NAME, {
texture: '',
color: { r: 1, g: 1, b: 1, a: 1 },
}, {
assetFields: [{ field: 'texture', type: 'texture' }],
});
});
function scene(texRef: string): SceneData {
return {
version: '1.0',
name: 'test',
entities: [{
id: 1, name: 'e', parent: null, children: [],
components: [{
type: SPRITE_NAME,
data: { texture: texRef, color: { r: 1, g: 1, b: 1, a: 1 } },
}],
}],
};
}
describe('discoverSceneAssets → unresolved → missing plumbing', () => {
it('populates `unresolved` when a @uuid: ref is unknown', () => {
const reg = new AssetRegistry();
reg.addEntry({ uuid: UUID_KNOWN, path: 'assets/ok.png', type: 'texture' });
const refs = discoverSceneAssets(
scene(makeUuidRef(UUID_MISSING)),
(r) => reg.resolveRef(r),
);
expect(refs.unresolved).toEqual([makeUuidRef(UUID_MISSING)]);
});
it('leaves `unresolved` empty when every ref resolves', () => {
const reg = new AssetRegistry();
reg.addEntry({ uuid: UUID_KNOWN, path: 'assets/ok.png', type: 'texture' });
const refs = discoverSceneAssets(
scene(makeUuidRef(UUID_KNOWN)),
(r) => reg.resolveRef(r),
);
expect(refs.unresolved).toEqual([]);
});
it('reports each unresolved ref in the order encountered', () => {
const reg = new AssetRegistry();
const sceneData: SceneData = {
version: '1.0', name: 't',
entities: [
{
id: 1, name: 'a', parent: null, children: [],
components: [{
type: SPRITE_NAME,
data: { texture: makeUuidRef(UUID_MISSING), color: { r: 1, g: 1, b: 1, a: 1 } },
}],
},
{
id: 2, name: 'b', parent: null, children: [],
components: [{
type: SPRITE_NAME,
data: { texture: makeUuidRef(UUID_KNOWN), color: { r: 1, g: 1, b: 1, a: 1 } },
}],
},
],
};
const refs = discoverSceneAssets(sceneData, (r) => reg.resolveRef(r));
// Both UUIDs are unknown because the registry is empty.
expect(refs.unresolved).toEqual([
makeUuidRef(UUID_MISSING),
makeUuidRef(UUID_KNOWN),
]);
});
});
describe('MissingAssetsError', () => {
it('carries the missing list and a descriptive message', () => {
const missing = [
{ ref: makeUuidRef(UUID_MISSING), reason: 'unresolved' as const },
];
const err = new MissingAssetsError(missing);
expect(err.name).toBe('MissingAssetsError');
expect(err.missing).toBe(missing);
expect(err.message).toMatch(/1 asset/);
});
});
describe('a load that failed says so', () => {
// An `unresolved` ref warns; a load that THREW only reached `missing`, which
// the editor's caller ignores — leaving a scene with no textures and no
// complaint, indistinguishable from a scene that has none.
it('warns when an asset fails to load, not only when a ref is unresolved', async () => {
const { Assets } = await import('../src/asset/Assets');
const { log } = await import('../src/util/logger');
const warn = vi.spyOn(log, 'warn').mockImplementation(() => {});
try {
const assets = Assets.create({
backend: {
resolveUrl: (p: string) => p,
fetchText: async () => { throw new Error('disk went away'); },
fetchBinary: async () => { throw new Error('disk went away'); },
} as never,
module: { _malloc: () => 0, _free: () => {} } as never,
});
const res = await assets.preloadSceneAssets(scene('assets/ok.png'));
const failed = res.missing.filter((m: { reason: string }) => m.reason === 'load-failed');
expect(failed.length).toBeGreaterThan(0);
const said = warn.mock.calls.some(([scope, msg]) =>
scope === 'asset' && String(msg).includes('failed to load'));
expect(said, `a failed load was recorded but never logged: ${JSON.stringify(res.missing)}`).toBe(true);
} finally {
warn.mockRestore();
}
});
});