Files
bworld/tests/block_predictions_test.ts
2026-11-04 21:51:26 -03:00

68 lines
3.2 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what a client guesses about blocks waits for the server's block_ack before the server's word replaces it, like
// minecraft's BlockStatePredictionHandler, so clicking fast doesn't flicker. see client/level/block_predictions.ts
import { assertEquals } from "@std/assert";
import { BlockPredictions } from "@/client/level/block_predictions.ts";
import { test_game } from "./helpers.ts";
const CLOSED = { x: 0, y: 100, z: 0, id: "bworld:oak_trapdoor", state: 0 };
const OPEN = { ...CLOSED, state: 4 };
Deno.test("what the server says about a guessed block waits for the ack of the newest guess that changed it", () => {
const predictions = new BlockPredictions();
// a trapdoor clicked open, then closed again before the server answers
const first = predictions.start();
predictions.retain(0, 100, 0, () => CLOSED);
predictions.end();
const second = predictions.start();
predictions.retain(0, 100, 0, () => OPEN);
predictions.end();
// the answer to the first click opens it, but the second is still on its way
assertEquals(predictions.from_server(0, 100, 0, OPEN.id, OPEN.state), true);
assertEquals(predictions.ack(first), []);
// and the answer to the second closes it, which shows now
assertEquals(predictions.from_server(0, 100, 0, CLOSED.id, CLOSED.state), true);
assertEquals(predictions.ack(second), [CLOSED]);
assertEquals(predictions.from_server(0, 100, 0, OPEN.id, OPEN.state), false);
});
Deno.test("a guess the server says nothing about goes back to what it said before, once acked", () => {
const predictions = new BlockPredictions();
const seq = predictions.start();
predictions.retain(0, 100, 0, () => CLOSED);
predictions.end();
assertEquals(predictions.ack(seq), [CLOSED]);
});
Deno.test("changes outside a guess aren't kept track of", () => {
const predictions = new BlockPredictions();
predictions.retain(0, 100, 0, () => CLOSED);
assertEquals(predictions.from_server(0, 100, 0, OPEN.id, OPEN.state), false);
assertEquals(predictions.ack(1), []);
});
Deno.test("the server acks a use_block or break_block after the set_blocks it caused, even one it ignores", async () => {
const { game, join, take, send } = await test_game("mods");
join(1, "alice");
send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
game.set_block(0, 99, 0, "bworld:stone");
game.set_block(0, 100, 0, "bworld:oak_trapdoor");
// until alice's client has the chunk, nothing in it is sent there
while (!game.players()[0].chunks.has_block(0, 0)) game.tick();
take(1);
send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1], seq: 1 });
const types = take(1).map((m) => m.type).filter((type) => type === "set_block" || type === "block_ack");
assertEquals(types.at(-1), "block_ack");
assertEquals(types.includes("set_block"), true);
send(1, { type: "break_block", x: 0, y: 1000, z: 0, seq: 2 });
assertEquals(take(1).filter((m) => m.type === "block_ack"), [{ type: "block_ack", seq: 2 }]);
// without a seq, from a client that doesn't guess, there's nothing to ack
send(1, { type: "break_block", x: 0, y: 100, z: 0 });
assertEquals(take(1).filter((m) => m.type === "block_ack"), []);
});