Files
bworld/tests/importer_test.ts
T
2026-10-12 17:39:48 -03:00

211 lines
9.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { assert, assertEquals, assertStringIncludes, assertThrows } from "@std/assert";
import { validate_model } from "$/common/mod_data.ts";
import type { ModelJson } from "$/common/block_models.ts";
import { import_models } from "$/tools/importer/models.ts";
import { convert_mcmeta, import_textures } from "$/tools/importer/textures.ts";
// a java mod's resources in a temporary folder: { "block/gear": {...} } is assets/<namespace>/models/block/gear.json
function resources(models: Record<string, unknown>, namespace = "gears"): string {
const root = Deno.makeTempDirSync();
for (const [path, json] of Object.entries(models)) {
const file = `${root}/assets/${namespace}/models/${path}.json`;
Deno.mkdirSync(file.slice(0, file.lastIndexOf("/")), { recursive: true });
Deno.writeTextFileSync(file, JSON.stringify(json));
}
return root;
}
function convert(models: Record<string, unknown>, path: string): ModelJson {
const root = resources(models);
const report = import_models(`${root}/assets/gears/models/${path}.json`);
assertEquals(report.errors, []);
return JSON.parse(report.written[0].text!).model;
}
const GEAR = {
credit: "Made with Blockbench",
texture_size: [32, 32],
textures: { "0": "gears:block/gear", particle: "gears:block/gear" },
elements: [{
name: "hub",
from: [4, 0, 4],
to: [12, 4, 12],
rotation: { angle: 22.5, axis: "y", origin: [8, 8, 8] },
faces: {
up: { uv: [0, 0, 8, 8], texture: "#0", cullface: "up" },
down: { uv: [0, 0, 8, 8], texture: "#0", cullface: "down", tintindex: 0 },
north: { uv: [0, 8, 8, 12], texture: "#0", rotation: 90 },
},
}],
};
Deno.test("a java block model becomes a bworld model that passes its checks", () => {
const model = convert({ "block/gear": GEAR }, "block/gear");
assertEquals(validate_model(model), []);
// folders are joined into the id, the way textures in subfolders are named
assertEquals(model.id, "gears:block_gear");
assertEquals(model.textures, { "0": "gears:block_gear" });
const [hub] = model.elements;
assertEquals(Object.keys(hub.faces).sort(), ["bottom", "north", "top"]);
assertEquals(hub.faces.top, { texture: "#0", uv: [0, 0, 8, 8], cullface: "top" });
assertEquals(hub.faces.north?.rotation, 90);
assertEquals(hub.rotation, { origin: [8, 8, 8], axis: "y", angle: 22.5 });
});
Deno.test("a model gets its parents' elements and textures, its own textures winning", () => {
const model = convert({
"block/gear": GEAR,
"block/gear_brass": { parent: "gears:block/gear", textures: { "0": "gears:block/brass_gear" } },
}, "block/gear_brass");
assertEquals(model.textures, { "0": "gears:block_brass_gear" });
assertEquals(model.elements.length, GEAR.elements.length);
});
Deno.test("vanilla parents like cube_all and stairs convert without minecraft's files", () => {
const cube = convert(
{ "block/casing": { parent: "minecraft:block/cube_all", textures: { all: "block/stone" } } },
"block/casing",
);
assertEquals(validate_model(cube), []);
assertEquals(Object.keys(cube.elements[0].faces).length, 6);
// minecraft's block textures are taken to be the base game's of the same name
assertEquals(cube.textures?.all, "bworld:stone");
const stairs = convert({
"block/stairs": {
parent: "block/stairs",
textures: { top: "gears:block/a", bottom: "gears:block/a", side: "gears:block/a" },
},
}, "block/stairs");
assertEquals(validate_model(stairs), []);
assert(stairs.elements.length > 1);
});
Deno.test("a folder converts into the same folders, with models that are only parents left out", () => {
const root = resources({
"block/gear": GEAR,
"block/large/gear": { parent: "gears:block/gear" },
"block/template": { parent: "block/block", textures: { "0": "gears:block/gear" } },
});
const output = Deno.makeTempDirSync();
const report = import_models(root, { output });
assertEquals(report.errors, []);
assertEquals(report.parents, ["gears:block/template"]);
for (const path of ["block/gear", "block/large/gear"]) {
const json = JSON.parse(Deno.readTextFileSync(`${output}/${path}.json`));
assertEquals(json.format_version, 1);
assertEquals(validate_model(json.model), []);
}
});
Deno.test("models that can't be converted are reported, not written", () => {
const root = resources({
"block/obj": { loader: "forge:obj", model: "gears:models/block/thing.obj" },
"item/ingot": { parent: "item/generated", textures: { layer0: "gears:item/ingot" } },
"block/orphan": { parent: "gears:block/nowhere" },
"block/tilted": {
...GEAR,
elements: [{ ...GEAR.elements[0], rotation: { angle: 30, axis: "x", origin: [8, 8, 8] } }],
},
// these two would both be gears:block_a_b
"block/a/b": GEAR,
"block/a_b": GEAR,
});
const output = Deno.makeTempDirSync();
const report = import_models(root, { output });
assertEquals(report.skipped.map(({ location }) => location).sort(), ["gears:block/obj", "gears:item/ingot"]);
const errors = Object.fromEntries(report.errors.map(({ location, problems }) => [location, problems.join(" ")]));
assertStringIncludes(errors["gears:block/orphan"], "gears:block/nowhere");
assertStringIncludes(errors["gears:block/tilted"], "30");
assertStringIncludes(errors["gears:block/a_b"] ?? errors["gears:block/a/b"], "--remap");
assertEquals(report.written.length, 1);
});
Deno.test("--remap renames locations before they become ids", () => {
const root = resources({ "block/gear": GEAR });
const report = import_models(`${root}/assets/gears/models/block/gear.json`, {
remaps: { textures: { "gears:block/": "cogs:" }, models: { "gears:block/gear": "cogs:big_gear" } },
});
const model = JSON.parse(report.written[0].text!).model;
assertEquals(model.id, "cogs:big_gear");
assertEquals(model.textures, { "0": "cogs:gear" });
});
// a png with only the header, which is all that's read to size it
function png(width: number, height: number): Uint8Array {
const bytes = new Uint8Array(33);
bytes.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52]);
const view = new DataView(bytes.buffer);
view.setUint32(16, width);
view.setUint32(20, height);
return bytes;
}
// textures in a temporary resources folder: { "gears:block/gear": [16, 16] }, with a .png.mcmeta when given
function textures(files: Record<string, { size: [number, number]; mcmeta?: unknown }>): string {
const root = Deno.makeTempDirSync();
for (const [location, { size, mcmeta }] of Object.entries(files)) {
const [namespace, path] = location.split(":");
const file = `${root}/assets/${namespace}/textures/${path}.png`;
Deno.mkdirSync(file.slice(0, file.lastIndexOf("/")), { recursive: true });
Deno.writeFileSync(file, png(...size));
if (mcmeta !== undefined) Deno.writeTextFileSync(`${file}.mcmeta`, JSON.stringify(mcmeta));
}
return root;
}
Deno.test("textures keep their folders, so bworld names them like the models that use them", () => {
const root = textures({
"gears:block/gear": { size: [16, 16] },
"gears:block/belt/scroll": {
size: [16, 64],
mcmeta: { animation: { frametime: 2, interpolate: true }, villager: { hat: "full" } },
},
});
const output = Deno.makeTempDirSync();
const report = import_textures(root, { output });
assertEquals(report.errors, []);
assertEquals(report.written.filter(({ copy }) => copy).map(({ id }) => id).sort(), [
"gears:block_belt_scroll",
"gears:block_gear",
]);
assertEquals(Deno.readFileSync(`${output}/block/gear.png`), png(16, 16));
// the animation is the same format, only renamed, and what bworld can't use is left out
const animation = JSON.parse(Deno.readTextFileSync(`${output}/block/belt/scroll.json`));
assertEquals(animation, { animation: { frametime: 2, interpolate: true } });
assertEquals(report.warnings.length, 1);
});
Deno.test("textures bworld can't use are reported, and minecraft's aren't copied", () => {
const root = textures({
"gears:gui/panorama": { size: [512, 512] },
// 3 rows of 16 don't split into 16×32 frames
"gears:block/uneven": { size: [16, 48], mcmeta: { animation: { height: 32 } } },
"minecraft:block/stone": { size: [16, 16] },
});
const output = Deno.makeTempDirSync();
const report = import_textures(root, { output });
assertEquals(report.skipped.map(({ location }) => location).sort(), [
"gears:gui/panorama",
"minecraft:block/stone",
]);
assertEquals(report.errors.map(({ location }) => location), ["gears:block/uneven"]);
assertEquals(report.written, []);
});
Deno.test("a folder with two mods' textures needs --namespace to pick one", () => {
const root = textures({ "gears:block/gear": { size: [16, 16] }, "belts:block/belt": { size: [16, 16] } });
const output = Deno.makeTempDirSync();
assertThrows(() => import_textures(root, { output }), Error, "--namespace");
const report = import_textures(root, { output, namespace: "belts" });
assertEquals(report.written.map(({ id }) => id), ["belts:block_belt"]);
});
Deno.test("an animation that doesn't fit its png is an error, not a broken texture", () => {
const { problems } = convert_mcmeta({ animation: { frames: [0, 5] } }, { width: 16, height: 32 });
assert(problems.length > 0);
});