import { describe, expect, it } from "vitest"; import { accessibleText, splitWatermarkedText } from "./watermark-text"; const ZWSP = "\u200b"; const ZWNJ = "\u200c"; const ZWJ = "\u200d"; const WORD_JOINER = "\u2060"; const run = `${ZWSP}${ZWNJ}${ZWJ}${WORD_JOINER}${ZWSP}${ZWSP}`; const statement = "A cache holds at most 3 entries. A read of a missing key is not a hit."; const marked = `A cache ${run}holds at most 3 entries. ${run}A read of a missing key is not a hit.`; describe("watermarked task text", () => { it("separates the readable words from the invisible runs without losing either", () => { const segments = splitWatermarkedText(marked); expect(segments.filter((segment) => segment.kind === "mark")).toHaveLength(2); expect(segments.map((segment) => segment.value).join("")).toBe(marked); for (const segment of segments) { if (segment.kind === "mark") expect(segment.value).toBe(run); } }); it("reads exactly the words of the task for assistive technology", () => { expect(accessibleText(marked)).toBe(statement); // No format character is left for a screen reader to announce. expect(accessibleText(marked)).not.toMatch(/\p{Cf}/u); }); it("handles runs at the edges and a text without a mark", () => { expect(accessibleText(`${run}text${run}`)).toBe("text"); expect(splitWatermarkedText(`${run}text${run}`).map((segment) => segment.kind)).toEqual([ "mark", "text", "mark", ]); expect(splitWatermarkedText("plain")).toEqual([{ kind: "text", value: "plain" }]); expect(splitWatermarkedText("")).toEqual([]); }); it("keeps Cyrillic text and ordinary punctuation as they are", () => { const text = `Сколько запросов отклонено? ${run}Ответ: число.`; expect(accessibleText(text)).toBe("Сколько запросов отклонено? Ответ: число."); }); });