// bun test names/forget.test.js import { test, expect } from 'bun:test'; import { accountOf, accountBlockers, forgetAccount } from './forget.js'; const hex = (i) => i.toString(16).padStart(64, '0'); const WALLET = hex(1), LOGIN = hex(2), OTHER = hex(3); const fixture = () => ({ names: { 'andy@coinos.io': { pubkey: LOGIN, manager: WALLET, recordId: 'r1', domain: 'coinos.io', offerId: 'o-name' }, 'bob@coinos.io': { pubkey: OTHER, recordId: 'r2', domain: 'coinos.io' }, }, offers: { 'o-custom': { key: 'andy@coinos.io', pubkey: LOGIN }, 'o-orphan': { key: 'gone@coinos.io', pubkey: WALLET }, 'o-bob': { key: 'bob@coinos.io', pubkey: OTHER } }, posTokens: { t1: { key: 'andy@coinos.io' }, t2: { key: 'bob@coinos.io' } }, posInvoices: { h1: { key: 'andy@coinos.io', sat: 5 }, h2: { key: 'bob@coinos.io', sat: 5 } }, invoices: { h9: { key: 'andy@coinos.io' } }, hats: { [LOGIN]: { owned: ['cap'] }, [OTHER]: { owned: ['crown'] } }, hatInvoices: { hh: { pubkey: WALLET }, hb: { pubkey: OTHER } }, welcomed: { [LOGIN]: 1, [WALLET]: 1, [OTHER]: 1 }, lnReceipts: { 'andy@coinos.io': [{ sat: 1 }], 'bob@coinos.io': [{ sat: 2 }] }, migrations: { 'andy@coinos.io': { pubkey: LOGIN, manager: WALLET }, 'bob@coinos.io': { pubkey: OTHER } }, webhookQueue: [{ key: 'andy@coinos.io' }, { key: 'bob@coinos.io' }], withdraws: { w1: { id: 'w1', pubkey: WALLET, status: 'closed', totalSat: 100, spentSat: 100, claims: [] }, w2: { id: 'w2', pubkey: WALLET, status: 'unfunded', totalSat: 50, spentSat: 0, fundHash: 'fh', claims: [] }, w3: { id: 'w3', pubkey: OTHER, status: 'open', totalSat: 50, spentSat: 0, claims: [] }, }, withdrawFunding: { fh: 'w2' }, withdrawPaid: { ph: 'w1' }, delivered: { d1: 1 }, pending: [], parked: [], inflight: {}, }); test('the wallet key answers for the login identity\'s name it manages', () => { const a = accountOf(fixture(), WALLET); expect(a.names).toEqual(['andy@coinos.io']); expect([...a.keys].sort()).toEqual([WALLET, LOGIN].sort()); expect(accountOf(fixture(), LOGIN).names).toEqual(['andy@coinos.io']); expect(accountOf(fixture(), hex(77)).names).toEqual([]); }); test('everything of theirs goes, nobody else\'s does', () => { const s = fixture(); const acct = accountOf(s, WALLET); expect(accountBlockers(s, acct).any).toBe(false); const out = forgetAccount(s, acct); expect(out.dns).toEqual([{ domain: 'coinos.io', recordId: 'r1' }]); expect(out.offers.sort()).toEqual(['o-custom', 'o-name', 'o-orphan']); expect(Object.keys(s.names)).toEqual(['bob@coinos.io']); expect(Object.keys(s.offers)).toEqual(['o-bob']); expect(Object.keys(s.posTokens)).toEqual(['t2']); expect(Object.keys(s.posInvoices)).toEqual(['h2']); expect(Object.keys(s.hats)).toEqual([OTHER]); expect(Object.keys(s.hatInvoices)).toEqual(['hb']); expect(Object.keys(s.welcomed)).toEqual([OTHER]); expect(Object.keys(s.lnReceipts)).toEqual(['bob@coinos.io']); expect(Object.keys(s.migrations)).toEqual(['bob@coinos.io']); expect(s.webhookQueue).toEqual([{ key: 'bob@coinos.io' }]); expect(Object.keys(s.withdraws)).toEqual(['w3']); expect(s.withdrawFunding).toEqual({}); // what guards against paying twice, and the unpaid-invoice map, stay expect(s.withdrawPaid).toEqual({ ph: 'w1' }); expect(s.delivered).toEqual({ d1: 1 }); expect(Object.keys(s.invoices)).toEqual(['h9']); // a second run finds nothing expect(forgetAccount(s, accountOf(s, WALLET)).removed).toEqual({}); }); test('money on its way, or held in a withdraw link, blocks', () => { let s = fixture(); s.pending.push({ name: 'andy@coinos.io', sat: 21, hash: 'x' }); expect(accountBlockers(s, accountOf(s, WALLET))).toMatchObject({ pendingSat: 21, any: true }); s = fixture(); s.parked.push({ name: 'gone@coinos.io', pubkey: LOGIN, sat: 7 }); expect(accountBlockers(s, accountOf(s, WALLET))).toMatchObject({ pendingSat: 7, any: true }); s = fixture(); s.inflight.z = { name: 'andy@coinos.io', sat: 9 }; expect(accountBlockers(s, accountOf(s, WALLET)).pendingSat).toBe(9); s = fixture(); s.withdraws.w1 = { id: 'w1', pubkey: WALLET, status: 'open', totalSat: 100, spentSat: 40, claims: [] }; expect(accountBlockers(s, accountOf(s, WALLET))).toMatchObject({ withdrawSat: 60, any: true }); s = fixture(); s.withdraws.w1.claims.push({ status: 'paying' }); expect(accountBlockers(s, accountOf(s, WALLET)).any).toBe(true); // someone else's open link is not ours to wait on expect(accountBlockers(fixture(), accountOf(fixture(), WALLET)).any).toBe(false); });