|
| 1 | +/* eslint-disable test/prefer-lowercase-title */ |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { decode, encode } from '../src/index' |
| 4 | + |
| 5 | +describe('JavaScript-specific type normalization', () => { |
| 6 | + describe('BigInt normalization', () => { |
| 7 | + it('converts BigInt within safe integer range to number', () => { |
| 8 | + const result = encode(BigInt(123)) |
| 9 | + expect(result).toBe('123') |
| 10 | + }) |
| 11 | + |
| 12 | + it('converts BigInt at MAX_SAFE_INTEGER boundary to number', () => { |
| 13 | + const result = encode(BigInt(Number.MAX_SAFE_INTEGER)) |
| 14 | + expect(result).toBe('9007199254740991') |
| 15 | + }) |
| 16 | + |
| 17 | + it('converts BigInt beyond safe integer range to quoted string', () => { |
| 18 | + const result = encode(BigInt('9007199254740992')) |
| 19 | + expect(result).toBe('"9007199254740992"') |
| 20 | + }) |
| 21 | + |
| 22 | + it('converts large BigInt to quoted decimal string', () => { |
| 23 | + const result = encode(BigInt('12345678901234567890')) |
| 24 | + expect(result).toBe('"12345678901234567890"') |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + describe('Date normalization', () => { |
| 29 | + it('converts Date to ISO 8601 quoted string', () => { |
| 30 | + const result = encode(new Date('2025-01-01T00:00:00.000Z')) |
| 31 | + expect(result).toBe('"2025-01-01T00:00:00.000Z"') |
| 32 | + }) |
| 33 | + |
| 34 | + it('converts Date with milliseconds to ISO quoted string', () => { |
| 35 | + const result = encode(new Date('2025-11-05T12:34:56.789Z')) |
| 36 | + expect(result).toBe('"2025-11-05T12:34:56.789Z"') |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + describe('Set normalization', () => { |
| 41 | + it('converts Set to array', () => { |
| 42 | + const input = new Set(['a', 'b', 'c']) |
| 43 | + const encoded = encode(input) |
| 44 | + const decoded = decode(encoded) |
| 45 | + expect(decoded).toEqual(['a', 'b', 'c']) |
| 46 | + }) |
| 47 | + |
| 48 | + it('converts empty Set to empty array', () => { |
| 49 | + const result = encode(new Set()) |
| 50 | + expect(result).toBe('[0]:') |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + describe('Map normalization', () => { |
| 55 | + it('converts Map to object', () => { |
| 56 | + const input = new Map([['key1', 'value1'], ['key2', 'value2']]) |
| 57 | + const encoded = encode(input) |
| 58 | + const decoded = decode(encoded) |
| 59 | + expect(decoded).toEqual({ key1: 'value1', key2: 'value2' }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('converts empty Map to empty object', () => { |
| 63 | + const input = new Map() |
| 64 | + const result = encode(input) |
| 65 | + expect(result).toBe('') |
| 66 | + }) |
| 67 | + |
| 68 | + it('converts Map with numeric keys to object with quoted string keys', () => { |
| 69 | + const input = new Map([[1, 'one'], [2, 'two']]) |
| 70 | + const result = encode(input) |
| 71 | + expect(result).toBe('"1": one\n"2": two') |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe('undefined, function, and Symbol normalization', () => { |
| 76 | + it('converts undefined to null', () => { |
| 77 | + const result = encode(undefined) |
| 78 | + expect(result).toBe('null') |
| 79 | + }) |
| 80 | + |
| 81 | + it('converts function to null', () => { |
| 82 | + const result = encode(() => {}) |
| 83 | + expect(result).toBe('null') |
| 84 | + }) |
| 85 | + |
| 86 | + it('converts Symbol to null', () => { |
| 87 | + const result = encode(Symbol('test')) |
| 88 | + expect(result).toBe('null') |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + describe('NaN and Infinity normalization', () => { |
| 93 | + it('converts NaN to null', () => { |
| 94 | + const result = encode(Number.NaN) |
| 95 | + expect(result).toBe('null') |
| 96 | + }) |
| 97 | + |
| 98 | + it('converts Infinity to null', () => { |
| 99 | + const result = encode(Number.POSITIVE_INFINITY) |
| 100 | + expect(result).toBe('null') |
| 101 | + }) |
| 102 | + |
| 103 | + it('converts negative Infinity to null', () => { |
| 104 | + const result = encode(Number.NEGATIVE_INFINITY) |
| 105 | + expect(result).toBe('null') |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + describe('negative zero normalization', () => { |
| 110 | + it('normalizes -0 to 0', () => { |
| 111 | + const result = encode(-0) |
| 112 | + expect(result).toBe('0') |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments