canonify
Version:
**NOTE: This is effictively a fork of [@truestamp/canonify](https://www.npmjs.com/package/@truestamp/canonify) to fix an issue with exporting types. Since a repo could not be found, a fork/PR was not possible. All copyrights/attribution has been left in p
101 lines (90 loc) • 3.06 kB
text/typescript
// Copyright © 2020-2023 Truestamp Inc. All rights reserved.
import { describe, test, expect } from 'vitest'
import jsonfile from 'jsonfile'
import fs from 'fs'
import { encode as encodeHex } from '@stablelib/hex'
import { encode as encodeUtf8 } from '@stablelib/utf8'
import toHex from 'to-hex'
import { canonify } from '../src/index'
test('arrays', () => {
const input = jsonfile.readFileSync('tests/testdata/input/arrays.json')
const expected = fs
.readFileSync('tests/testdata/output/arrays.json', 'utf8')
.trim()
const expectedHex = fs
.readFileSync('tests/testdata/outhex/arrays.txt', 'utf8')
.trim()
.replace(/\s+/g, '')
expect(canonify(input)).toEqual(expected)
expect(encodeHex(encodeUtf8(canonify(input) ?? ''), true)).toEqual(
expectedHex,
)
})
test('french', () => {
const input = jsonfile.readFileSync('tests/testdata/input/french.json')
const expected = fs
.readFileSync('tests/testdata/output/french.json', 'utf8')
.trim()
const expectedHex = fs
.readFileSync('tests/testdata/outhex/french.txt', 'utf8')
.trim()
.replace(/\s+/g, '')
expect(canonify(input)).toEqual(expected)
expect(encodeHex(encodeUtf8(canonify(input) ?? ''), true)).toEqual(
expectedHex,
)
})
test('structures', () => {
const input = jsonfile.readFileSync('tests/testdata/input/structures.json')
const expected = fs
.readFileSync('tests/testdata/output/structures.json', 'utf8')
.trim()
const expectedHex = fs
.readFileSync('tests/testdata/outhex/structures.txt', 'utf8')
.trim()
.replace(/\s+/g, '')
expect(canonify(input)).toEqual(expected)
expect(encodeHex(encodeUtf8(canonify(input) ?? ''), true)).toEqual(
expectedHex,
)
})
test('unicode', () => {
const input = jsonfile.readFileSync('tests/testdata/input/unicode.json')
const expected = fs
.readFileSync('tests/testdata/output/unicode.json', 'utf8')
.trim()
const expectedHex = fs
.readFileSync('tests/testdata/outhex/unicode.txt', 'utf8')
.trim()
.replace(/\s+/g, '')
expect(canonify(input)).toEqual(expected)
expect(encodeHex(encodeUtf8(canonify(input) ?? ''), true)).toEqual(
expectedHex,
)
})
test('values', () => {
const input = jsonfile.readFileSync('tests/testdata/input/values.json')
const expected = fs
.readFileSync('tests/testdata/output/values.json', 'utf8')
.trim()
const expectedHex = fs
.readFileSync('tests/testdata/outhex/values.txt', 'utf8')
.trim()
.replace(/\s+/g, '')
expect(canonify(input)).toEqual(expected)
expect(encodeHex(encodeUtf8(canonify(input) ?? ''), true)).toEqual(
expectedHex,
)
})
test('weird', () => {
const input = jsonfile.readFileSync('tests/testdata/input/weird.json')
const expected = fs
.readFileSync('tests/testdata/output/weird.json', 'utf8')
.trim()
const expectedHex = fs
.readFileSync('tests/testdata/outhex/weird.txt', 'utf8')
.trim()
.replace(/\s+/g, '')
expect(canonify(input)).toEqual(expected)
expect(toHex(canonify(input))).toEqual(expectedHex)
})