morse-code / tests /unit /rules.test.js
RemiFabre
feat: Demo mode for filming two robots conversing
a33a583
Raw
History Blame Contribute Delete
1.1 kB
import { describe, it, expect } from "vitest";
import { normalizePhrase, matchRule, DEFAULT_RULES } from "../../lib/rules.js";
describe("normalizePhrase", () => {
it("uppercases and strips spaces + punctuation", () => {
expect(normalizePhrase("U OK?")).toBe("UOK");
expect(normalizePhrase("u ok")).toBe("UOK");
expect(normalizePhrase("S.O.S")).toBe("SOS");
});
});
describe("matchRule", () => {
it("fires on punctuation/spacing variants of the input", () => {
expect(matchRule("U OK", DEFAULT_RULES)?.output).toBe("Y");
expect(matchRule("UOK", DEFAULT_RULES)?.output).toBe("Y");
expect(matchRule("SOS", DEFAULT_RULES)?.output).toBe("WTF");
});
it("returns null for no match or empty text", () => {
expect(matchRule("HELLO", DEFAULT_RULES)).toBeNull();
expect(matchRule("", DEFAULT_RULES)).toBeNull();
});
it("ignores rules missing input or output", () => {
const rules = [{ input: "A", output: "" }, { input: "", output: "B" }];
expect(matchRule("A", rules)).toBeNull();
});
});