Files
bworld/tests/worldgen_golden_test.ts
2026-11-06 23:13:34 -03:00

41 lines
1.7 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// world generation gives the same world block for block, release after release: a save only keeps what players
// changed, so any change to what generation makes changes every existing world (docs/modding_plan.md, rule 2).
// this generates chunks for a few seeds and compares them with tests/fixtures/worldgen_golden.json.
// after a change that's meant to change generation, record it again:
// deno test -A tests/worldgen_golden_test.ts -- --update
import { assertEquals } from "@std/assert";
import { generate_raw_chunk } from "@/common/generation.ts";
import { test_game } from "./helpers.ts";
import { final_hash, FIXTURE, golden_chunks, type Hashes, namer, raw_hash, recorded, SEEDS } from "./golden.ts";
Deno.test("world generation makes the same chunks as recorded", async () => {
const hashes: Hashes = {};
for (const seed of SEEDS) {
const { game } = await test_game("mods", undefined, seed);
const world = game.world;
const name = namer(world.block_ids);
hashes[seed] = {};
for (const [x, z] of golden_chunks()) {
const raw = generate_raw_chunk(x, z, seed, world.block_ids, world.worldgen, world.default_values);
hashes[seed][`${x},${z}`] = {
raw: await raw_hash(raw, name),
final: await final_hash(world.generated_blocks(x, z).to_flat(), name),
};
}
}
if (Deno.args.includes("--update")) {
Deno.writeTextFileSync(FIXTURE, JSON.stringify(hashes, null, "\t") + "\n");
return;
}
const fixture = recorded();
for (const seed of SEEDS) {
for (const [chunk, hash] of Object.entries(hashes[seed])) {
assertEquals(hash, fixture[seed]?.[chunk], `seed ${seed}, chunk ${chunk} isn't what was recorded`);
}
}
});