Files
MineGeo/tests/protocol.test.ts

99 lines
2.8 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { boundsSchema, datasetSchema, scoreGuess } from "../shared/protocol";
const bounds = { minX: -512, minZ: -256, maxX: 512, maxZ: 256 };
test("horizontal scoring ignores elevation and handles negative coordinates", () => {
assert.equal(
scoreGuess({ x: -13, z: -25 }, { x: -13, z: -25 }, bounds).score,
5000,
);
assert.equal(scoreGuess({ x: 0, z: 0 }, { x: 1, z: 0 }, bounds).score, 5000);
assert.equal(
scoreGuess({ x: -50, z: 0 }, { x: 50, z: 0 }, bounds).distance,
100,
);
assert.ok(
scoreGuess({ x: 0, z: 0 }, { x: 100, z: 100 }, bounds).score < 5000,
);
});
test("invalid, zero, and oversized bounds cannot enter rendering", () => {
assert.equal(
boundsSchema.safeParse({ ...bounds, maxX: -512 }).success,
false,
);
assert.equal(
boundsSchema.safeParse({ ...bounds, maxX: 3000 }).success,
false,
);
assert.equal(boundsSchema.safeParse(bounds).success, true);
});
test("dataset requires five unique in-bounds viewpoints and safe paths", () => {
const base = {
format: "minegeo-dataset/v1",
minecraftVersion: "1.21.11",
dimension: "minecraft:overworld",
source: "plugin",
name: "Test map",
serverName: "Test server",
north: "-z",
bounds,
viewpoints: Array.from({ length: 5 }, (_, i) => ({
id: `p${i}`,
x: i,
y: 70,
z: i,
})),
assets: [{ path: "terrain.json.gz", sha256: "a".repeat(64), bytes: 100 }],
};
assert.equal(datasetSchema.safeParse(base).success, true);
assert.equal(datasetSchema.safeParse({ ...base, assets: [] }).success, false);
assert.equal(
datasetSchema.safeParse({ ...base, source: "fabric" }).success,
false,
);
const capture = {
...base,
source: "fabric",
assets: [
"map.png",
...base.viewpoints.flatMap((point) =>
["px", "nx", "py", "ny", "pz", "nz"].map(
(face) => `${point.id}/${face}.png`,
),
),
].map((path) => ({ path, sha256: "a".repeat(64), bytes: 100 })),
};
assert.equal(datasetSchema.safeParse(capture).success, true);
assert.equal(
datasetSchema.safeParse({ ...capture, assets: capture.assets.slice(1) })
.success,
false,
);
assert.equal(
datasetSchema.safeParse({ ...base, viewpoints: base.viewpoints.slice(1) })
.success,
false,
);
assert.equal(
datasetSchema.safeParse({
...base,
assets: [{ ...base.assets[0], path: "../secrets" }],
}).success,
false,
);
assert.equal(
datasetSchema.safeParse({
...base,
viewpoints: base.viewpoints.map((p) => ({ ...p, id: "same" })),
}).success,
false,
);
assert.equal(
datasetSchema.safeParse({
...base,
viewpoints: base.viewpoints.map((p) => ({ ...p, x: 9999 })),
}).success,
false,
);
});