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.

3.0 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    query-cache-stats.test.ts
 * @brief   QueryCache hit/miss/invalidation accounting.
 */
import { describe, expect, it, beforeEach } from 'vitest';
import { QueryCache } from '../src/ecs/QueryCache';

describe('QueryCache.getStats', () => {
    let cache: QueryCache;

    beforeEach(() => {
        cache = new QueryCache();
    });

    it('counts the first lookup as a miss', () => {
        let computed = 0;
        cache.getOrCompute('q1', [], () => { computed++; return []; });
        const s = cache.getStats();
        expect(s.misses).toBe(1);
        expect(s.hits).toBe(0);
        expect(s.size).toBe(1);
        expect(computed).toBe(1);
    });

    it('counts a repeated lookup as a hit', () => {
        let computed = 0;
        cache.getOrCompute('q1', [], () => { computed++; return []; });
        cache.getOrCompute('q1', [], () => { computed++; return []; });
        expect(computed).toBe(1);
        const s = cache.getStats();
        expect(s.hits).toBe(1);
        expect(s.misses).toBe(1);
    });

    it('does not invalidate a cached query on a bare structural change', () => {
        // markStructuralChange only advances the world version for coarse external
        // gates (physics); query invalidation is per-component. A query whose
        // dependent components are untouched must survive structural churn.
        let computed = 0;
        cache.getOrCompute('q1', [], () => { computed++; return []; });
        cache.markStructuralChange();
        cache.getOrCompute('q1', [], () => { computed++; return []; });
        expect(computed).toBe(1);
        const s = cache.getStats();
        expect(s.hits).toBe(1);
        expect(s.componentInvalidations).toBe(0);
    });

    it('attributes a component-dirty miss to componentInvalidations', () => {
        const comp = Symbol('Position');
        cache.getOrCompute('q1', [comp], () => []);
        cache.markComponentDirty(comp);
        cache.getOrCompute('q1', [comp], () => []);
        const s = cache.getStats();
        expect(s.componentInvalidations).toBe(1);
    });

    it('resetStats zeroes counters but keeps cached entries', () => {
        cache.getOrCompute('q1', [], () => [1, 2, 3]);
        cache.getOrCompute('q1', [], () => [99]);  // hit
        cache.resetStats();

        const s = cache.getStats();
        expect(s.hits).toBe(0);
        expect(s.misses).toBe(0);
        expect(s.size).toBe(1);

        let called = false;
        cache.getOrCompute('q1', [], () => { called = true; return []; });
        expect(called).toBe(false);
        expect(cache.getStats().hits).toBe(1);
    });

    it('size reflects entry count independent of hit/miss volume', () => {
        cache.getOrCompute('q1', [], () => []);
        cache.getOrCompute('q2', [], () => []);
        cache.getOrCompute('q1', [], () => []);
        cache.getOrCompute('q2', [], () => []);
        expect(cache.getStats().size).toBe(2);
    });
});