|
1 | 1 | import { expect, test, vi } from 'vitest'; |
2 | | -import type { Interactor } from './interactor-types.ts'; |
| 2 | +import { AppError } from '@agent-device/kernel/errors'; |
| 3 | +import type { Interactor, PressPointOptions } from './interactor-types.ts'; |
3 | 4 | import { |
4 | 5 | bindLocalTouchInteractor, |
5 | 6 | HOVER_UNAVAILABLE_HINT, |
@@ -143,3 +144,190 @@ test('the shared primary series preserves jitter, interval, and every press', as |
143 | 144 | ]); |
144 | 145 | expect(pause.mock.calls).toEqual([[40], [40]]); |
145 | 146 | }); |
| 147 | + |
| 148 | +// A point press is one operation whose options carry several shapes, and two of them ask for more |
| 149 | +// than the shared series can compose. A timed hold is a capability an owner states with its |
| 150 | +// `longPressPoint` cell; a fused double-click is a mechanic its interactor either has or leaves |
| 151 | +// undefined. Either way the shape is refused with a typed reason instead of resolving a member that |
| 152 | +// only knows how to throw. |
| 153 | +const holdRefused = (hint?: string) => |
| 154 | + touchRuntimeOperationFacts({ |
| 155 | + unsupported: unavailable, |
| 156 | + tap: available, |
| 157 | + longPress: |
| 158 | + hint === undefined |
| 159 | + ? unavailable |
| 160 | + : { available: false, reason: 'unsupported-platform-leaf', hint }, |
| 161 | + fill: available, |
| 162 | + }); |
| 163 | + |
| 164 | +const pressOptions = { |
| 165 | + button: 'primary', |
| 166 | + count: 1, |
| 167 | + intervalMs: 0, |
| 168 | + holdMs: 0, |
| 169 | + jitterPx: 0, |
| 170 | + doubleTap: false, |
| 171 | +} as const; |
| 172 | + |
| 173 | +function bindSharedSeries( |
| 174 | + seriesFacts: ReturnType<typeof touchRuntimeOperationFacts>, |
| 175 | + mechanics: Partial< |
| 176 | + Record<'tap' | 'doubleTap' | 'longPress' | 'pressPoint', ReturnType<typeof vi.fn>> |
| 177 | + >, |
| 178 | +) { |
| 179 | + return bindLocalTouchInteractor({ |
| 180 | + device, |
| 181 | + signal: new AbortController().signal, |
| 182 | + resolveInteractor: async () => mechanics as unknown as Interactor, |
| 183 | + facts: seriesFacts, |
| 184 | + pause: async () => {}, |
| 185 | + }); |
| 186 | +} |
| 187 | + |
| 188 | +const refusedBy = |
| 189 | + (reason: string) => |
| 190 | + (error: unknown): boolean => |
| 191 | + error instanceof AppError && |
| 192 | + error.code === 'UNSUPPORTED_OPERATION' && |
| 193 | + error.details?.['reason'] === reason; |
| 194 | + |
| 195 | +const press = ( |
| 196 | + operations: ReturnType<typeof bindSharedSeries>, |
| 197 | + options: Partial<PressPointOptions> = {}, |
| 198 | +) => |
| 199 | + operations.tapPoint!({ |
| 200 | + point: { x: 20, y: 30 }, |
| 201 | + options: { ...pressOptions, ...options }, |
| 202 | + }); |
| 203 | + |
| 204 | +test('a hold the owner refuses is refused by its fact, over an interactor that can hold', async () => { |
| 205 | + const longPress = vi.fn(async () => undefined); |
| 206 | + const operations = bindSharedSeries(holdRefused('Use a native gesture instead.'), { |
| 207 | + tap: vi.fn(async () => undefined), |
| 208 | + longPress, |
| 209 | + }); |
| 210 | + |
| 211 | + await expect(press(operations, { holdMs: 600 })).rejects.toSatisfy( |
| 212 | + refusedBy('unsupported-platform-leaf'), |
| 213 | + ); |
| 214 | + await expect(press(operations, { holdMs: 600 })).rejects.toMatchObject({ |
| 215 | + details: { reason: 'unsupported-platform-leaf', hint: 'Use a native gesture instead.' }, |
| 216 | + }); |
| 217 | + |
| 218 | + expect(longPress).not.toHaveBeenCalled(); |
| 219 | +}); |
| 220 | + |
| 221 | +test('an owner with no double-click mechanic refuses --double before pressing anything', async () => { |
| 222 | + const tap = vi.fn(async () => undefined); |
| 223 | + const operations = bindSharedSeries(holdRefused(), { tap }); |
| 224 | + |
| 225 | + await expect(press(operations, { doubleTap: true })).rejects.toSatisfy( |
| 226 | + refusedBy('owner-capability-missing'), |
| 227 | + ); |
| 228 | + |
| 229 | + expect(tap).not.toHaveBeenCalled(); |
| 230 | +}); |
| 231 | + |
| 232 | +test('the same hold refusal wording carries whatever reason the owner states', async () => { |
| 233 | + const refusals = await Promise.all( |
| 234 | + [ |
| 235 | + touchRuntimeOperationFacts({ |
| 236 | + unsupported: unavailable, |
| 237 | + tap: available, |
| 238 | + longPress: unavailable, |
| 239 | + fill: available, |
| 240 | + }), |
| 241 | + touchRuntimeOperationFacts({ |
| 242 | + unsupported: unavailable, |
| 243 | + tap: available, |
| 244 | + longPress: { available: false, reason: 'unsupported-device-kind' }, |
| 245 | + fill: available, |
| 246 | + }), |
| 247 | + ].map( |
| 248 | + async (seriesFacts) => |
| 249 | + await bindSharedSeries(seriesFacts, { longPress: vi.fn() }).tapPoint!({ |
| 250 | + point: { x: 1, y: 1 }, |
| 251 | + options: { ...pressOptions, holdMs: 600 }, |
| 252 | + }) |
| 253 | + .then(() => undefined) |
| 254 | + .catch((error: unknown) => error), |
| 255 | + ), |
| 256 | + ); |
| 257 | + |
| 258 | + const [leaf, deviceKind] = refusals as [AppError, AppError]; |
| 259 | + // Identical wording and code, so nothing reading the message can tell the two owners apart… |
| 260 | + expect(deviceKind.message).toEqual(leaf.message); |
| 261 | + expect(deviceKind.code).toEqual(leaf.code); |
| 262 | + // …and the typed reason is the only thing that can. |
| 263 | + expect(leaf).toSatisfy(refusedBy('unsupported-platform-leaf')); |
| 264 | + expect(deviceKind).toSatisfy(refusedBy('unsupported-device-kind')); |
| 265 | +}); |
| 266 | + |
| 267 | +// The three tests below pin behavior this change must not disturb, so they are green before it too. |
| 268 | +test('an owner that states each shape reaches its own mechanic', async () => { |
| 269 | + const mechanics = { |
| 270 | + tap: vi.fn(async () => undefined), |
| 271 | + doubleTap: vi.fn(async () => ({ double: true })), |
| 272 | + longPress: vi.fn(async () => ({ held: true })), |
| 273 | + }; |
| 274 | + const operations = bindSharedSeries(facts, mechanics); |
| 275 | + |
| 276 | + await expect(press(operations, { doubleTap: true })).resolves.toEqual({ double: true }); |
| 277 | + await expect(press(operations, { holdMs: 600 })).resolves.toEqual({ held: true }); |
| 278 | + |
| 279 | + expect(mechanics.doubleTap).toHaveBeenCalledWith(20, 30); |
| 280 | + expect(mechanics.longPress).toHaveBeenCalledWith(20, 30, 600); |
| 281 | +}); |
| 282 | + |
| 283 | +test('an owner with a fused point press keeps its own --double route without a double-click member', async () => { |
| 284 | + const pressPoint = vi.fn(async () => ({ fused: true })); |
| 285 | + const operations = bindSharedSeries(facts, { pressPoint }); |
| 286 | + |
| 287 | + await expect( |
| 288 | + operations.tapPoint!({ |
| 289 | + point: { x: 20, y: 30 }, |
| 290 | + options: { ...pressOptions, doubleTap: true }, |
| 291 | + }), |
| 292 | + ).resolves.toEqual({ fused: true }); |
| 293 | + |
| 294 | + expect(pressPoint).toHaveBeenCalledWith( |
| 295 | + { x: 20, y: 30 }, |
| 296 | + expect.objectContaining({ doubleTap: true }), |
| 297 | + ); |
| 298 | +}); |
| 299 | + |
| 300 | +test('a repeated single tap keeps running where the owner refuses a hold and has no double-click', async () => { |
| 301 | + const tap = vi.fn(async () => undefined); |
| 302 | + const operations = bindSharedSeries(holdRefused(), { tap }); |
| 303 | + |
| 304 | + await expect(press(operations, { count: 2, jitterPx: 1 })).resolves.toBeUndefined(); |
| 305 | + |
| 306 | + expect(tap.mock.calls).toEqual([ |
| 307 | + [20, 30], |
| 308 | + [21, 30], |
| 309 | + ]); |
| 310 | +}); |
| 311 | + |
| 312 | +// The mechanic is read off the interactor, so an owner written as a class has to keep its own |
| 313 | +// receiver: both provider interactors are classes that reach their session through `this`. |
| 314 | +test('the fused double-click keeps the interactor as its receiver', async () => { |
| 315 | + class PointerOwner { |
| 316 | + readonly pressed: Array<[number, number]> = []; |
| 317 | + async doubleTap(x: number, y: number): Promise<void> { |
| 318 | + this.pressed.push([x, y]); |
| 319 | + } |
| 320 | + } |
| 321 | + const owner = new PointerOwner(); |
| 322 | + const operations = bindLocalTouchInteractor({ |
| 323 | + device, |
| 324 | + signal: new AbortController().signal, |
| 325 | + resolveInteractor: async () => owner as unknown as Interactor, |
| 326 | + facts, |
| 327 | + pause: async () => {}, |
| 328 | + }); |
| 329 | + |
| 330 | + await press(operations, { doubleTap: true }); |
| 331 | + |
| 332 | + expect(owner.pressed).toEqual([[20, 30]]); |
| 333 | +}); |
0 commit comments