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.

6.1 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  Every type loadGroup acquires, releaseGroup gives back.
 *
 * They were two switch statements. Load handled `font`; release listed only
 * `bitmap-font`, so a group carrying a font held its reference for the life of
 * the process — and nothing failed, because no test walked both halves for the
 * same type. Now one table drives both, and this walks every entry in it.
 *
 * A new addressable type is a new row here. That is the point: a row with a
 * loader and no release has to say so out loud (spine does), rather than being
 * a case somebody forgot to add to the second switch.
 */
import { describe, it, expect, vi } from 'vitest';
import { Assets } from '../src/asset/Assets';
import type { AddressableManifest, AddressableAssetType } from '../src/asset/AddressableManifest';
import type { Backend } from '../src/asset/Backend';

vi.mock('../src/wasm/resourceManager', () => ({
    initResourceManager: vi.fn(),
    shutdownResourceManager: vi.fn(),
    getResourceManager: vi.fn(() => null),
    requireResourceManager: vi.fn(() => ({
        releaseTexture: vi.fn(),
        getTextureDimensions: vi.fn(() => null),
        setTextureMetadata: vi.fn(),
    })),
    evictTextureDimensions: vi.fn(),
    textureDimensions: vi.fn(() => null),
}));

const mockModule = { _malloc: () => 0, _free: () => {}, HEAPU8: new Uint8Array(8) } as never;

const backend = {
    fetchBinary: async () => new ArrayBuffer(8),
    fetchText: async () => '{}',
    resolveUrl: (p: string) => p,
} as unknown as Backend;

/**
 * One row per addressable type a group can carry, with the loader to stub and
 * the release the type is supposed to route to. `releasesVia: null` means the
 * type's lifetime is owned elsewhere and releaseGroup must NOT touch it.
 */
const TYPES: Array<{
    type: AddressableAssetType;
    loadMethod: keyof Assets;
    /** The typed door's first argument; absent when the method takes the path first. */
    loadArg?: string;
    releasesVia: string | null;
}> = [
    // The group loads through the ACQUIRE doors, so what it takes out is
    // recorded as a receipt on the bundle rather than left to be guessed from
    // the manifest at release time.
    { type: 'texture', loadMethod: 'acquireTexture', releasesVia: 'texture' },
    { type: 'material', loadMethod: 'acquireTyped', loadArg: 'material', releasesVia: 'material' },
    { type: 'font', loadMethod: 'acquireTyped', loadArg: 'font', releasesVia: 'font' },
    { type: 'bitmap-font', loadMethod: 'acquireTyped', loadArg: 'font', releasesVia: 'font' },
    { type: 'prefab', loadMethod: 'acquireTyped', loadArg: 'prefab', releasesVia: 'prefab' },
    { type: 'audio', loadMethod: 'acquireTyped', loadArg: 'audio', releasesVia: 'audio' },
    // Nothing is special about spine any more: the bundle holds a receipt for
    // the era it took, and gives back that one. What a live entity holds it
    // holds through a receipt of its own.
    { type: 'spine', loadMethod: 'acquireSpine', releasesVia: 'spine' },
];

function manifestWith(type: AddressableAssetType): AddressableManifest {
    return {
        version: '2.0',
        groups: {
            dlc: {
                bundleMode: 'local',
                labels: [],
                assets: { 'a.bin': { path: 'a.bin', type, size: 0, labels: [] } },
            },
        },
    } as AddressableManifest;
}

describe.each(TYPES)('group channel: $type', (row) => {
    it('is loaded by loadGroup and released by releaseGroup', async () => {
        const assets = Assets.create({ backend, module: mockModule });
        assets.setManifest(manifestWith(row.type));

        const load = vi.spyOn(assets, row.loadMethod as 'acquireTexture')
            .mockResolvedValue({
                key: `${row.type}:a.bin`, generation: 1,
                value: { handle: 1, width: 1, height: 1 }, release: () => {},
            } as never);
        // releaseTexture and releaseTyped are the only two release doors; both
        // are watched so a row cannot pass by going through neither.
        const releaseTexture = vi.spyOn(assets, 'releaseTexture').mockImplementation(() => {});
        const releaseTyped = vi.spyOn(assets, 'releaseTyped').mockImplementation(() => {});

        await assets.loadGroup('dlc');
        if (row.loadArg) {
            expect(load, `loadGroup never loaded a ${row.type}`).toHaveBeenCalledWith(row.loadArg, 'a.bin');
        } else {
            expect(load, `loadGroup never loaded a ${row.type}`).toHaveBeenCalledWith('a.bin');
        }

        assets.releaseGroup('dlc');

        if (row.releasesVia === null) {
            expect(releaseTexture, `releaseGroup released a ${row.type}, whose lifetime is owned elsewhere`).not.toHaveBeenCalled();
            expect(releaseTyped, `releaseGroup released a ${row.type}, whose lifetime is owned elsewhere`).not.toHaveBeenCalled();
            return;
        }
        if (row.releasesVia === 'texture') {
            expect(releaseTexture, 'a texture was loaded by the group and never released').toHaveBeenCalledWith('a.bin');
            return;
        }
        expect(
            releaseTyped.mock.calls.map((c) => c[0]),
            `a ${row.type} was loaded by the group and never released`,
        ).toContain(row.releasesVia);
    });
});

describe('group release addressing', () => {
    it('releases the same path it loaded', async () => {
        // A release that resolves the path differently frees a key nobody holds:
        // a leak that looks exactly like a working release.
        const assets = Assets.create({ backend, module: mockModule });
        assets.setManifest(manifestWith('texture'));
        const load = vi.spyOn(assets, 'acquireTexture').mockResolvedValue({
            key: 'texture:a.bin', generation: 1,
            value: { handle: 1, width: 1, height: 1 }, release: () => {},
        } as never);
        const release = vi.spyOn(assets, 'releaseTexture').mockImplementation(() => {});

        await assets.loadGroup('dlc');
        assets.releaseGroup('dlc');

        expect(release.mock.calls[0][0]).toBe(load.mock.calls[0][0]);
    });
});