Skip to content

Code-first video & image, rendered natively on WebGPU.

2.6 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import { TextureCache } from "./texture-cache.js";

describe("TextureCache", () => {
	let cache: TextureCache;
	let mockTexture1: any;
	let mockTexture2: any;
	let mockDevice: any;

	beforeEach(() => {
		cache = new TextureCache();
		mockTexture1 = {
			destroy: vi.fn(),
		};
		mockTexture2 = {
			destroy: vi.fn(),
		};
		mockDevice = {
			createTexture: vi.fn().mockReturnValue(mockTexture1),
		};
	});

	it("should set and acquire a texture", () => {
		cache.set("tex1", mockTexture1);
		const acquired = cache.acquire("tex1");

		expect(acquired).toBe(mockTexture1);
	});

	it("should increment reference count upon acquire", () => {
		cache.set("tex1", mockTexture1);

		// Set starts with ref = 1
		// Acquire increments to 2
		cache.acquire("tex1");

		// We can indirectly verify references by checking that it gets destroyed/evicted/etc.
		// Wait, ref is private. We can test that release decrements correctly (or just runs without throwing)
		cache.release("tex1"); // count to 1
		cache.release("tex1"); // count to 0
	});

	it("should return undefined when acquiring non-existent key", () => {
		const acquired = cache.acquire("non-existent");
		expect(acquired).toBeUndefined();
	});

	it("should evict previous texture when setting a new texture for the same key", () => {
		cache.set("tex1", mockTexture1);

		// Overwriting "tex1" with mockTexture2 should evict mockTexture1
		cache.set("tex1", mockTexture2);

		expect(cache.acquire("tex1")).toBe(mockTexture2);
	});

	it("should create a texture using GPUDevice and set it in cache", () => {
		// Mock GPUTextureUsage
		globalThis.GPUTextureUsage = {
			TEXTURE_BINDING: 1,
			COPY_DST: 2,
			RENDER_ATTACHMENT: 4,
		} as any;

		const created = cache.create("new-tex", mockDevice, 100, 200, "rgba8unorm");

		expect(mockDevice.createTexture).toHaveBeenCalledWith({
			size: [100, 200],
			format: "rgba8unorm",
			usage: 1 | 2 | 4,
		});
		expect(created).toBe(mockTexture1);
		expect(cache.acquire("new-tex")).toBe(mockTexture1);
	});

	it("should evict and delete from cache", () => {
		cache.set("tex1", mockTexture1);
		expect(cache.acquire("tex1")).toBe(mockTexture1);

		cache.evict("tex1");
		expect(cache.acquire("tex1")).toBeUndefined();
	});

	it("should destroy all cached textures", () => {
		cache.set("tex1", mockTexture1);
		cache.set("tex2", mockTexture2);

		cache.destroy();

		expect(mockTexture1.destroy).toHaveBeenCalledTimes(1);
		expect(mockTexture2.destroy).toHaveBeenCalledTimes(1);
		expect(cache.acquire("tex1")).toBeUndefined();
		expect(cache.acquire("tex2")).toBeUndefined();
	});
});