@@ -0,0 +1,189 @@
1+import { describe, expect, test } from "bun:test";
2+import { layout } from "./layout.js";
3+import {
4+ hitTest,
5+ findClickHandler,
6+ findScrollTarget,
7+ findKeyPressHandler,
8+ collectFocusable,
9+} from "./hit-test.js";
10+import { VStack, HStack } from "./primitives/stacks.js";
11+import { Text } from "./primitives/text.js";
12+
13+describe("hitTest", () => {
14+ test("returns path to deepest node at position", () => {
15+ const node = VStack({ width: 20, height: 10 }, [Text("hello")]);
16+ const ln = layout(node, 20, 10);
17+ const path = hitTest(ln, 5, 0);
18+ expect(path.length).toBe(2); // root VStack + Text
19+ expect(path[path.length - 1]!.node.type).toBe("text");
20+ });
21+
22+ test("returns root only when hitting empty area", () => {
23+ const node = VStack({ width: 20, height: 10 }, [
24+ VStack({ height: 2 }, [Text("top")]),
25+ ]);
26+ const ln = layout(node, 20, 10);
27+ // Hit below the child (y=5)
28+ const path = hitTest(ln, 5, 5);
29+ expect(path.length).toBe(1);
30+ expect(path[0]!.node.type).toBe("vstack");
31+ });
32+
33+ test("returns empty for out-of-bounds", () => {
34+ const node = VStack({ width: 20, height: 10 }, []);
35+ const ln = layout(node, 20, 10);
36+ const path = hitTest(ln, 25, 5);
37+ expect(path.length).toBe(0);
38+ });
39+
40+ test("finds correct child in HStack", () => {
41+ const node = HStack({ width: 20, height: 5 }, [
42+ VStack({ width: 10 }, [Text("left")]),
43+ VStack({ width: 10 }, [Text("right")]),
44+ ]);
45+ const ln = layout(node, 20, 5);
46+
47+ const pathLeft = hitTest(ln, 3, 0);
48+ expect(pathLeft[pathLeft.length - 1]!.node.type).toBe("text");
49+ expect((pathLeft[pathLeft.length - 1]!.node as any).content).toBe("left");
50+
51+ const pathRight = hitTest(ln, 13, 0);
52+ expect((pathRight[pathRight.length - 1]!.node as any).content).toBe(
53+ "right",
54+ );
55+ });
56+
57+ test("finds deepest nested node", () => {
58+ const node = VStack({ width: 30, height: 10 }, [
59+ HStack({ height: 5 }, [VStack({ width: 15 }, [Text("deep")])]),
60+ ]);
61+ const ln = layout(node, 30, 10);
62+ const path = hitTest(ln, 3, 0);
63+ expect(path.length).toBe(4); // root > HStack > VStack > Text
64+ expect((path[path.length - 1]!.node as any).content).toBe("deep");
65+ });
66+});
67+
68+describe("findClickHandler", () => {
69+ test("finds nearest ancestor with onClick", () => {
70+ const handler = () => {};
71+ const node = VStack({ width: 20, height: 10 }, [
72+ HStack({ onClick: handler }, [Text("clickable")]),
73+ ]);
74+ const ln = layout(node, 20, 10);
75+ const path = hitTest(ln, 3, 0);
76+ const result = findClickHandler(path);
77+ expect(result).not.toBeNull();
78+ expect(result!.handler).toBe(handler);
79+ });
80+
81+ test("returns null when no onClick in path", () => {
82+ const node = VStack({ width: 20, height: 10 }, [Text("plain")]);
83+ const ln = layout(node, 20, 10);
84+ const path = hitTest(ln, 3, 0);
85+ expect(findClickHandler(path)).toBeNull();
86+ });
87+
88+ test("innermost clickable wins", () => {
89+ const outer = () => {};
90+ const inner = () => {};
91+ const node = VStack({ width: 20, height: 10, onClick: outer }, [
92+ HStack({ onClick: inner }, [Text("nested")]),
93+ ]);
94+ const ln = layout(node, 20, 10);
95+ const path = hitTest(ln, 3, 0);
96+ const result = findClickHandler(path);
97+ expect(result!.handler).toBe(inner);
98+ });
99+});
100+
101+describe("findScrollTarget", () => {
102+ test("finds nearest scrollable ancestor", () => {
103+ const node = VStack({ width: 20, height: 10, overflow: "scroll" }, [
104+ Text("scrollable"),
105+ ]);
106+ const ln = layout(node, 20, 10);
107+ const path = hitTest(ln, 3, 0);
108+ const target = findScrollTarget(path);
109+ expect(target).not.toBeNull();
110+ expect(target!.node.type).toBe("vstack");
111+ });
112+
113+ test("returns null when no scrollable in path", () => {
114+ const node = VStack({ width: 20, height: 10 }, [Text("plain")]);
115+ const ln = layout(node, 20, 10);
116+ const path = hitTest(ln, 3, 0);
117+ expect(findScrollTarget(path)).toBeNull();
118+ });
119+
120+ test("innermost scrollable wins", () => {
121+ const node = VStack({ width: 20, height: 10, overflow: "scroll" }, [
122+ VStack({ height: 5, overflow: "scroll" }, [Text("inner")]),
123+ ]);
124+ const ln = layout(node, 20, 10);
125+ const path = hitTest(ln, 3, 0);
126+ const target = findScrollTarget(path);
127+ // The inner VStack should win
128+ const innerNode = target!.node as any;
129+ expect(innerNode.props.height).toBe(5);
130+ });
131+});
132+
133+describe("findKeyPressHandler", () => {
134+ test("finds nearest ancestor with onKeyPress", () => {
135+ const handler = (_key: string) => {};
136+ const node = VStack({ width: 20, height: 10, onKeyPress: handler }, [
137+ Text("hello"),
138+ ]);
139+ const ln = layout(node, 20, 10);
140+ const path = hitTest(ln, 3, 0);
141+ const result = findKeyPressHandler(path);
142+ expect(result).not.toBeNull();
143+ expect(result!.handler).toBe(handler);
144+ });
145+
146+ test("returns null when no onKeyPress in path", () => {
147+ const node = VStack({ width: 20, height: 10 }, [Text("hello")]);
148+ const ln = layout(node, 20, 10);
149+ const path = hitTest(ln, 3, 0);
150+ expect(findKeyPressHandler(path)).toBeNull();
151+ });
152+});
153+
154+describe("collectFocusable", () => {
155+ test("collects TextInput nodes", () => {
156+ // TextInput is always focusable — use a container with onClick as proxy
157+ const node = VStack({ width: 20, height: 10 }, [
158+ Text("not focusable"),
159+ HStack({ onClick: () => {} }, [Text("button 1")]),
160+ HStack({ onClick: () => {} }, [Text("button 2")]),
161+ ]);
162+ const ln = layout(node, 20, 10);
163+ const focusable = collectFocusable(ln);
164+ expect(focusable.length).toBe(2);
165+ });
166+
167+ test("respects focusable: false", () => {
168+ const node = VStack({ width: 20, height: 10 }, [
169+ HStack({ onClick: () => {}, focusable: false }, [
170+ Text("not in tab order"),
171+ ]),
172+ HStack({ onClick: () => {} }, [Text("in tab order")]),
173+ ]);
174+ const ln = layout(node, 20, 10);
175+ const focusable = collectFocusable(ln);
176+ expect(focusable.length).toBe(1);
177+ });
178+
179+ test("returns nodes in document order (depth-first)", () => {
180+ const node = VStack({ width: 20, height: 10 }, [
181+ HStack({ onClick: () => {} }, [Text("first")]),
182+ VStack({}, [HStack({ onClick: () => {} }, [Text("second")])]),
183+ HStack({ onClick: () => {} }, [Text("third")]),
184+ ]);
185+ const ln = layout(node, 20, 10);
186+ const focusable = collectFocusable(ln);
187+ expect(focusable.length).toBe(3);
188+ });
189+});