Files
Chris Troutner 6b7a7e2b86 Reduce CRAP in like/tip slice and add property/coverage tests
- Extract helpers in memo-like to bring all CRAP scores to 6 or below.
- Consolidate duplicated rejection assertions in memo-like and like-tip-page tests.
- Add hex unit tests to close a coverage gap.
- Add property tests for hex round-trip, like validation, and spendable sum.

By refactorer.
2026-08-26 06:23:23 -07:00

51 lines
1.3 KiB
JavaScript

/*
Unit tests for the hex conversion helper (src/services/hex.js).
Memo actions like replies and likes embed a post txid in the OP_RETURN
payload as raw bytes, so the helper must decode a canonical 64-character hex
string into exactly 32 bytes and reject anything else with a clear error.
*/
'use strict'
const test = require('node:test')
const assert = require('node:assert/strict')
const { hexToBytes } = require('../../src/services/hex')
test('hexToBytes decodes a 64-char hex string into 32 bytes', () => {
const hex = 'a'.repeat(64)
const bytes = hexToBytes(hex, 32)
assert.ok(bytes instanceof Uint8Array)
assert.equal(bytes.length, 32)
assert.equal(Buffer.from(bytes).toString('hex'), hex)
})
test('hexToBytes rejects a string of the wrong length', () => {
assert.throws(
() => hexToBytes('a'.repeat(10), 32),
/64-character hex string/
)
})
test('hexToBytes rejects a 64-char string containing a non-hex character', () => {
assert.throws(
() => hexToBytes(`z${'a'.repeat(63)}`, 32),
/valid hex string/
)
})
test('hexToBytes rejects non-string input', () => {
assert.throws(
() => hexToBytes(null, 32),
/64-character hex string/
)
})
test('hexToBytes uses a custom label in error messages', () => {
assert.throws(
() => hexToBytes('nope', 32, 'Post txid'),
/Post txid/
)
})