mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
A refused form says what is wrong under the field that is wrong, in the same words in cloud and local-first. This first slice covers the four category dialogs (Categories, Budgets, Bills, Goals): one shared category schema, a 400 with `fields` from both runtimes, ApiError on both client surfaces, and the form kit with its busy state. An edit checks only the fields it changes, so rows saved under older rules still save. An ended session asks to sign in again, and a save that lands after a cancel leaves the reopened dialog alone.
140 lines
5.8 KiB
TypeScript
140 lines
5.8 KiB
TypeScript
/**
|
|
* Test helpers for Playwright tests
|
|
*/
|
|
|
|
import { expect } from '@playwright/test'
|
|
import { OFFLINE, UNREACHABLE } from '../src/core/apiError'
|
|
import { E2E_BASE, E2E_PROFILE } from './e2e-constants'
|
|
|
|
export { E2E_BASE }
|
|
|
|
/**
|
|
* Whether a console line only says the network failed, which the CRUD specs leave out of the
|
|
* errors they count. fetch's own words are "Failed to fetch"; the app says it in a sentence of
|
|
* its own (core/apiError.ts), and a page's loader logs "Failed to load goals: ApiError: Couldn't
|
|
* reach Token Circles. ...", which has "Error" in it. Read from the app, so a reworded sentence
|
|
* cannot slip past the filter.
|
|
*/
|
|
export function isNetworkNoise(message: string): boolean {
|
|
return ['Failed to fetch', UNREACHABLE, OFFLINE].some((words) => message.includes(words))
|
|
}
|
|
|
|
/**
|
|
* Navigate to a hash route in serverless/demo mode and wait until the app shell is ready.
|
|
*
|
|
* Serverless mode auto-seeds a large demo dataset (transactions from 2000 → present) into
|
|
* IndexedDB on first load. Until that resolves the app renders a full-screen "Loading…" gate
|
|
* and no page — not even the unconditional page header — is mounted. A fixed `waitForTimeout`
|
|
* races that seed: under parallel CI load the seed can take well over 10s, which is exactly why
|
|
* the analytics/mobile specs flaked. Gate on a stable readiness `data-test-id` with a generous
|
|
* timeout instead of a magic sleep, so the wait is exactly as long as the seed needs and no more.
|
|
*/
|
|
export async function gotoServerless(page: any, route: string, readyTestId: string) {
|
|
await page.addInitScript(() => {
|
|
localStorage.setItem('finance_storage_mode', 'serverless')
|
|
})
|
|
await page.goto(`${E2E_BASE}/#${route}`, {
|
|
waitUntil: 'domcontentloaded',
|
|
timeout: 30000,
|
|
})
|
|
await expect(page.getByTestId(readyTestId)).toBeVisible({ timeout: 30000 })
|
|
}
|
|
|
|
/**
|
|
* Navigate in serverless mode with a genuinely EMPTY workspace: setting
|
|
* `finance_had_profiles` suppresses the first-run demo seed, so the app boots
|
|
* with zero profiles, accounts, and transactions — the state a brand-new
|
|
* signup sees, and the one that auto-opens the onboarding wizard. Gate on a
|
|
* test-id that exists in that empty state (e.g. `onboarding-wizard`).
|
|
*/
|
|
export async function gotoServerlessZeroState(page: any, route: string, readyTestId: string) {
|
|
await page.addInitScript(() => {
|
|
localStorage.setItem('finance_storage_mode', 'serverless')
|
|
localStorage.setItem('finance_had_profiles', '1')
|
|
})
|
|
await page.goto(`${E2E_BASE}/#${route}`, {
|
|
waitUntil: 'domcontentloaded',
|
|
timeout: 30000,
|
|
})
|
|
await expect(page.getByTestId(readyTestId)).toBeVisible({ timeout: 30000 })
|
|
}
|
|
|
|
/**
|
|
* Open the app already signed in.
|
|
*
|
|
* There is no request here any more. `global.setup.ts` signs in once and Playwright hands every
|
|
* context that session — cookie and the localStorage keys that put the app in server mode — via
|
|
* `storageState`. Each context signing in for itself is what spent the login rate limit on a
|
|
* machine with enough cores to run a context per CPU.
|
|
*/
|
|
export async function login(page: any) {
|
|
await page.goto(`${E2E_BASE}/`, { waitUntil: 'domcontentloaded', timeout: 30000 })
|
|
await settleOnShell(page)
|
|
}
|
|
|
|
/**
|
|
* Navigate to a hash route and wait for content to load
|
|
*/
|
|
export async function navigateToRoute(page: any, route: string) {
|
|
await page.goto(`${E2E_BASE}/#${route}`, {
|
|
waitUntil: 'domcontentloaded',
|
|
timeout: 30000,
|
|
})
|
|
await page.waitForTimeout(500)
|
|
await settleOnShell(page)
|
|
}
|
|
|
|
/**
|
|
* Best-effort wait for the app shell to be up, for the two navigation helpers above.
|
|
*
|
|
* This replaces a `waitForLoadState('networkidle')` in each. `networkidle` is the wrong signal
|
|
* twice over: it resolves on *network* quiet, which this app reaches before the shell has
|
|
* rendered (the same mistake `gotoServerless` above documents), and it never resolves at all on a
|
|
* page that polls. `sonarjs/no-networkidle-wait` flags it for exactly that reason.
|
|
*
|
|
* `profile-dropdown-btn` (App.tsx) is the header control that exists on every signed-in page, so
|
|
* its visibility is the shell being mounted — the condition the old wait was approximating.
|
|
*
|
|
* Deliberately best-effort, like the wait it replaces: every caller asserts its own page-specific
|
|
* readiness immediately afterwards, and those assertions are what should report a failure. A throw
|
|
* here would relabel 30 specs' real failures as a helper timeout.
|
|
*/
|
|
async function settleOnShell(page: any) {
|
|
await page
|
|
.getByTestId('profile-dropdown-btn')
|
|
.waitFor({ state: 'visible', timeout: 10000 })
|
|
.catch(() => {})
|
|
}
|
|
|
|
/**
|
|
* Helper to find elements by data-test-id
|
|
* Uses data-test-id attribute (matches source code)
|
|
*/
|
|
export function getByTestId(page: any, testId: string, options: any = {}) {
|
|
return page.locator(`[data-test-id="${testId}"]`, options)
|
|
}
|
|
|
|
/**
|
|
* Helper to find multiple elements by data-test-id
|
|
*/
|
|
export function getByTestIdMulti(page: any, testId: string, options: any = {}) {
|
|
return page.locator(`[data-test-id="${testId}"]`, options)
|
|
}
|
|
|
|
/**
|
|
* The id of the seeded fixture profile.
|
|
*
|
|
* Specs used to hardcode `X-Profile-Id: '1'`, which held only because the Express server's seeded
|
|
* database always produced it. The profile is created through the API now, so its id is whatever
|
|
* the database hands out — and specs that create their own leave others behind it in the list.
|
|
*/
|
|
export async function firstProfileId(ctx: any): Promise<number> {
|
|
const res = await ctx.request.get(`${E2E_BASE}/api/profiles`)
|
|
expect(res.ok(), `could not read profiles: ${res.status()}`).toBeTruthy()
|
|
const body = await res.json()
|
|
const profiles = Array.isArray(body) ? body : body.profiles
|
|
const fixture = profiles?.find((p: { name: string }) => p.name === E2E_PROFILE)
|
|
expect(fixture, `no "${E2E_PROFILE}" profile — did the setup project run?`).toBeTruthy()
|
|
return fixture.id as number
|
|
}
|