securityos/node_modules/multiformats/test/test-varint.spec.js

25 lines
814 B
JavaScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
/* globals describe, it */
import { varint } from '../src/index.js'
import { assert } from 'aegir/chai'
const UTF8 = new TextEncoder()
describe('varint', () => {
it('can decode with offset', () => {
const message = UTF8.encode('hello-world')
const outerTag = 0x55
const innerTag = 0xe3
const outerTagSize = varint.encodingLength(outerTag)
const innerTagSize = varint.encodingLength(innerTag)
const bytes = new Uint8Array(message.byteLength + outerTagSize + innerTagSize)
varint.encodeTo(outerTag, bytes)
varint.encodeTo(innerTag, bytes, outerTagSize)
bytes.set(message, outerTagSize + innerTagSize)
assert.deepStrictEqual(varint.decode(bytes), [outerTag, outerTagSize])
assert.deepStrictEqual(varint.decode(bytes, outerTagSize), [innerTag, innerTagSize])
})
})