// Find a member (/lookup): chain.lookupPublic against a seeded index, no RPC. // node qa/lookup-unit.mjs import { createRequire } from 'node:module'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const require = createRequire(import.meta.url); const here = path.dirname(fileURLToPath(import.meta.url)); const chain = require('../chain.js'); chain.__test.setState(JSON.parse(fs.readFileSync(path.join(here, 'fixtures', 'lookup-index.json'), 'utf8'))); const ok = [], bad = []; const t = (n, c, extra) => { (c ? ok : bad).push(n + (c || !extra ? '' : ' -> ' + extra)); }; const off = { allowRpc: false }; // input validation const P = chain.parseLookupQuery; t('a plain number is an id', P('21').kind === 'id' && P('21').id === 21); t('a #-prefixed number is an id', P('#7230').id === 7230); t('whitespace is trimmed', P(' 21 ').id === 21); t('zero is refused', !!P('0').error); t('a 16-digit number is refused', !!P('1234567890123456').error); t('a full address is an address, lowercased', P('0x7c7D9F0F11bbc66a700245795c374971BfBed3c2').address === '0x7c7d9f0f11bbc66a700245795c374971bfbed3c2'); t('a short address is refused', !!P('0x7c7D9F0F11bbc66a').error); t('non-hex address is refused', !!P('0xZZ7D9F0F11bbc66a700245795c374971BfBed3c2').error); t('free text is refused', !!P('marty').error); t('script text is refused', !!P('').error); t('empty is refused', !!P('').error); // by id, from the index const a = await chain.lookupPublic('7230', off); t('#7230 is found from the index', a.found && a.source === 'index', JSON.stringify(a)); t('#7230 wallet is lowercased', a.account === '0x7c7d9f0f11bbc66a700245795c374971bfbed3c2'); t('#7230 sponsor is #21', a.sponsorId === 21); t('#7230 level/tier names', a.levelName === 'Scintilla' && a.tierName === 'Premium' && a.level === 1); t('#7230 direct count', a.directCount === 1); t('#7230 join time', a.joinedAt === 1762900000); // by wallet, mixed case, from the index const b = await chain.lookupPublic('0x7C7D9F0F11BBC66A700245795C374971BFBED3C2', off); t('wallet lookup resolves to #7230 with no RPC', b.found && b.id === 7230 && b.source === 'index', JSON.stringify(b)); const c = await chain.lookupPublic('21', off); t('#21 is Apex, qualified, sponsored by root', c.levelName === 'Apex' && c.directCount === 2 && c.sponsorId === 1); t('never copies unknown index fields (no email)', !('email' in c) && !JSON.stringify(c).includes('example.com')); const r = await chain.lookupPublic('1', off); t('the root shows no sponsor', r.found && r.sponsorId === null); // misses never call RPC when RPC is not allowed const m1 = await chain.lookupPublic('99999', off); t('an unknown id reports busy (needs a chain read)', m1.busy === true && !m1.found); const m2 = await chain.lookupPublic('0x3333333333333333333333333333333333333333', off); t('an unknown wallet reports busy (needs a chain read)', m2.busy === true && !m2.found); const m3 = await chain.lookupPublic('7231', off); t('a provisional (preIndexed) entry is re-read from the chain, not trusted', m3.busy === true); const e = await chain.lookupPublic('hello', off); t('bad input returns an error, not a lookup', !!e.error); console.log(ok.map(x => 'ok ' + x).join('\n')); if (bad.length) { console.log(bad.map(x => 'FAIL ' + x).join('\n')); process.exit(1); } console.log(`\n${ok.length} passed`);