Files
psf-memo/psf-memo-client/test/unit/set-bio-page.test.js
T
Chris Troutner ecb6ee7f28 Review and harden set-bio profile-text refactor
Merge refactorer set-bio work: MemoAction config-driven byte-limit and
profile-store reflection, shared ProfileTextPage base for SetBioPage and
SetNamePage, and byte-counting property tests. Harden the shared base with
a test asserting the in-flight flag initializes false.

By architect.
2026-08-27 08:09:02 -07:00

117 lines
3.5 KiB
JavaScript

/*
Unit tests for the Set Bio page controller.
The page controller wraps the Memo set-bio behavior, exposes a remaining
byte count, and navigates to the account page on a successful broadcast.
*/
'use strict'
const test = require('node:test')
const assert = require('node:assert/strict')
const SetBioPage = require('../../src/services/set-bio-page')
const MemoSetBio = require('../../src/services/memo-set-bio')
function makeWallet (address = 'bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d') {
return {
walletInfo: { cashAddress: address },
broadcasts: [],
async getUtxos () {
return []
},
async sendOpReturn (msg, prefix) {
this.broadcasts.push({ msg, prefix })
return 'aa'.repeat(32)
}
}
}
function makeMemoSetBio () {
const wallet = makeWallet()
return new MemoSetBio({ wallet })
}
test('the in-flight flag starts false', () => {
const page = new SetBioPage({ navigate: () => {} })
assert.equal(page.settingBio, false)
})
test('remainingCount returns full byte budget for empty input', () => {
const page = new SetBioPage({ memoSetBio: makeMemoSetBio(), navigate: () => {} })
assert.equal(page.remainingCount(), MemoSetBio.MAX_BIO_BYTES)
})
test('remainingCount subtracts the byte length of the input', () => {
const page = new SetBioPage({ memoSetBio: makeMemoSetBio(), navigate: () => {} })
page.setInput('hello')
assert.equal(page.remainingCount(), MemoSetBio.MAX_BIO_BYTES - 5)
})
test('remainingCount counts multi-byte characters correctly', () => {
const page = new SetBioPage({ memoSetBio: makeMemoSetBio(), navigate: () => {} })
page.setInput('é')
assert.equal(page.remainingCount(), MemoSetBio.MAX_BIO_BYTES - 2)
})
test('submit navigates to the account page on success', async () => {
const navigated = []
const page = new SetBioPage({
memoSetBio: makeMemoSetBio(),
navigate: (path) => navigated.push(path)
})
page.setInput('Building on BCH')
const result = await page.submit()
assert.equal(result.ok, true)
assert.deepEqual(navigated, [SetBioPage.ACCOUNT_PATH])
})
test('submit records a validation error for empty input', async () => {
const page = new SetBioPage({ memoSetBio: makeMemoSetBio(), navigate: () => {} })
page.setInput('')
const result = await page.submit()
assert.equal(result.ok, false)
assert.equal(result.error, 'bio_validation')
})
test('submit records a length error for over-long input', async () => {
const page = new SetBioPage({ memoSetBio: makeMemoSetBio(), navigate: () => {} })
page.setInput('a'.repeat(MemoSetBio.MAX_BIO_BYTES + 1))
const result = await page.submit()
assert.equal(result.ok, false)
assert.equal(result.error, 'bio_length')
})
test('submit surfaces a broadcast failure', async () => {
const memoSetBio = makeMemoSetBio()
memoSetBio.wallet.sendOpReturn = async () => { throw new Error('network down') }
const page = new SetBioPage({ memoSetBio, navigate: () => {} })
page.setInput('Building on BCH')
const result = await page.submit()
assert.equal(result.ok, false)
assert.equal(result.error, 'broadcast')
assert.match(result.message, /network down/)
})
test('submit records a broadcast error when no memo set-bio handler is injected', async () => {
const page = new SetBioPage({ navigate: () => {} })
page.setInput('Building on BCH')
const result = await page.submit()
assert.equal(result.ok, false)
assert.equal(result.error, 'broadcast')
assert.match(result.message, /Set bio requires a memo set-bio handler/)
})