Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(e2e-connector-test): add fuel wallet tests #199

Merged
merged 15 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,3 @@ jobs:
working-directory: ./packages/fuelet-wallet
json-summary-path: ./coverage/coverage-summary.json
json-final-path: ./coverage/coverage-final.json

31 changes: 0 additions & 31 deletions e2e-tests/react-app/FuelConnector.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
downloadFuel,
getButtonByText,
getByAriaLabel,
} from '@fuels/playwright-utils';
import { FuelWalletTestHelper } from '@fuels/playwright-utils';
import { test } from '@fuels/playwright-utils';
import { type Page, expect } from '@playwright/test';
import dotenv from 'dotenv';
import { type WalletUnlocked, bn } from 'fuels';
import { testSetup, transferMaxBalance } from '../setup';
dotenv.config();

const fuelPathToExtension = await downloadFuel('0.27.0');
test.use({ pathToExtension: fuelPathToExtension });

const connect = async (
page: Page,
fuelWalletTestHelper: FuelWalletTestHelper,
) => {
const connectButton = getButtonByText(page, 'Connect');
await connectButton.click();
await getByAriaLabel(page, 'Connect to Fuel Wallet', true).click();
await fuelWalletTestHelper.walletConnect();
};

test.describe('FuelWalletConnector', () => {
let fuelWalletTestHelper: FuelWalletTestHelper;
let fuelWallet: WalletUnlocked;
let masterWallet: WalletUnlocked;

const depositAmount = '0.0003'; // Should be enough to cover the increment and transfer

test.beforeEach(async ({ context, extensionId, page }) => {
const { fuelProvider, chainName, randomMnemonic } = await testSetup({
context,
page,
extensionId,
amountToFund: bn.parseUnits(depositAmount),
});

fuelWalletTestHelper = await FuelWalletTestHelper.walletSetup(
context,
extensionId,
fuelProvider.url,
chainName,
randomMnemonic,
);

await page.goto('/');
await connect(page, fuelWalletTestHelper);
});

test.afterEach(async () => {
await transferMaxBalance({
fromWallet: fuelWallet,
toWallet: masterWallet,
});
});

test('should connect and show fuel address', async ({ page }) => {
expect(await page.waitForSelector('text=Your Fuel Address')).toBeTruthy();
});

test('should connect and increment', async ({ page }) => {
await page.click('text=Increment');
await fuelWalletTestHelper.walletApprove();

expect(await page.waitForSelector('text=Success')).toBeTruthy();
expect(
await page.waitForSelector('text=Counter Incremented!'),
).toBeTruthy();
});

test('should connect and transfer', async ({ page }) => {
await page.click('text=Transfer 0.0001 ETH');
await fuelWalletTestHelper.walletApprove();

expect(await page.waitForSelector('text=Success')).toBeTruthy();
expect(
await page.waitForSelector('text=Transferred successfully!'),
).toBeTruthy();
});

test('should connect, disconnect, and reconnect', async ({ page }) => {
await page.click('text=Disconnect');
await page.waitForSelector('text=Connect Wallet');

expect(await page.waitForSelector('text=Your Fuel Address')).toBeTruthy();
});

test('should connect, refresh and stay connected', async ({ page }) => {
await page.reload();
await page.waitForSelector('text=Your Fuel Address');
});

test('should connect, disconnect, refresh and stay disconnected', async ({
page,
}) => {
await page.click('text=Disconnect');
await page.waitForSelector('text=Connect Wallet');

await page.reload();
await page.waitForSelector('text=Connect Wallet');
});
});
File renamed without changes.
91 changes: 91 additions & 0 deletions e2e-tests/react-app/connectors/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { seedWallet } from '@fuels/playwright-utils';
import type { BrowserContext, Page } from '@playwright/test';
import dotenv from 'dotenv';
import {
type BNInput,
Mnemonic,
Provider,
Wallet,
type WalletUnlocked,
bn,
} from 'fuels';
dotenv.config();

const {
VITE_FUEL_PROVIDER_URL,
VITE_WALLET_SECRET,
VITE_MASTER_WALLET_MNEMONIC,
} = process.env as Record<string, string>;

export const testSetup = async ({
amountToFund,
}: {
context: BrowserContext;
page: Page;
extensionId: string;
amountToFund: BNInput;
}) => {
const fuelProvider = await Provider.create(VITE_FUEL_PROVIDER_URL);
const masterWallet = Wallet.fromMnemonic(VITE_MASTER_WALLET_MNEMONIC);
masterWallet.connect(fuelProvider);
if (VITE_WALLET_SECRET) {
await seedWallet(
masterWallet.address.toString(),
bn.parseUnits('100'),
VITE_FUEL_PROVIDER_URL,
VITE_WALLET_SECRET,
);
}
const randomMnemonic = Mnemonic.generate();
const fuelWallet = Wallet.fromMnemonic(randomMnemonic);
fuelWallet.connect(fuelProvider);
const chainName = (await fuelProvider.fetchChain()).name;
const txResponse = await masterWallet.transfer(
fuelWallet.address,
bn(amountToFund),
);
await txResponse.waitForResult();

return { fuelWallet, masterWallet, fuelProvider, randomMnemonic, chainName };
};

export const transferMaxBalance = async ({
fromWallet,
toWallet,
}: {
fromWallet: WalletUnlocked;
toWallet: WalletUnlocked;
}) => {
if (!fromWallet || !toWallet) return;

const MAX_ATTEMPTS = 10;
const trySendMax = async (attempt = 1) => {
if (attempt > MAX_ATTEMPTS) return;

try {
const remainingBalance = await fromWallet.getBalance();
const nextSubTry = bn(attempt * 10_000);

if (nextSubTry.lt(remainingBalance)) {
const targetAmount = remainingBalance.sub(nextSubTry);
const amountToSend = targetAmount.gt(0) ? targetAmount : bn(1);

const txResponse = await fromWallet.transfer(
toWallet.address,
amountToSend,
);
await txResponse.waitForResult();
console.log(
`----- Success sending ${amountToSend?.format()} back to ${
toWallet.address
}`,
);
}
} catch (e) {
console.log('error sending remaining balance', e.message);
await trySendMax(attempt + 1);
}
};

await trySendMax();
};
14 changes: 0 additions & 14 deletions e2e-tests/react-app/utils/connect.ts

This file was deleted.

1 change: 0 additions & 1 deletion e2e-tests/react-app/utils/index.ts

This file was deleted.

57 changes: 0 additions & 57 deletions e2e-tests/utils/setup.ts

This file was deleted.

Loading