test: add test for precision preservation of repeating decimals

This commit is contained in:
Johann Schopplich
2025-10-31 12:40:15 +01:00
parent bf592b0bba
commit bb1f61030e

View File

@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { encode } from '../src/index'
import { decode, encode } from '../src/index'
describe('primitives', () => {
it('encodes safe strings without quotes', () => {
@@ -57,6 +57,14 @@ describe('primitives', () => {
expect(encode(Number.MAX_SAFE_INTEGER)).toBe('9007199254740991')
})
it('preserves precision for repeating decimals', () => {
const value = 1 / 3
const encodedValue = encode({ value })
const decodedValue = decode(encodedValue)
expect((decodedValue as Record<string, unknown>)?.value).toBe(value) // Round-trip fidelity
expect(encodedValue).toContain('0.3333333333333333') // Default JS precision
})
it('encodes booleans', () => {
expect(encode(true)).toBe('true')
expect(encode(false)).toBe('false')