import { test, describe } from 'node:test' import assert from 'node:assert' import { cborEncode, createCid, cidToString, base32Encode, createTid } from '../src/pds.js' describe('CBOR Encoding', () => { test('encodes simple map', () => { const encoded = cborEncode({ hello: 'world', num: 42 }) // Expected: a2 65 68 65 6c 6c 6f 65 77 6f 72 6c 64 63 6e 75 6d 18 2a const expected = new Uint8Array([ 0xa2, 0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x65, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x63, 0x6e, 0x75, 0x6d, 0x18, 0x2a ]) assert.deepStrictEqual(encoded, expected) }) test('encodes null', () => { const encoded = cborEncode(null) assert.deepStrictEqual(encoded, new Uint8Array([0xf6])) }) test('encodes booleans', () => { assert.deepStrictEqual(cborEncode(true), new Uint8Array([0xf5])) assert.deepStrictEqual(cborEncode(false), new Uint8Array([0xf4])) }) test('encodes small integers', () => { assert.deepStrictEqual(cborEncode(0), new Uint8Array([0x00])) assert.deepStrictEqual(cborEncode(1), new Uint8Array([0x01])) assert.deepStrictEqual(cborEncode(23), new Uint8Array([0x17])) }) test('encodes integers >= 24', () => { assert.deepStrictEqual(cborEncode(24), new Uint8Array([0x18, 0x18])) assert.deepStrictEqual(cborEncode(255), new Uint8Array([0x18, 0xff])) }) test('encodes negative integers', () => { assert.deepStrictEqual(cborEncode(-1), new Uint8Array([0x20])) assert.deepStrictEqual(cborEncode(-10), new Uint8Array([0x29])) }) test('encodes strings', () => { const encoded = cborEncode('hello') // 0x65 = text string of length 5 assert.deepStrictEqual(encoded, new Uint8Array([0x65, 0x68, 0x65, 0x6c, 0x6c, 0x6f])) }) test('encodes byte strings', () => { const bytes = new Uint8Array([1, 2, 3]) const encoded = cborEncode(bytes) // 0x43 = byte string of length 3 assert.deepStrictEqual(encoded, new Uint8Array([0x43, 1, 2, 3])) }) test('encodes arrays', () => { const encoded = cborEncode([1, 2, 3]) // 0x83 = array of length 3 assert.deepStrictEqual(encoded, new Uint8Array([0x83, 0x01, 0x02, 0x03])) }) test('sorts map keys deterministically', () => { const encoded1 = cborEncode({ z: 1, a: 2 }) const encoded2 = cborEncode({ a: 2, z: 1 }) assert.deepStrictEqual(encoded1, encoded2) // First key should be 'a' (0x61) assert.strictEqual(encoded1[1], 0x61) }) }) describe('Base32 Encoding', () => { test('encodes bytes to base32lower', () => { const bytes = new Uint8Array([0x01, 0x71, 0x12, 0x20]) const encoded = base32Encode(bytes) assert.strictEqual(typeof encoded, 'string') assert.match(encoded, /^[a-z2-7]+$/) }) }) describe('CID Generation', () => { test('creates CIDv1 with dag-cbor codec', async () => { const data = cborEncode({ test: 'data' }) const cid = await createCid(data) assert.strictEqual(cid.length, 36) // 2 prefix + 2 multihash header + 32 hash assert.strictEqual(cid[0], 0x01) // CIDv1 assert.strictEqual(cid[1], 0x71) // dag-cbor assert.strictEqual(cid[2], 0x12) // sha-256 assert.strictEqual(cid[3], 0x20) // 32 bytes }) test('cidToString returns base32lower with b prefix', async () => { const data = cborEncode({ test: 'data' }) const cid = await createCid(data) const cidStr = cidToString(cid) assert.strictEqual(cidStr[0], 'b') assert.match(cidStr, /^b[a-z2-7]+$/) }) test('same input produces same CID', async () => { const data1 = cborEncode({ test: 'data' }) const data2 = cborEncode({ test: 'data' }) const cid1 = cidToString(await createCid(data1)) const cid2 = cidToString(await createCid(data2)) assert.strictEqual(cid1, cid2) }) test('different input produces different CID', async () => { const cid1 = cidToString(await createCid(cborEncode({ a: 1 }))) const cid2 = cidToString(await createCid(cborEncode({ a: 2 }))) assert.notStrictEqual(cid1, cid2) }) }) describe('TID Generation', () => { test('creates 13-character TIDs', () => { const tid = createTid() assert.strictEqual(tid.length, 13) }) test('uses valid base32-sort characters', () => { const tid = createTid() assert.match(tid, /^[234567abcdefghijklmnopqrstuvwxyz]+$/) }) test('generates monotonically increasing TIDs', () => { const tid1 = createTid() const tid2 = createTid() const tid3 = createTid() assert.ok(tid1 < tid2, `${tid1} should be less than ${tid2}`) assert.ok(tid2 < tid3, `${tid2} should be less than ${tid3}`) }) test('generates unique TIDs', () => { const tids = new Set() for (let i = 0; i < 100; i++) { tids.add(createTid()) } assert.strictEqual(tids.size, 100) }) })