|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { utilTable } from '../blocks-mapping/tablePDF'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for utilTable utility. |
| 7 | + * Scenarios covered: |
| 8 | + * - All widths specified and below full width |
| 9 | + * - Mix of known / unknown widths (fallback distribution) |
| 10 | + * - All widths unknown |
| 11 | + * - Widths exceeding full table width (clamping & scale=100) |
| 12 | + * - Sum exceeding full width without unknowns (no division by zero side-effects) |
| 13 | + */ |
| 14 | + |
| 15 | +describe('utilTable', () => { |
| 16 | + it('returns unchanged widths and correct scale when all widths are known and below full width', () => { |
| 17 | + const input = [165, 200]; |
| 18 | + const { columnWidths, tableScale } = utilTable(730, input); |
| 19 | + expect(columnWidths).toEqual(input); // unchanged |
| 20 | + expect(tableScale).toBe(50); |
| 21 | + }); |
| 22 | + |
| 23 | + it('distributes fallback width equally among unknown columns', () => { |
| 24 | + const input: (number | undefined)[] = [100, undefined, 200, undefined]; |
| 25 | + const { columnWidths, tableScale } = utilTable(730, input); |
| 26 | + expect(columnWidths).toEqual([100, 215, 200, 215]); |
| 27 | + expect(tableScale).toBe(100); // fills full width exactly |
| 28 | + }); |
| 29 | + |
| 30 | + it('handles all columns unknown', () => { |
| 31 | + const input: (number | undefined)[] = [undefined, undefined]; |
| 32 | + const { columnWidths, tableScale } = utilTable(730, input); |
| 33 | + expect(columnWidths).toEqual([365, 365]); |
| 34 | + expect(tableScale).toBe(100); |
| 35 | + }); |
| 36 | + |
| 37 | + it('clamps total width to full width when sum exceeds it (single large column)', () => { |
| 38 | + const input = [800]; |
| 39 | + const { columnWidths, tableScale } = utilTable(730, input); |
| 40 | + expect(columnWidths).toEqual([800]); |
| 41 | + expect(tableScale).toBe(100); |
| 42 | + }); |
| 43 | + |
| 44 | + it('clamps total width to full width when multiple columns exceed it', () => { |
| 45 | + const input = [500, 300]; // sum = 800 > 730 |
| 46 | + const { columnWidths, tableScale } = utilTable(730, input); |
| 47 | + expect(columnWidths).toEqual([500, 300]); |
| 48 | + expect(tableScale).toBe(100); |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not assign fallback when there are no unknown widths (avoid division by zero impact)', () => { |
| 52 | + const input = [400, 400]; |
| 53 | + const { columnWidths, tableScale } = utilTable(730, input); |
| 54 | + expect(columnWidths).toEqual([400, 400]); |
| 55 | + expect(tableScale).toBe(100); |
| 56 | + }); |
| 57 | + |
| 58 | + it('computes proportional scale with custom fullWidth', () => { |
| 59 | + const input = [100, 200]; // total 300 |
| 60 | + const { columnWidths, tableScale } = utilTable(1000, input); |
| 61 | + expect(columnWidths).toEqual([100, 200]); |
| 62 | + expect(tableScale).toBe(30); |
| 63 | + }); |
| 64 | +}); |
0 commit comments