// Pure resize plans: preview geometry, reachable stops, and drag latch history. import assert from 'node:assert/strict'; import test from 'node:test'; import { check, fc } from './property.mjs'; import { close, column, leaf, makeRow, resize, stack, widths, tree, } from './tiles-fixture.mjs'; const { planResize, planGutter } = resize; const resizedWidth = fc.integer({ min: 192, max: 660 }); const direction = fc.constantFrom(-1, 1); const delta = fc.integer({ min: -1200, max: 1200 }); test('horizontal drag changes only its column and anchors the left edge with the actual change', () => { check( fc.property( fc.array(resizedWidth, { minLength: 2, maxLength: 6 }), fc.integer({ min: 480, max: 1800 }), fc.nat(), direction, delta, (lengths, viewport, index, x, dx) => { const at = index % lengths.length; const length = lengths.reduce((sum, width) => sum + width, 0); const span = (length + tree.GUTTER * (lengths.length - 1)) / viewport; const root = makeRow(lengths, span, column); const { sample: plan } = planResize( root, `bottom-${at}`, { x, y: 0 }, viewport, () => { assert.fail('horizontal drag does not measure height'); }, ); const preview = plan(dx, 0); const expected = Math.max(tree.LEAST, lengths[at] + x * dx); assert.equal(preview.root.id, root.id); assert.strictEqual(preview.root.children, root.children); assert.ok(tree.valid(preview.root)); const nextWidths = widths(preview.root, viewport); for (let i = 0; i < lengths.length; i++) close( nextWidths.get(root.children[i].id), i === at ? expected : lengths[i], ); close(preview.shift, x === -1 ? expected - lengths[at] : 0); assert.deepEqual(plan(dx, 0), preview); assert.strictEqual(plan(0, 0).root, root); }, ), ); }); test('resize snaps to the visible board edge or an equal pair, then releases to free drag', () => { check( fc.property( fc.integer({ min: 500, max: 2000 }), direction, fc.boolean(), fc.integer({ min: -7, max: 7 }), (viewport, x, half, near) => { const w = viewport * 0.38; const root = makeRow( [w, w, w], (3 * w + 2 * tree.GUTTER) / viewport, column, ); const fixed = viewport * (x === 1 ? 0.3 : 0.7); const offset = w + tree.GUTTER + (x === -1 ? w : 0) - fixed; const { sample: plan } = planResize( root, 'bottom-1', { x, y: 0 }, viewport, () => 0, { viewOffset: offset }, ); const target = half ? (viewport - tree.GUTTER) / 2 : 0.7 * viewport; for (const [wideDistance, held] of [ [13, false], [11.75, true], [near, true], [0, true], [19.75, true], [20.25, false], [13, false], ]) { const distance = wideDistance * Math.min(viewport / 1000, 1); const expected = held ? target : target + distance; const preview = plan(x * (target + distance - w), 0); if (held) { assert.equal(preview.snapX?.kind, half ? 'half' : 'edge'); close(preview.snapX?.at, fixed + x * target); } else assert.equal(preview.snapX, undefined); assert.equal(preview.snapY, undefined); const cols = [...widths(preview.root, viewport).values()]; close(cols[0], w); close(cols[1], expected); close(cols[2], w); close(preview.shift, x === -1 ? expected - w : 0); } assert.deepEqual(plan(0, 0), { root, shift: 0 }); }, ), ); }); test('vertical grabs align laid-out dividers, then release without moving the other column', () => { check( fc.property( fc.array(fc.integer({ min: 420, max: 580 }), { minLength: 4, maxLength: 4, }), fc.integer({ min: 1, max: 5 }), direction, fc.nat(), (base, nudge, y, index) => { const at = (index % 3) + (y === -1 ? 1 : 0); const before = y === -1 ? at : at + 1; // A sibling pins early when the grabbed part grows. Keep the outer // siblings tall so the line can still move after it pins. const heights = base.map((height, i) => i === (at === 1 ? 2 : 1) ? tree.LEAST + nudge : height, ); const length = heights.reduce((sum, height) => sum + height, 0); const height = length + 3 * tree.GUTTER; const dragged = stack( 'dragged', heights.map((_, i) => leaf(`part-${i}`)), heights, ); const line = (column, count) => column.sizes .slice(0, count) .reduce( (top, share) => top + share * (height - tree.GUTTER * (column.sizes.length - 1)), (count - 0.5) * tree.GUTTER, ); const rowWith = (neighbor) => makeRow([1, 1], 1.2, (i) => (i === 0 ? neighbor : dragged)); const plan = (root, options, part = at, edge = y) => planResize( root, `part-${part}`, { x: 0, y: edge }, 1000, () => height, options, )?.sample ?? null; const free = plan(rowWith(column('unused'))); const center = (dy) => line(free(0, dy).root.children[1], before); // Pick a neighbor line from a reachable *laid-out* free position, not // from dy: the grabbed part and the visible line travel different distances. for (const sign of [-1, 1]) { const dy = sign * 115; const stop = center(dy); const neighbor = stack( 'neighbor', [leaf('above'), leaf('below')], [stop - tree.GUTTER / 2, height - stop - tree.GUTTER / 2], ); const root = rowWith(neighbor); const stopsMeasured = []; const snap = plan(root, { viewOffset: 0, dividerStops: (id) => { stopsMeasured.push(id); return [line(neighbor, 1)]; }, }); const distanceFrom = (line, lo, hi) => { for (let move = dy; Math.abs(move) < 1000; move += sign) { const distance = Math.abs(center(move) - line); if (distance >= lo && distance <= hi) return move; } assert.fail(`no free divider ${lo}..${hi}px from ${line}`); }; assert.deepEqual(snap(0, 0), { root, shift: 0 }); const outsideCapture = distanceFrom(stop, 12.1, 14); const missed = snap(0, outsideCapture); assert.equal(missed.snapY, undefined); close(line(missed.root.children[1], before), center(outsideCapture)); for (const move of [ distanceFrom(stop, 10, 11.9), dy, distanceFrom(stop, 18.1, 19.9), ]) { const { root: after, shift, snapX, snapY } = snap(0, move); assert.equal(snapX, undefined); close(snapY, line(neighbor, 1)); close(line(after.children[1], before), line(after.children[0], 1)); assert.equal(shift, 0); assert.strictEqual(after.children[0], neighbor); assert.strictEqual(after.sizes, root.sizes); assert.equal(after.span, root.span); assert.ok(tree.valid(after)); } const past = distanceFrom(stop, 20.1, 22); const released = snap(0, past); assert.equal(released.snapY, undefined); close(line(released.root.children[1], before), center(past)); assert.deepEqual(stopsMeasured, [dragged.id]); const limit = center(sign * 1000); const unreachable = plan(root, { viewOffset: 0, dividerStops: () => [limit + 3 * sign], }); const nearLimit = distanceFrom(limit, 1, 4); const freeAtLimit = unreachable(0, nearLimit); assert.equal(freeAtLimit.snapY, undefined); close(line(freeAtLimit.root.children[1], before), center(nearLimit)); const reachable = plan(root, { viewOffset: 0, dividerStops: () => [limit - 4 * sign], }); const caught = reachable(0, nearLimit); close(caught.snapY, limit - 4 * sign); close(line(caught.root.children[1], before), limit - 4 * sign); const floor = reachable(0, sign * 1000); assert.equal(floor.snapY, undefined); close(line(floor.root.children[1], before), limit); } const root = rowWith(column('neighbor')); const external = { viewOffset: 0, dividerStops: () => assert.fail('no outer divider'), }; assert.equal(plan(root, external, 0, -1), null); assert.deepEqual( plan(root, external, 3, 1)(0, 60), plan(root, undefined, 3, 1)(0, 60), ); }, ), ); }); test('corner cues combine and stay off on a stationary axis', () => { check( fc.property( fc.integer({ min: 500, max: 2000 }), fc.integer({ min: 240, max: 640 }), direction, direction, fc.integer({ min: 4, max: 11 }), (viewport, partHeight, x, y, wideNudge) => { const scale = Math.min(1, viewport / 1000); const nudge = wideNudge * scale; const root = makeRow([1, 1], undefined, column); const width = (viewport - tree.GUTTER) / 2; const center = partHeight + tree.GUTTER / 2; const { sample: drag } = planResize( root, y === 1 ? 'top-0' : 'bottom-0', { x, y }, viewport, () => 2 * center, { viewOffset: -65, dividerStops: () => [center] }, ); const h = { at: 65 + (x === 1 ? width : 0), kind: 'half' }; assert.deepEqual(drag(0, 0), { root, shift: 0 }); const both = drag(nudge, nudge); assert.equal(both.shift, 0); assert.deepEqual(both.snapX, h); assert.equal(both.snapY, center); close(both.root.children[0].sizes[0], 0.5); const onlyY = drag(0, nudge); assert.equal(onlyY.snapX, undefined); assert.equal(onlyY.snapY, center); const onlyX = drag(nudge, 0); assert.strictEqual(onlyX.root, root); assert.deepEqual(onlyX, { root, shift: 0, snapX: h }); }, ), ); }); test('solo resize keeps one stable width-bearing wrapper and ignores vertical drag', () => { check( fc.property( fc.integer({ min: 350, max: 2000 }), direction, (viewport, x) => { const root = leaf('alone'); const measure = () => assert.fail('solo drag has no height to measure'); assert.equal( planResize(root, root.window, { x: 0, y: x }, viewport, measure), null, ); const { edges, sample: plan } = planResize( root, root.window, { x, y: -x }, viewport, measure, ); assert.deepEqual(edges, { x, y: 0 }); const shrunk = plan(-x * viewport, 90); const grown = plan(x * 70, 90); const { sample: shrunkAgain } = planResize( shrunk.root, root.window, { x, y: 0 }, viewport, measure, ); assert.strictEqual(shrunkAgain(-x * 999, 0).root, shrunk.root); assert.strictEqual(plan(0, 90).root, root); assert.equal(shrunk.root.id, grown.root.id); assert.strictEqual(shrunk.root.children[0], root); close(widths(shrunk.root, viewport).get(root.window), tree.LEAST); close(widths(grown.root, viewport).get(root.window), viewport + 70); assert.strictEqual(tree.normalize(shrunk.root), shrunk.root); }, ), ); }); test('corner resize combines outer width and the nearest stack, pinning weighted sibling floors', () => { check( fc.property( fc.array(fc.integer({ min: 180, max: 750 }), { minLength: 3, maxLength: 7, }), direction, direction, delta, fc.boolean(), (heights, x, y, dy, solo) => { const pane = leaf('gripped'); const sidecar = leaf('sidecar'); const branch = { ...makeRow([1, 1], undefined, (at) => (at === 0 ? pane : sidecar)), id: 'branch', }; const at = 1; const inner = stack( 'inner', heights.map((_, i) => (i === at ? branch : leaf(`peer-${i}`))), heights, ); const outer = stack('outer', [leaf('above'), inner], [400, 2000]); const root = solo ? outer : makeRow([1, 1], undefined, (i) => i === 0 ? leaf('neighbor') : outer, ); const viewport = 1000; const available = heights.reduce((sum, height) => sum + height, 0); const splitHeight = available + tree.GUTTER * (heights.length - 1); const sizesSeen = []; const { edges, sample: plan } = planResize( root, pane.window, { x, y }, viewport, (id) => { sizesSeen.push(id); return splitHeight; }, ); const after = plan(-x * viewport, dy).root; assert.deepEqual(edges, { x, y }); assert.deepEqual(sizesSeen, [inner.id]); assert.ok(tree.valid(after)); assert.strictEqual(tree.find(after, outer.id).sizes, outer.sizes); assert.strictEqual(tree.find(after, branch.id), branch); close( widths(after, viewport).get(outer.id), 2 * tree.LEAST + tree.GUTTER, ); const innerAfter = tree.find(after, inner.id); const newHeights = innerAfter.sizes.map((weight) => weight * available); close( newHeights.reduce((sum, height) => sum + height, 0), available, ); close( newHeights[at], Math.min( available - (heights.length - 1) * tree.LEAST, Math.max(tree.LEAST, heights[at] + y * dy), ), ); const scales = newHeights.flatMap((height, i) => i !== at && height > tree.LEAST + 1e-8 ? [height / heights[i]] : [], ); for (const [i, height] of newHeights.entries()) { assert.ok(height >= tree.LEAST - 1e-8); if (i === at || !scales.length) continue; close(height, Math.max(tree.LEAST, heights[i] * scales[0])); } assert.equal( planResize(root, 'peer-0', { x: 0, y: -1 }, viewport, () => { assert.fail('topmost branch cannot resize by its top edge'); }), null, ); }, ), ); }); // 160 / 525 rounded back into CSS pixels is just above the 160px floor. test('a solo left edge already at its minimum does not produce a roundoff-only resize', () => { const pane = leaf('alone'); const root = makeRow([1], tree.LEAST / 525, () => pane); const { sample: plan } = planResize( root, pane.window, { x: -1, y: 0 }, 525, () => 0, ); assert.strictEqual(plan(1, 0).root, root); assert.equal(plan(1, 0).shift, 0); }); // Construct a split from layout pixels so the oracle can inspect actual pane edges. function gutterFixture( axis, lengths, children = lengths.map((_, i) => leaf(`gutter-${i}`)), ) { const length = lengths.reduce((sum, width) => sum + width, 0); return { length, split: { kind: 'split', id: 'gutter', axis, children, sizes: lengths.map((width) => width / length), ...(axis === 'row' ? { span: 2.5 } : {}), }, }; } // Starting widths and divider positions lie just inside capture distance. // A 5px move away crosses capture but not release distance. for (const axis of ['x', 'y']) { test(`returning to the press on ${axis} clears only that axis's snap`, () => { const viewport = 1000; const dragged = column('dragged'); const root = makeRow([486, 508], 1, (at) => at === 0 ? dragged : leaf('neighbor'), ); const { sample } = planResize( root, 'top-dragged', { x: 1, y: 1 }, viewport, () => 606, { viewOffset: 0, dividerStops: () => [314] }, ); const first = sample(11, 11); assert.deepEqual(first.snapX, { kind: 'half', at: 497 }); assert.equal(first.snapY, 314); const zero = axis === 'x' ? sample(0, -5) : sample(-5, 0); assert.deepEqual(zero.snapX, axis === 'x' ? undefined : first.snapX); assert.equal(zero.snapY, axis === 'y' ? undefined : first.snapY); const away = sample(-5, -5); assert.deepEqual(away.snapX, axis === 'x' ? undefined : first.snapX); assert.equal(away.snapY, axis === 'y' ? undefined : first.snapY); }); } const axis = fc.constantFrom('row', 'column'); const boardWidth = fc.integer({ min: 420, max: 1600 }); test('a near-edge half width snaps to the actual board boundary, never a clipped width guide', () => { check( fc.property(boardWidth, direction, (viewport, x) => { const half = (viewport - tree.GUTTER) / 2; const edge = x === 1 ? viewport : 0; // Place half-width just inside, just outside, then well outside the viewport. for (const drift of [-1.25, 1.25, 40]) { const fixed = edge - x * (half - drift); const expected = drift === 40 ? undefined : { kind: 'edge', at: edge }; const solo = leaf('dragged'); const { sample } = planResize( solo, solo.window, { x, y: 0 }, viewport, () => 0, { viewOffset: x === 1 ? -fixed : viewport - fixed }, ); const preview = sample(x * (half - viewport), 0); assert.deepEqual(preview.snapX, expected); if (expected) close(fixed + x * preview.root.span * viewport, edge); const { split, length } = gutterFixture('row', [half + 45, half + 55]); const start = x === 1 ? fixed : fixed - length - tree.GUTTER; const gutter = planGutter(split, 0, length, { viewport, start }); const result = gutter(x === 1 ? -45 : 55); assert.deepEqual(result.snapX, expected); if (expected) close( start + length * result.sizes[0] + (x === 1 ? 0 : tree.GUTTER), edge, ); } }), ); }); test('gutter moves from the grabbed seam, trades only its pair, and respects nested minima', () => { check( fc.property( axis, fc.array(fc.integer({ min: 50, max: 600 }), { minLength: 3, maxLength: 7, }), fc.nat(), fc.integer({ min: 1, max: 700 }), direction, (axis, lengths, choice, distance, direction) => { const at = choice % (lengths.length - 1); const children = lengths.map((_, i) => leaf(`gutter-${i}`)); children[at] = { ...column('nested'), axis }; const { split, length } = gutterFixture(axis, lengths, children); Object.freeze(split.sizes); Object.freeze(split); const plan = planGutter(split, at, length); const delta = distance * direction; const preview = plan(delta); const first = lengths[at]; const second = lengths[at + 1]; const room = direction < 0 ? first - 2 * tree.LEAST - tree.GUTTER : second - tree.LEAST; const moved = direction * Math.min(distance, Math.max(0, room)); assert.equal(preview.snapX, undefined); assert.equal(preview.snapY, undefined); split.sizes.forEach((size, i) => { if (i === at) close(preview.sizes[i] * length, first + moved); else if (i === at + 1) close(preview.sizes[i] * length, second - moved); else assert.equal(preview.sizes[i], size); }); if (moved === 0) assert.equal(preview.sizes, split.sizes); assert.deepEqual(plan(delta), preview); }, ), 250, ); }); test('gutter half-width and divider snaps capture, hold, release and reset', () => { check( fc.property( axis, boardWidth, direction, direction, (axis, viewport, pane, sign) => { const half = (viewport - tree.GUTTER) / 2; const pair = 2 * half + 100; const target = pane < 0 ? half : pair - half; // The press is inside capture range, but zero delta must never latch. const first = target + sign * 2; const { split, length } = gutterFixture(axis, [ 230, first, pair - first, ]); const start = axis === 'row' ? 0 : 230; const guideX = start + target + (pane < 0 ? 0 : tree.GUTTER); const guideY = start + target + tree.GUTTER / 2; const plan = planGutter(split, 1, length, { viewport, start, dividerStops: [guideY], }); const capture = Math.min(12, 0.012 * viewport); const release = Math.min(20, 0.02 * viewport); const between = (capture + release) / 2; const checkAt = (offset, snapped) => { const free = target + sign * offset; const expected = snapped ? target : free; const result = plan(free - first); close(result.sizes[1] * length, expected); close(result.sizes[2] * length, pair - expected); assert.equal(result.sizes[0], split.sizes[0]); if (snapped && axis === 'row') { assert.equal(result.snapX?.kind, 'half'); close(result.snapX.at, guideX); } else assert.equal(result.snapX, undefined); assert.equal( result.snapY, snapped && axis === 'column' ? guideY : undefined, ); }; const checkOrigin = () => { const result = plan(0); assert.strictEqual(result.sizes, split.sizes); assert.equal(result.snapX, undefined); assert.equal(result.snapY, undefined); }; checkOrigin(); checkAt(capture + 0.01, false); checkAt(capture - 0.01, true); checkAt(release - 0.01, true); checkAt(release + 0.01, false); checkAt(between, false); checkAt(0, true); checkOrigin(); checkAt(between, false); }, ), 250, ); }); test('gutter aligns the moving edge of either row pane to either visible board edge', () => { check( fc.property(boardWidth, direction, direction, (viewport, pane, edge) => { const half = (viewport - tree.GUTTER) / 2; const first = half + 35; const target = half + 60; const { split, length } = gutterFixture('row', [first, half + 95]); const x = edge < 0 ? 0 : viewport; const gap = pane < 0 ? 0 : tree.GUTTER; const start = x - target - gap; const result = planGutter(split, 0, length, { viewport, start })( target - first + 2 * pane, ); assert.deepEqual(result.snapX, { kind: 'edge', at: x }); assert.equal(result.snapY, undefined); close(start + result.sizes[0] * length + gap, x); }), 120, ); }); test('gutter rejects stops just past either pane’s minimum', () => { check( fc.property( axis, boardWidth, direction, fc.boolean(), (axis, viewport, direction, halfSnap) => { const half = (viewport - tree.GUTTER) / 2; const minimum = tree.LEAST; const first = half + 50; const pair = first + minimum + 50; const limit = direction < 0 ? minimum : pair - minimum; // To test a half stop just past a limit, make that pane's nested minimum // greater than half; it started just above half and cannot shrink at all. const children = [leaf('gutter-before'), leaf('gutter-after')]; const shrinking = direction < 0 ? 0 : 1; let lengths = [first, pair - first]; let target = limit + direction * 3; let clamp = limit; let start = 37; if (axis === 'row' && halfSnap) { const count = Math.ceil( (half + 3 + tree.GUTTER) / (minimum + tree.GUTTER), ); children[shrinking] = { ...column('minimum'), axis, children: Array.from({ length: count }, (_, i) => leaf(`minimum-${i}`), ), sizes: Array(count).fill(1 / count), }; lengths = [half + 80, half + 80]; lengths[shrinking] = half + 3; clamp = lengths[0]; target = clamp + direction * 3; // Keep both candidate half-width guides onscreen; reachability, // not clipping or edge precedence, must reject the stop. start = (viewport - lengths[0] - lengths[1] - tree.GUTTER) / 2; } else if (axis === 'row') { // Put even the nearer pane edge on the unreachable side of the limit. start = -target - (direction < 0 ? 0 : tree.GUTTER); } const { split, length } = gutterFixture(axis, lengths, children); const { sizes, snapX, snapY } = planGutter(split, 0, length, { viewport, start, dividerStops: [start + target + tree.GUTTER / 2], })(target - lengths[0]); assert.equal(snapX, undefined); assert.equal(snapY, undefined); close(sizes[0] * length, clamp); }, ), 180, ); });