270 lines
11 KiB
TypeScript
270 lines
11 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { assert, assertEquals, assertThrows } from "@std/assert";
|
|
import {
|
|
argument,
|
|
boolean,
|
|
choice,
|
|
CommandDispatcher,
|
|
CommandError,
|
|
greedy_string,
|
|
integer,
|
|
literal,
|
|
word,
|
|
} from "@/common/commands.ts";
|
|
import { test_game } from "./helpers.ts";
|
|
|
|
interface Source {
|
|
admin: boolean;
|
|
said: string[];
|
|
}
|
|
|
|
function dispatcher() {
|
|
const commands = new CommandDispatcher<Source>();
|
|
commands.register(
|
|
literal<Source>("add").then(
|
|
argument<number, Source>("a", integer()).then(
|
|
argument<number, Source>("b", integer(0, 10))
|
|
.executes((c) => c.source.said.push(`${c.get<number>("a") + c.get<number>("b")}`)),
|
|
),
|
|
),
|
|
);
|
|
commands.register(
|
|
literal<Source>("say")
|
|
.executes((c) => c.source.said.push("..."))
|
|
.then(argument<string, Source>("text", greedy_string()).executes((c) => c.source.said.push(c.get("text")))),
|
|
);
|
|
commands.register(
|
|
literal<Source>("mode")
|
|
.then(literal<Source>("all").executes((c) => c.source.said.push("literal all")))
|
|
.then(argument<string, Source>("name", word()).executes((c) => c.source.said.push(`word ${c.get("name")}`)))
|
|
.then(
|
|
literal<Source>("flag").then(
|
|
argument<boolean, Source>("on", boolean()).executes((c) =>
|
|
c.source.said.push(`flag ${c.get("on")}`)
|
|
),
|
|
),
|
|
),
|
|
);
|
|
commands.register(
|
|
literal<Source>("secret").requires((s) => s.admin).executes((c) => c.source.said.push("secret")),
|
|
);
|
|
commands.register(
|
|
literal<Source>("color").then(
|
|
argument<string, Source>("color", choice(["red", "green", "blue"]))
|
|
.suggests((partial) => [{ text: `${partial}!`, tooltip: "custom" }])
|
|
.executes((c) => c.source.said.push(c.get("color"))),
|
|
),
|
|
);
|
|
return commands;
|
|
}
|
|
|
|
const run = (input: string, admin = false) => {
|
|
const source = { admin, said: [] as string[] };
|
|
dispatcher().execute(input, source);
|
|
return source.said;
|
|
};
|
|
const error = (input: string, admin = false) => assertThrows(() => run(input, admin), CommandError).message;
|
|
const suggest = (input: string, admin = false) => {
|
|
const { start, suggestions } = dispatcher().suggest(input, { admin, said: [] });
|
|
return { start, texts: suggestions.map((s) => s.text) };
|
|
};
|
|
|
|
Deno.test("commands run down the tree with typed arguments, words winning over arguments", () => {
|
|
assertEquals(run("add 2 3"), ["5"]);
|
|
assertEquals(run("add -2 3"), ["1"]);
|
|
assertEquals(run("say"), ["..."]);
|
|
assertEquals(run("say hello there, you"), ["hello there, you"]);
|
|
assertEquals(run("mode all"), ["literal all"]);
|
|
assertEquals(run("mode alls"), ["word alls"]);
|
|
assertEquals(run("mode flag true"), ["flag true"]);
|
|
assertEquals(run("secret", true), ["secret"]);
|
|
});
|
|
|
|
Deno.test("what went wrong is from the furthest the command got", () => {
|
|
assertEquals(error("nope"), "Unknown command /nope");
|
|
assertEquals(error(""), "Unknown command /");
|
|
assertEquals(error("add 2"), "Not enough: /add <a> <b>");
|
|
assertEquals(error("add 2 11"), "11 is too big, the most is 10");
|
|
assertEquals(error("add x 1"), `"x" isn't a whole number`);
|
|
assertEquals(error("add 1 2 3"), `Didn't expect "3": /add <a> <b>`);
|
|
assertEquals(error("mode flag maybe"), `"maybe" isn't true or false`);
|
|
assertEquals(error("color pink"), `"pink" isn't one of red, green, blue`);
|
|
// not allowed is the same as not there
|
|
assertEquals(error("secret"), "Unknown command /secret");
|
|
});
|
|
|
|
Deno.test("usage reads like brigadier's smart usage", () => {
|
|
const commands = dispatcher();
|
|
const source = { admin: false, said: [] };
|
|
assertEquals(commands.usage(commands.get("add")!, source), "add <a> <b>");
|
|
assertEquals(commands.usage(commands.get("say")!, source), "say [<text>]");
|
|
assertEquals(commands.usage(commands.get("mode")!, source), "mode (all|flag|<name>)");
|
|
assertThrows(() => commands.register(literal<Source>("add")), Error, "/add already exists");
|
|
});
|
|
|
|
Deno.test("suggestions are for the word being typed, from words, types and providers", () => {
|
|
assertEquals(suggest(""), { start: 0, texts: ["add", "color", "mode", "say"] });
|
|
assertEquals(suggest("", true).texts, ["add", "color", "mode", "say", "secret"]);
|
|
assertEquals(suggest("m"), { start: 0, texts: ["mode"] });
|
|
assertEquals(suggest("mode "), { start: 5, texts: ["all", "flag"] });
|
|
assertEquals(suggest("mode f"), { start: 5, texts: ["flag"] });
|
|
assertEquals(suggest("mode flag t"), { start: 10, texts: ["true"] });
|
|
assertEquals(suggest("color g").texts, ["g!"]);
|
|
// nothing fits after a bad argument, or in a greedy one
|
|
assertEquals(suggest("add x ").texts, []);
|
|
assertEquals(suggest("say a ").texts, []);
|
|
});
|
|
|
|
Deno.test("/give suggests item ids as they're typed", async () => {
|
|
const { join, take, send } = await test_game("mods");
|
|
join(1, "alice");
|
|
take(1);
|
|
const suggest = (text: string) => {
|
|
send(1, { type: "suggest", id: 3, text });
|
|
return take(1).find((m) => m.type === "suggestions");
|
|
};
|
|
|
|
const carrots = suggest("/give carr");
|
|
assertEquals(carrots.id, 3);
|
|
assertEquals(carrots.start, 6);
|
|
// just the ids
|
|
assertEquals(carrots.suggestions, [{ text: "bworld:carrot" }]);
|
|
|
|
// items from every mod, but not air. the server sends at most 100, so these look past the first ones
|
|
const ids = (text: string) => suggest(text).suggestions.map((s: { text: string }) => s.text);
|
|
assertEquals(ids("/give ").length, 100);
|
|
assert(ids("/give bworld:sto").includes("bworld:stone"));
|
|
assert(ids("/give bworld:cab").includes("bworld:cabbage"));
|
|
assert(!ids("/give bworld:a").includes("cubi:air"));
|
|
assertEquals(suggest("/give bworld:tom").suggestions.map((s: { text: string }) => s.text), [
|
|
"bworld:tomato",
|
|
"bworld:tomato_sauce",
|
|
"bworld:tomato_seeds",
|
|
]);
|
|
|
|
assertEquals(suggest("/gi").suggestions, [{ text: "give" }]);
|
|
assertEquals(suggest("/time set ").suggestions.map((s: { text: string }) => s.text), [
|
|
"day",
|
|
"midnight",
|
|
"night",
|
|
"noon",
|
|
"sunset",
|
|
]);
|
|
// only commands
|
|
assertEquals(suggest("hello"), undefined);
|
|
|
|
send(1, { type: "chat", text: "/give carrot 2" });
|
|
assertEquals(take(1).find((m) => m.type === "chat").text, "Gave 2 Carrot");
|
|
send(1, { type: "chat", text: "/give air" });
|
|
assertEquals(take(1).find((m) => m.type === "chat").text, "There's no item called air");
|
|
send(1, { type: "chat", text: "/give" });
|
|
assertEquals(take(1).find((m) => m.type === "chat").text, "Not enough: /give <item> [<count> [fluid|<data>]]");
|
|
});
|
|
|
|
Deno.test("/give gives items with data, and fluid containers full of a fluid", async () => {
|
|
const { game, join, take, send } = await test_game("mods");
|
|
join(1, "alice");
|
|
take(1);
|
|
const player = game.players()[0];
|
|
const give = (text: string) => {
|
|
player.inventory.clear();
|
|
send(1, { type: "chat", text });
|
|
return take(1).find((m) => m.type === "chat").text;
|
|
};
|
|
const given = () => player.inventory.to_data().filter((item) => item);
|
|
|
|
assertEquals(give('/give bworld:wood_pickaxe 1 {"damage": 5}'), "Gave 1 Wood Pickaxe");
|
|
assertEquals(given(), [{ id: "bworld:wood_pickaxe", count: 1, data: { damage: 5 } }]);
|
|
assertEquals(give("/give bworld:stone 1 {oops"), "{oops isn't JSON");
|
|
|
|
const juice = { fluid: { id: "bworld:juice", data: { item: "bworld:carrot" } } };
|
|
assertEquals(
|
|
give('/give bworld:glass_bottle 3 fluid bworld:juice {"item": "bworld:carrot"}'),
|
|
"Gave 3 Glass Bottle of Carrot Juice",
|
|
);
|
|
assertEquals(given(), [{ id: "bworld:glass_bottle", count: 3, data: juice }]);
|
|
// a full bucket stacks to 1, and the fluid can be without data
|
|
assertEquals(give("/give bworld:bucket 2 fluid bworld:juice"), "Gave 2 Bucket of Juice");
|
|
assertEquals(given().map((item) => item!.count), [1, 1]);
|
|
assertEquals(give("/give bworld:stone 1 fluid bworld:juice"), "Stone doesn't hold fluids");
|
|
|
|
send(1, { type: "suggest", id: 4, text: "/give bworld:bucket 1 fluid bworld:ju" });
|
|
const suggested = take(1).find((m) => m.type === "suggestions").suggestions;
|
|
assertEquals(suggested.map((s: { text: string }) => s.text), ["bworld:juice"]);
|
|
});
|
|
|
|
Deno.test("/locatebiome says where the nearest biome of a kind is", async () => {
|
|
const { game, join, take, send } = await test_game("mods");
|
|
join(1, "alice");
|
|
take(1);
|
|
const say = (text: string) => {
|
|
send(1, { type: "chat", text });
|
|
return take(1).filter((m) => m.type === "chat").map((m) => m.text).join("\n");
|
|
};
|
|
|
|
const answer = say("/locatebiome bworld:plains");
|
|
const [x, y, z] = answer.match(/at (-?\d+) (-?\d+) (-?\d+)/)!.slice(1).map(Number);
|
|
assertEquals(game.world.locate_biome("bworld:plains", x, z), y);
|
|
assert(say("/locatebiome bworld:nowhere").includes("There's no biome bworld:nowhere"));
|
|
});
|
|
|
|
Deno.test("/fill replaces every block between two corners, both included", async () => {
|
|
const { game, join, take, send } = await test_game("mods");
|
|
join(1, "alice");
|
|
take(1);
|
|
const say = (text: string) => {
|
|
send(1, { type: "chat", text });
|
|
return take(1).filter((m) => m.type === "chat").map((m) => m.text).join("\n");
|
|
};
|
|
const ids = (x0: number, y0: number, z0: number, x1: number, y1: number, z1: number) => {
|
|
const found = new Set<string>();
|
|
for (let x = x0; x <= x1; x++) {
|
|
for (let y = y0; y <= y1; y++) {
|
|
for (let z = z0; z <= z1; z++) found.add(game.world.get_block_id(x, y, z));
|
|
}
|
|
}
|
|
return [...found];
|
|
};
|
|
|
|
// corners in any order
|
|
assertEquals(say("/fill 12 101 21 10 100 20 stone"), "Filled 12 blocks with Stone");
|
|
assertEquals(ids(10, 100, 20, 12, 101, 21), ["bworld:stone"]);
|
|
assert(!ids(9, 100, 20, 9, 101, 21).includes("bworld:stone"));
|
|
assert(!ids(10, 102, 20, 12, 102, 21).includes("bworld:stone"));
|
|
|
|
assertEquals(say("/fill 10 100 20 12 101 21 cubi:air"), "Filled 12 blocks with Air");
|
|
assertEquals(ids(10, 100, 20, 12, 101, 21), ["cubi:air"]);
|
|
|
|
assert(say("/fill 0 0 0 100 100 100 stone").startsWith("Couldn't fill it"));
|
|
assertEquals(game.world.get_block_id(50, 120, 50), "cubi:air");
|
|
assertEquals(say("/fill 0 0 0 1 1 1 bworld:nothing"), "There's no block bworld:nothing");
|
|
});
|
|
|
|
Deno.test("/fill and /exportschematic suggest the block the player is looking at for each corner", async () => {
|
|
const { game, join, take, send } = await test_game("mods");
|
|
join(1, "alice");
|
|
for (let z = -6; z <= 1; z++) {
|
|
for (let y = 150; y <= 152; y++) game.set_block(0, y, z, "cubi:air");
|
|
}
|
|
// looking north, level, at a block 3 away at eye height
|
|
send(1, { type: "move", x: 0.5, y: 150, z: 0.5, yaw: 0, pitch: 0 });
|
|
const suggest = (text: string) => {
|
|
take(1);
|
|
send(1, { type: "suggest", id: 1, text });
|
|
return take(1).find((m) => m.type === "suggestions").suggestions.map((s: { text: string }) => s.text);
|
|
};
|
|
assertEquals(suggest("/fill "), []);
|
|
|
|
game.set_block(0, 151, -3, "bworld:stone");
|
|
assertEquals(suggest("/fill "), ["0 151 -3"]);
|
|
assertEquals(suggest("/fill 0 "), ["151 -3"]);
|
|
assertEquals(suggest("/fill 0 151 "), ["-3"]);
|
|
assertEquals(suggest("/fill 5"), []);
|
|
assertEquals(suggest("/fill 0 151 -3 "), ["0 151 -3"]);
|
|
assertEquals(suggest("/fill 0 151 -3 0 151 "), ["-3"]);
|
|
assertEquals(suggest("/exportschematic "), ["0 151 -3"]);
|
|
assertEquals(suggest("/exportschematic 0 151 -3 0 "), ["151 -3"]);
|
|
});
|