import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import { imageSize } from '../test/arbitraries'; import { GAP_ROWS, masonryRowSpan, ROWS_PER_COLUMN_WIDTH, } from './masonryRowSpan'; const K = ROWS_PER_COLUMN_WIDTH; describe('masonryRowSpan', () => { it('spans rows by aspect ratio plus the gap', () => { // Portrait 2:3, landscape 3:2, square expect(masonryRowSpan({ width: 800, height: 1200 })).toBe( 60 + GAP_ROWS, ); expect(masonryRowSpan({ width: 1200, height: 800 })).toBe( 27 + GAP_ROWS, ); expect(masonryRowSpan({ width: 800, height: 800 })).toBe(40 + GAP_ROWS); }); it('gives even a very wide photo at least one row', () => { expect(masonryRowSpan({ width: 10000, height: 10 })).toBe(1 + GAP_ROWS); }); it('is at least one row plus the gap (invariant)', () => { fc.assert( fc.property(imageSize, (size) => { expect(masonryRowSpan(size)).toBeGreaterThanOrEqual( GAP_ROWS + 1, ); }), ); }); it('grows with the photo height relative to its width (invariant)', () => { fc.assert( fc.property(imageSize, imageSize, (a, b) => { const ratioA = a.height / a.width; const ratioB = b.height / b.width; if (ratioA <= ratioB) { expect(masonryRowSpan(a)).toBeLessThanOrEqual( masonryRowSpan(b), ); } }), ); }); it('depends on the shape, not the file size (invariant)', () => { fc.assert( fc.property( imageSize, fc.integer({ min: 2, max: 4 }), (size, factor) => { expect( masonryRowSpan({ width: size.width * factor, height: size.height * factor, }), ).toBe(masonryRowSpan(size)); }, ), ); }); it('crops at most half a row (easy verification)', () => { fc.assert( fc.property(imageSize, (size) => { const exact = (K * size.height) / size.width; if (exact >= 1) { const rows = masonryRowSpan(size) - GAP_ROWS; expect(Math.abs(rows - exact)).toBeLessThanOrEqual(0.5); } }), ); }); });