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(); commands.register( literal("add").then( argument("a", integer()).then( argument("b", integer(0, 10)) .executes((c) => c.source.said.push(`${c.get("a") + c.get("b")}`)), ), ), ); commands.register( literal("say") .executes((c) => c.source.said.push("...")) .then(argument("text", greedy_string()).executes((c) => c.source.said.push(c.get("text")))), ); commands.register( literal("mode") .then(literal("all").executes((c) => c.source.said.push("literal all"))) .then(argument("name", word()).executes((c) => c.source.said.push(`word ${c.get("name")}`))) .then( literal("flag").then( argument("on", boolean()).executes((c) => c.source.said.push(`flag ${c.get("on")}`) ), ), ), ); commands.register( literal("secret").requires((s) => s.admin).executes((c) => c.source.said.push("secret")), ); commands.register( literal("color").then( argument("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 "); 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 `); 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 "); assertEquals(commands.usage(commands.get("say")!, source), "say []"); assertEquals(commands.usage(commands.get("mode")!, source), "mode (all|flag|)"); assertThrows(() => commands.register(literal("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 car"); assertEquals(carrots.id, 3); assertEquals(carrots.start, 6); // just the ids assertEquals(carrots.suggestions, [{ text: "bworld:carrot" }]); // every item, but not air const all = suggest("/give ").suggestions.map((s: { text: string }) => s.text); assert(all.includes("bworld:stone") && all.includes("farmersdelight:cabbage"), String(all)); assert(!all.includes("bworld:air")); assertEquals(suggest("/give farmersdelight:tom").suggestions.map((s: { text: string }) => s.text), [ "farmersdelight:tomato", "farmersdelight:tomato_sauce", "farmersdelight: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 []"); });