@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test";
22 import { layout, type LayoutNode, type Rect } from "./layout.js";
33 import { VStack, HStack } from "./primitives/stacks.js";
44 import { Text } from "./primitives/text.js";
5+import { TextInput } from "./primitives/text-input.js";
56
67 /** Helper to extract just the rect from a layout result. */
78 function rect(ln: LayoutNode): Rect {
@@ -1129,4 +1130,105 @@ describe("layout", () => {
11291130 });
11301131 });
11311132 });
1133+
1134+ describe("cross-axis constraints", () => {
1135+ test("maxHeight on child in HStack is respected", () => {
1136+ const node = HStack({ width: 40, height: 10 }, [
1137+ VStack({ flex: 1, maxHeight: 3 }, [Text("hello")]),
1138+ ]);
1139+ const result = layout(node, 80, 24);
1140+ expect(childRects(result)).toEqual([
1141+ { x: 0, y: 0, width: 40, height: 3 },
1142+ ]);
1143+ });
1144+
1145+ test("minHeight on child in HStack is respected", () => {
1146+ const node = HStack({ width: 40, height: 10, alignItems: "start" }, [
1147+ VStack({ flex: 1, minHeight: 5 }, [Text("hi")]),
1148+ ]);
1149+ const result = layout(node, 80, 24);
1150+ // Intrinsic height of VStack containing "hi" is 1, but minHeight is 5
1151+ expect(childRects(result)[0]!.height).toBe(5);
1152+ });
1153+
1154+ test("maxWidth on child in VStack is respected", () => {
1155+ const node = VStack({ width: 40, height: 10 }, [
1156+ HStack({ flex: 1, maxWidth: 20 }, [Text("x")]),
1157+ ]);
1158+ const result = layout(node, 80, 24);
1159+ expect(childRects(result)[0]!.width).toBe(20);
1160+ });
1161+
1162+ test("TextInput maxHeight in HStack creates autogrowing input", () => {
1163+ // Nest HStack in VStack so HStack gets intrinsic height
1164+ // Empty value โ height 1
1165+ const node1 = VStack({ width: 80, height: 24 }, [
1166+ HStack({}, [
1167+ TextInput({
1168+ flex: 1,
1169+ maxHeight: 5,
1170+ value: "",
1171+ onChange: () => {},
1172+ }),
1173+ ]),
1174+ Text("after"),
1175+ ]);
1176+ const r1 = layout(node1, 80, 24);
1177+ expect(r1.children[0]!.rect.height).toBe(1);
1178+ expect(r1.children[0]!.children[0]!.rect.height).toBe(1);
1179+
1180+ // Multi-line value โ grows to content height
1181+ const node2 = VStack({ width: 80, height: 24 }, [
1182+ HStack({}, [
1183+ TextInput({
1184+ flex: 1,
1185+ maxHeight: 5,
1186+ value: "line1\nline2\nline3",
1187+ onChange: () => {},
1188+ }),
1189+ ]),
1190+ Text("after"),
1191+ ]);
1192+ const r2 = layout(node2, 80, 24);
1193+ expect(r2.children[0]!.rect.height).toBe(3);
1194+ expect(r2.children[0]!.children[0]!.rect.height).toBe(3);
1195+
1196+ // Many lines โ capped at maxHeight
1197+ const node3 = VStack({ width: 80, height: 24 }, [
1198+ HStack({}, [
1199+ TextInput({
1200+ flex: 1,
1201+ maxHeight: 5,
1202+ value: "1\n2\n3\n4\n5\n6\n7\n8",
1203+ onChange: () => {},
1204+ }),
1205+ ]),
1206+ Text("after"),
1207+ ]);
1208+ const r3 = layout(node3, 80, 24);
1209+ expect(r3.children[0]!.rect.height).toBe(5);
1210+ expect(r3.children[0]!.children[0]!.rect.height).toBe(5);
1211+ });
1212+
1213+ test("cross-axis constraints in intrinsic container sizing", () => {
1214+ // HStack with no explicit height, child has maxHeight:
1215+ // HStack intrinsic height should respect child's maxHeight
1216+ const node = VStack({ width: 80, height: 24 }, [
1217+ HStack({}, [
1218+ TextInput({
1219+ flex: 1,
1220+ maxHeight: 3,
1221+ value: "1\n2\n3\n4\n5",
1222+ onChange: () => {},
1223+ }),
1224+ ]),
1225+ Text("after"),
1226+ ]);
1227+ const result = layout(node, 80, 24);
1228+ // HStack should be 3 tall (capped by TextInput's maxHeight)
1229+ expect(result.children[0]!.rect.height).toBe(3);
1230+ // Text "after" should be right after it
1231+ expect(result.children[1]!.rect.y).toBe(3);
1232+ });
1233+ });
11321234 });