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
/**
* @file assets-invalidate-event.test.ts
* @brief Assets.onInvalidate subscription contract: listeners fire on
* successful invalidations carrying WHICH KIND of asset ended,
* unsubscribe works, and a throwing listener doesn't starve others.
*/
import { describe, expect, it, vi } from 'vitest';
import { Assets } from '../src/asset/Assets';
import type { Backend } from '../src/asset/Backend';
import type { ESEngineModule } from '../src/wasm';
function buildAssets(): Assets {
const backend: Backend = {
resolveUrl: (p: string) => p,
fetchText: async () => '',
fetchBinary: async () => new ArrayBuffer(0),
} as unknown as Backend;
return new Assets({
backend,
module: { _malloc: () => 0, _free: () => {} } as unknown as ESEngineModule,
});
}
describe('Assets.onInvalidate', () => {
it('does NOT fire when invalidate() finds no matching cache entry', () => {
const assets = buildAssets();
const listener = vi.fn();
assets.onInvalidate(listener);
// No cache populated → invalidate returns false → no listener fires.
const hit = assets.invalidate('tex/never.png');
expect(hit).toBe(false);
expect(listener).not.toHaveBeenCalled();
});
it('fires with the original ref when invalidate hits a cache', async () => {
const assets = buildAssets();
// Seed the texture cache manually via internal map to avoid
// wiring up a real WASM texture loader.
const internal = assets as unknown as {
textureCache_: { cache_: Map<string, unknown> };
textureCacheKey_: (path: string, flip: boolean) => string;
textureRefs_: { acquire(key: string, handle: number): void };
};
const key = internal.textureCacheKey_('tex/hot.png', false);
internal.textureCache_.cache_.set(key, { handle: 42 });
internal.textureRefs_.acquire(key, 42);
const listener = vi.fn();
assets.onInvalidate(listener);
const hit = assets.invalidate('tex/hot.png');
expect(hit).toBe(true);
// Fires with the original ref, the KIND of asset that ended, and the value
// holders were bound to (0 here — the seeded entry is the unflipped
// variant; the rebinder reads flip=true).
expect(listener).toHaveBeenCalledWith({
ref: 'tex/hot.png', type: 'texture', oldValue: expect.any(Number),
});
});
it('unsubscribe function prevents further notifications', () => {
const assets = buildAssets();
const internal = assets as unknown as {
textureCache_: { cache_: Map<string, unknown> };
textureCacheKey_: (path: string, flip: boolean) => string;
textureRefs_: { acquire(key: string, handle: number): void };
};
const key = internal.textureCacheKey_('tex/hot.png', false);
internal.textureCache_.cache_.set(key, { handle: 42 });
internal.textureRefs_.acquire(key, 42);
const listener = vi.fn();
const unsubscribe = assets.onInvalidate(listener);
assets.invalidate('tex/hot.png');
expect(listener).toHaveBeenCalledTimes(1);
unsubscribe();
// Re-populate cache and invalidate again — listener should NOT fire.
internal.textureCache_.cache_.set(key, { handle: 43 });
internal.textureRefs_.acquire(key, 43);
assets.invalidate('tex/hot.png');
expect(listener).toHaveBeenCalledTimes(1);
});
it('isolates listeners from each other — a throwing one does not starve others', () => {
const assets = buildAssets();
const internal = assets as unknown as {
textureCache_: { cache_: Map<string, unknown> };
textureCacheKey_: (path: string, flip: boolean) => string;
textureRefs_: { acquire(key: string, handle: number): void };
};
const key = internal.textureCacheKey_('tex/hot.png', false);
internal.textureCache_.cache_.set(key, { handle: 42 });
internal.textureRefs_.acquire(key, 42);
const bad = vi.fn(() => { throw new Error('listener failed'); });
const good = vi.fn();
assets.onInvalidate(bad);
assets.onInvalidate(good);
expect(() => assets.invalidate('tex/hot.png')).not.toThrow();
expect(bad).toHaveBeenCalledTimes(1);
expect(good).toHaveBeenCalledTimes(1);
});
it('supports multiple listeners observing the same invalidation', () => {
const assets = buildAssets();
const internal = assets as unknown as {
textureCache_: { cache_: Map<string, unknown> };
textureCacheKey_: (path: string, flip: boolean) => string;
textureRefs_: { acquire(key: string, handle: number): void };
};
const key = internal.textureCacheKey_('tex/a.png', false);
internal.textureCache_.cache_.set(key, { handle: 1 });
internal.textureRefs_.acquire(key, 1);
const a = vi.fn();
const b = vi.fn();
const c = vi.fn();
assets.onInvalidate(a);
assets.onInvalidate(b);
assets.onInvalidate(c);
assets.invalidate('tex/a.png');
const event = { ref: 'tex/a.png', type: 'texture', oldValue: expect.any(Number) };
expect(a).toHaveBeenCalledWith(event);
expect(b).toHaveBeenCalledWith(event);
expect(c).toHaveBeenCalledWith(event);
});
});