|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { safeCompare } from './compare' |
| 3 | + |
| 4 | +describe('safeCompare', () => { |
| 5 | + it('returns true for identical strings', () => { |
| 6 | + expect(safeCompare('abc', 'abc')).toBe(true) |
| 7 | + }) |
| 8 | + |
| 9 | + it('returns false for equal-length different strings', () => { |
| 10 | + expect(safeCompare('abc', 'abd')).toBe(false) |
| 11 | + }) |
| 12 | + |
| 13 | + it('returns false for different-length strings without throwing', () => { |
| 14 | + expect(safeCompare('short', 'longer-value')).toBe(false) |
| 15 | + expect(safeCompare('', 'a')).toBe(false) |
| 16 | + expect(safeCompare('a', '')).toBe(false) |
| 17 | + }) |
| 18 | + |
| 19 | + it('returns true for two empty strings', () => { |
| 20 | + expect(safeCompare('', '')).toBe(true) |
| 21 | + }) |
| 22 | + |
| 23 | + it('handles long inputs', () => { |
| 24 | + const a = 'x'.repeat(10_000) |
| 25 | + const b = 'x'.repeat(10_000) |
| 26 | + expect(safeCompare(a, b)).toBe(true) |
| 27 | + expect(safeCompare(a, `${b.slice(0, -1)}y`)).toBe(false) |
| 28 | + }) |
| 29 | + |
| 30 | + it('is case-sensitive', () => { |
| 31 | + expect(safeCompare('ABC', 'abc')).toBe(false) |
| 32 | + }) |
| 33 | + |
| 34 | + it('distinguishes hex digests that differ in one nibble', () => { |
| 35 | + const a = 'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7' |
| 36 | + const b = 'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff8' |
| 37 | + expect(safeCompare(a, b)).toBe(false) |
| 38 | + expect(safeCompare(a, a)).toBe(true) |
| 39 | + }) |
| 40 | +}) |
0 commit comments