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(); 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, ); }), ); }); });