1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
import { describe, expect, it } from "vitest";
import { type DragPhase, type DragTransition, nextDragPhase } from "./drag-controller";
const OPTS = { dragThresholdPx: 6 };
function down(over: Partial<Parameters<typeof nextDragPhase>[1] & { type: "pointer-down" }> = {}) {
return {
type: "pointer-down" as const,
id: "t1",
x: 10,
y: 10,
touch: false,
...over,
};
}
function press(touch = false): DragPhase {
return nextDragPhase({ kind: "idle" }, down({ touch }), OPTS).phase;
}
describe("nextDragPhase — mouse", () => {
it("a pointer-down enters the undecided pressing state", () => {
expect(press()).toEqual({ kind: "pressing", id: "t1", x: 10, y: 10, touch: false });
});
it("movement past the threshold starts a drag", () => {
const transition = nextDragPhase(
press(),
{ type: "pointer-move", x: 20, y: 12, targetCell: { col: 4, row: 1 } },
OPTS,
);
expect(transition.phase).toEqual({
kind: "dragging",
id: "t1",
x: 20,
y: 12,
targetCell: { col: 4, row: 1 },
touch: false,
});
expect(transition.emit).toBeUndefined();
});
it("movement within the threshold stays pressing", () => {
const transition = nextDragPhase(
press(),
{ type: "pointer-move", x: 12, y: 14, targetCell: { col: 1, row: 1 } },
OPTS,
);
expect(transition.phase).toEqual({ kind: "pressing", id: "t1", x: 10, y: 10, touch: false });
});
it("release without moving emits a tap (open the workspace)", () => {
const transition = nextDragPhase(press(), { type: "pointer-up" }, OPTS);
expect(transition.phase).toEqual({ kind: "idle" });
expect(transition.emit).toEqual({ kind: "tap", id: "t1" });
});
it("release while dragging emits a drop with the last tracked index", () => {
let transition: DragTransition = nextDragPhase(
press(),
{ type: "pointer-move", x: 30, y: 30, targetCell: { col: 3, row: 1 } },
OPTS,
);
transition = nextDragPhase(
transition.phase,
{ type: "pointer-move", x: 31, y: 31, targetCell: { col: 6, row: 1 } },
OPTS,
);
transition = nextDragPhase(transition.phase, { type: "pointer-up" }, OPTS);
expect(transition.emit).toEqual({ kind: "drop", id: "t1", targetCell: { col: 6, row: 1 } });
expect(transition.phase).toEqual({ kind: "idle" });
});
});
describe("nextDragPhase — touch", () => {
it("a long-press lifts the tile and emits lift (arm the drag + select)", () => {
const transition = nextDragPhase(press(true), { type: "long-press" }, OPTS);
expect(transition.emit).toEqual({ kind: "lift", id: "t1" });
expect(transition.phase).toEqual({
kind: "dragging",
id: "t1",
x: 10,
y: 10,
targetCell: null,
touch: true,
});
});
it("movement past the threshold BEFORE the lift cancels (vertical scrolling wins)", () => {
const transition = nextDragPhase(
press(true),
{ type: "pointer-move", x: 10, y: 80, targetCell: { col: 1, row: 1 } },
OPTS,
);
expect(transition.phase).toEqual({ kind: "idle" });
expect(transition.emit).toBeUndefined();
});
it("movement within the threshold before the lift keeps pressing", () => {
const transition = nextDragPhase(
press(true),
{ type: "pointer-move", x: 13, y: 12, targetCell: { col: 1, row: 1 } },
OPTS,
);
expect(transition.phase?.kind).toBe("pressing");
});
it("after the lift, movement drags (never cancels)", () => {
let transition = nextDragPhase(press(true), { type: "long-press" }, OPTS);
transition = nextDragPhase(
transition.phase,
{ type: "pointer-move", x: 10, y: 90, targetCell: { col: 5, row: 1 } },
OPTS,
);
expect(transition.phase).toEqual({
kind: "dragging",
id: "t1",
x: 10,
y: 90,
targetCell: { col: 5, row: 1 },
touch: true,
});
});
it("a long-press never lifts a mouse press (desktop selects via right-click)", () => {
const transition = nextDragPhase(press(false), { type: "long-press" }, OPTS);
expect(transition.emit).toBeUndefined();
expect(transition.phase?.kind).toBe("pressing");
});
it("release while lifted (no movement) drops at the lifted tile's own slot", () => {
let transition = nextDragPhase(press(true), { type: "long-press" }, OPTS);
transition = nextDragPhase(transition.phase, { type: "pointer-up" }, OPTS);
expect(transition.emit).toEqual({ kind: "drop", id: "t1", targetCell: null });
});
});
describe("nextDragPhase — cancellations", () => {
it("cancel returns to idle from any phase without emitting", () => {
for (const phase of [
{ kind: "idle" as const },
press(),
nextDragPhase(
press(),
{ type: "pointer-move", x: 40, y: 40, targetCell: { col: 2, row: 1 } },
OPTS,
).phase,
]) {
const transition = nextDragPhase(phase, { type: "cancel" }, OPTS);
expect(transition.phase).toEqual({ kind: "idle" });
expect(transition.emit).toBeUndefined();
}
});
it("events in idle are inert", () => {
expect(nextDragPhase({ kind: "idle" }, { type: "pointer-up" }, OPTS).phase).toEqual({
kind: "idle",
});
expect(
nextDragPhase(
{ kind: "idle" },
{ type: "pointer-move", x: 1, y: 1, targetCell: { col: 1, row: 1 } },
OPTS,
).phase,
).toEqual({ kind: "idle" });
expect(nextDragPhase({ kind: "idle" }, { type: "long-press" }, OPTS).phase).toEqual({
kind: "idle",
});
});
it("a second pointer-down restarts the gesture with the new tile", () => {
const dragging = nextDragPhase(
press(),
{ type: "pointer-move", x: 40, y: 40, targetCell: { col: 2, row: 1 } },
OPTS,
).phase;
const transition = nextDragPhase(dragging, down({ id: "t2" }), OPTS);
expect(transition.phase).toEqual({ kind: "pressing", id: "t2", x: 10, y: 10, touch: false });
});
});
|