import { test } from 'node:test'; import assert from 'node:assert/strict'; import { classify, hrefFor, ign, ignFromWei, shortHash, hashrate, rel, int, utc } from './explorer.mjs'; const H = '2622db7698c6825b370bbffba03ea11bba250792040296971d2b790a954203ee'; test('classify: a block hash with or without 0x, any case', () => { assert.deepEqual(classify(H), { type: 'hash', value: H }); assert.deepEqual(classify('0x' + H.toUpperCase()), { type: 'hash', value: H }); assert.deepEqual(classify(' ' + H + '\n'), { type: 'hash', value: H }); }); test('classify: an EVM address and a bech32 address', () => { assert.deepEqual(classify('0x37B55C8531053a5cef7fe1f19ba67012719773f9'), { type: 'address', value: '0x37b55c8531053a5cef7fe1f19ba67012719773f9', kind: 'evm' }); const b = 'igneumdev:qr4yfyf9mzn643fj8faksflmxur0wxgtmpg8y7fa6qkm8pcjh3ngzqedfx47e'; assert.deepEqual(classify(b), { type: 'address', value: b, kind: 'utxo' }); assert.equal(classify('igneum:qr4yfyf9mzn643fj8faksflmxur0wxgtmpg8y7fa6qkm8pcjh3ngzqedfx47e').kind, 'utxo'); }); test('classify: digits are a height, the rest is nothing', () => { assert.deepEqual(classify('81263'), { type: 'height', value: 81263 }); assert.equal(classify('0x1234').type, 'none'); assert.equal(classify('').type, 'none'); assert.equal(classify('hello').type, 'none'); assert.equal(classify('0x' + 'a'.repeat(63)).type, 'none'); assert.equal(classify('1'.repeat(13)).type, 'none'); }); test('hrefFor routes hash, height and address', () => { assert.equal(hrefFor(classify(H)), '/block/' + H); assert.equal(hrefFor(classify('12')), '/block/12'); assert.equal(hrefFor(classify('0x37b55c8531053a5cef7fe1f19ba67012719773f9')), '/address/0x37b55c8531053a5cef7fe1f19ba67012719773f9'); assert.equal(hrefFor(classify('nope')), null); }); test('ign formats sompi, trims zeros, keeps places', () => { assert.equal(ign('454485299'), '4.5448'); assert.equal(ign('454485299', 8), '4.54485299'); assert.equal(ign(100000000n), '1'); assert.equal(ign('123456789012345678', 2), '1,234,567,890.12'); assert.equal(ign(null), 'n/a'); assert.equal(ign('x'), 'n/a'); }); test('ignFromWei formats wei', () => { assert.equal(ignFromWei('0x3275f7e5ad621000'), '3.636'); assert.equal(ignFromWei('3636084840000000000', 9), '3.63608484'); assert.equal(ignFromWei(0), '0'); }); test('shortHash, hashrate, rel, int, utc', () => { assert.equal(shortHash(H), '2622db76…4203ee'); assert.equal(shortHash('abc'), 'abc'); assert.equal(hashrate(27759288), '27.8 MH/s'); assert.equal(hashrate(999), '999 H/s'); assert.equal(hashrate(null), 'n/a'); assert.equal(rel(12_400), '12 s ago'); assert.equal(rel(3 * 60_000), '3 min ago'); assert.equal(rel(125 * 60_000), '2 h 5 min ago'); assert.equal(rel(49 * 3_600_000), '2 d ago'); assert.equal(int(1234567.4), '1,234,567'); assert.equal(utc(1791227608731), '2026-10-05 19:13:28 UTC'); assert.equal(utc(null), 'n/a'); });