Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/loop/__tests__/goal-markdown.test.ts

Raw
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("<p>Ordinary goal</p>\n");
		const html = render(
			[
				"# Actual heading",
				"",
				"Paragraph **strong** and *emphasis* with `code`.",
				"",
				"- first",
				"- [x] done",
				"- [ ] pending",
				"",
				"1. ordered",
				"",
				"> quoted",
				"",
				"```ts",
				"const x = '<safe>';",
				"```",
				"",
				"| Gate | Result |",
				"| --- | --- |",
				"| tests | pass |",
			].join("\n"),
		);
		for (const fragment of [
			"<h1>Actual heading</h1>",
			"<p>Paragraph <strong>strong</strong> and <em>emphasis</em> with <code>code</code>.</p>",
			"<ul>",
			"<ol>",
			"<blockquote>",
			"<table>",
			"<th>Gate</th>",
			'<input checked="" disabled="" type="checkbox">',
			'<input disabled="" type="checkbox">',
			'<pre><code class="language-ts">const x = &#39;&lt;safe&gt;&#39;;',
		])
			expect(html).toContain(fragment);
	});

	it("escapes raw HTML, code and attributes while never emitting images", () => {
		const html = createGoalRenderer()(
			[
				'<svg onload="alert(1)"><a href="javascript:alert(1)">bad</a></svg>',
				"",
				'Inline <img src=x onerror="alert(1)"> and <script>alert(1)</script>.',
				"",
				'![alt <tag>](https://remote.example/tracker.svg "image title")',
				"",
				'[safe](https://example.com/?a=1&b=2 "title & &#34; onclick=evil")',
				"",
				"`<img src=x onerror=alert(1)>`",
			].join("\n"),
		);
		expect(html).not.toMatch(/<(?:svg|img|script)\b/u);
		expect(html).toContain("&lt;svg onload=&quot;");
		expect(html).toContain("alt &lt;tag&gt;");
		expect(html).not.toContain("tracker.svg");
		expect(html).toContain('href="https://example.com/?a=1&amp;b=2"');
		expect(html).toContain('title="title &amp; &amp;#34; onclick=evil"');
		expect(html).toContain('target="_blank" rel="noopener noreferrer"');
	});

	it.each([
		"javascript:alert%281%29",
		"JaVaScRiPt:alert%281%29",
		"jav&#x61;script:alert%281%29",
		"javascript&#58;alert%281%29",
		"java&#9;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("<p>label</p>\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("<p>Changed</p>\n");
			expect(lex).toHaveBeenCalledTimes(3);
			expect(first(goal)).toBe(html);
			expect(lex).toHaveBeenCalledTimes(4);
		} finally {
			lex.mockRestore();
		}
	});
});