mancala/packages/core/tests/models/Bot.test.ts

56 lines
2.4 KiB
TypeScript
Raw Normal View History

import { BOT_LEVELS, getBotLevel, getBotLevelTextKey, getLastBotLevel, isBotId, isBotLevel, setLastBotLevel } from "../../src/models";
import { en, tr } from "../../src/localization";
import { getPlayerDisplayName } from "../../src/util";
describe("bot ids", () => {
it("recognises bot ids and their levels", () => {
expect(isBotId("bot:medium")).toBe(true);
expect(isBotId("kc:alice")).toBe(false);
expect(isBotId(undefined)).toBe(false);
expect(getBotLevel("bot:master")).toBe("master");
expect(getBotLevel("bot:impossible")).toBeUndefined();
expect(getBotLevel("kc:alice")).toBeUndefined();
});
it("checks levels", () => {
expect(isBotLevel("hard")).toBe(true);
expect(isBotLevel("impossible")).toBe(false);
expect(isBotLevel(3)).toBe(false);
});
it("has a name for every level in both languages", () => {
for (const level of BOT_LEVELS) {
const key = getBotLevelTextKey(level) as keyof typeof en;
expect(en[key]).toBeTruthy();
expect(tr[key as keyof typeof tr]).toBeTruthy();
}
});
});
describe("last bot level", () => {
it("is Medium until one is chosen, then the chosen one", () => {
const values = new Map<string, string>();
const storage = { getString: (key: string) => values.get(key), setString: (key: string, value: string) => { values.set(key, value); } };
expect(getLastBotLevel(storage)).toBe("medium");
setLastBotLevel(storage, "master");
expect(getLastBotLevel(storage)).toBe("master");
values.set("botLevel", "impossible");
expect(getLastBotLevel(storage)).toBe("medium");
});
});
describe("getPlayerDisplayName", () => {
const t = (key: string) => (tr as Record<string, string>)[key] ?? key;
it("shows the bot with its level", () => {
expect(getPlayerDisplayName({ userId: "bot:medium", name: "Bot", isGuest: false }, t)).toBe("Bot · Orta");
expect(getPlayerDisplayName({ userId: "bot:easy" }, t)).toBe("Bot · Kolay");
});
it("still shows people and guests as before", () => {
expect(getPlayerDisplayName({ userId: "kc:alice", name: "alice", isGuest: false }, t)).toBe("alice");
expect(getPlayerDisplayName({ userId: "kc:g", name: "guest-4f2a9b", isGuest: true }, t)).toBe("Misafir 4F2A");
expect(getPlayerDisplayName({ userId: "0b9f6d1e" }, t)).toBeUndefined();
});
});