@@ -1,298 +0,0 @@
1-import { describe, expect, test } from "bun:test";
2-import { renderMarkdown } from "./markdown.ts";
3-
4-// ---------------------------------------------------------------------------
5-// Helpers
6-// ---------------------------------------------------------------------------
7-
8-/** Strip all ANSI escape sequences, leaving only the visible text. */
9-function strip(s: string): string {
10- // Use a dynamic RegExp so the ESC literal doesn't trigger the lint rule
11- // for control characters in regex literals.
12- const esc = String.fromCharCode(0x1b);
13- return s.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
14-}
15-
16-/** Return true if the string contains the given ANSI SGR sequence (e.g. "[1m"). */
17-function hasAnsi(s: string, code: string): boolean {
18- return s.includes(`${String.fromCharCode(0x1b)}${code}`);
19-}
20-
21-/** Return true if the string contains ANSI bold codes. */
22-function hasBold(s: string): boolean {
23- return hasAnsi(s, "[1m");
24-}
25-
26-/** Return true if the string contains ANSI dim codes. */
27-function hasDim(s: string): boolean {
28- return hasAnsi(s, "[2m");
29-}
30-
31-/** Return true if the string contains ANSI cyan codes. */
32-function hasCyan(s: string): boolean {
33- return hasAnsi(s, "[36m");
34-}
35-
36-/** Return true if the string contains ANSI yellow codes. */
37-function hasYellow(s: string): boolean {
38- return hasAnsi(s, "[33m");
39-}
40-
41-// ---------------------------------------------------------------------------
42-// renderMarkdown — block elements
43-// ---------------------------------------------------------------------------
44-
45-describe("renderMarkdown – headings", () => {
46- for (const [input, visible] of [
47- ["# Hello World", "Hello World"],
48- ["## Section", "Section"],
49- ] as const) {
50- test(`${input.slice(0, input.indexOf(" "))} strips its marker and keeps bold cyan styling`, () => {
51- const out = renderMarkdown(input);
52- expect(strip(out)).toBe(visible);
53- expect(hasBold(out)).toBe(true);
54- expect(hasCyan(out)).toBe(true);
55- });
56- }
57-
58- test("h3+ strips markers, stays bold, and drops cyan", () => {
59- const h3 = renderMarkdown("### Sub");
60- const h4 = renderMarkdown("#### Deep");
61- expect(strip(h3)).toBe("Sub");
62- expect(hasBold(h3)).toBe(true);
63- expect(hasCyan(h3)).toBe(false);
64- expect(strip(h4)).toBe("Deep");
65- expect(hasBold(h4)).toBe(true);
66- expect(hasCyan(h4)).toBe(false);
67- });
68-
69- test("multiple headings each rendered on own line", () => {
70- const out = strip(renderMarkdown("# H1\n## H2\n### H3"));
71- expect(out).toBe("H1\nH2\nH3");
72- });
73-});
74-
75-describe("renderMarkdown – horizontal rules", () => {
76- test("supported rule markers render a dim divider", () => {
77- for (const input of ["---", "===", "***", "----"]) {
78- const out = renderMarkdown(input);
79- expect(hasDim(out)).toBe(true);
80- expect(strip(out)).toMatch(/^─+$/);
81- }
82- });
83-
84- test("too-short markers are passed through as plain text", () => {
85- const out = strip(renderMarkdown("--"));
86- expect(out).toBe("--");
87- });
88-});
89-
90-describe("renderMarkdown – fenced code blocks", () => {
91- test("lines inside ``` fence are yellow", () => {
92- const out = renderMarkdown("```\ncode line\n```");
93- const lines = out.split("\n");
94- // line index 1 is "code line"
95- expect(hasYellow(lines[1] ?? "")).toBe(true);
96- });
97-
98- test("fence delimiter lines are dim", () => {
99- const out = renderMarkdown("```\ncode\n```");
100- const lines = out.split("\n");
101- expect(hasDim(lines[0] ?? "")).toBe(true);
102- expect(hasDim(lines[2] ?? "")).toBe(true);
103- });
104-
105- test("language hint on opening fence is preserved in stripped output", () => {
106- const out = strip(renderMarkdown("```ts\nconst x = 1;\n```"));
107- expect(out).toBe("```ts\nconst x = 1;\n```");
108- });
109-
110- test("inline markdown inside fenced block is NOT processed", () => {
111- const out = strip(renderMarkdown("```\n**not bold**\n```"));
112- expect(out).toContain("**not bold**");
113- });
114-
115- test("~~~ also opens and closes a fenced block", () => {
116- const out = renderMarkdown("~~~\ncode\n~~~");
117- const lines = out.split("\n");
118- expect(hasYellow(lines[1] ?? "")).toBe(true);
119- });
120-
121- test("two consecutive fenced blocks are both fenced correctly", () => {
122- const input = "```\nfirst\n```\n```\nsecond\n```";
123- const out = renderMarkdown(input);
124- const lines = out.split("\n");
125- // line 1 = "first" (inside first fence), line 4 = "second" (inside second fence)
126- expect(hasYellow(lines[1] ?? "")).toBe(true);
127- expect(hasYellow(lines[4] ?? "")).toBe(true);
128- // line 3 = "```" (opening second fence), should be dim not yellow
129- expect(hasDim(lines[3] ?? "")).toBe(true);
130- expect(hasYellow(lines[3] ?? "")).toBe(false);
131- });
132-
133- test("unclosed fence: lines after opening ``` are treated as code", () => {
134- const out = renderMarkdown("```\ncode\nnormal line");
135- const lines = out.split("\n");
136- expect(hasYellow(lines[1] ?? "")).toBe(true);
137- expect(hasYellow(lines[2] ?? "")).toBe(true);
138- });
139-});
140-
141-describe("renderMarkdown – blockquotes", () => {
142- test("> line renders with │ prefix and dim styling", () => {
143- const out = renderMarkdown("> quoted");
144- expect(strip(out)).toBe("│ quoted");
145- expect(hasDim(out)).toBe(true);
146- });
147-
148- test("> with no space also matches", () => {
149- const out = strip(renderMarkdown(">no space"));
150- expect(out).toBe("│ no space");
151- });
152-});
153-
154-describe("renderMarkdown – unordered lists", () => {
155- test("all supported unordered markers render the same bullet", () => {
156- for (const input of ["- item", "* item", "+ item"]) {
157- expect(strip(renderMarkdown(input))).toBe("· item");
158- }
159- });
160-
161- test("indented items preserve leading whitespace", () => {
162- const out = strip(renderMarkdown(" - nested"));
163- expect(out).toBe(" · nested");
164- });
165-
166- test("list item content still goes through inline processing", () => {
167- const out = strip(renderMarkdown("- **bold** item"));
168- expect(out).toBe("· bold item");
169- });
170-});
171-
172-describe("renderMarkdown – ordered lists", () => {
173- test("ordered list numbers stay visible while the marker is dimmed", () => {
174- const out = renderMarkdown("1. first");
175- expect(strip(out)).toBe("1. first");
176- expect(hasDim(out)).toBe(true);
177- });
178-
179- test("multi-item ordered lists preserve numbering across lines", () => {
180- const out = strip(renderMarkdown("1. alpha\n2. beta\n3. gamma"));
181- expect(out).toBe("1. alpha\n2. beta\n3. gamma");
182- });
183-
184- test("ordered list item content still goes through inline processing", () => {
185- const out = strip(renderMarkdown("1. **bold** text"));
186- expect(out).toBe("1. bold text");
187- });
188-});
189-
190-describe("renderMarkdown – plain text", () => {
191- test("plain text is passed through unchanged", () => {
192- const out = strip(renderMarkdown("hello world"));
193- expect(out).toBe("hello world");
194- });
195-
196- test("empty string produces empty string", () => {
197- expect(renderMarkdown("")).toBe("");
198- });
199-
200- test("empty line in multi-line input is preserved", () => {
201- const out = strip(renderMarkdown("line1\n\nline3"));
202- expect(out).toBe("line1\n\nline3");
203- });
204-});
205-
206-// ---------------------------------------------------------------------------
207-// renderMarkdown — inline spans (via plain text lines)
208-// ---------------------------------------------------------------------------
209-
210-describe("renderMarkdown – inline code", () => {
211- test("`code` renders yellow and preserves backticks in stripped text", () => {
212- const out = renderMarkdown("`code`");
213- expect(hasYellow(out)).toBe(true);
214- expect(strip(out)).toBe("`code`");
215- });
216-
217- test("unclosed backtick is passed through as plain text", () => {
218- const out = strip(renderMarkdown("hello `world"));
219- expect(out).toBe("hello `world");
220- });
221-
222- test("two separate inline code spans on one line both get highlighted", () => {
223- const out = renderMarkdown("`foo` and `bar`");
224- expect(strip(out)).toBe("`foo` and `bar`");
225- // Two yellow regions should appear
226- const yellowCount =
227- out.split(`${String.fromCharCode(0x1b)}[33m`).length - 1;
228- expect(yellowCount).toBe(2);
229- });
230-});
231-
232-describe("renderMarkdown – bold", () => {
233- test("**bold** renders bold styling and strips markers from plain text", () => {
234- const out = renderMarkdown("**bold**");
235- expect(hasBold(out)).toBe(true);
236- expect(strip(out)).toBe("bold");
237- });
238-
239- test("bold with surrounding text", () => {
240- const out = strip(renderMarkdown("before **bold** after"));
241- expect(out).toBe("before bold after");
242- });
243-
244- test("unclosed ** is passed through as plain text", () => {
245- const out = strip(renderMarkdown("**not closed"));
246- expect(out).toBe("**not closed");
247- });
248-});
249-
250-describe("renderMarkdown – italic", () => {
251- test("*italic* renders dim styling", () => {
252- const out = renderMarkdown("*italic*");
253- expect(hasDim(out)).toBe(true);
254- expect(strip(out)).toBe("italic");
255- });
256-
257- test("italic with surrounding text", () => {
258- const out = strip(renderMarkdown("before *italic* after"));
259- expect(out).toBe("before italic after");
260- });
261-
262- test("unclosed * is passed through as plain text", () => {
263- const out = strip(renderMarkdown("*not closed"));
264- expect(out).toBe("*not closed");
265- });
266-});
267-
268-describe("renderMarkdown – inline combinations", () => {
269- test("bold and italic on same line both render correctly", () => {
270- const out = strip(renderMarkdown("**bold** and *italic*"));
271- expect(out).toBe("bold and italic");
272- });
273-
274- test("inline code and bold on same line both render correctly", () => {
275- const out = strip(renderMarkdown("`code` and **bold**"));
276- expect(out).toBe("`code` and bold");
277- });
278-
279- test("heading with inline bold strips # and bold markers", () => {
280- const out = strip(renderMarkdown("## Section **key**"));
281- expect(out).toBe("Section key");
282- });
283-
284- test("italic immediately after bold (no space) renders both correctly", () => {
285- const out = strip(renderMarkdown("**bold***italic*"));
286- expect(out).toBe("bolditalic");
287- });
288-
289- test("bold immediately after italic (no space) renders both correctly", () => {
290- const out = strip(renderMarkdown("*italic***bold**"));
291- expect(out).toBe("italicbold");
292- });
293-
294- test("unclosed ** does not corrupt a following *italic* span", () => {
295- const out = strip(renderMarkdown("**foo *bar*"));
296- expect(out).toBe("**foo bar");
297- });
298-});