|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + analyzeIconSource, |
| 4 | + effectiveFractionDigits, |
| 5 | + findPrecisionCandidates, |
| 6 | +} from './check-icon-path-precision' |
| 7 | + |
| 8 | +const FIXTURE_PATH = '/repo/packages/emcn/src/icons/fixture.tsx' |
| 9 | + |
| 10 | +describe('icon path precision audit', () => { |
| 11 | + it('accepts the three-decimal boundary and ignores geometry outside literal paths', () => { |
| 12 | + const source = ` |
| 13 | + const dynamicPath = 'M0.1234 1' |
| 14 | + const unrelated = "d='M0.1234 1'" |
| 15 | + export function SafeIcon() { |
| 16 | + return ( |
| 17 | + <svg viewBox='0 0 10.1234 10' transform='scale(0.16624)'> |
| 18 | + <path d='M.5 1.200L1.2e1 2' /> |
| 19 | + <path d={dynamicPath} /> |
| 20 | + </svg> |
| 21 | + ) |
| 22 | + } |
| 23 | + ` |
| 24 | + |
| 25 | + expect(analyzeIconSource(source, FIXTURE_PATH)).toEqual({ |
| 26 | + candidates: [], |
| 27 | + invalidExceptions: [], |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('finds ordinary decimals and exponents finer than a thousandth', () => { |
| 32 | + const source = ` |
| 33 | + export function PreciseIcon() { |
| 34 | + return <svg><path d='M0.1234 1e-4L2.345 5' /></svg> |
| 35 | + } |
| 36 | + ` |
| 37 | + |
| 38 | + const candidates = findPrecisionCandidates(source, FIXTURE_PATH) |
| 39 | + expect(candidates).toHaveLength(1) |
| 40 | + expect(candidates[0]).toMatchObject({ |
| 41 | + icon: 'PreciseIcon', |
| 42 | + maxFractionDigits: 4, |
| 43 | + offendingNumbers: ['0.1234', '1e-4'], |
| 44 | + }) |
| 45 | + expect(effectiveFractionDigits('2.13949e-05')).toBe(10) |
| 46 | + }) |
| 47 | + |
| 48 | + it('checks literal JSX expressions and static template literals', () => { |
| 49 | + const source = ` |
| 50 | + export function ExpressionIcon() { |
| 51 | + return ( |
| 52 | + <svg> |
| 53 | + <path d={'M0.1234 1'} /> |
| 54 | + <path d={\`M2.3456 3\`} /> |
| 55 | + </svg> |
| 56 | + ) |
| 57 | + } |
| 58 | + ` |
| 59 | + |
| 60 | + expect(findPrecisionCandidates(source, FIXTURE_PATH)).toHaveLength(2) |
| 61 | + }) |
| 62 | + |
| 63 | + it('accepts a reasoned TSDoc exception on the immediately following path', () => { |
| 64 | + const source = ` |
| 65 | + export function PreciseBrandIcon() { |
| 66 | + return ( |
| 67 | + <svg> |
| 68 | + {/** |
| 69 | + * svg-path-precision-exception: Rounding visibly distorts the provider-authored mark. |
| 70 | + */} |
| 71 | + <path d='M0.1234 1' /> |
| 72 | + </svg> |
| 73 | + ) |
| 74 | + } |
| 75 | + ` |
| 76 | + |
| 77 | + expect(analyzeIconSource(source, FIXTURE_PATH)).toEqual({ |
| 78 | + candidates: [], |
| 79 | + invalidExceptions: [], |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('rejects exceptions without a reason and exceptions on already-clean paths', () => { |
| 84 | + const missingReason = ` |
| 85 | + export function PreciseIcon() { |
| 86 | + return <svg>{/** svg-path-precision-exception: */}<path d='M0.1234 1' /></svg> |
| 87 | + } |
| 88 | + ` |
| 89 | + const unnecessary = ` |
| 90 | + export function CleanIcon() { |
| 91 | + return <svg>{/** svg-path-precision-exception: Keep detail. */}<path d='M0.12 1' /></svg> |
| 92 | + } |
| 93 | + ` |
| 94 | + |
| 95 | + const missingReasonAnalysis = analyzeIconSource(missingReason, FIXTURE_PATH) |
| 96 | + expect(missingReasonAnalysis.candidates).toHaveLength(1) |
| 97 | + expect(missingReasonAnalysis.invalidExceptions[0]?.message).toContain('specific reason') |
| 98 | + |
| 99 | + const unnecessaryAnalysis = analyzeIconSource(unnecessary, FIXTURE_PATH) |
| 100 | + expect(unnecessaryAnalysis.candidates).toEqual([]) |
| 101 | + expect(unnecessaryAnalysis.invalidExceptions[0]?.message).toContain('unnecessary') |
| 102 | + }) |
| 103 | + |
| 104 | + it('requires a reasoned exception to remain while its precise path remains', () => { |
| 105 | + const excepted = ` |
| 106 | + export function PreciseBrandIcon() { |
| 107 | + return ( |
| 108 | + <svg> |
| 109 | + {/** svg-path-precision-exception: Rounding visibly distorts the brand mark. */} |
| 110 | + <path d='M0.1234 1' /> |
| 111 | + </svg> |
| 112 | + ) |
| 113 | + } |
| 114 | + ` |
| 115 | + const exceptionRemoved = excepted.replace( |
| 116 | + '{/** svg-path-precision-exception: Rounding visibly distorts the brand mark. */}', |
| 117 | + '' |
| 118 | + ) |
| 119 | + |
| 120 | + expect(findPrecisionCandidates(excepted, FIXTURE_PATH)).toEqual([]) |
| 121 | + expect(findPrecisionCandidates(exceptionRemoved, FIXTURE_PATH)).toHaveLength(1) |
| 122 | + }) |
| 123 | +}) |
0 commit comments