|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { |
| 3 | + DISPOSED_PANE_DRAG_STATE, |
| 4 | + INITIAL_PANE_DRAG_STATE, |
| 5 | + selectDraggingKey, |
| 6 | + selectDragLayer, |
| 7 | + selectMoveKey, |
| 8 | + transitionPaneDrag, |
| 9 | + type PaneDragEvent, |
| 10 | + type PaneDragState, |
| 11 | + type PaneDropTarget, |
| 12 | +} from "./pane-drag-machine" |
| 13 | + |
| 14 | +const strip: PaneDropTarget = { |
| 15 | + pane: "side", |
| 16 | + before: "file://b.ts", |
| 17 | + strip: true, |
| 18 | + rect: { left: 10, top: 2, width: 2, height: 20 }, |
| 19 | +} |
| 20 | + |
| 21 | +const bottom: PaneDropTarget = { |
| 22 | + pane: "bottom", |
| 23 | + strip: false, |
| 24 | + rect: { left: 0, top: 400, width: 800, height: 200 }, |
| 25 | +} |
| 26 | + |
| 27 | +const down = (pointerId = 1, key = "file://a.ts", button = 0): PaneDragEvent => ({ |
| 28 | + type: "pointer.down", |
| 29 | + pointerId, |
| 30 | + button, |
| 31 | + x: 0, |
| 32 | + y: 0, |
| 33 | + key, |
| 34 | + title: key.slice(7), |
| 35 | +}) |
| 36 | + |
| 37 | +const move = (x: number, y: number, target: PaneDropTarget | undefined, pointerId = 1): PaneDragEvent => ({ |
| 38 | + type: "pointer.move", |
| 39 | + pointerId, |
| 40 | + x, |
| 41 | + y, |
| 42 | + target, |
| 43 | +}) |
| 44 | + |
| 45 | +function replay(events: PaneDragEvent[], state: PaneDragState = INITIAL_PANE_DRAG_STATE) { |
| 46 | + return events.reduce<{ state: PaneDragState; effects: unknown[] }>( |
| 47 | + (current, event) => { |
| 48 | + const next = transitionPaneDrag(current.state, event) |
| 49 | + |
| 50 | + return { state: next.state, effects: [...current.effects, ...next.effects] } |
| 51 | + }, |
| 52 | + { state, effects: [] }, |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +const pressedState = replay([down()]).state |
| 57 | + |
| 58 | +const draggingState = replay([down(), move(4, 0, strip)]).state |
| 59 | + |
| 60 | +describe("disposal", () => { |
| 61 | + test("mid-drag releases the gesture and unlocks the body without dropping", () => { |
| 62 | + const next = transitionPaneDrag(draggingState, { type: "disposed" }) |
| 63 | + |
| 64 | + expect(next.state).toBe(DISPOSED_PANE_DRAG_STATE) |
| 65 | + expect(next.effects).toEqual([{ type: "gesture.cleanup", unlockBody: true }, { type: "click-suppression.clear" }]) |
| 66 | + |
| 67 | + const late = replay([move(40, 0, bottom), { type: "pointer.up", pointerId: 1 }, down(2)], next.state) |
| 68 | + |
| 69 | + expect(late.state).toBe(DISPOSED_PANE_DRAG_STATE) |
| 70 | + expect(late.effects).toEqual([]) |
| 71 | + }) |
| 72 | + |
| 73 | + test("mid-press releases the gesture, and a later move starts no drag", () => { |
| 74 | + const next = transitionPaneDrag(pressedState, { type: "disposed" }) |
| 75 | + |
| 76 | + expect(next.effects).toEqual([{ type: "gesture.cleanup", unlockBody: false }, { type: "click-suppression.clear" }]) |
| 77 | + expect(replay([move(40, 0, strip)], next.state).state).toBe(DISPOSED_PANE_DRAG_STATE) |
| 78 | + }) |
| 79 | + |
| 80 | + test("after a release clears the click swallower the release armed", () => { |
| 81 | + const released = transitionPaneDrag(draggingState, { type: "pointer.up", pointerId: 1 }) |
| 82 | + |
| 83 | + expect(released.effects).toContainEqual({ type: "click-suppression.arm" }) |
| 84 | + expect(transitionPaneDrag(released.state, { type: "disposed" }).effects).toEqual([ |
| 85 | + { type: "click-suppression.clear" }, |
| 86 | + ]) |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +describe("a second pointer", () => { |
| 91 | + test("neither starts a gesture nor moves or ends the first one", () => { |
| 92 | + const pressedSecond = transitionPaneDrag(pressedState, down(2, "file://b.ts")) |
| 93 | + |
| 94 | + expect(pressedSecond.state).toBe(pressedState) |
| 95 | + expect(pressedSecond.effects).toEqual([]) |
| 96 | + |
| 97 | + const events: PaneDragEvent[] = [ |
| 98 | + down(2, "file://b.ts"), |
| 99 | + move(90, 90, bottom, 2), |
| 100 | + { type: "pointer.up", pointerId: 2 }, |
| 101 | + { type: "pointer.cancel", pointerId: 2 }, |
| 102 | + ] |
| 103 | + |
| 104 | + events.forEach((event) => { |
| 105 | + const next = transitionPaneDrag(draggingState, event) |
| 106 | + |
| 107 | + expect(next.state).toBe(draggingState) |
| 108 | + expect(next.effects).toEqual([]) |
| 109 | + }) |
| 110 | + expect(selectMoveKey(draggingState, 2, 90, 90)).toBeUndefined() |
| 111 | + expect(selectMoveKey(pressedState, 2, 90, 90)).toBeUndefined() |
| 112 | + |
| 113 | + expect(transitionPaneDrag(draggingState, { type: "pointer.up", pointerId: 1 }).effects).toContainEqual({ |
| 114 | + type: "drop", |
| 115 | + key: "file://a.ts", |
| 116 | + target: strip, |
| 117 | + }) |
| 118 | + }) |
| 119 | +}) |
| 120 | + |
| 121 | +describe("cancelling", () => { |
| 122 | + test.each([ |
| 123 | + { type: "session.changed" }, |
| 124 | + { type: "blur" }, |
| 125 | + { type: "escape" }, |
| 126 | + { type: "pointer.cancel", pointerId: 1 }, |
| 127 | + ] satisfies PaneDragEvent[])("$type mid-drag returns to idle without dropping", (event) => { |
| 128 | + const next = transitionPaneDrag(draggingState, event) |
| 129 | + |
| 130 | + expect(next.state).toBe(INITIAL_PANE_DRAG_STATE) |
| 131 | + expect(next.effects).toEqual([{ type: "gesture.cleanup", unlockBody: true }]) |
| 132 | + expect(transitionPaneDrag(next.state, { type: "pointer.up", pointerId: 1 }).effects).toEqual([]) |
| 133 | + }) |
| 134 | + |
| 135 | + test.each([ |
| 136 | + { type: "session.changed" }, |
| 137 | + { type: "blur" }, |
| 138 | + { type: "escape" }, |
| 139 | + { type: "pointer.cancel", pointerId: 1 }, |
| 140 | + ] satisfies PaneDragEvent[])("$type mid-press returns to idle without touching the body", (event) => { |
| 141 | + const next = transitionPaneDrag(pressedState, event) |
| 142 | + |
| 143 | + expect(next.state).toBe(INITIAL_PANE_DRAG_STATE) |
| 144 | + expect(next.effects).toEqual([{ type: "gesture.cleanup", unlockBody: false }]) |
| 145 | + }) |
| 146 | +}) |
| 147 | + |
| 148 | +describe("activation", () => { |
| 149 | + test("only the primary button presses", () => { |
| 150 | + expect(transitionPaneDrag(INITIAL_PANE_DRAG_STATE, down(1, "file://a.ts", 1)).state).toBe(INITIAL_PANE_DRAG_STATE) |
| 151 | + expect(transitionPaneDrag(INITIAL_PANE_DRAG_STATE, down(1, "file://a.ts", 2)).state).toBe(INITIAL_PANE_DRAG_STATE) |
| 152 | + expect(transitionPaneDrag(INITIAL_PANE_DRAG_STATE, down()).effects).toEqual([{ type: "gesture.attach" }]) |
| 153 | + }) |
| 154 | + |
| 155 | + test("a move under the threshold stays a press, and its release is a click", () => { |
| 156 | + expect(selectMoveKey(pressedState, 1, 3, 2)).toBeUndefined() |
| 157 | + expect(transitionPaneDrag(pressedState, move(3, 2, strip)).state).toBe(pressedState) |
| 158 | + expect(selectDraggingKey(pressedState)).toBeUndefined() |
| 159 | + |
| 160 | + const released = transitionPaneDrag(pressedState, { type: "pointer.up", pointerId: 1 }) |
| 161 | + |
| 162 | + expect(released.state).toBe(INITIAL_PANE_DRAG_STATE) |
| 163 | + expect(released.effects).toEqual([{ type: "gesture.cleanup", unlockBody: false }]) |
| 164 | + }) |
| 165 | + |
| 166 | + test("crossing the threshold drags and locks the body", () => { |
| 167 | + expect(selectMoveKey(pressedState, 1, 4, 0)).toBe("file://a.ts") |
| 168 | + |
| 169 | + const over = transitionPaneDrag(pressedState, move(4, 0, strip)) |
| 170 | + |
| 171 | + expect(selectDraggingKey(over.state)).toBe("file://a.ts") |
| 172 | + expect(selectDragLayer(over.state)).toMatchObject({ x: 4, y: 0, target: strip, title: "a.ts" }) |
| 173 | + expect(over.effects).toEqual([{ type: "body.lock", cursor: "grabbing" }]) |
| 174 | + expect(transitionPaneDrag(pressedState, move(4, 0, undefined)).effects).toEqual([{ type: "body.lock", cursor: "" }]) |
| 175 | + }) |
| 176 | +}) |
| 177 | + |
| 178 | +describe("dragging", () => { |
| 179 | + test("an unchanged move returns the same state", () => { |
| 180 | + const next = transitionPaneDrag(draggingState, move(4, 0, { ...strip, rect: { ...strip.rect } })) |
| 181 | + |
| 182 | + expect(next.state).toBe(draggingState) |
| 183 | + expect(next.effects).toEqual([]) |
| 184 | + }) |
| 185 | + |
| 186 | + test("a move over an equal target keeps the target object", () => { |
| 187 | + const next = transitionPaneDrag(draggingState, move(20, 0, { ...strip, rect: { ...strip.rect } })) |
| 188 | + |
| 189 | + expect(selectDragLayer(next.state)?.target).toBe(strip) |
| 190 | + expect(next.effects).toEqual([]) |
| 191 | + }) |
| 192 | + |
| 193 | + test("the cursor follows whether a drop zone is under the pointer", () => { |
| 194 | + const away = transitionPaneDrag(draggingState, move(20, 0, undefined)) |
| 195 | + |
| 196 | + expect(away.effects).toEqual([{ type: "body.cursor", cursor: "" }]) |
| 197 | + expect(transitionPaneDrag(away.state, move(30, 0, bottom)).effects).toEqual([ |
| 198 | + { type: "body.cursor", cursor: "grabbing" }, |
| 199 | + ]) |
| 200 | + |
| 201 | + const released = transitionPaneDrag(away.state, { type: "pointer.up", pointerId: 1 }) |
| 202 | + |
| 203 | + expect(released.state).toBe(INITIAL_PANE_DRAG_STATE) |
| 204 | + expect(released.effects).toEqual([{ type: "gesture.cleanup", unlockBody: true }, { type: "click-suppression.arm" }]) |
| 205 | + }) |
| 206 | + |
| 207 | + test("a release over a drop zone drops the tab there", () => { |
| 208 | + const released = replay([move(30, 0, bottom), { type: "pointer.up", pointerId: 1 }], draggingState) |
| 209 | + |
| 210 | + expect(released.state).toBe(INITIAL_PANE_DRAG_STATE) |
| 211 | + expect(released.effects).toEqual([ |
| 212 | + { type: "gesture.cleanup", unlockBody: true }, |
| 213 | + { type: "click-suppression.arm" }, |
| 214 | + { type: "drop", key: "file://a.ts", target: bottom }, |
| 215 | + ]) |
| 216 | + }) |
| 217 | +}) |
0 commit comments