aboutsummaryrefslogtreecommitdiffstats
path: root/names/forget.test.js
blob: ec1c22a91f4b328bbce02b94758e86fb745246e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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);
});