This commit is contained in:
2026-09-24 23:28:01 -03:00
parent 79faa556de
commit bb42dd662e
73 changed files with 2349 additions and 34 deletions
+65
View File
@@ -0,0 +1,65 @@
import { assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import {
block_from_json,
block_to_json,
grid_recipe_from_json,
grid_recipe_to_json,
item_from_json,
item_to_json,
} from "$/common/mod_data.ts";
import { CRAFTING_RECIPES } from "$/server/game/crafting.ts";
import { bworld_data_files } from "$/tools/export_bworld_mod.ts";
// mods/bworld is generated from the typescript definitions until the loader exists, keep them in sync
Deno.test("mods/bworld matches what the game registers", () => {
const expected = new Map(bworld_data_files().map(({ path, content }) => [path, content]));
const actual = new Map<string, unknown>();
for (const folder of ["blocks", "items", "recipes"]) {
for (const file of Deno.readDirSync(`mods/bworld/${folder}`)) {
actual.set(
`${folder}/${file.name}`,
JSON.parse(Deno.readTextFileSync(`mods/bworld/${folder}/${file.name}`)),
);
}
}
assertEquals(
[...actual.keys()].sort(),
[...expected.keys()].sort(),
"files differ, run deno task export-bworld",
);
for (const [path, content] of expected) {
assertEquals(actual.get(path), content, `${path} is out of date, run deno task export-bworld`);
}
});
// what the loader will do with the json has to give back exactly what the game uses now
Deno.test("blocks survive going to json and back", () => {
const items = new Map(EverythingRegistry.entries<ItemRegistry>("items"));
for (const [id, block] of EverythingRegistry.entries<BlockRegistry>("blocks")) {
const has_item = items.get(id)?.block_id === id;
const back = block_from_json(block_to_json(block, has_item));
assertEquals(back.block, without_undefined(block), id);
assertEquals(back.has_item, has_item, id);
}
});
Deno.test("items survive going to json and back", () => {
for (const [id, item] of EverythingRegistry.entries<ItemRegistry>("items")) {
if (item.block_id !== undefined) continue;
const { on_create: _on_create, get_lore: _get_lore, ...data } = item;
assertEquals(item_from_json(item_to_json(id, item)), without_undefined(data), id);
}
});
Deno.test("crafting recipes survive going to json and back", () => {
for (const recipe of CRAFTING_RECIPES) {
const json = grid_recipe_to_json(recipe);
if (json.type !== "shaped") throw new Error("expected a shaped recipe");
assertEquals(grid_recipe_from_json(json), recipe, recipe.result.id);
}
});
function without_undefined<T extends object>(value: T): T {
return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined)) as T;
}