import { Marked } from "@earendil-works/pi-tui"; import { describe, expect, it, vi } from "vitest"; import { createGoalRenderer } from "../src/goal-markdown.ts"; describe("browser goal Markdown", () => { it("renders paragraphs and explicit Markdown structures, not one giant heading", () => { const render = createGoalRenderer(); expect(render("Ordinary goal")).toBe("
Ordinary goal
\n"); const html = render( [ "# Actual heading", "", "Paragraph **strong** and *emphasis* with `code`.", "", "- first", "- [x] done", "- [ ] pending", "", "1. ordered", "", "> quoted", "", "```ts", "const x = 'Paragraph strong and emphasis with code.
", "", "
Gate ", '', '', 'const x = '<safe>';', ]) expect(html).toContain(fragment); }); it("escapes raw HTML, code and attributes while never emitting images", () => { const html = createGoalRenderer()( [ '', "", 'Inlineand .', "", '', "", '[safe](https://example.com/?a=1&b=2 "title & " onclick=evil")', "", "` `", ].join("\n"), ); expect(html).not.toMatch(/<(?:svg|img|script)\b/u); expect(html).toContain("<svg onload=""); expect(html).toContain("alt <tag>"); expect(html).not.toContain("tracker.svg"); expect(html).toContain('href="https://example.com/?a=1&b=2"'); expect(html).toContain('title="title & " onclick=evil"'); expect(html).toContain('target="_blank" rel="noopener noreferrer"'); }); it.each([ "javascript:alert%281%29", "JaVaScRiPt:alert%281%29", "javascript:alert%281%29", "javascript:alert%281%29", "java script:alert%281%29", "data:image/svg+xml,evil", "vbscript:evil", "file:///etc/passwd", "//remote.example/path", "/relative", ])("renders unsupported URL %s as a plain label", (href) => { expect(createGoalRenderer()(`[label](${href})`)).toBe("
label
\n"); }); it.each([ "https://example.com/", "http://example.com/", "mailto:me@example.com", ])("allows explicit %s links", (href) => { expect(createGoalRenderer()(`[label](${href})`)).toContain( `href="${href}"`, ); }); it("updates only its last goal without mixing observer instances", () => { const first = createGoalRenderer(); const second = createGoalRenderer(); const goal = "Paragraph ".repeat(3200); const lex = vi.spyOn(new Marked().Lexer, "lex"); try { const html = first(goal); expect(lex).toHaveBeenCalledTimes(1); expect(first(goal)).toBe(html); expect(first(goal)).toBe(html); expect(lex).toHaveBeenCalledTimes(1); expect(second(goal)).toBe(html); expect(lex).toHaveBeenCalledTimes(2); expect(first("Changed")).toBe("Changed
\n"); expect(lex).toHaveBeenCalledTimes(3); expect(first(goal)).toBe(html); expect(lex).toHaveBeenCalledTimes(4); } finally { lex.mockRestore(); } }); });