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.

4.6 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  A mini-game keeps hot update's verified bytes under its user data
 *        directory, so an updated asset loads on the next launch without the CDN.
 */
import { describe, it, expect } from 'vitest';
import { MiniGamePlatformAdapter } from '../src/platform/minigame/adapter';
import { setPlatform, platformWriteCacheFile, platformReadCacheFile } from '../src/platform/base';
import { cacheEntryName } from '../src/platform/cacheEntryName';
import type { MiniGameGlobal, MiniGameProfile } from '../src/platform/minigame/api';

const URL = 'https://cdn.example/v2/assets/0123456789abcdef.png';

function host(opts: { dir?: string; refuses?: boolean } = {}) {
    const files = new Map<string, ArrayBuffer>();
    const fs = {
        readFile: (o: { filePath: string; success?: (r: { data: ArrayBuffer }) => void; fail?: (e: { errMsg: string }) => void }) => {
            const data = files.get(o.filePath);
            if (data) o.success?.({ data });
            else o.fail?.({ errMsg: 'readFile:fail no such file or directory' });
        },
        writeFile: (o: { filePath: string; data: ArrayBuffer; success?: () => void; fail?: (e: { errMsg: string }) => void }) => {
            if (opts.refuses) { o.fail?.({ errMsg: 'writeFile:fail the maximum size of the file storage limit is exceeded' }); return; }
            files.set(o.filePath, o.data);
            o.success?.();
        },
    };
    const profile: MiniGameProfile = {
        id: 'wechat',
        hostLabel: 'Test',
        global: {
            getSystemInfoSync: () => ({ pixelRatio: 1, screenWidth: 1, screenHeight: 1, platform: 'devtools', language: 'zh_CN' }),
            getFileSystemManager: () => fs,
            ...(opts.dir ? { env: { USER_DATA_PATH: opts.dir } } : {}),
        } as unknown as MiniGameGlobal,
    };
    setPlatform(new MiniGamePlatformAdapter(profile));
    return files;
}

describe('a mini-game content cache', () => {
    it('stores under the user data directory, named as on every platform, and reads it back', async () => {
        const files = host({ dir: 'wxfile://usr' });
        const bytes = new Uint8Array([1, 2, 3, 4]).buffer;
        expect(await platformWriteCacheFile(URL, bytes)).toBe('stored');
        expect([...files.keys()]).toEqual([`wxfile://usr/${cacheEntryName(URL)}`]);
        expect(new Uint8Array((await platformReadCacheFile(URL))!)).toEqual(new Uint8Array([1, 2, 3, 4]));
    });

    it('is a miss, not an error, for what was never stored', async () => {
        host({ dir: 'wxfile://usr' });
        expect(await platformReadCacheFile(URL)).toBeNull();
    });

    it('reports a write the host refused, so the update can say it is not cached', async () => {
        host({ dir: 'wxfile://usr', refuses: true });
        expect(await platformWriteCacheFile(URL, new ArrayBuffer(4))).toBe('failed');
    });

    it('reports a host with no user data directory as unsupported, not stored', async () => {
        host();
        expect(await platformWriteCacheFile(URL, new ArrayBuffer(4))).toBe('unsupported');
        expect(await platformReadCacheFile(URL)).toBeNull();
    });

    it('draws a cached image from the disk, not the CDN, so an updated texture shows offline', async () => {
        const files = host({ dir: 'wxfile://usr' });
        await platformWriteCacheFile(URL, new Uint8Array([1]).buffer);
        const sources: string[] = [];
        const adapter = new MiniGamePlatformAdapter({
            id: 'wechat', hostLabel: 'Test',
            global: {
                getSystemInfoSync: () => ({ pixelRatio: 1, screenWidth: 1, screenHeight: 1, platform: 'devtools', language: 'zh_CN' }),
                getFileSystemManager: () => ({
                    accessSync: (p: string) => { if (!files.has(p)) throw new Error('no such file'); },
                    readFile: () => {}, writeFile: () => {},
                }),
                env: { USER_DATA_PATH: 'wxfile://usr' },
                createImage: () => {
                    const img: Record<string, unknown> = {};
                    Object.defineProperty(img, 'src', { set: (v: string) => { sources.push(v); (img.onerror as (e: unknown) => void)?.({}); } });
                    return img;
                },
            } as unknown as MiniGameGlobal,
        } as MiniGameProfile);
        await adapter.loadImagePixels(URL).catch(() => {});
        await adapter.loadImagePixels('https://cdn.example/v2/never-cached.png').catch(() => {});
        expect(sources).toEqual([`wxfile://usr/${cacheEntryName(URL)}`, 'https://cdn.example/v2/never-cached.png']);
    });
});