70160
Ce
cel-tui / files
cel-tui / packages / core / src / hit-test.test.ts
12.0 KB
1import { describe, expect, test } from "bun:test"; 2import { 3 collectFocusable, 4 collectKeyPressHandlers, 5 collectScrollTargets, 6 findClickHandler, 7 findScrollTarget, 8 hitTest, 9} from "./hit-test.js"; 10import { type LayoutNode, layout } from "./layout.js"; 11import { HStack, VStack } from "./primitives/stacks.js"; 12import { Text } from "./primitives/text.js"; 13import { TextInput } from "./primitives/text-input.js"; 14 15function item<T>(items: readonly T[], index: number): T { 16 const value = items[index]; 17 expect(value).toBeDefined(); 18 if (value === undefined) { 19 throw new Error(`Missing item at index ${index}`); 20 } 21 return value; 22} 23 24function expectSome<T>(value: T | null | undefined): T { 25 expect(value).toBeDefined(); 26 expect(value).not.toBeNull(); 27 if (value == null) { 28 throw new Error("Expected value to be present"); 29 } 30 return value; 31} 32 33function last<T>(items: readonly T[]): T { 34 return item(items, items.length - 1); 35} 36 37function textContent(layoutNode: LayoutNode): string { 38 expect(layoutNode.node.type).toBe("text"); 39 if (layoutNode.node.type !== "text") { 40 throw new Error(`Expected text node, got ${layoutNode.node.type}`); 41 } 42 return layoutNode.node.content; 43} 44 45describe("hitTest", () => { 46 test("returns path to deepest node at position", () => { 47 const node = VStack({ width: 20, height: 10 }, [Text("hello")]); 48 const ln = layout(node, 20, 10); 49 const path = hitTest(ln, 5, 0); 50 expect(path.length).toBe(2); // root VStack + Text 51 expect(last(path).node.type).toBe("text"); 52 }); 53 54 test("returns root only when hitting empty area", () => { 55 const node = VStack({ width: 20, height: 10 }, [ 56 VStack({ height: 2 }, [Text("top")]), 57 ]); 58 const ln = layout(node, 20, 10); 59 // Hit below the child (y=5) 60 const path = hitTest(ln, 5, 5); 61 expect(path.length).toBe(1); 62 expect(item(path, 0).node.type).toBe("vstack"); 63 }); 64 65 test("returns empty for out-of-bounds", () => { 66 const node = VStack({ width: 20, height: 10 }, []); 67 const ln = layout(node, 20, 10); 68 const path = hitTest(ln, 25, 5); 69 expect(path.length).toBe(0); 70 }); 71 72 test("finds correct child in HStack", () => { 73 const node = HStack({ width: 20, height: 5 }, [ 74 VStack({ width: 10 }, [Text("left")]), 75 VStack({ width: 10 }, [Text("right")]), 76 ]); 77 const ln = layout(node, 20, 5); 78 79 const pathLeft = hitTest(ln, 3, 0); 80 expect(last(pathLeft).node.type).toBe("text"); 81 expect(textContent(last(pathLeft))).toBe("left"); 82 83 const pathRight = hitTest(ln, 13, 0); 84 expect(textContent(last(pathRight))).toBe("right"); 85 }); 86 87 test("finds deepest nested node", () => { 88 const node = VStack({ width: 30, height: 10 }, [ 89 HStack({ height: 5 }, [VStack({ width: 15 }, [Text("deep")])]), 90 ]); 91 const ln = layout(node, 30, 10); 92 const path = hitTest(ln, 3, 0); 93 expect(path.length).toBe(4); // root > HStack > VStack > Text 94 expect(textContent(last(path))).toBe("deep"); 95 }); 96 97 test("adjusts for vertical scroll offset", () => { 98 // VStack with 5 children of height 1 in a 3-row viewport 99 const node = VStack( 100 { width: 20, height: 3, overflow: "scroll", scrollOffset: 2 }, 101 [ 102 Text("line 0"), 103 Text("line 1"), 104 Text("line 2"), 105 Text("line 3"), 106 Text("line 4"), 107 ], 108 ); 109 const ln = layout(node, 20, 3); 110 111 // With scrollOffset=2, clicking at row 0 should hit "line 2" 112 // (the 3rd child, which is visually at the top) 113 const path = hitTest(ln, 3, 0); 114 expect(textContent(last(path))).toBe("line 2"); 115 116 // Row 2 should hit "line 4" 117 const path2 = hitTest(ln, 3, 2); 118 expect(textContent(last(path2))).toBe("line 4"); 119 }); 120 121 test("adjusts for horizontal scroll offset", () => { 122 const node = HStack( 123 { width: 10, height: 3, overflow: "scroll", scrollOffset: 5 }, 124 [ 125 VStack({ width: 5 }, [Text("a")]), 126 VStack({ width: 5 }, [Text("b")]), 127 VStack({ width: 5 }, [Text("c")]), 128 ], 129 ); 130 const ln = layout(node, 10, 3); 131 132 // With scrollOffset=5, clicking at col 0 should hit child "b" (starts at x=5 in layout) 133 const path = hitTest(ln, 0, 0); 134 expect(textContent(last(path))).toBe("b"); 135 }); 136 137 test("clamps vertical scroll offset before hit testing", () => { 138 const node = VStack( 139 { width: 20, height: 3, overflow: "scroll", scrollOffset: Infinity }, 140 [ 141 Text("line 0"), 142 Text("line 1"), 143 Text("line 2"), 144 Text("line 3"), 145 Text("line 4"), 146 ], 147 ); 148 const ln = layout(node, 20, 3); 149 150 const topPath = hitTest(ln, 3, 0); 151 expect(textContent(last(topPath))).toBe("line 2"); 152 153 const bottomPath = hitTest(ln, 3, 2); 154 expect(textContent(last(bottomPath))).toBe("line 4"); 155 }); 156 157 test("clamps horizontal over-max scroll offset before hit testing", () => { 158 const node = HStack( 159 { width: 10, height: 3, overflow: "scroll", scrollOffset: 999 }, 160 [ 161 VStack({ width: 5 }, [Text("a")]), 162 VStack({ width: 5 }, [Text("b")]), 163 VStack({ width: 5 }, [Text("c")]), 164 ], 165 ); 166 const ln = layout(node, 10, 3); 167 168 const path = hitTest(ln, 0, 0); 169 expect(textContent(last(path))).toBe("b"); 170 }); 171 172 test("no scroll adjustment without overflow scroll", () => { 173 const node = VStack({ width: 20, height: 3 }, [ 174 Text("line 0"), 175 Text("line 1"), 176 Text("line 2"), 177 ]); 178 const ln = layout(node, 20, 3); 179 const path = hitTest(ln, 3, 0); 180 expect(textContent(last(path))).toBe("line 0"); 181 }); 182}); 183 184describe("findClickHandler", () => { 185 test("finds nearest ancestor with onClick", () => { 186 const handler = () => {}; 187 const node = VStack({ width: 20, height: 10 }, [ 188 HStack({ onClick: handler }, [Text("clickable")]), 189 ]); 190 const ln = layout(node, 20, 10); 191 const path = hitTest(ln, 3, 0); 192 const result = findClickHandler(path); 193 expect(result).not.toBeNull(); 194 expect(expectSome(result).handler).toBe(handler); 195 }); 196 197 test("returns null when no onClick in path", () => { 198 const node = VStack({ width: 20, height: 10 }, [Text("plain")]); 199 const ln = layout(node, 20, 10); 200 const path = hitTest(ln, 3, 0); 201 expect(findClickHandler(path)).toBeNull(); 202 }); 203 204 test("innermost clickable wins", () => { 205 const outer = () => {}; 206 const inner = () => {}; 207 const node = VStack({ width: 20, height: 10, onClick: outer }, [ 208 HStack({ onClick: inner }, [Text("nested")]), 209 ]); 210 const ln = layout(node, 20, 10); 211 const path = hitTest(ln, 3, 0); 212 const result = findClickHandler(path); 213 expect(expectSome(result).handler).toBe(inner); 214 }); 215}); 216 217describe("findScrollTarget", () => { 218 test("finds nearest scrollable ancestor", () => { 219 const node = VStack({ width: 20, height: 10, overflow: "scroll" }, [ 220 Text("scrollable"), 221 ]); 222 const ln = layout(node, 20, 10); 223 const path = hitTest(ln, 3, 0); 224 const target = expectSome(findScrollTarget(path)); 225 expect(target.node.type).toBe("vstack"); 226 }); 227 228 test("returns null when no scrollable in path", () => { 229 const node = VStack({ width: 20, height: 10 }, [Text("plain")]); 230 const ln = layout(node, 20, 10); 231 const path = hitTest(ln, 3, 0); 232 expect(findScrollTarget(path)).toBeNull(); 233 }); 234 235 test("findScrollTarget returns the innermost scrollable", () => { 236 const node = VStack({ width: 20, height: 10, overflow: "scroll" }, [ 237 VStack({ height: 5, overflow: "scroll" }, [Text("inner")]), 238 ]); 239 const ln = layout(node, 20, 10); 240 const path = hitTest(ln, 3, 0); 241 const target = expectSome(findScrollTarget(path)); 242 // The inner VStack should win 243 expect(target.node.type).toBe("vstack"); 244 if (target.node.type === "vstack") { 245 expect(target.node.props.height).toBe(5); 246 } 247 }); 248}); 249 250describe("collectScrollTargets", () => { 251 test("collects scrollable ancestors from innermost to outermost", () => { 252 const node = VStack({ width: 20, height: 10, overflow: "scroll" }, [ 253 VStack({ height: 6 }, [ 254 VStack({ height: 4, overflow: "scroll" }, [Text("inner")]), 255 ]), 256 ]); 257 const ln = layout(node, 20, 10); 258 const path = hitTest(ln, 3, 0); 259 const targets = collectScrollTargets(path); 260 261 expect(targets).toHaveLength(2); 262 const inner = item(targets, 0); 263 const outer = item(targets, 1); 264 expect(inner.node.type).toBe("vstack"); 265 expect(outer.node.type).toBe("vstack"); 266 if (inner.node.type === "vstack") { 267 expect(inner.node.props.height).toBe(4); 268 } 269 if (outer.node.type === "vstack") { 270 expect(outer.node.props.height).toBe(10); 271 } 272 }); 273}); 274 275describe("collectKeyPressHandlers", () => { 276 test("collects handlers from deepest to root", () => { 277 const rootHandler = (_key: string) => {}; 278 const midHandler = (_key: string) => {}; 279 const node = VStack({ width: 20, height: 10, onKeyPress: rootHandler }, [ 280 VStack({ onKeyPress: midHandler }, [Text("hello")]), 281 ]); 282 const ln = layout(node, 20, 10); 283 const path = hitTest(ln, 3, 0); 284 const handlers = collectKeyPressHandlers(path); 285 expect(handlers).toHaveLength(2); 286 expect(item(handlers, 0).handler).toBe(midHandler); 287 expect(item(handlers, 1).handler).toBe(rootHandler); 288 }); 289 290 test("returns single handler when only root has onKeyPress", () => { 291 const handler = (_key: string) => {}; 292 const node = VStack({ width: 20, height: 10, onKeyPress: handler }, [ 293 Text("hello"), 294 ]); 295 const ln = layout(node, 20, 10); 296 const path = hitTest(ln, 3, 0); 297 const handlers = collectKeyPressHandlers(path); 298 expect(handlers).toHaveLength(1); 299 expect(item(handlers, 0).handler).toBe(handler); 300 }); 301 302 test("returns empty array when no onKeyPress in path", () => { 303 const node = VStack({ width: 20, height: 10 }, [Text("hello")]); 304 const ln = layout(node, 20, 10); 305 const path = hitTest(ln, 3, 0); 306 expect(collectKeyPressHandlers(path)).toEqual([]); 307 }); 308}); 309 310describe("collectFocusable", () => { 311 test("collects TextInput nodes", () => { 312 const node = VStack({ width: 20, height: 10 }, [ 313 Text("not focusable"), 314 TextInput({ value: "editable", onChange: () => {} }), 315 HStack({ onClick: () => {} }, [Text("button")]), 316 ]); 317 const ln = layout(node, 20, 10); 318 const focusable = collectFocusable(ln); 319 expect(focusable.length).toBe(2); 320 expect(focusable[0]?.node.type).toBe("textinput"); 321 }); 322 323 test("containers respect focusable: false", () => { 324 const node = VStack({ width: 20, height: 10 }, [ 325 HStack({ onClick: () => {}, focusable: false }, [ 326 Text("not in tab order"), 327 ]), 328 HStack({ onClick: () => {} }, [Text("in tab order")]), 329 ]); 330 const ln = layout(node, 20, 10); 331 const focusable = collectFocusable(ln); 332 expect(focusable.length).toBe(1); 333 }); 334 335 test("TextInput respects focusable: false", () => { 336 const node = VStack({ width: 20, height: 10 }, [ 337 TextInput({ 338 value: "display only", 339 onChange: () => {}, 340 focusable: false, 341 }), 342 TextInput({ value: "editable", onChange: () => {} }), 343 ]); 344 const ln = layout(node, 20, 10); 345 const focusable = collectFocusable(ln); 346 347 expect(focusable).toHaveLength(1); 348 expect(focusable[0]?.node.type).toBe("textinput"); 349 if (focusable[0]?.node.type === "textinput") { 350 expect(focusable[0].node.props.value).toBe("editable"); 351 } 352 }); 353 354 test("returns nodes in document order (depth-first)", () => { 355 const node = VStack({ width: 20, height: 10 }, [ 356 HStack({ onClick: () => {} }, [Text("first")]), 357 VStack({}, [HStack({ onClick: () => {} }, [Text("second")])]), 358 HStack({ onClick: () => {} }, [Text("third")]), 359 ]); 360 const ln = layout(node, 20, 10); 361 const focusable = collectFocusable(ln); 362 expect(focusable.length).toBe(3); 363 }); 364 365 test("explicit focusable: true without onClick is focusable", () => { 366 const node = VStack({ width: 20, height: 10 }, [ 367 HStack({ focusable: true }, [Text("keyboard-only")]), 368 HStack({ onClick: () => {} }, [Text("clickable")]), 369 ]); 370 const ln = layout(node, 20, 10); 371 const focusable = collectFocusable(ln); 372 expect(focusable.length).toBe(2); 373 }); 374});