// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // chunks generated in worldgen workers are the ones the game would generate, in any order and with any number of // workers. see server/game/worldgen_pool.ts import { assert, assertEquals } from "@std/assert"; import { chunk_key } from "@/common/utils.ts"; import type { RawChunk } from "@/common/generation.ts"; import { type WorkerPort, WorldgenPool } from "@/server/game/worldgen_pool.ts"; import { start_worldgen_workers } from "@/server/worldgen_workers.ts"; import { test_game } from "./helpers.ts"; import { golden_chunks, namer, raw_hash, recorded } from "./golden.ts"; // workers and their ports outlive a test's sanitizers until they're closed const options = { sanitizeOps: false, sanitizeResources: false }; async function pool_for(seed: string, workers: number) { const { game } = await test_game("mods", undefined, seed); // tests load worldgen phases from files, so the workers need to read them const started = start_worldgen_workers(workers, { permissions: "inherit", on_restart: () => {} }); const pool = new WorldgenPool(started.ports as unknown as WorkerPort[], game.worldgen_bundle!); const chunks = new Map(); const order: [number, number][] = []; pool.on_chunk = (x, z, raw) => { chunks.set(chunk_key(x, z), raw); order.push([x, z]); }; await pool.ready(); return { game, pool, started, chunks, order }; } // waits until there's nothing queued or being generated async function drained(pool: WorldgenPool) { while (pool.queued > 0 || pool.in_flight > 0) await new Promise((resolve) => setTimeout(resolve, 5)); } Deno.test({ name: "the pool generates the golden chunks, asked for in any order, with any number of workers", ...options, fn: async () => { const fixture = recorded(); for (const workers of [1, 3]) { const { game, pool, started, chunks } = await pool_for("golden-2", workers); try { // a different order each time, and priorities that don't follow it const asked = golden_chunks().sort(() => Math.random() - 0.5); pool.want(new Map(asked.map(([x, z], i) => [chunk_key(x, z), { x, z, priority: (i * 7) % 11 }]))); await drained(pool); assertEquals(chunks.size, asked.length); const name = namer(game.world.block_ids); for (const [x, z] of asked) { const raw = chunks.get(chunk_key(x, z))!; assertEquals(await raw_hash(raw, name), fixture["golden-2"][`${x},${z}`].raw, `${x},${z}`); } } finally { pool.close(); started.terminate(); } } }, }); Deno.test({ name: "the nearest go first, what's no longer wanted is dropped, and a replaced worker's chunks are sent again", ...options, fn: async () => { const { pool, started, chunks, order } = await pool_for("pool-seed", 1); try { // priority is the distance: the worker takes two straight away, then the rest nearest first const wanted = new Map([5, 1, 4, 2, 3, 0].map((d) => [chunk_key(d, 0), { x: d, z: 0, priority: d }])); pool.want(wanted); await drained(pool); assertEquals(order.map(([x]) => x), [0, 1, 2, 3, 4, 5]); // the player moved away before these started order.length = 0; pool.want(new Map([10, 11, 12, 13].map((d) => [chunk_key(d, 0), { x: d, z: 0, priority: d }]))); pool.want(new Map()); await drained(pool); assertEquals(order.map(([x]) => x), [10, 11], "the two being generated finish, the rest never start"); // a worker that crashed and was started again gets what the old one was working on order.length = 0; pool.want(new Map([20, 21].map((d) => [chunk_key(d, 0), { x: d, z: 0, priority: d }]))); const fresh = start_worldgen_workers(1, { permissions: "inherit", on_restart: () => {} }); pool.replace(0, fresh.ports[0] as unknown as WorkerPort); await pool.prefetch([[30, 0]]); await drained(pool); assert(chunks.has(chunk_key(20, 0)) && chunks.has(chunk_key(21, 0)) && chunks.has(chunk_key(30, 0))); fresh.terminate(); } finally { pool.close(); started.terminate(); } }, });