Spaces:
Running
Running
| import { describe, it, expect } from "vitest"; | |
| import { | |
| textToMorse, | |
| morseToText, | |
| morseToTokens, | |
| isEncodable, | |
| } from "../../lib/morse.js"; | |
| describe("textToMorse", () => { | |
| it("encodes SOS", () => { | |
| expect(textToMorse("SOS").morse).toBe("... --- ..."); | |
| }); | |
| it("is case-insensitive", () => { | |
| expect(textToMorse("sos").morse).toBe("... --- ..."); | |
| }); | |
| it("separates words with /", () => { | |
| expect(textToMorse("HI YOU").morse).toBe(".... .. / -.-- --- ..-"); | |
| }); | |
| it("encodes digits and punctuation", () => { | |
| expect(textToMorse("E1?").morse).toBe(". .---- ..--.."); | |
| }); | |
| it("reports skipped (non-encodable) characters", () => { | |
| const r = textToMorse("Aé😀"); | |
| expect(r.morse).toBe(".-"); | |
| expect(r.skipped).toContain("É"); | |
| }); | |
| }); | |
| describe("morseToText", () => { | |
| it("decodes SOS", () => { | |
| expect(morseToText("... --- ...")).toBe("SOS"); | |
| }); | |
| it("decodes multi-word with /", () => { | |
| expect(morseToText(".... .. / -.-- --- ..-")).toBe("HI YOU"); | |
| }); | |
| it("round-trips a sentence", () => { | |
| const text = "HELLO WORLD 123"; | |
| expect(morseToText(textToMorse(text).morse)).toBe(text); | |
| }); | |
| it("maps unknown pattern to ?", () => { | |
| expect(morseToText("........")).toBe("?"); | |
| }); | |
| }); | |
| describe("morseToTokens", () => { | |
| it("flattens A (.-) into dot, elemGap, dah", () => { | |
| expect(morseToTokens(".-")).toEqual(["dot", "elemGap", "dah"]); | |
| }); | |
| it("inserts letterGap between letters and wordGap between words", () => { | |
| // "E E / E" -> dot, letterGap, dot, wordGap, dot | |
| expect(morseToTokens(". . / .")).toEqual([ | |
| "dot", "letterGap", "dot", "wordGap", "dot", | |
| ]); | |
| }); | |
| }); | |
| describe("isEncodable", () => { | |
| it("accepts letters/digits, rejects emoji", () => { | |
| expect(isEncodable("a")).toBe(true); | |
| expect(isEncodable("7")).toBe(true); | |
| expect(isEncodable("😀")).toBe(false); | |
| }); | |
| }); | |