diff options
Diffstat (limited to 'test/e2e/helpers.ts')
| -rw-r--r-- | test/e2e/helpers.ts | 155 |
1 files changed, 114 insertions, 41 deletions
diff --git a/test/e2e/helpers.ts b/test/e2e/helpers.ts index ad915d0b..4b84f8e7 100644 --- a/test/e2e/helpers.ts +++ b/test/e2e/helpers.ts @@ -1,6 +1,8 @@ import { execSync } from "child_process"; +import { scryptSync } from "crypto"; import type { Browser, Page } from "@playwright/test"; -import { expect } from "@playwright/test"; +import { expect, test } from "@playwright/test"; +import { nip19 } from "nostr-tools"; // --- Environment --- export const arkWalletPassword = process.env.E2E_ARK_WALLET_PASSWORD || "testpw123"; @@ -14,22 +16,39 @@ export const pauseAtEnd = process.env.E2E_PAUSE_AT_END === "true"; // --- Login helpers --- +// Derive nsec from username+password using the same scrypt params as the app +function deriveNsec(username: string, password: string): string { + const salt = `coinos:auth:${username.toLowerCase().replace(/\s/g, "")}`; + const sk = scryptSync(password, salt, 32, { N: 65536, r: 8, p: 1, maxmem: 256 * 1024 * 1024 }); + return nip19.nsecEncode(new Uint8Array(sk)); +} + export async function login(page: Page, username: string, password: string) { - for (let attempt = 0; attempt < 5; attempt++) { - if (attempt > 0) await page.waitForTimeout(3_000); - await page.goto("/login"); - await page.getByTestId("login-username").waitFor({ state: "visible", timeout: 15_000 }); - await page.getByTestId("login-username").fill(username); - await page.locator('input[name="password"]').first().fill(password); - await page.getByTestId("login-submit").click(); - const ok = await page - .waitForURL(new RegExp(`/${username}(?:[/?#]|$)`), { timeout: 15_000 }) - .then(() => true) - .catch(() => false); - if (ok) return; - console.log(`[e2e] Login attempt ${attempt + 1} failed, retrying...`); - } - throw new Error(`Login failed for ${username} after 5 attempts`); + // Use API login to set auth cookies directly — avoids slow scrypt derivation in browser + const res = await page.request.post(`${apiBaseUrl}/login`, { + data: { username, password }, + }); + if (!res.ok()) throw new Error(`API login failed for ${username}: ${res.status()}`); + const { token } = await res.json(); + + // Set cookies that SvelteKit expects + await page.context().addCookies([ + { name: "token", value: token, domain: "127.0.0.1", path: "/" }, + { name: "username", value: username, domain: "127.0.0.1", path: "/" }, + ]); + + // Derive nsec and register init script so ark wallet can auto-unlock on any page load. + // Also skip settle() to prevent it from registering VTXOs in pending rounds that + // never complete (arkd rounds abort with "0 intents"), blocking sendArk(). + const nsec = deriveNsec(username, password); + await page.addInitScript((ns) => { + localStorage.setItem("nsec", ns); + localStorage.setItem("ark:skipSettle", "1"); + }, nsec); + + // Navigate to dashboard to confirm login worked + await page.goto(`/${username}`); + await page.waitForURL(new RegExp(`/${username}(?:[/?#]|$)`), { timeout: 15_000 }); } export async function loginNewContext(browser: Browser, username: string, password: string) { @@ -245,11 +264,12 @@ export async function fillNumpadAmount(page: Page, amount: number) { const amountInput = page.locator('[aria-label="Amount input"]'); await expect(amountInput).toBeVisible({ timeout: 5_000 }); - // Ensure sats mode: swap button shows lightning icon when in fiat mode + // Ensure sats mode: when in fiat mode, the lightning icon span is visible const swapButton = page.locator('[aria-label="Swap currency display"]'); if (await swapButton.isVisible({ timeout: 2_000 }).catch(() => false)) { - const hasLightning = await swapButton.locator('iconify-icon[icon="ph:lightning-fill"]').count(); - if (hasLightning > 0) { + const lightningSpan = swapButton.locator('iconify-icon[icon="ph:lightning-fill"]').first(); + const isInFiatMode = await lightningSpan.isVisible({ timeout: 1_000 }).catch(() => false); + if (isInFiatMode) { await swapButton.click(); await page.waitForTimeout(300); } @@ -364,6 +384,8 @@ export async function waitForInvoicePaid( // --- Ark helpers --- +const ARK_LIQUIDITY_RE = /VTXO_RECOVERABLE|VTXO_ALREADY_SPENT|VTXO_ALREADY_REGISTERED|insufficient funds|not enough liquidity/i; + export async function sendArkFromTestEndpoint( page: Page, address: string, @@ -387,6 +409,11 @@ export async function sendArkFromTestEndpoint( }, { address, testSecret: testSecret!, amount, iid }, ); + + if (!sendResult.ok && ARK_LIQUIDITY_RE.test(sendResult.text)) { + test.skip(true, `Ark server has no liquidity: ${sendResult.text}`); + } + expect( sendResult.ok, `/test/ark/send failed (${sendResult.status}): ${sendResult.text}`, @@ -408,6 +435,14 @@ export async function waitForPageReady(page: Page) { await page.waitForLoadState("domcontentloaded"); } +/** Wait for ark wallet to discover VTXOs without triggering settle() */ +export async function waitForArkSync(page: Page, _username: string, _timeout = 15_000) { + // Don't navigate to the dashboard — that triggers initArk → settle() which + // registers VTXOs in a round, making them unavailable for sendArk(). + // Just wait a bit for the ark server to index the new VTXOs. + await page.waitForTimeout(3_000); +} + // --- Account helpers --- export async function setActiveAccount(page: Page, accountId: string) { @@ -417,31 +452,69 @@ export async function setActiveAccount(page: Page, accountId: string) { } export async function ensureArkAccount(page: Page, _password: string) { - await page.goto("/account/ark"); - await page.waitForLoadState("domcontentloaded"); - - // The /account/ark page either: - // 1. Redirects immediately if user already has an ark account (server-side) - // 2. Auto-creates via getWalletEntropy() on mount then redirects (client-side goto) - // 3. Shows unlock buttons (btn-accent) if no wallet entropy available - // - // Wait for redirect away from /account/ark (covers cases 1 and 2) - const redirected = await page - .waitForURL((u) => !u.pathname.includes("/account/ark"), { timeout: 45_000 }) - .then(() => true) - .catch(() => false); + // Wait for autoUnlockArk() to derive the arkkey and write it to localStorage. + // This happens in onMount of the app layout after login() navigates to the dashboard. + // IMPORTANT: We must use THIS key (not re-derive) because Schnorr signing uses random + // nonces, so each call to tryDeriveFromNsec() produces a different key. + const arkKey = await page.evaluate(async () => { + for (let i = 0; i < 30; i++) { + const raw = localStorage.getItem("arkkey"); + if (raw && raw !== '""' && raw !== '"undefined"' && raw !== "undefined") { + try { + const parsed = JSON.parse(raw); + if (typeof parsed === "string" && parsed.length >= 32) return parsed; + } catch {} + } + await new Promise((r) => setTimeout(r, 500)); + } + return null; + }); - if (redirected) { - console.log("[e2e] Ark account ready (redirected from /account/ark)"); + if (!arkKey) { + console.log("[e2e] WARNING: arkkey not found in localStorage after 15s"); return; } + console.log(`[e2e] arkkey ready: ${arkKey.slice(0, 12)}...`); + + // Compute the client wallet address — the fresh import reads arkkey from localStorage + const clientAddress = await page.evaluate(async () => { + const { getAddress } = await import("/src/lib/ark.ts"); + return await getAddress(); + }); + + console.log(`[e2e] Client ark address: ...${clientAddress.slice(-30)}`); + + // Check/create ark account — retry to handle race conditions with parallel tests + for (let attempt = 0; attempt < 3; attempt++) { + const accountsRes = await page.request.get(`${apiBaseUrl}/accounts`); + if (accountsRes.ok()) { + const accounts = await accountsRes.json(); + const arkAccount = accounts.find((a: any) => a.type === "ark"); + + if (arkAccount?.arkAddress === clientAddress) { + console.log("[e2e] Ark account exists with matching address"); + return; + } + + if (arkAccount) { + console.log(`[e2e] Ark address mismatch, deleting stale account`); + await page.request.post(`${apiBaseUrl}/account/delete`, { + data: { id: arkAccount.id }, + }); + } + } + + const createRes = await page.request.post(`${apiBaseUrl}/accounts`, { + data: { name: "ark", type: "ark", arkAddress: clientAddress }, + }); + + if (createRes.ok()) { + console.log("[e2e] Ark account created via API with matching address"); + return; + } - // If still on /account/ark, check if unlock buttons appeared - const unlockBtn = page.locator("button.btn-accent"); - const hasUnlock = await unlockBtn.first().isVisible().catch(() => false); - if (hasUnlock) { - console.log("[e2e] Ark account page shows unlock buttons — wallet entropy not available"); - } else { - console.log("[e2e] Ark account page did not redirect — account may already exist or creation failed"); + // "Already have an Ark vault" means another test created one — retry to check/update + console.log(`[e2e] Account creation attempt ${attempt + 1} failed, retrying...`); + await page.waitForTimeout(1_000); } } |
