Files
bworld/tests/mod_tools_test.ts
T

201 lines
8.3 KiB
TypeScript

import { assert, assertEquals, assertThrows } from "@std/assert";
import { check_mods } from "$/tools/check_mods.ts";
import { MAX_TEXTURE_SIZE } from "$/common/constants.ts";
import { create_mod } from "$/tools/new_mod.ts";
import { item_from_json, item_to_json } from "$/common/mod_data.ts";
function temp_mods_dir() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_mods_" });
// most mods depend on the base game
Deno.mkdirSync(`${dir}/bworld`);
for (const entry of ["manifest.json", "blocks", "items", "recipes"]) {
copy(`mods/bworld/${entry}`, `${dir}/bworld/${entry}`);
}
return dir;
}
function copy(from: string, to: string) {
if (Deno.statSync(from).isDirectory) {
Deno.mkdirSync(to, { recursive: true });
for (const entry of Deno.readDirSync(from)) copy(`${from}/${entry.name}`, `${to}/${entry.name}`);
} else {
Deno.copyFileSync(from, to);
}
}
function write_json(path: string, value: unknown) {
Deno.writeTextFileSync(path, JSON.stringify(value));
}
// a blockbench model with one bone, a box like an arm's
function geo(bone: string) {
return {
format_version: "1.12.0",
"minecraft:geometry": [{
description: { identifier: "geometry.test", texture_width: 32, texture_height: 32 },
bones: [{
name: bone,
pivot: [-5, 22, 0],
cubes: [{ origin: [-8, 12, -2], size: [4, 12, 4], uv: [16, 0] }],
}],
}],
};
}
Deno.test("a mod made from the template passes the checks", async () => {
const dir = temp_mods_dir();
create_mod("copper_tools", "Copper Tools", dir);
const manifest = JSON.parse(Deno.readTextFileSync(`${dir}/copper_tools/manifest.json`));
assertEquals(manifest.id, "copper_tools");
assertEquals(manifest.name, "Copper Tools");
assert(!Deno.readTextFileSync(`${dir}/copper_tools/blocks/example_block.json`).includes("example_mod"));
const report = (await check_mods(dir)).find((r) => r.id === "copper_tools")!;
assertEquals(report.errors, []);
assertEquals(report.warnings, []);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("new-mod rejects bad and reserved ids", () => {
const dir = temp_mods_dir();
assertThrows(() => create_mod("Has Spaces", "x", dir));
assertThrows(() => create_mod("bworld", "x", dir));
assertThrows(() => create_mod("engine", "x", dir));
create_mod("once", "x", dir);
assertThrows(() => create_mod("once", "x", dir));
Deno.removeSync(dir, { recursive: true });
});
Deno.test("check-mods finds broken mods", async () => {
const dir = temp_mods_dir();
create_mod("broken", "Broken", dir);
const mod = `${dir}/broken`;
// wrong namespace, a drop that doesn't exist, and a texture that doesn't exist
write_json(`${mod}/blocks/wrong.json`, {
format_version: 1,
block: { id: "other:thing", textures: "broken:nope", drops: "broken:no_such_item" },
});
// defines a block the base game already has
write_json(`${mod}/blocks/dupe.json`, { format_version: 1, block: { id: "broken:x", textures: "bworld:stone" } });
write_json(`${mod}/blocks/stone_copy.json`, {
format_version: 1,
block: { id: "broken:x", textures: "bworld:stone" },
});
// a model that doesn't exist, a variant for a state the block doesn't have, and a broken model
write_json(`${mod}/blocks/plant.json`, {
format_version: 1,
block: { id: "broken:plant", textures: { cross: "broken:plant" }, model: "broken:no_such_model" },
});
write_json(`${mod}/blocks/grows.json`, {
format_version: 1,
block: { id: "broken:grows", textures: "broken:plant", variants: { "size=1": { y: 90 } } },
});
Deno.mkdirSync(`${mod}/models`);
write_json(`${mod}/models/bad.json`, {
format_version: 1,
model: { id: "broken:bad", elements: [{ from: [0, 0], to: [16, 16, 16], faces: { up: { texture: "#all" } } }] },
});
// not json
Deno.writeTextFileSync(`${mod}/items/bad.json`, "{ nope");
// worn with a model that doesn't exist, as something that isn't { model, texture }, and with a model where nothing
// is named like a player part
const item = (id: string, worn: unknown) => ({ format_version: 1, item: { id, texture: "bworld:coal", worn } });
write_json(
`${mod}/items/ghost.json`,
item("broken:ghost", { model: "broken:no_such_worn", texture: "bworld:coal" }),
);
write_json(`${mod}/items/shapeless.json`, item("broken:shapeless", "broken:hat"));
write_json(`${mod}/items/stiff.json`, item("broken:stiff", { model: "broken:stiff", texture: "bworld:coal" }));
write_json(`${mod}/models/stiff.geo.json`, geo("hat"));
// uses a letter the key doesn't define
write_json(`${mod}/recipes/bad.json`, {
format_version: 1,
recipe: {
type: "shaped",
pattern: ["AB"],
key: { A: "bworld:stone" },
result: { id: "bworld:stone", count: 1 },
},
});
// wrong texture size
const huge = new Uint8Array(24);
huge.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
new DataView(huge.buffer).setUint32(16, MAX_TEXTURE_SIZE + 1);
new DataView(huge.buffer).setUint32(20, 16);
Deno.writeFileSync(`${mod}/textures/huge.png`, huge);
// a script that doesn't typecheck
Deno.writeTextFileSync(`${mod}/scripts/client.ts`, "export function setup(ctx: number) { ctx.nope(); }\n");
// a dependency that isn't installed, and no longer depending on bworld
const manifest = JSON.parse(Deno.readTextFileSync(`${mod}/manifest.json`));
manifest.dependencies = [{ id: "missing_mod", version: "1.0.0" }];
write_json(`${mod}/manifest.json`, manifest);
const report = (await check_mods(dir)).find((r) => r.id === "broken")!;
const has = (list: string[], text: string) =>
assert(list.some((e) => e.includes(text)), `expected "${text}" in\n${list.join("\n")}`);
has(report.errors, `other:thing isn't in this mod's namespace`);
has(report.errors, "item broken:no_such_item doesn't exist");
has(report.errors, "block broken:x is also defined by broken, broken");
has(report.errors, "items/bad.json: isn't valid json");
has(report.errors, "model broken:no_such_model doesn't exist");
has(report.errors, `"size=1" must be a state of this block`);
has(report.errors, "elements[0].from must be [x, y, z]");
has(report.errors, `elements[0].faces: "up" isn't one of`);
has(report.errors, `pattern uses "B" but key doesn't define it`);
has(report.errors, "textures/huge.png: is");
has(report.errors, "scripts don't typecheck");
has(report.errors, `depends on "missing_mod", which isn't installed`);
has(report.errors, "entity model broken:no_such_worn doesn't exist");
has(report.errors, "worn must be { model, texture }");
has(report.warnings, "worn model broken:stiff has no bone named like a player part");
has(report.warnings, "texture broken:nope doesn't exist");
has(report.warnings, `uses bworld:stone but doesn't list "bworld" in dependencies`);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("an item can say how it looks worn, with a model for slim arms too", async () => {
const dir = temp_mods_dir();
create_mod("gloves", "Gloves", dir);
const mod = `${dir}/gloves`;
Deno.mkdirSync(`${mod}/models`);
write_json(`${mod}/models/glove_wide.geo.json`, geo("right_arm"));
write_json(`${mod}/models/glove_slim.geo.json`, geo("right_arm"));
const worn = {
model: "gloves:glove_wide",
texture: "gloves:example_item",
slim_model: "gloves:glove_slim",
slim_texture: "gloves:example_item",
};
write_json(`${mod}/items/glove.json`, {
format_version: 1,
item: { id: "gloves:glove", texture: "gloves:example_item", worn },
});
const report = (await check_mods(dir)).find((r) => r.id === "gloves")!;
assertEquals(report.errors, []);
assertEquals(report.warnings, []);
// and the registry, which clients draw from, has it
const glove = JSON.parse(Deno.readTextFileSync(`${mod}/items/glove.json`)).item;
assertEquals(item_from_json(glove).worn, worn);
assertEquals(item_to_json("gloves:glove", item_from_json(glove)).worn, worn);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("an item without a texture must be the item of one of the mod's blocks", async () => {
const dir = temp_mods_dir();
create_mod("coins", "Coins", dir);
const mod = `${dir}/coins`;
write_json(`${mod}/items/example_block.json`, {
format_version: 1,
item: { id: "coins:example_block", base_sell_value: 3 },
});
write_json(`${mod}/items/nothing.json`, { format_version: 1, item: { id: "coins:nothing", base_sell_value: 3 } });
const report = (await check_mods(dir)).find((r) => r.id === "coins")!;
assertEquals(report.errors, [
"items/nothing.json: item coins:nothing has no texture, and no block of this mod gives it an item",
]);
Deno.removeSync(dir, { recursive: true });
});