163 lines
6.1 KiB
TypeScript
163 lines
6.1 KiB
TypeScript
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 items as they're typed, with their names", 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);
|
|
assert(
|
|
carrots.suggestions.some((s: { text: string; tooltip: string }) =>
|
|
s.text === "bworld:carrot" && s.tooltip === "Carrot"
|
|
),
|
|
);
|
|
assert(carrots.suggestions.every((s: { text: string }) => s.text.split(":")[1].startsWith("car")));
|
|
|
|
// 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_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>]");
|
|
});
|