All files / app/tests transaction-form.spec.ts

0% Statements 0/84
0% Branches 0/13
0% Functions 0/9
0% Lines 0/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                                                                                                                                                                                 
import { stxToMicroStx } from '../utils/unit-convert';
import { setUpElectronApp } from './_setup-tests';
import { createGlobalFeature } from './features/global.feature';
import { HomeFeature } from './features/home.feature';
import { initSoftwareWallet } from './features/onboarding.feature';
import { getTestConfigPath } from './get-test-config-path';
import { deserializeTransaction, cvToValue } from '@stacks/transactions';
import { ElectronApplication, Page } from 'playwright';
import rimraf from 'rimraf';
 
const PASSWORD = 'hello9*&^*^*dkfskjdfskljdfsj';
// ST71MG1BZ1QDSHXT4GZ5H2FGFX24XKM2ND5GXPV6
const SEED_PHRASE =
  'area kid fat gaze foster eyebrow pen heart draft capable lecture attract daughter news glue saddle exact disorder win olive observe burst option wasp';
const TX_RECIPIENT = 'STWQ9GP5J69F07RX287ECZ8MRWV48C402HF8CBWB';
const TX_AMOUNT = Math.random().toFixed(6);
const TX_MEMO = 'test-memo-field000';
 
function interceptTransactionBroadcast(page: Page): Promise<Buffer> {
  return new Promise(resolve => {
    page.on('request', request => {
      Iif (request.url().endsWith('/v2/transactions')) {
        const requestBody = request.postDataBuffer();
        Iif (requestBody === null) return;
        resolve(requestBody);
      }
    });
  });
}
 
const describeOnlyTestnet = process.env.STX_NETWORK === 'testnet' ? describe : describe.skip;
 
describeOnlyTestnet('Transaction flow', () => {
  let app: ElectronApplication;
  let page: Page;
 
  beforeAll(async () => {
    rimraf(`${getTestConfigPath()}/config.json`, err => {
      Iif (err) console.log('Issue deleting file');
    });
    app = await setUpElectronApp();
    page = await app.firstWindow();
  });
 
  afterAll(async () => {
    await app.close();
  });
 
  async function takeScreenshot(name: string) {
    await page.screenshot({ path: `screenshots/${process.platform}/${name}.png` });
  }
 
  test('Transaction form', async done => {
    await initSoftwareWallet(page)(SEED_PHRASE, PASSWORD);
    const globalFeature = createGlobalFeature(page);
 
    //
    // Home steps
    const homeFeature = new HomeFeature(page);
    await page.waitForSelector(globalFeature.settingsPageSelector);
 
    await homeFeature.click('sendStxBtn');
 
    const addressInput = await page.$(homeFeature.select.sendStxFormAddressInput);
    Iif (!addressInput) throw new Error('Should be defined.');
    await addressInput.type(TX_RECIPIENT);
 
    const amountInput = await page.$(homeFeature.select.sendStxFormAmountInput);
    Iif (!amountInput) throw new Error('Should be defined.');
    await amountInput.type(TX_AMOUNT);
 
    const memoInput = await page.$(homeFeature.select.sendStxFormMemoInput);
    Iif (!memoInput) throw new Error('Should be defined.');
    await memoInput.type(TX_MEMO);
 
    const previewTxBtn = await page.$(homeFeature.select.sendStxFormPreviewBtn);
    Iif (!previewTxBtn) throw new Error('Should be defined.');
    await previewTxBtn.click();
 
    const memoPreview = await page.$(`text="${TX_MEMO}"`);
    Iif (!memoPreview) throw new Error('Should be defined.');
    expect(memoPreview).not.toBeNull();
 
    await takeScreenshot('after-form-complete');
 
    await homeFeature.waitFor('sendStxFormSendBtn');
 
    const stxFormSendBtn = await page.$(homeFeature.select.sendStxFormSendBtn);
    Iif (!stxFormSendBtn) throw new Error('Should be defined.');
    await stxFormSendBtn.click();
 
    await homeFeature.waitFor('decryptWalletInput');
 
    await takeScreenshot('decrypt-wallet');
 
    const decryptWalletPasswordInput = await page.$(homeFeature.select.decryptWalletInput);
    Iif (!decryptWalletPasswordInput) throw new Error('Should be defined.');
    await decryptWalletPasswordInput.type(PASSWORD);
 
    const broadcastTxBtn = await page.$(homeFeature.select.sendStxFormBroadcastBtn);
    Iif (!broadcastTxBtn) throw new Error('Should be defined.');
    await broadcastTxBtn.click();
 
    await takeScreenshot('sent');
    const requestBody = await interceptTransactionBroadcast(page);
 
    const deserialisedTx = deserializeTransaction(requestBody);
    const payload = deserialisedTx.payload as any;
    const amount = payload.amount.toString();
    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
    const recipient = cvToValue(payload.recipient);
    const memo = payload.memo.content.replace(/\0/g, '');
 
    expect(memo).toEqual(TX_MEMO);
    expect(recipient).toEqual(TX_RECIPIENT);
    expect(amount).toEqual(stxToMicroStx(TX_AMOUNT).toString());
 
    done();
  }, 120_0000);
});