@@ -0,0 +1,321 @@
1+import { describe, expect, test } from "bun:test";
2+import { layout, type LayoutNode, type Rect } from "./layout.js";
3+import { VStack, HStack } from "./primitives/stacks.js";
4+import { Text } from "./primitives/text.js";
5+
6+/** Helper to extract just the rect from a layout result. */
7+function rect(ln: LayoutNode): Rect {
8+ return ln.rect;
9+}
10+
11+/** Helper to get child rects. */
12+function childRects(ln: LayoutNode): Rect[] {
13+ return ln.children.map((c) => c.rect);
14+}
15+
16+describe("layout", () => {
17+ describe("fixed sizing", () => {
18+ test("container with fixed width and height", () => {
19+ const node = VStack({ width: 30, height: 10 }, []);
20+ const result = layout(node, 80, 24);
21+ expect(rect(result)).toEqual({ x: 0, y: 0, width: 30, height: 10 });
22+ });
23+
24+ test("child with fixed size inside a container", () => {
25+ const node = VStack({ width: 80, height: 24 }, [
26+ VStack({ width: 20, height: 5 }, []),
27+ ]);
28+ const result = layout(node, 80, 24);
29+ expect(childRects(result)).toEqual([
30+ { x: 0, y: 0, width: 20, height: 5 },
31+ ]);
32+ });
33+
34+ test("root without sizing fills available space", () => {
35+ const node = VStack({}, []);
36+ const result = layout(node, 80, 24);
37+ expect(rect(result)).toEqual({ x: 0, y: 0, width: 80, height: 24 });
38+ });
39+ });
40+
41+ describe("intrinsic sizing", () => {
42+ test("Text height is 1 for single line", () => {
43+ const node = VStack({ width: 80, height: 24 }, [Text("hello")]);
44+ const result = layout(node, 80, 24);
45+ expect(childRects(result)[0]!.height).toBe(1);
46+ });
47+
48+ test("Text height follows newlines", () => {
49+ const node = VStack({ width: 80, height: 24 }, [
50+ Text("line1\nline2\nline3"),
51+ ]);
52+ const result = layout(node, 80, 24);
53+ expect(childRects(result)[0]!.height).toBe(3);
54+ });
55+
56+ test("Text width fills parent in VStack", () => {
57+ const node = VStack({ width: 40, height: 10 }, [Text("hi")]);
58+ const result = layout(node, 80, 24);
59+ expect(childRects(result)[0]!.width).toBe(40);
60+ });
61+
62+ test("VStack without height sizes to children", () => {
63+ const node = VStack({ width: 80 }, [
64+ VStack({ height: 3 }, []),
65+ VStack({ height: 5 }, []),
66+ ]);
67+ const result = layout(node, 80, 24);
68+ expect(rect(result).height).toBe(8);
69+ });
70+
71+ test("HStack without width sizes to children", () => {
72+ const node = HStack({ height: 10 }, [
73+ VStack({ width: 15 }, []),
74+ VStack({ width: 25 }, []),
75+ ]);
76+ const result = layout(node, 80, 24);
77+ expect(rect(result).width).toBe(40);
78+ });
79+ });
80+
81+ describe("flex sizing", () => {
82+ test("single flex child fills remaining space in VStack", () => {
83+ const node = VStack({ width: 80, height: 24 }, [
84+ VStack({ height: 4 }, []),
85+ VStack({ flex: 1 }, []),
86+ ]);
87+ const result = layout(node, 80, 24);
88+ expect(childRects(result)).toEqual([
89+ { x: 0, y: 0, width: 80, height: 4 },
90+ { x: 0, y: 4, width: 80, height: 20 },
91+ ]);
92+ });
93+
94+ test("multiple flex children share space proportionally", () => {
95+ const node = VStack({ width: 80, height: 24 }, [
96+ VStack({ flex: 1 }, []),
97+ VStack({ flex: 2 }, []),
98+ ]);
99+ const result = layout(node, 80, 24);
100+ expect(childRects(result)).toEqual([
101+ { x: 0, y: 0, width: 80, height: 8 },
102+ { x: 0, y: 8, width: 80, height: 16 },
103+ ]);
104+ });
105+
106+ test("flex in HStack divides width", () => {
107+ const node = HStack({ width: 80, height: 24 }, [
108+ VStack({ flex: 1 }, []),
109+ VStack({ flex: 3 }, []),
110+ ]);
111+ const result = layout(node, 80, 24);
112+ expect(childRects(result)).toEqual([
113+ { x: 0, y: 0, width: 20, height: 24 },
114+ { x: 20, y: 0, width: 60, height: 24 },
115+ ]);
116+ });
117+
118+ test("fixed + flex children", () => {
119+ const node = VStack({ width: 80, height: 24 }, [
120+ VStack({ height: 1 }, []),
121+ VStack({ flex: 1 }, []),
122+ VStack({ height: 1 }, []),
123+ ]);
124+ const result = layout(node, 80, 24);
125+ expect(childRects(result)).toEqual([
126+ { x: 0, y: 0, width: 80, height: 1 },
127+ { x: 0, y: 1, width: 80, height: 22 },
128+ { x: 0, y: 23, width: 80, height: 1 },
129+ ]);
130+ });
131+ });
132+
133+ describe("percentage sizing", () => {
134+ test("percentage width relative to parent", () => {
135+ const node = VStack({ width: 80, height: 24 }, [
136+ VStack({ width: "50%", height: 10 }, []),
137+ ]);
138+ const result = layout(node, 80, 24);
139+ expect(childRects(result)[0]!.width).toBe(40);
140+ });
141+
142+ test("percentage height relative to parent", () => {
143+ const node = VStack({ width: 80, height: 24 }, [
144+ VStack({ width: 80, height: "50%" }, []),
145+ ]);
146+ const result = layout(node, 80, 24);
147+ expect(childRects(result)[0]!.height).toBe(12);
148+ });
149+ });
150+
151+ describe("rounding (largest remainder)", () => {
152+ test("distributes remainder to children with largest fractional parts", () => {
153+ // 80 / 3 = 26.667 each → 27, 27, 26
154+ const node = HStack({ width: 80, height: 24 }, [
155+ VStack({ flex: 1 }, []),
156+ VStack({ flex: 1 }, []),
157+ VStack({ flex: 1 }, []),
158+ ]);
159+ const result = layout(node, 80, 24);
160+ const widths = childRects(result).map((r) => r.width);
161+ expect(widths).toEqual([27, 27, 26]);
162+ expect(widths.reduce((a, b) => a + b)).toBe(80);
163+ });
164+ });
165+
166+ describe("constraints", () => {
167+ test("minWidth prevents shrinking below minimum", () => {
168+ const node = HStack({ width: 80, height: 24 }, [
169+ VStack({ flex: 1, minWidth: 30 }, []),
170+ VStack({ flex: 1 }, []),
171+ ]);
172+ const result = layout(node, 80, 24);
173+ expect(childRects(result)[0]!.width).toBeGreaterThanOrEqual(30);
174+ });
175+
176+ test("maxHeight caps growth", () => {
177+ const node = VStack({ width: 80, height: 24 }, [
178+ VStack({ flex: 1, maxHeight: 10 }, []),
179+ VStack({ flex: 1 }, []),
180+ ]);
181+ const result = layout(node, 80, 24);
182+ expect(childRects(result)[0]!.height).toBeLessThanOrEqual(10);
183+ });
184+ });
185+
186+ describe("gap", () => {
187+ test("VStack gap adds spacing between children", () => {
188+ const node = VStack({ width: 80, height: 24, gap: 1 }, [
189+ VStack({ height: 3 }, []),
190+ VStack({ height: 3 }, []),
191+ VStack({ height: 3 }, []),
192+ ]);
193+ const result = layout(node, 80, 24);
194+ const rects = childRects(result);
195+ expect(rects[0]).toEqual({ x: 0, y: 0, width: 80, height: 3 });
196+ expect(rects[1]).toEqual({ x: 0, y: 4, width: 80, height: 3 });
197+ expect(rects[2]).toEqual({ x: 0, y: 8, width: 80, height: 3 });
198+ });
199+
200+ test("HStack gap adds spacing between children", () => {
201+ const node = HStack({ width: 80, height: 24, gap: 2 }, [
202+ VStack({ width: 20 }, []),
203+ VStack({ width: 20 }, []),
204+ ]);
205+ const result = layout(node, 80, 24);
206+ const rects = childRects(result);
207+ expect(rects[0]).toEqual({ x: 0, y: 0, width: 20, height: 24 });
208+ expect(rects[1]).toEqual({ x: 22, y: 0, width: 20, height: 24 });
209+ });
210+
211+ test("gap is subtracted before flex distribution", () => {
212+ const node = VStack({ width: 80, height: 24, gap: 1 }, [
213+ VStack({ height: 4 }, []),
214+ VStack({ flex: 1 }, []),
215+ ]);
216+ const result = layout(node, 80, 24);
217+ const rects = childRects(result);
218+ // 24 - 4 (fixed) - 1 (gap) = 19 for flex
219+ expect(rects[0]).toEqual({ x: 0, y: 0, width: 80, height: 4 });
220+ expect(rects[1]).toEqual({ x: 0, y: 5, width: 80, height: 19 });
221+ });
222+ });
223+
224+ describe("padding", () => {
225+ test("padding reduces available space for children", () => {
226+ const node = VStack({ width: 80, height: 24, padding: { x: 2, y: 1 } }, [
227+ VStack({ flex: 1 }, []),
228+ ]);
229+ const result = layout(node, 80, 24);
230+ // Children start at (2, 1) with reduced size
231+ expect(childRects(result)[0]).toEqual({
232+ x: 2,
233+ y: 1,
234+ width: 76,
235+ height: 22,
236+ });
237+ });
238+
239+ test("padding with gap", () => {
240+ const node = VStack(
241+ { width: 80, height: 24, padding: { x: 1 }, gap: 1 },
242+ [VStack({ height: 3 }, []), VStack({ height: 3 }, [])],
243+ );
244+ const result = layout(node, 80, 24);
245+ const rects = childRects(result);
246+ expect(rects[0]).toEqual({ x: 1, y: 0, width: 78, height: 3 });
247+ expect(rects[1]).toEqual({ x: 1, y: 4, width: 78, height: 3 });
248+ });
249+ });
250+
251+ describe("VStack child positioning", () => {
252+ test("children stack top to bottom", () => {
253+ const node = VStack({ width: 80, height: 24 }, [
254+ VStack({ height: 5 }, []),
255+ VStack({ height: 3 }, []),
256+ VStack({ height: 7 }, []),
257+ ]);
258+ const result = layout(node, 80, 24);
259+ const rects = childRects(result);
260+ expect(rects[0]!.y).toBe(0);
261+ expect(rects[1]!.y).toBe(5);
262+ expect(rects[2]!.y).toBe(8);
263+ });
264+ });
265+
266+ describe("HStack child positioning", () => {
267+ test("children stack left to right", () => {
268+ const node = HStack({ width: 80, height: 24 }, [
269+ VStack({ width: 20 }, []),
270+ VStack({ width: 15 }, []),
271+ VStack({ width: 30 }, []),
272+ ]);
273+ const result = layout(node, 80, 24);
274+ const rects = childRects(result);
275+ expect(rects[0]!.x).toBe(0);
276+ expect(rects[1]!.x).toBe(20);
277+ expect(rects[2]!.x).toBe(35);
278+ });
279+ });
280+
281+ describe("nested layout", () => {
282+ test("editor-like layout: sidebar + main area", () => {
283+ const node = HStack({ width: 80, height: 24 }, [
284+ VStack({ width: 20 }, []),
285+ VStack({ flex: 1 }, [
286+ VStack({ height: 1 }, []),
287+ VStack({ flex: 1 }, []),
288+ VStack({ height: 1 }, []),
289+ ]),
290+ ]);
291+ const result = layout(node, 80, 24);
292+ const rects = childRects(result);
293+
294+ // Sidebar
295+ expect(rects[0]).toEqual({ x: 0, y: 0, width: 20, height: 24 });
296+ // Main area
297+ expect(rects[1]).toEqual({ x: 20, y: 0, width: 60, height: 24 });
298+
299+ // Main area children
300+ const mainChildren = result.children[1]!.children;
301+ expect(mainChildren[0]!.rect).toEqual({
302+ x: 20,
303+ y: 0,
304+ width: 60,
305+ height: 1,
306+ });
307+ expect(mainChildren[1]!.rect).toEqual({
308+ x: 20,
309+ y: 1,
310+ width: 60,
311+ height: 22,
312+ });
313+ expect(mainChildren[2]!.rect).toEqual({
314+ x: 20,
315+ y: 23,
316+ width: 60,
317+ height: 1,
318+ });
319+ });
320+ });
321+});