|
| 1 | +// The sticky-stack arithmetic, and the easing curve the entrances share. |
| 2 | +// |
| 3 | +// stackDepths was 24 lines inside a useEffect closure in |
| 4 | +// components/stacking-agent-cards.tsx, so the only way to check it was to scroll |
| 5 | +// the page and watch. It is now pure, and this is its first test. |
| 6 | +// |
| 7 | +// Run with `node --test`. The rest of the module needs a DOM and is exercised |
| 8 | +// through the page; see docs/adr/0003-node-test-runner-without-ci-until-a-second-file.md |
| 9 | +// for why there is still no test script or CI step. |
| 10 | + |
| 11 | +import assert from 'node:assert/strict' |
| 12 | +import { readFileSync } from 'node:fs' |
| 13 | +import { test } from 'node:test' |
| 14 | + |
| 15 | +import { EASE, stackDepths } from './motion.ts' |
| 16 | + |
| 17 | +// A sticky card parks at `80 + 16 * i` and its rect.top falls to that value as |
| 18 | +// it sticks, so a top of 0 means fully stuck and a large top means not yet |
| 19 | +// reached. These four are the offsets the cards actually use. |
| 20 | +const STICKY = [80, 96, 112, 128] |
| 21 | + |
| 22 | +// Far below every offset: nothing has stuck yet. |
| 23 | +const UNSTUCK = [500, 400, 300, 200] |
| 24 | + |
| 25 | +test('no cards stacked when no card has reached its sticky position', () => { |
| 26 | + assert.deepEqual(stackDepths(UNSTUCK, STICKY), [0, 0, 0, 0]) |
| 27 | +}) |
| 28 | + |
| 29 | +test('every card stacked when every card has reached its sticky position', () => { |
| 30 | + assert.deepEqual(stackDepths([80, 96, 112, 128], STICKY), [3, 2, 1, 0]) |
| 31 | +}) |
| 32 | + |
| 33 | +test('depth counts only the cards below, never the ones above', () => { |
| 34 | + // Card 0 is at the bottom of the stack and has three on it; the last card has |
| 35 | + // none, whatever the others are doing. |
| 36 | + assert.deepEqual(stackDepths([200, 96, 112, 128], STICKY), [3, 2, 1, 0]) |
| 37 | + assert.equal(stackDepths([200, 96, 112, 128], STICKY).at(-1), 0) |
| 38 | +}) |
| 39 | + |
| 40 | +test('a card part-way through the stack counts the ones that have passed it', () => { |
| 41 | + // Only card 3 has reached its offset, so it sits on top of all three cards |
| 42 | + // below it. Depth is what is stacked ON a card, not how far down it is. |
| 43 | + assert.deepEqual(stackDepths([500, 400, 300, 128], STICKY), [1, 1, 1, 0]) |
| 44 | +}) |
| 45 | + |
| 46 | +test('an unmeasured card is not counted as stacked', () => { |
| 47 | + // null means the ref has not been attached yet, which is not the same as |
| 48 | + // having reached the sticky offset. |
| 49 | + assert.deepEqual(stackDepths([500, null, 300, 200], STICKY), [0, 0, 0, 0]) |
| 50 | + assert.deepEqual(stackDepths([500, null, null, 128], STICKY), [1, 1, 1, 0]) |
| 51 | +}) |
| 52 | + |
| 53 | +test('the tolerance absorbs a card sitting a hair above its offset', () => { |
| 54 | + // 129 is one pixel past card 3's own offset, so without a tolerance it reads |
| 55 | + // as not yet stuck and the card flickers between two depths on the scroll. |
| 56 | + assert.deepEqual(stackDepths([500, 400, 300, 129], STICKY), [1, 1, 1, 0]) |
| 57 | + assert.deepEqual(stackDepths([500, 400, 300, 131], STICKY), [0, 0, 0, 0]) |
| 58 | +}) |
| 59 | + |
| 60 | +test('the tolerance is a parameter, not a constant buried in the loop', () => { |
| 61 | + assert.deepEqual(stackDepths([500, 400, 300, 131], STICKY), [0, 0, 0, 0]) |
| 62 | + assert.deepEqual(stackDepths([500, 400, 300, 131], STICKY, 4), [1, 1, 1, 0]) |
| 63 | +}) |
| 64 | + |
| 65 | +test('no cards in, no depths out', () => { |
| 66 | + assert.deepEqual(stackDepths([], []), []) |
| 67 | +}) |
| 68 | + |
| 69 | +test('the easing curve is defined once and typed literally nowhere else', () => { |
| 70 | + assert.equal(EASE, 'cubic-bezier(0.16, 1, 0.3, 1)') |
| 71 | + |
| 72 | + const files = [ |
| 73 | + 'components/hero.tsx', |
| 74 | + 'components/reveal-text.tsx', |
| 75 | + 'components/stacking-agent-cards.tsx', |
| 76 | + ] |
| 77 | + for (const file of files) { |
| 78 | + const src = readFileSync(new URL(`../${file}`, import.meta.url), 'utf-8') |
| 79 | + assert.ok( |
| 80 | + src.includes("from '@/lib/motion'"), |
| 81 | + `${file} does not import the curve from lib/motion` |
| 82 | + ) |
| 83 | + } |
| 84 | +}) |
0 commit comments