@@ -1,4 +1,4 @@
1-import { describe, test, expect } from "bun:test";
1+import { describe, expect, test } from "bun:test";
22 import type { ContainerNode, Node, TextNode } from "@cel-tui/types";
33 import { Markdown } from "./markdown";
44
@@ -22,6 +22,15 @@ function asContainer(node: Node): ContainerNode {
2222 return node as ContainerNode;
2323 }
2424
25+function item<T>(items: readonly T[], index: number): T {
26+ const value = items[index];
27+ expect(value).toBeDefined();
28+ if (value === undefined) {
29+ throw new Error(`Missing item at index ${index}`);
30+ }
31+ return value;
32+}
33+
2534 // ─── Markdown component ─────────────────────────────────────────
2635
2736 describe("Markdown", () => {
@@ -33,7 +42,7 @@ describe("Markdown", () => {
3342 test("heading renders as styled Text", () => {
3443 const nodes = Markdown("# Hello");
3544 expect(nodes).toHaveLength(1);
36- const t = asText(nodes[0]!);
45+ const t = asText(item(nodes, 0));
3746 expect(t.content).toBe("Hello");
3847 expect(t.props.bold).toBe(true);
3948 expect(t.props.fgColor).toBe("color06");
@@ -41,9 +50,9 @@ describe("Markdown", () => {
4150
4251 test("heading levels have different styles", () => {
4352 const nodes = Markdown("# H1\n## H2\n### H3");
44- const h1 = asText(nodes[0]!);
45- const h2 = asText(nodes[1]!);
46- const h3 = asText(nodes[2]!);
53+ const h1 = asText(item(nodes, 0));
54+ const h2 = asText(item(nodes, 1));
55+ const h3 = asText(item(nodes, 2));
4756 expect(h1.props.fgColor).toBe("color06");
4857 expect(h2.props.fgColor).toBe("color03");
4958 expect(h3.props.fgColor).toBe("color02");
@@ -52,7 +61,7 @@ describe("Markdown", () => {
5261 test("plain paragraph renders as wrapping Text", () => {
5362 const nodes = Markdown("Hello world");
5463 expect(nodes).toHaveLength(1);
55- const t = asText(nodes[0]!);
64+ const t = asText(item(nodes, 0));
5665 expect(t.content).toBe("Hello world");
5766 expect(t.props.wrap).toBe("word");
5867 });
@@ -60,47 +69,47 @@ describe("Markdown", () => {
6069 test("paragraph with bold renders as wrapping HStack", () => {
6170 const nodes = Markdown("Hello **bold** world");
6271 expect(nodes).toHaveLength(1);
63- const hstack = asContainer(nodes[0]!);
72+ const hstack = asContainer(item(nodes, 0));
6473 expect(hstack.type).toBe("hstack");
6574 expect(hstack.props.flexWrap).toBe("wrap");
6675 // Children: "Hello", " ", bold("bold"), " ", "world"
6776 expect(hstack.children).toHaveLength(5);
68- expect(asText(hstack.children[0]!).content).toBe("Hello");
69- expect(asText(hstack.children[1]!).content).toBe(" ");
70- const boldNode = asText(hstack.children[2]!);
77+ expect(asText(item(hstack.children, 0)).content).toBe("Hello");
78+ expect(asText(item(hstack.children, 1)).content).toBe(" ");
79+ const boldNode = asText(item(hstack.children, 2));
7180 expect(boldNode.content).toBe("bold");
7281 expect(boldNode.props.bold).toBe(true);
73- expect(asText(hstack.children[3]!).content).toBe(" ");
74- expect(asText(hstack.children[4]!).content).toBe("world");
82+ expect(asText(item(hstack.children, 3)).content).toBe(" ");
83+ expect(asText(item(hstack.children, 4)).content).toBe("world");
7584 });
7685
7786 test("paragraph with italic renders styled nodes", () => {
7887 const nodes = Markdown("some *italic* text");
79- const hstack = asContainer(nodes[0]!);
88+ const hstack = asContainer(item(nodes, 0));
8089 expect(hstack.type).toBe("hstack");
8190 // "some", " ", italic("italic"), " ", "text"
82- const italicNode = asText(hstack.children[2]!);
91+ const italicNode = asText(item(hstack.children, 2));
8392 expect(italicNode.content).toBe("italic");
8493 expect(italicNode.props.italic).toBe(true);
8594 });
8695
8796 test("paragraph with inline code keeps code span atomic", () => {
8897 const nodes = Markdown("use `foo bar` here");
89- const hstack = asContainer(nodes[0]!);
98+ const hstack = asContainer(item(nodes, 0));
9099 expect(hstack.type).toBe("hstack");
91100 // "use", " ", code("foo bar"), " ", "here"
92101 expect(hstack.children).toHaveLength(5);
93- const codeNode = asText(hstack.children[2]!);
102+ const codeNode = asText(item(hstack.children, 2));
94103 expect(codeNode.content).toBe("foo bar");
95104 expect(codeNode.props.fgColor).toBe("color03");
96105 });
97106
98107 test("paragraph with link keeps link text atomic", () => {
99108 const nodes = Markdown("see [the docs](https://example.com) here");
100- const hstack = asContainer(nodes[0]!);
109+ const hstack = asContainer(item(nodes, 0));
101110 // "see", " ", link("the docs"), " ", "here"
102111 expect(hstack.children).toHaveLength(5);
103- const linkNode = asText(hstack.children[2]!);
112+ const linkNode = asText(item(hstack.children, 2));
104113 expect(linkNode.content).toBe("the docs");
105114 expect(linkNode.props.fgColor).toBe("color06");
106115 expect(linkNode.props.underline).toBe(true);
@@ -108,69 +117,69 @@ describe("Markdown", () => {
108117
109118 test("paragraph with mixed formatting", () => {
110119 const nodes = Markdown("Hello, **bold**!");
111- const hstack = asContainer(nodes[0]!);
120+ const hstack = asContainer(item(nodes, 0));
112121 // "Hello,", " ", bold("bold"), "!"
113122 expect(hstack.children).toHaveLength(4);
114- expect(asText(hstack.children[0]!).content).toBe("Hello,");
115- expect(asText(hstack.children[1]!).content).toBe(" ");
116- expect(asText(hstack.children[2]!).props.bold).toBe(true);
123+ expect(asText(item(hstack.children, 0)).content).toBe("Hello,");
124+ expect(asText(item(hstack.children, 1)).content).toBe(" ");
125+ expect(asText(item(hstack.children, 2)).props.bold).toBe(true);
117126 // No space before "!" — punctuation is adjacent
118- expect(asText(hstack.children[3]!).content).toBe("!");
127+ expect(asText(item(hstack.children, 3)).content).toBe("!");
119128 });
120129
121130 test("code block renders as VStack with Text", () => {
122131 const nodes = Markdown("```js\nconst x = 1;\n```");
123132 expect(nodes).toHaveLength(1);
124- const container = asContainer(nodes[0]!);
133+ const container = asContainer(item(nodes, 0));
125134 expect(container.type).toBe("vstack");
126135 expect(container.props.padding).toEqual({ x: 1 });
127136 expect(container.props.bgColor).toBe("color08");
128137 expect(container.children).toHaveLength(1);
129- expect(textContent(container.children[0]!)).toBe("const x = 1;");
138+ expect(textContent(item(container.children, 0))).toBe("const x = 1;");
130139 });
131140
132141 test("unordered list item renders as HStack with marker", () => {
133142 const nodes = Markdown("- hello world");
134143 expect(nodes).toHaveLength(1);
135- const row = asContainer(nodes[0]!);
144+ const row = asContainer(item(nodes, 0));
136145 expect(row.type).toBe("hstack");
137146 expect(row.children).toHaveLength(2);
138147
139148 // Marker
140- const marker = asText(row.children[0]!);
149+ const marker = asText(item(row.children, 0));
141150 expect(marker.content).toBe("• ");
142151 expect(marker.props.fgColor).toBe("color03");
143152
144153 // Content in flex VStack
145- const wrapper = asContainer(row.children[1]!);
154+ const wrapper = asContainer(item(row.children, 1));
146155 expect(wrapper.type).toBe("vstack");
147156 expect(wrapper.props.flex).toBe(1);
148- const content = asText(wrapper.children[0]!);
157+ const content = asText(item(wrapper.children, 0));
149158 expect(content.content).toBe("hello world");
150159 expect(content.props.wrap).toBe("word");
151160 });
152161
153162 test("ordered list item uses index as marker", () => {
154163 const nodes = Markdown("3. third item");
155- const row = asContainer(nodes[0]!);
156- const marker = asText(row.children[0]!);
164+ const row = asContainer(item(nodes, 0));
165+ const marker = asText(item(row.children, 0));
157166 expect(marker.content).toBe("3. ");
158167 });
159168
160169 test("blockquote renders with bar and italic content", () => {
161170 const nodes = Markdown("> quoted text");
162171 expect(nodes).toHaveLength(1);
163- const row = asContainer(nodes[0]!);
172+ const row = asContainer(item(nodes, 0));
164173 expect(row.type).toBe("hstack");
165174
166175 // Bar
167- const bar = asText(row.children[0]!);
176+ const bar = asText(item(row.children, 0));
168177 expect(bar.content).toBe("│ ");
169178 expect(bar.props.fgColor).toBe("color02");
170179
171180 // Content
172- const wrapper = asContainer(row.children[1]!);
173- const content = asText(wrapper.children[0]!);
181+ const wrapper = asContainer(item(row.children, 1));
182+ const content = asText(item(wrapper.children, 0));
174183 expect(content.content).toBe("quoted text");
175184 expect(content.props.italic).toBe(true);
176185 expect(content.props.wrap).toBe("word");
@@ -179,7 +188,7 @@ describe("Markdown", () => {
179188 test("horizontal rule renders as Divider", () => {
180189 const nodes = Markdown("---");
181190 expect(nodes).toHaveLength(1);
182- const t = asText(nodes[0]!);
191+ const t = asText(item(nodes, 0));
183192 // Divider is a Text with repeat: "fill"
184193 expect(t.content).toBe("─");
185194 expect(t.props.repeat).toBe("fill");
@@ -189,14 +198,14 @@ describe("Markdown", () => {
189198 test("blank line renders as empty Text", () => {
190199 const nodes = Markdown("hello\n\nworld");
191200 expect(nodes).toHaveLength(3);
192- expect(textContent(nodes[1]!)).toBe("");
201+ expect(textContent(item(nodes, 1))).toBe("");
193202 });
194203
195204 test("custom theme overrides defaults", () => {
196205 const nodes = Markdown("# Hello", {
197206 theme: { heading1: { bold: true, fgColor: "color05" } },
198207 });
199- const t = asText(nodes[0]!);
208+ const t = asText(item(nodes, 0));
200209 expect(t.props.fgColor).toBe("color05");
201210 });
202211
@@ -205,82 +214,82 @@ describe("Markdown", () => {
205214 theme: { heading1: { fgColor: "color01" } },
206215 });
207216 // Heading uses custom
208- expect(asText(nodes[0]!).props.fgColor).toBe("color01");
217+ expect(asText(item(nodes, 0)).props.fgColor).toBe("color01");
209218 // Blockquote bar uses default
210- const row = asContainer(nodes[1]!);
211- expect(asText(row.children[0]!).props.fgColor).toBe("color02");
219+ const row = asContainer(item(nodes, 1));
220+ expect(asText(item(row.children, 0)).props.fgColor).toBe("color02");
212221 });
213222
214223 test("inline formatting in headings is stripped to plain text", () => {
215224 const nodes = Markdown("# Hello **world** and `code`");
216- const t = asText(nodes[0]!);
225+ const t = asText(item(nodes, 0));
217226 expect(t.content).toBe("Hello world and code");
218227 });
219228
220229 test("link text is used, url is stripped", () => {
221230 const nodes = Markdown("See [the docs](https://example.com) here.");
222- const hstack = asContainer(nodes[0]!);
231+ const hstack = asContainer(item(nodes, 0));
223232 expect(hstack.type).toBe("hstack");
224233 expect(hstack.props.flexWrap).toBe("wrap");
225234 // "See", " ", link("the docs"), " ", "here."
226235 expect(hstack.children).toHaveLength(5);
227- expect(asText(hstack.children[0]!).content).toBe("See");
228- const link = asText(hstack.children[2]!);
236+ expect(asText(item(hstack.children, 0)).content).toBe("See");
237+ const link = asText(item(hstack.children, 2));
229238 expect(link.content).toBe("the docs");
230239 expect(link.props.fgColor).toBe("color06");
231- expect(asText(hstack.children[4]!).content).toBe("here.");
240+ expect(asText(item(hstack.children, 4)).content).toBe("here.");
232241 });
233242
234243 test("streaming: unclosed code block renders as code", () => {
235244 const nodes = Markdown("```js\nconst x = 1;");
236245 expect(nodes).toHaveLength(1);
237- const container = asContainer(nodes[0]!);
246+ const container = asContainer(item(nodes, 0));
238247 expect(container.props.bgColor).toBe("color08");
239- expect(textContent(container.children[0]!)).toBe("const x = 1;");
248+ expect(textContent(item(container.children, 0))).toBe("const x = 1;");
240249 });
241250
242251 test("streaming: unclosed bold appears as plain text", () => {
243252 // Unclosed ** is not parsed as bold — stays as text span
244253 const nodes = Markdown("hello **world");
245- const t = asText(nodes[0]!);
254+ const t = asText(item(nodes, 0));
246255 expect(t.content).toBe("hello **world");
247256 expect(t.props.wrap).toBe("word");
248257 });
249258
250259 test("list item with inline formatting", () => {
251260 const nodes = Markdown("- hello **bold** world");
252- const row = asContainer(nodes[0]!);
261+ const row = asContainer(item(nodes, 0));
253262 expect(row.type).toBe("hstack");
254- const wrapper = asContainer(row.children[1]!);
263+ const wrapper = asContainer(item(row.children, 1));
255264 expect(wrapper.type).toBe("vstack");
256265 // Content is a wrapping HStack
257- const content = asContainer(wrapper.children[0]!);
266+ const content = asContainer(item(wrapper.children, 0));
258267 expect(content.type).toBe("hstack");
259268 expect(content.props.flexWrap).toBe("wrap");
260269 // "hello", " ", bold("bold"), " ", "world"
261270 expect(content.children).toHaveLength(5);
262- expect(asText(content.children[2]!).props.bold).toBe(true);
271+ expect(asText(item(content.children, 2)).props.bold).toBe(true);
263272 });
264273
265274 test("blockquote with inline formatting", () => {
266275 const nodes = Markdown("> hello **bold** text");
267- const row = asContainer(nodes[0]!);
268- const wrapper = asContainer(row.children[1]!);
269- const content = asContainer(wrapper.children[0]!);
276+ const row = asContainer(item(nodes, 0));
277+ const wrapper = asContainer(item(row.children, 1));
278+ const content = asContainer(item(wrapper.children, 0));
270279 expect(content.type).toBe("hstack");
271280 expect(content.props.flexWrap).toBe("wrap");
272281 // blockquoteText style (italic) applied to the wrapping HStack
273282 expect(content.props.italic).toBe(true);
274283 // bold node inside inherits italic from parent, also has bold
275- expect(asText(content.children[2]!).props.bold).toBe(true);
284+ expect(asText(item(content.children, 2)).props.bold).toBe(true);
276285 });
277286
278287 test("inline code theme is customizable", () => {
279288 const nodes = Markdown("use `code` here", {
280289 theme: { inlineCode: { fgColor: "color01", bgColor: "color00" } },
281290 });
282- const hstack = asContainer(nodes[0]!);
283- const codeNode = asText(hstack.children[2]!);
291+ const hstack = asContainer(item(nodes, 0));
292+ const codeNode = asText(item(hstack.children, 2));
284293 expect(codeNode.props.fgColor).toBe("color01");
285294 expect(codeNode.props.bgColor).toBe("color00");
286295 });