|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import { test } from 'vitest'; |
3 | | -import { buildDragGesturePlan, singlePointerPlanEndpoints } from './gesture-plan.ts'; |
| 3 | +import { |
| 4 | + buildDragGesturePlan, |
| 5 | + densifyGesturePath, |
| 6 | + singlePointerPlanEndpoints, |
| 7 | +} from './gesture-plan.ts'; |
4 | 8 |
|
5 | 9 | const DRAG_PROPERTY_RUNS = 100; |
6 | 10 |
|
@@ -77,5 +81,96 @@ test('every drag plan preserves ordered samples and exact hold/move endpoints', |
77 | 81 | for (let index = 1; index < samples.length; index += 1) { |
78 | 82 | assert.ok(samples[index]!.offsetMs >= samples[index - 1]!.offsetMs); |
79 | 83 | } |
| 84 | + const dense = densifyGesturePath(samples, 'android'); |
| 85 | + assert.deepEqual(dense[0]?.point, from); |
| 86 | + assert.deepEqual(dense.at(-1)?.point, to); |
80 | 87 | } |
81 | 88 | }); |
| 89 | + |
| 90 | +test('densifyGesturePath merges the profile grid into the canonical path and keeps its endpoints', () => { |
| 91 | + const canonical = [ |
| 92 | + { offsetMs: 0, point: { x: 0, y: 0 } }, |
| 93 | + { offsetMs: 100, point: { x: 100, y: 0 } }, |
| 94 | + { offsetMs: 200, point: { x: 100, y: 100 } }, |
| 95 | + ]; |
| 96 | + const dense = densifyGesturePath(canonical, 'default'); |
| 97 | + |
| 98 | + // The 16 ms grid and the canonical offsets merge into one ascending timeline. |
| 99 | + const offsets = dense.map(({ offsetMs }) => offsetMs); |
| 100 | + assert.equal(offsets[0], 0); |
| 101 | + assert.equal(offsets.at(-1), 200); |
| 102 | + assert.ok([...offsets].sort((left, right) => left - right).every((o, i) => o === offsets[i])); |
| 103 | + assert.deepEqual(dense[0]?.point, { x: 0, y: 0 }); |
| 104 | + assert.deepEqual(dense.at(-1)?.point, { x: 100, y: 100 }); |
| 105 | + // A merged grid offset inside the first segment interpolates on that segment's line. |
| 106 | + const at50 = dense.find(({ offsetMs }) => offsetMs === 50); |
| 107 | + assert.deepEqual(at50?.point, { x: 50, y: 0 }); |
| 108 | + const at150 = dense.find(({ offsetMs }) => offsetMs === 150); |
| 109 | + assert.deepEqual(at150?.point, { x: 100, y: 50 }); |
| 110 | + // Canonical offsets survive verbatim. |
| 111 | + const at100 = dense.find(({ offsetMs }) => offsetMs === 100); |
| 112 | + assert.deepEqual(at100?.point, { x: 100, y: 0 }); |
| 113 | +}); |
| 114 | + |
| 115 | +test('densifyGesturePath hands within-segment pacing to the progress hook and keeps its endpoint rule', () => { |
| 116 | + const canonical = [ |
| 117 | + { offsetMs: 0, point: { x: 0, y: 0 } }, |
| 118 | + { offsetMs: 100, point: { x: 100, y: 0 } }, |
| 119 | + ]; |
| 120 | + const seen: Array<[number, number]> = []; |
| 121 | + const dense = densifyGesturePath(canonical, 'default', (progress, segmentDurationMs) => { |
| 122 | + seen.push([progress, segmentDurationMs]); |
| 123 | + return progress * progress; |
| 124 | + }); |
| 125 | + |
| 126 | + const at50 = dense.find(({ offsetMs }) => offsetMs === 50); |
| 127 | + assert.deepEqual(at50?.point, { x: 25, y: 0 }); |
| 128 | + assert.ok( |
| 129 | + seen.some(([progress, segmentDurationMs]) => progress === 0.5 && segmentDurationMs === 100), |
| 130 | + ); |
| 131 | + // The hook only shapes interior offsets: every progress it saw lies strictly |
| 132 | + // between 0 and 1, so the endpoints never passed through it. |
| 133 | + assert.ok(seen.length >= 1); |
| 134 | + for (const [progress] of seen) { |
| 135 | + assert.ok(progress > 0 && progress < 1, `hook saw progress ${progress}`); |
| 136 | + } |
| 137 | + assert.deepEqual(dense[0]?.point, { x: 0, y: 0 }); |
| 138 | + assert.deepEqual(dense.at(-1)?.point, { x: 100, y: 0 }); |
| 139 | +}); |
| 140 | + |
| 141 | +test('densifyGesturePath refuses a path of fewer than two timed samples', () => { |
| 142 | + assert.throws(() => densifyGesturePath([{ offsetMs: 0, point: { x: 0, y: 0 } }], 'default'), { |
| 143 | + code: 'INVALID_ARGS', |
| 144 | + details: { reason: 'GESTURE_PATH_TOO_SHORT' }, |
| 145 | + }); |
| 146 | + assert.throws( |
| 147 | + () => |
| 148 | + densifyGesturePath( |
| 149 | + [ |
| 150 | + { offsetMs: 0, point: { x: 0, y: 0 } }, |
| 151 | + { offsetMs: 0, point: { x: 1, y: 0 } }, |
| 152 | + ], |
| 153 | + 'default', |
| 154 | + ), |
| 155 | + { code: 'INVALID_ARGS', details: { reason: 'GESTURE_PATH_TOO_SHORT' } }, |
| 156 | + ); |
| 157 | +}); |
| 158 | + |
| 159 | +for (const [name, offsets] of [ |
| 160 | + ['a nonzero first offset', [5, 100]], |
| 161 | + ['tied first offsets', [0, 0, 100]], |
| 162 | + ['tied last offsets', [0, 100, 100]], |
| 163 | + ['reversed offsets', [0, 75, 50, 100]], |
| 164 | + ['a non-finite interior offset', [0, Number.NaN, 100]], |
| 165 | +] as const) { |
| 166 | + test(`densifyGesturePath refuses ${name}`, () => { |
| 167 | + const canonical = offsets.map((offsetMs, index) => ({ |
| 168 | + offsetMs, |
| 169 | + point: { x: index * 10, y: 0 }, |
| 170 | + })); |
| 171 | + assert.throws(() => densifyGesturePath(canonical, 'default'), { |
| 172 | + code: 'INVALID_ARGS', |
| 173 | + details: { reason: 'GESTURE_PATH_INVALID_TIMELINE' }, |
| 174 | + }); |
| 175 | + }); |
| 176 | +} |
0 commit comments