|
| 1 | +/** @jsxImportSource @opentui/solid */ |
| 2 | +import { expect, test } from "bun:test" |
| 3 | +import type { TextareaRenderable } from "@opentui/core" |
| 4 | +import { ManualClock } from "@opentui/core/testing" |
| 5 | +import { testRender } from "@opentui/solid" |
| 6 | +import { createPointerSelection, MULTI_CLICK_INTERVAL_MS } from "../../src/component/prompt/pointer-selection" |
| 7 | + |
| 8 | +async function fixture(enabled = true) { |
| 9 | + const clock = new ManualClock() |
| 10 | + const clicks: number[] = [] |
| 11 | + const refs: { input?: TextareaRenderable } = {} |
| 12 | + const app = await testRender( |
| 13 | + () => ( |
| 14 | + <textarea |
| 15 | + width={30} |
| 16 | + height={3} |
| 17 | + focused |
| 18 | + initialValue="hello world again" |
| 19 | + ref={(input: TextareaRenderable) => { |
| 20 | + refs.input = input |
| 21 | + }} |
| 22 | + /> |
| 23 | + ), |
| 24 | + { width: 40, height: 5, clock, kittyKeyboard: true }, |
| 25 | + ) |
| 26 | + await app.renderOnce() |
| 27 | + const input = refs.input! |
| 28 | + const pointer = createPointerSelection(input, { |
| 29 | + clock, |
| 30 | + enabled: () => enabled, |
| 31 | + onClick: () => clicks.push(input.cursorOffset), |
| 32 | + }) |
| 33 | + input.cursorOffset = 0 |
| 34 | + return { app, clock, input, pointer, clicks, [Symbol.dispose]: () => app.renderer.destroy() } |
| 35 | +} |
| 36 | + |
| 37 | +test("a mouse drag selects text without moving the cursor", async () => { |
| 38 | + using setup = await fixture() |
| 39 | + |
| 40 | + await setup.app.mockMouse.drag(6, 0, 10, 0) |
| 41 | + |
| 42 | + expect(setup.input.getSelectedText()).toBe("world") |
| 43 | + expect(setup.pointer.selected()).toBe(true) |
| 44 | + expect(setup.input.cursorOffset).toBe(0) |
| 45 | + setup.clock.advance(MULTI_CLICK_INTERVAL_MS + 1) |
| 46 | + expect(setup.input.cursorOffset).toBe(0) |
| 47 | + expect(setup.clicks).toEqual([]) |
| 48 | +}) |
| 49 | + |
| 50 | +test("a single click places the cursor once it can no longer become a double click", async () => { |
| 51 | + using setup = await fixture() |
| 52 | + |
| 53 | + await setup.app.mockMouse.click(6, 0) |
| 54 | + expect(setup.input.cursorOffset).toBe(0) |
| 55 | + setup.clock.advance(MULTI_CLICK_INTERVAL_MS - 1) |
| 56 | + expect(setup.input.cursorOffset).toBe(0) |
| 57 | + setup.clock.advance(1) |
| 58 | + |
| 59 | + expect(setup.input.cursorOffset).toBe(6) |
| 60 | + expect(setup.clicks).toEqual([6]) |
| 61 | + expect(setup.pointer.selected()).toBe(false) |
| 62 | +}) |
| 63 | + |
| 64 | +test("a double click selects a word and never places the cursor", async () => { |
| 65 | + using setup = await fixture() |
| 66 | + |
| 67 | + await setup.app.mockMouse.click(7, 0) |
| 68 | + await setup.app.mockMouse.click(7, 0) |
| 69 | + setup.clock.advance(MULTI_CLICK_INTERVAL_MS + 1) |
| 70 | + |
| 71 | + expect(setup.input.getSelectedText()).toBe("world") |
| 72 | + expect(setup.pointer.selected()).toBe(true) |
| 73 | + expect(setup.input.cursorOffset).toBe(0) |
| 74 | + expect(setup.clicks).toEqual([]) |
| 75 | +}) |
| 76 | + |
| 77 | +test("flush places a pending click at once", async () => { |
| 78 | + using setup = await fixture() |
| 79 | + |
| 80 | + await setup.app.mockMouse.click(12, 0) |
| 81 | + setup.pointer.flush() |
| 82 | + |
| 83 | + expect(setup.input.cursorOffset).toBe(12) |
| 84 | + expect(setup.clicks).toEqual([12]) |
| 85 | + setup.clock.advance(MULTI_CLICK_INTERVAL_MS + 1) |
| 86 | + expect(setup.clicks).toEqual([12]) |
| 87 | +}) |
| 88 | + |
| 89 | +test("keyboard selection still moves the cursor", async () => { |
| 90 | + using setup = await fixture() |
| 91 | + |
| 92 | + setup.app.mockInput.pressArrow("right", { shift: true }) |
| 93 | + setup.app.mockInput.pressArrow("right", { shift: true }) |
| 94 | + |
| 95 | + expect(setup.input.hasSelection()).toBe(true) |
| 96 | + expect(setup.input.cursorOffset).toBe(2) |
| 97 | + expect(setup.pointer.selected()).toBe(false) |
| 98 | +}) |
| 99 | + |
| 100 | +test("the mouse moves the cursor as before when disabled", async () => { |
| 101 | + using setup = await fixture(false) |
| 102 | + |
| 103 | + await setup.app.mockMouse.click(6, 0) |
| 104 | + expect(setup.input.cursorOffset).toBe(6) |
| 105 | + |
| 106 | + setup.app.renderer.clearSelection() |
| 107 | + await setup.app.mockMouse.drag(12, 0, 16, 0) |
| 108 | + expect(setup.input.getSelectedText()).toBe("again") |
| 109 | + expect(setup.input.cursorOffset).toBe(16) |
| 110 | + expect(setup.pointer.selected()).toBe(false) |
| 111 | + setup.clock.advance(MULTI_CLICK_INTERVAL_MS + 1) |
| 112 | + expect(setup.clicks).toEqual([]) |
| 113 | +}) |
0 commit comments