Files
token-circles/frontend/tests/portfolio-form-errors.spec.ts
KomediruzeckiandGitHub 08ad641383 feat(forms): housing, portfolio, tags, recurring and onboarding say what is wrong at the field (slice 5 of 6) (#614)
Housing, Portfolio, Tags, the Recurring dialog, the subscription catalog and scan, and the setup
wizard move onto the form kit from #602: a refused entry is said under the field that is wrong, in
the same words in cloud and local-first. Both runtimes run one set of rules from shared/ for a
tag, a housing expense, a holding and a recurring rule, and answer a refusal with 400
{ error, fields }. An edit checks and writes only what it changes, so a row an older version
stored can still be edited.

- A tag name that is blank, over 50 characters, or another tag's in other case is refused at name.
  A colour must be #RRGGBB; a tag created without one takes the palette's next colour, and an edit
  without one keeps it.
- A housing expense is refused at the field: a name up to 100 characters, six types, an amount to
  the cent, a real due month and a day that month has (29 February taken). One posted without a
  due month falls due this month.
- A holding needs a ticker up to 20 characters, stored in capitals, shares and a price above zero
  and below one trillion, and a real date. The edit dialog sends back what is stored, and a buy
  merges only into the open profile's holding.
- A recurring rule needs a description, an amount to the cent, a type, a frequency and a next date.
  Deleting one the profile does not have is a 404. A rule's period is added once, and a second add
  of it is a 409 in both runtimes. Local-first's upcoming list is the Worker's.
- Another profile's tag, category or account is a 400 at the field it names.
- A new tag and a new recurring rule answer 201 in both runtimes, as housing does.
- Deleting a housing expense, holding, tag or recurring rule another tab already deleted says so
  and reloads the list.
- MCP's upsert_tag_rule finds and creates tags by the tag rules.
- The contract suite settles 12 pins (31 to 19).

Slice 5 of 6 in docs/plans/2026-10-07-form-errors.md.
2026-10-10 09:19:28 +02:00

117 lines
4.7 KiB
TypeScript

/**
* The Portfolio dialog, in both storage modes: a holding without a ticker, shares, a price or a
* date is marked at each field and nothing is sent, and a buy merged into a holding the runtime
* then refuses is marked at the shares, in the runtime's words.
*
* The dialog said "Please fill all required fields" or "Failed to save holding" in a toast, with
* nothing marked. Now it checks with the rules both runtimes run (shared/holdingSchema.ts) before
* it sends anything. What only the runtime can tell is the merged holding: a buy of a ticker
* already held is added to it, and shares that come to one trillion or more are refused.
*
* Every case runs signed in (the Worker) and local-first (the IndexedDB router). Pull requests
* run the `@smoke` one; main runs them all.
*/
import { expect, test } from '@playwright/test'
import {
caseStamp,
daysFromToday,
errorToasts,
MODES,
sweepProfiles,
viaApp,
watchErrors,
watchWrites,
} from './form-errors-helpers'
import type { Locator, Page } from '@playwright/test'
interface Holding {
id: number
ticker: string
shares: number
}
/** A made-up ticker: nothing real is held in a fixture. */
const TICKER = 'ZZQX'
const header = (page: Page): Locator => page.getByTestId('portfolio-header')
const dialog = (page: Page): Locator => page.getByTestId('portfolio-modal')
const tickerField = (page: Page): Locator => page.getByTestId('portfolio-form-ticker')
const sharesField = (page: Page): Locator => page.getByTestId('portfolio-form-shares')
const priceField = (page: Page): Locator => page.getByTestId('portfolio-form-price')
const dateField = (page: Page): Locator => page.getByTestId('portfolio-form-date')
const submit = (page: Page) => page.getByTestId('portfolio-modal-submit').click()
for (const mode of MODES) {
test.describe(`the holding dialog, ${mode.name}`, () => {
/** In the name of everything a case makes, so the sweep can find it. */
let stamp = ''
test.beforeEach(() => {
test.setTimeout(120_000)
stamp = caseStamp()
})
test.afterEach(async ({ page }) => {
await sweepProfiles(page, mode, stamp)
})
test('a holding without its fields is marked at each and nothing is sent, and a merge past one trillion shares is marked at the shares @smoke', async ({
page,
}) => {
const errors = watchErrors(page)
await mode.open(page, 'portfolio', header, `zz-portfolio-${stamp}`)
const writes = watchWrites(page, /^\/api\/portfolio\/holdings/)
await page.getByTestId('add-holding-btn').click()
await expect(dialog(page)).toBeVisible()
await submit(page)
await expect(tickerField(page)).toHaveAttribute('aria-invalid', 'true')
await expect(tickerField(page)).toHaveAccessibleDescription('Enter the ticker symbol.')
await expect(sharesField(page)).toHaveAccessibleDescription('Enter the number of shares.')
await expect(priceField(page)).toHaveAccessibleDescription(
'Enter the price you paid per share.'
)
await expect(dateField(page)).toHaveAccessibleDescription(
'Choose the date you bought the shares.'
)
await expect(tickerField(page)).toBeFocused()
expect(writes).toEqual([])
await expect(errorToasts(page)).toHaveCount(0)
// A holding one share short of a trillion, added through the app: the page lists it, so a
// buy of the same ticker is offered as a merge.
await viaApp(page, 'POST', '/api/portfolio/holdings', {
ticker: TICKER,
shares: 999_999_999_999,
purchase_price: 1,
purchase_date: daysFromToday(-30),
})
await expect(
page.getByTestId('portfolio-ticker').filter({ hasText: TICKER }).first()
).toBeVisible({ timeout: 15_000 })
await tickerField(page).fill(TICKER.toLowerCase())
await sharesField(page).fill('1')
await priceField(page).fill('1')
await dateField(page).fill(daysFromToday(0))
await submit(page)
await page.getByTestId('confirm-accept').click()
await expect(sharesField(page)).toHaveAttribute('aria-invalid', 'true')
await expect(sharesField(page)).toHaveAccessibleDescription(
'Enter fewer than one trillion shares.'
)
await expect(sharesField(page)).toBeFocused()
await expect(tickerField(page)).not.toHaveAttribute('aria-invalid', 'true')
await expect(dialog(page)).toBeVisible()
await expect(errorToasts(page)).toHaveCount(0)
const held = (await viaApp<Holding[]>(page, 'GET', '/api/portfolio/holdings')).filter(
(holding) => holding.ticker === TICKER
)
expect(held.map((holding) => holding.shares)).toEqual([999_999_999_999])
expect(errors).toEqual([])
})
})
}