Files
bworld/tests/shared_rules_test.ts
2026-11-14 12:18:41 -03:00

88 lines
3.9 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// shared phases' block rules run on clients too, so what a client shows the moment it places, breaks or right clicks
// a block is what the server ends up with
import { assertEquals } from "@std/assert";
import { AIR_ID } from "@/common/protocol.ts";
import { get_state_value } from "@/common/utils.ts";
import type { Face } from "@/common/mod_api/shared.ts";
import { type Area, client_of, server_blocks, test_game } from "./helpers.ts";
type Game = Awaited<ReturnType<typeof test_game>>;
const AREA: Area = { x: [-3, 3], y: [99, 101], z: [-3, 3] };
function state(t: Game, x: number, y: number, z: number, name: string) {
return get_state_value(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!, name);
}
// alice on a stone floor at y 99, south of 0 100 0, holding id
async function setup(id: string, yaw = 0) {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = AREA.x[0]; x <= AREA.x[1]; x++) {
for (let z = AREA.z[0]; z <= AREA.z[1]; z++) {
t.game.set_block(x, 99, z, "bworld:stone");
for (let y = 100; y <= AREA.y[1]; y++) t.game.set_block(x, y, z, AIR_ID);
}
}
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw, pitch: 0 });
t.send(1, { type: "chat", text: `/give ${id} 64` });
t.send(1, { type: "select_slot", slot: 0 });
const use = (x: number, y: number, z: number, face: Face, hit: [number, number, number]) =>
t.send(1, { type: "use_block", x, y, z, face, hit });
return { t, use };
}
Deno.test("a client places a fence joined to the fences and walls beside it, the same as the server", async () => {
const { t, use } = await setup("bworld:oak_fence");
t.game.set_block(-1, 100, 0, "bworld:oak_fence");
t.game.set_block(0, 100, -1, "bworld:stone");
t.game.set_block(1, 100, 0, "bworld:oak_leaves");
const client = client_of(t.game, AREA);
client.place("bworld:oak_fence", 0, 100, 0, 0, "top", [0.5, 1, 0.5]);
use(0, 99, 0, "top", [0.5, 1, 0.5]);
assertEquals(client.blocks(), server_blocks(t.game, AREA));
// it joined the fence and the stone, and the fence joined it back
assertEquals(state(t, 0, 100, 0, "west"), 1);
assertEquals(state(t, 0, 100, 0, "north"), 1);
assertEquals(state(t, -1, 100, 0, "east"), 1);
// breaking it lets go of the one beside it, on both
client.break(0, 100, 0);
t.send(1, { type: "break_block", x: 0, y: 100, z: 0 });
assertEquals(client.blocks(), server_blocks(t.game, AREA));
assertEquals(state(t, -1, 100, 0, "east"), 0);
});
Deno.test("a client places a trapdoor turned and in the half the server puts it", async () => {
const { t, use } = await setup("bworld:oak_trapdoor", Math.PI / 2);
t.game.set_block(0, 100, 0, "bworld:stone");
const client = client_of(t.game, AREA);
// on the upper half of the stone's east side, and on top of the floor facing alice, who looks west
client.place("bworld:oak_trapdoor", 1, 100, 0, Math.PI / 2, "east", [1, 0.8, 0.5]);
use(0, 100, 0, "east", [1, 0.8, 0.5]);
client.place("bworld:oak_trapdoor", 0, 100, 2, Math.PI / 2, "top", [0.5, 1, 0.5]);
use(0, 99, 2, "top", [0.5, 1, 0.5]);
assertEquals(client.blocks(), server_blocks(t.game, AREA));
assertEquals(state(t, 1, 100, 0, "half"), 1);
assertEquals(state(t, 0, 100, 2, "facing"), 1);
});
Deno.test("a client opens and closes a trapdoor right away, the same as the server, without placing anything", async () => {
const { t, use } = await setup("bworld:oak_trapdoor");
t.game.set_block(0, 100, 0, "bworld:oak_trapdoor");
const client = client_of(t.game, AREA);
for (const open of [1, 0]) {
assertEquals(client.interact(0, 100, 0, 0, "south", [0.5, 0.5, 1]), true);
use(0, 100, 0, "south", [0.5, 0.5, 1]);
assertEquals(client.blocks(), server_blocks(t.game, AREA));
assertEquals(state(t, 0, 100, 0, "open"), open);
}
// alice still has all of hers
assertEquals(t.game.players()[0].inventory.get_slot(0).amount, 64);
});