aboutsummaryrefslogtreecommitdiffstats
path: root/test/e2e/vault-receive-bitcoin.spec.ts
blob: 32fc9be4e439b65dec1c85c642e56d350e3bee52 (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
import { expect, test } from "@playwright/test";
import {
  aliceUsername,
  alicePassword,
  login,
  apiLogin,
  ensureVaultAccount,
  setActiveAccount,
  createVaultBitcoinInvoiceViaUI,
  bitcoinSend,
  mineBlocks,
  waitForInvoicePaid,
} from "./helpers";

test("bitcoin vault receives external bitcoin payment from bitcoind", async ({ page }) => {
  test.setTimeout(120_000);

  // --- Alice: log in and ensure vault account exists ---
  await login(page, aliceUsername, alicePassword);
  const token = await apiLogin(page, aliceUsername, alicePassword);
  const vaultAccount = await ensureVaultAccount(page, token, aliceUsername, alicePassword);
  await setActiveAccount(page, vaultAccount.id);
  await page.goto(`/${aliceUsername}`);

  const { invoiceId, address } = await createVaultBitcoinInvoiceViaUI(page);
  console.log(`[e2e] Alice vault bitcoin address: ${address}`);
  console.log(`[e2e] Alice vault invoice ID: ${invoiceId}`);

  // Alice stays on the invoice page

  // --- External: send bitcoin from bitcoind and mine ---
  console.log("[e2e] Sending bitcoin to vault from bitcoind...");
  const txid = bitcoinSend(address, "0.00001");
  console.log(`[e2e] Bitcoin txid: ${txid}`);

  console.log("[e2e] Mining 1 block...");
  mineBlocks(1);
  // Give nbxplorer time to index then mine another to trigger webhook
  await page.waitForTimeout(3_000);
  mineBlocks(1);

  // --- Wait for invoice to be paid ---
  const status = await waitForInvoicePaid(page, invoiceId, 1000, {
    interval: 2000,
    maxAttempts: 45,
  });
  console.log(`[e2e] Vault invoice paid: received=${status?.received}, pending=${status?.pending}`);

  // --- Alice: should be redirected to /paid ---
  const paidReached = await page
    .waitForURL(new RegExp(`/invoice/${invoiceId}/paid(?:[/?#]|$)`), {
      timeout: 30_000,
    })
    .then(() => true)
    .catch(() => false);

  if (paidReached) {
    console.log(`[e2e] Alice redirected to paid: ${page.url()}`);
  } else {
    console.log(`[e2e] No /paid redirect (vault may not auto-redirect), current: ${page.url()}`);
  }

  // Vault payments may show as pending — verify the invoice API shows received/pending
  expect(
    (status?.received || 0) >= 1000 || (status?.pending || 0) >= 1000,
    `Expected invoice to show received or pending >= 1000, got: ${JSON.stringify(status)}`,
  ).toBeTruthy();
});