aboutsummaryrefslogtreecommitdiffstats
path: root/names/forget.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'names/forget.test.js')
-rw-r--r--names/forget.test.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/names/forget.test.js b/names/forget.test.js
new file mode 100644
index 0000000..ec1c22a
--- /dev/null
+++ b/names/forget.test.js
@@ -0,0 +1,86 @@
+// 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: {
+ '[email protected]': { pubkey: LOGIN, manager: WALLET, recordId: 'r1', domain: 'coinos.io', offerId: 'o-name' },
+ '[email protected]': { pubkey: OTHER, recordId: 'r2', domain: 'coinos.io' },
+ },
+ offers: { 'o-custom': { key: '[email protected]', pubkey: LOGIN }, 'o-orphan': { key: '[email protected]', pubkey: WALLET }, 'o-bob': { key: '[email protected]', pubkey: OTHER } },
+ posTokens: { t1: { key: '[email protected]' }, t2: { key: '[email protected]' } },
+ posInvoices: { h1: { key: '[email protected]', sat: 5 }, h2: { key: '[email protected]', sat: 5 } },
+ invoices: { h9: { key: '[email protected]' } },
+ hats: { [LOGIN]: { owned: ['cap'] }, [OTHER]: { owned: ['crown'] } },
+ hatInvoices: { hh: { pubkey: WALLET }, hb: { pubkey: OTHER } },
+ welcomed: { [LOGIN]: 1, [WALLET]: 1, [OTHER]: 1 },
+ lnReceipts: { '[email protected]': [{ sat: 1 }], '[email protected]': [{ sat: 2 }] },
+ migrations: { '[email protected]': { pubkey: LOGIN, manager: WALLET }, '[email protected]': { pubkey: OTHER } },
+ webhookQueue: [{ key: '[email protected]' }, { key: '[email protected]' }],
+ 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(['[email protected]']);
+ expect([...a.keys].sort()).toEqual([WALLET, LOGIN].sort());
+ expect(accountOf(fixture(), LOGIN).names).toEqual(['[email protected]']);
+ 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(['[email protected]']);
+ 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(['[email protected]']);
+ expect(Object.keys(s.migrations)).toEqual(['[email protected]']);
+ expect(s.webhookQueue).toEqual([{ key: '[email protected]' }]);
+ 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: '[email protected]', sat: 21, hash: 'x' });
+ expect(accountBlockers(s, accountOf(s, WALLET))).toMatchObject({ pendingSat: 21, any: true });
+ s = fixture();
+ s.parked.push({ name: '[email protected]', pubkey: LOGIN, sat: 7 });
+ expect(accountBlockers(s, accountOf(s, WALLET))).toMatchObject({ pendingSat: 7, any: true });
+ s = fixture();
+ s.inflight.z = { name: '[email protected]', 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);
+});