|
| 1 | +/** |
| 2 | +* Copyright 2025 Shift Crypto AG |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { expect, Page } from '@playwright/test'; |
| 18 | +import { test } from './helpers/fixtures'; |
| 19 | +import { ServeWallet } from './helpers/servewallet'; |
| 20 | +import { launchRegtest, setupRegtestWallet, sendCoins, mineBlocks, cleanupRegtest } from './helpers/regtest'; |
| 21 | +import { ChildProcess } from 'child_process'; |
| 22 | + |
| 23 | +let servewallet: ServeWallet; |
| 24 | +let regtest: ChildProcess; |
| 25 | + |
| 26 | +test('Backup reminder banner is shown when currency is > 1000', async ({ page, host, frontendPort, servewalletPort }, testInfo) => { |
| 27 | + |
| 28 | + |
| 29 | + await test.step('Start regtest and init wallet', async () => { |
| 30 | + regtest = await launchRegtest(); |
| 31 | + // Give regtest some time to start |
| 32 | + await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 33 | + await setupRegtestWallet(); |
| 34 | + }); |
| 35 | + |
| 36 | + |
| 37 | + await test.step('Start servewallet', async () => { |
| 38 | + servewallet = new ServeWallet(page, servewalletPort, frontendPort, host, testInfo.title, testInfo.project.name, { regtest: true, testnet: false }); |
| 39 | + await servewallet.start(); |
| 40 | + }); |
| 41 | + |
| 42 | + let recvAdd: string; |
| 43 | + await test.step('Grab receive address', async () => { |
| 44 | + await page.getByRole('button', { name: 'Test wallet' }).click(); |
| 45 | + await page.getByRole('button', { name: 'Unlock' }).click(); |
| 46 | + await page.getByRole('link', { name: 'Bitcoin Regtest Bitcoin' }).click(); |
| 47 | + await page.getByRole('button', { name: 'Receive RBTC' }).click(); |
| 48 | + await page.getByRole('button', { name: 'Verify address on BitBox' }).click(); |
| 49 | + const addressLocator = page.locator('[data-testid="receive-address"]'); |
| 50 | + recvAdd = await addressLocator.inputValue(); |
| 51 | + console.log(`Receive address: ${recvAdd}`); |
| 52 | + }); |
| 53 | + |
| 54 | + await test.step('Verify that the backup banner is NOT shown initially', async () => { |
| 55 | + await page.goto('/'); |
| 56 | + await verifyBackupBanner(page, undefined, false); |
| 57 | + }); |
| 58 | + |
| 59 | + await test.step('Send RBTC to receive address', async () => { |
| 60 | + await page.waitForTimeout(2000); |
| 61 | + const sendAmount = '10'; |
| 62 | + sendCoins(recvAdd, sendAmount); |
| 63 | + mineBlocks(12); |
| 64 | + }); |
| 65 | + |
| 66 | + await test.step('Verify that the backup banner is shown with the correct currency', async () => { |
| 67 | + await page.goto('/'); |
| 68 | + await page.waitForTimeout(5000); |
| 69 | + const units = ['USD', 'EUR', 'CHF']; |
| 70 | + let currentIndex = 0; |
| 71 | + // First, verify that the banner shows USD by default. |
| 72 | + await verifyBackupBanner(page, units[currentIndex]!); |
| 73 | + |
| 74 | + // Then, cycle through the currency units and verify the banner updates accordingly. |
| 75 | + for (let i = 0; i < units.length; i++) { |
| 76 | + await page.locator(`header [data-testid="amount-unit-${units[currentIndex]!}"]`).click(); |
| 77 | + const nextIndex = (currentIndex + 1) % units.length; |
| 78 | + await page.waitForTimeout(1000); // wait for the UI to update |
| 79 | + await verifyBackupBanner(page, units[nextIndex]!); |
| 80 | + currentIndex = nextIndex; |
| 81 | + } |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +// Helper function to verify the banner presence or absence |
| 86 | +async function verifyBackupBanner( |
| 87 | + page: Page, |
| 88 | + expectedCurrency?: string, |
| 89 | + shouldExist = true |
| 90 | +) { |
| 91 | + await test.step( |
| 92 | + shouldExist |
| 93 | + ? `Verify that the backup banner is shown for ${expectedCurrency!}` |
| 94 | + : 'Verify that the backup banner is NOT shown', |
| 95 | + async () => { |
| 96 | + const textContent = await page.textContent('body'); |
| 97 | + |
| 98 | + if (shouldExist) { |
| 99 | + if (!expectedCurrency) { |
| 100 | + throw new Error('Currency must be provided when expecting banner.'); |
| 101 | + } |
| 102 | + |
| 103 | + const regex = new RegExp( |
| 104 | + `Your wallet\\s+Software keystore [a-f0-9]+\\s+passed ${expectedCurrency} 1[’,']000\\.00!` |
| 105 | + ); |
| 106 | + expect(textContent).toMatch(regex); |
| 107 | + |
| 108 | + expect(textContent).toContain( |
| 109 | + 'We recommend creating a paper backup for extra protection. It\'s quick and simple.' |
| 110 | + ); |
| 111 | + } else { |
| 112 | + // Check that the banner text is NOT present |
| 113 | + const bannerRegex = /Your wallet Software keystore [a-f0-9]+ passed [A-Z]{3} 1,000\.00!/; |
| 114 | + expect(textContent).not.toMatch(bannerRegex); |
| 115 | + expect(textContent).not.toContain( |
| 116 | + 'We recommend creating a paper backup for extra protection. It\'s quick and simple.' |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +test.afterAll(async () => { |
| 124 | + await servewallet.stop(); |
| 125 | + await cleanupRegtest(regtest); |
| 126 | +}); |
0 commit comments