A masonry photo grid that keeps the reading order (pure CSS, no measuring in the browser), an accessible lightbox on Chakra's Dialog with wrap-around browsing, keys, swipe and preloading, and a filter bar of toggle buttons. They work on generic gallery items, so other pages can use them. The swipe logic moves out of the hero into a shared hook; the hero behaves as before. The rules behind them live in pure helpers with property-based tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import fc from 'fast-check';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { neighbourIndexes } from './neighbourIndexes';
|
|
import { wrapIndex } from './wrapIndex';
|
|
|
|
// A list position together with the list length it belongs to
|
|
const position = fc.integer({ min: 1, max: 30 }).chain((count) =>
|
|
fc.record({
|
|
count: fc.constant(count),
|
|
index: fc.integer({ min: 0, max: count - 1 }),
|
|
radius: fc.integer({ min: 0, max: 4 }),
|
|
}),
|
|
);
|
|
|
|
describe('neighbourIndexes', () => {
|
|
it('lists the two photos on each side, nearest first', () => {
|
|
expect(neighbourIndexes(5, 10)).toEqual([6, 4, 7, 3]);
|
|
expect(neighbourIndexes(0, 10)).toEqual([1, 9, 2, 8]);
|
|
});
|
|
|
|
it('lists each photo once in a short list', () => {
|
|
expect(neighbourIndexes(0, 1)).toEqual([]);
|
|
expect(neighbourIndexes(0, 2)).toEqual([1]);
|
|
expect(neighbourIndexes(1, 3)).toEqual([2, 0]);
|
|
});
|
|
|
|
it('never repeats a photo or includes the current one (invariant)', () => {
|
|
fc.assert(
|
|
fc.property(position, ({ index, count, radius }) => {
|
|
const result = neighbourIndexes(index, count, radius);
|
|
expect(new Set(result).size).toBe(result.length);
|
|
expect(result).not.toContain(index);
|
|
for (const i of result) {
|
|
expect(i).toBeGreaterThanOrEqual(0);
|
|
expect(i).toBeLessThan(count);
|
|
}
|
|
expect(result).toHaveLength(Math.min(2 * radius, count - 1));
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('matches all positions within the radius (oracle)', () => {
|
|
fc.assert(
|
|
fc.property(position, ({ index, count, radius }) => {
|
|
const expected = new Set<number>();
|
|
for (let d = -radius; d <= radius; d++) {
|
|
expected.add(wrapIndex(index + d, count));
|
|
}
|
|
expected.delete(index);
|
|
expect(new Set(neighbourIndexes(index, count, radius))).toEqual(
|
|
expected,
|
|
);
|
|
}),
|
|
);
|
|
});
|
|
});
|