@@ -0,0 +1,185 @@
1+import { describe, expect, test } from "bun:test";
2+import { CellBuffer } from "./cell-buffer.js";
3+import { layout } from "./layout.js";
4+import { paint } from "./paint.js";
5+import { VStack, HStack } from "./primitives/stacks.js";
6+import { Text } from "./primitives/text.js";
7+
8+/** Read a row from the buffer as a string (trimming trailing spaces). */
9+function readRow(buf: CellBuffer, y: number): string {
10+ let row = "";
11+ for (let x = 0; x < buf.width; x++) {
12+ row += buf.get(x, y).char;
13+ }
14+ return row.trimEnd();
15+}
16+
17+/** Read all rows as strings. */
18+function readAll(buf: CellBuffer): string[] {
19+ const rows: string[] = [];
20+ for (let y = 0; y < buf.height; y++) {
21+ rows.push(readRow(buf, y));
22+ }
23+ return rows;
24+}
25+
26+describe("paint", () => {
27+ describe("Text rendering", () => {
28+ test("paints text at correct position", () => {
29+ const node = VStack({ width: 20, height: 3 }, [Text("Hello")]);
30+ const ln = layout(node, 20, 3);
31+ const buf = new CellBuffer(20, 3);
32+ paint(ln, buf);
33+ expect(readRow(buf, 0)).toBe("Hello");
34+ });
35+
36+ test("clips text at rect boundary", () => {
37+ const node = VStack({ width: 5, height: 1 }, [Text("Hello, world!")]);
38+ const ln = layout(node, 5, 1);
39+ const buf = new CellBuffer(5, 1);
40+ paint(ln, buf);
41+ expect(readRow(buf, 0)).toBe("Hello");
42+ });
43+
44+ test("paints multiline text from newlines", () => {
45+ const node = VStack({ width: 20, height: 5 }, [
46+ Text("line1\nline2\nline3"),
47+ ]);
48+ const ln = layout(node, 20, 5);
49+ const buf = new CellBuffer(20, 5);
50+ paint(ln, buf);
51+ expect(readRow(buf, 0)).toBe("line1");
52+ expect(readRow(buf, 1)).toBe("line2");
53+ expect(readRow(buf, 2)).toBe("line3");
54+ });
55+
56+ test("repeat fill fills the width", () => {
57+ const node = VStack({ width: 10, height: 1 }, [
58+ Text("-", { repeat: "fill" }),
59+ ]);
60+ const ln = layout(node, 10, 1);
61+ const buf = new CellBuffer(10, 1);
62+ paint(ln, buf);
63+ expect(readRow(buf, 0)).toBe("----------");
64+ });
65+
66+ test("repeat number repeats exact count", () => {
67+ const node = VStack({ width: 20, height: 1 }, [
68+ Text("ab", { repeat: 3 }),
69+ ]);
70+ const ln = layout(node, 20, 1);
71+ const buf = new CellBuffer(20, 1);
72+ paint(ln, buf);
73+ expect(readRow(buf, 0)).toBe("ababab");
74+ });
75+
76+ test("applies foreground color", () => {
77+ const node = VStack({ width: 10, height: 1 }, [
78+ Text("Hi", { fgColor: "red" }),
79+ ]);
80+ const ln = layout(node, 10, 1);
81+ const buf = new CellBuffer(10, 1);
82+ paint(ln, buf);
83+ expect(buf.get(0, 0).fgColor).toBe("red");
84+ expect(buf.get(1, 0).fgColor).toBe("red");
85+ });
86+
87+ test("applies background color", () => {
88+ const node = VStack({ width: 10, height: 1 }, [
89+ Text("Hi", { bgColor: "blue" }),
90+ ]);
91+ const ln = layout(node, 10, 1);
92+ const buf = new CellBuffer(10, 1);
93+ paint(ln, buf);
94+ expect(buf.get(0, 0).bgColor).toBe("blue");
95+ });
96+
97+ test("applies bold/italic/underline", () => {
98+ const node = VStack({ width: 10, height: 1 }, [
99+ Text("X", { bold: true, italic: true, underline: true }),
100+ ]);
101+ const ln = layout(node, 10, 1);
102+ const buf = new CellBuffer(10, 1);
103+ paint(ln, buf);
104+ const cell = buf.get(0, 0);
105+ expect(cell.bold).toBe(true);
106+ expect(cell.italic).toBe(true);
107+ expect(cell.underline).toBe(true);
108+ });
109+ });
110+
111+ describe("layout-driven positioning", () => {
112+ test("VStack children paint at correct vertical positions", () => {
113+ const node = VStack({ width: 20, height: 10 }, [
114+ Text("Header"),
115+ Text("Body"),
116+ ]);
117+ const ln = layout(node, 20, 10);
118+ const buf = new CellBuffer(20, 10);
119+ paint(ln, buf);
120+ expect(readRow(buf, 0)).toBe("Header");
121+ expect(readRow(buf, 1)).toBe("Body");
122+ });
123+
124+ test("HStack children paint at correct horizontal positions", () => {
125+ const node = HStack({ width: 20, height: 1 }, [
126+ VStack({ width: 8 }, [Text("Left")]),
127+ VStack({ width: 12 }, [Text("Right")]),
128+ ]);
129+ const ln = layout(node, 20, 1);
130+ const buf = new CellBuffer(20, 1);
131+ paint(ln, buf);
132+ expect(readRow(buf, 0)).toBe("Left Right");
133+ });
134+
135+ test("flex children fill space correctly", () => {
136+ const node = HStack({ width: 20, height: 1 }, [
137+ VStack({ width: 5 }, [Text("A")]),
138+ VStack({ flex: 1 }, [Text("B")]),
139+ ]);
140+ const ln = layout(node, 20, 1);
141+ const buf = new CellBuffer(20, 1);
142+ paint(ln, buf);
143+ expect(buf.get(0, 0).char).toBe("A");
144+ expect(buf.get(5, 0).char).toBe("B");
145+ });
146+
147+ test("padding offsets children", () => {
148+ const node = VStack({ width: 20, height: 5, padding: { x: 2, y: 1 } }, [
149+ Text("Hi"),
150+ ]);
151+ const ln = layout(node, 20, 5);
152+ const buf = new CellBuffer(20, 5);
153+ paint(ln, buf);
154+ // Row 0 should be empty (padding), text starts at row 1, col 2
155+ expect(readRow(buf, 0)).toBe("");
156+ expect(buf.get(2, 1).char).toBe("H");
157+ expect(buf.get(3, 1).char).toBe("i");
158+ });
159+ });
160+
161+ describe("nested layout", () => {
162+ test("editor-like layout paints correctly", () => {
163+ const node = HStack({ width: 30, height: 5 }, [
164+ VStack({ width: 10 }, [Text("sidebar")]),
165+ VStack({ flex: 1 }, [
166+ VStack({ height: 1 }, [Text("tab bar")]),
167+ VStack({ flex: 1 }, [Text("editor")]),
168+ VStack({ height: 1 }, [Text("status")]),
169+ ]),
170+ ]);
171+ const ln = layout(node, 30, 5);
172+ const buf = new CellBuffer(30, 5);
173+ paint(ln, buf);
174+
175+ // Sidebar at col 0
176+ expect(readRow(buf, 0).startsWith("sidebar")).toBe(true);
177+ // Tab bar at col 10, row 0
178+ expect(buf.get(10, 0).char).toBe("t");
179+ // Editor at col 10, row 1
180+ expect(buf.get(10, 1).char).toBe("e");
181+ // Status at col 10, row 4
182+ expect(buf.get(10, 4).char).toBe("s");
183+ });
184+ });
185+});