import { describe, it } from "@std/testing/bdd"; import { expect } from "@std/expect"; import { markdownTemplate, revealedText, } from "../../../console/src/markdown.ts"; import { templateText } from "./template-text.ts"; const rendered = ( source: string, revealed?: Readonly>, ): string => templateText(markdownTemplate(source, { revealed })); describe("console/src/markdown", () => { describe("markdownTemplate()", () => { it("returns each character reference HTML defines as its character", () => { expect( rendered("a & b <c> AB é—…"), ) .toContain("a & b AB \u00e9\u2014\u2026"); }); it("returns a name HTML does not define, or one with no semicolon, as written, and an impossible code point as the replacement character", () => { expect(rendered("¬aname; é � �")).toContain( "¬aname; é \ufffd \ufffd", ); }); it("returns a web link as a link that opens apart from the console", () => { const text = rendered("[shop](https://shop.example/?a=1&b=2)"); expect(text).toContain('href="https://shop.example/?a=1&b=2"'); expect(text).toContain('rel="noopener noreferrer"'); }); it("returns a script link as its label alone", () => { const text = rendered("[click](javascript:alert(1))"); expect(text).toContain("click"); expect(text).not.toContain(" { const text = rendered( '\n\n' + "![a red chair](https://x.example/chair.png)", ); expect(text).toContain("a red chair"); expect(text).not.toContain(" { const text = rendered( [ "# One", "## Two", "### Three", "#### Four", "", "> quoted", "", "- a", "- b", "", "---", "", "3. c", "4. d", "", "| x | y |", "| - | - |", "| 1 | 2 |", ].join("\n"), ); for ( const element of [ "

One

", "

Two

", "
Three
", "
Four
", "

quoted

", "
  • a
  • b
", "
", '
  1. c
  2. d
', "x", "2", ] ) { expect(text.replace(/\s+\s+/g, ">")).toContain( element, ); } }); it("returns emphasis, a struck word, a hard break and an escape as they read", () => { const text = rendered("**bold** *em* ~~gone~~ a \nb \\*not em\\*"); expect(text).toContain("bold"); expect(text).toContain("em"); expect(text).toContain("gone"); expect(text).toContain("a
b"); expect(text).toContain("*not em*"); }); it("returns a code block as written, with each revealed return referent as its string", () => { const text = rendered("```\nopen cfh:v:22222 &\n```", { "cfh:v:22222": "https://shop.example/", }); expect(text).toContain( '
open foundhttps://shop.example/ &
', ); }); it("returns a task list's boxes showing their state, and taking no input, in a tight list and a loose one", () => { for ( const source of ["- [x] done\n- [ ] to do", "- [x] done\n\n- [ ] to do"] ) { const text = rendered(source); expect(text).toContain(''); expect(text).toContain(''); } }); it("returns inline markup as nothing, keeping the words around it", () => { const text = rendered('a bold b'); expect(text).toContain("a bold b"); expect(text).not.toContain(" { const text = rendered("[shop](https://[shop)"); expect(text).toContain("shop"); expect(text).not.toContain(" { expect(rendered("`a & b`")).toContain("a & b"); }); it("returns each revealed return referent in the text as its string, marked as found", () => { const text = rendered("Bought **cfh:v:22222**; see cfh:v:33333.", { "cfh:v:22222": "https://shop.example/item/7", }); expect(text).toContain( 'foundhttps://shop.example/item/7', ); expect(text).toContain("see cfh:v:33333."); }); it("returns a link to a revealed return referent with its destination as written", () => { const text = rendered("[the item](cfh:v:22222)", { "cfh:v:22222": "https://shop.example/item/7", }); expect(text).not.toContain(" { const text = rendered("[cfh:v:22222](https://elsewhere.example/)", { "cfh:v:22222": "https://shop.example/item/7", }); expect(text).toContain('href="https://elsewhere.example/"'); expect(text).toContain("cfh:v:22222"); expect(text).not.toContain("https://shop.example/item/7"); }); it("returns a link with the host it goes to beside its label, and a mail link as its label alone", () => { const text = rendered( "Open [your bank](https://phish.example/login) or [write](mailto:a@b.example).", ); expect(text).toContain(">your bank"); expect(text).toContain( '(phish.example)', ); expect(text).toContain("write"); expect(text).not.toContain("mailto:"); }); it("returns a found string on one line, cut short with the whole of it on hover, its control characters, separators and direction marks spelled out", () => { const text = rendered("Bought cfh:v:22222.", { "cfh:v:22222": `a\nb\u202Ec\u2028${"x".repeat(200)}`, }); expect(text).toContain( `founda\\nb\\u{202E}c\\u{2028}${ "x".repeat(99) }…`, ); expect(text).not.toContain("\u202E"); expect(text).not.toContain("\n"); }); }); describe("revealedText()", () => { it("returns plain text with each revealed return referent as its string, marked as found", () => { const text = templateText( revealedText("Could not buy cfh:v:22222; see cfh:v:33333.", { "cfh:v:22222": "the blue one", }), ); expect(text).toBe( 'Could not buy foundthe blue one; see cfh:v:33333.', ); }); }); });