Files
hledger-web-reports/apps/web/e2e/account-navigation.e2e.ts

276 lines
12 KiB
TypeScript

import { expect, test, type Request } from '@playwright/test';
import { prepared, prepareReports, readPreparationTimestamp } from './overview-fixture';
test('desktop account navigation preserves client routing and observes later saved registers', async ({
page,
context
}) => {
await page.setViewportSize({ width: 1440, height: 900 });
await page.emulateMedia({ colorScheme: 'light' });
await prepared(page);
await page.goto('/settings');
const savedAt = await readPreparationTimestamp(page);
await page.goto('/');
const accounts = page.getByRole('complementary', { name: 'Accounts', exact: true });
const balanceDate = accounts.getByText(/^Balances as of/);
await expect(balanceDate).toBeVisible();
await expect(balanceDate.locator('time')).toHaveAttribute('datetime', '2026-04-16');
await expect(accounts.getByText('$8,520.00', { exact: true })).toBeVisible();
await expect(
accounts
.getByRole('region', { name: 'Net worth', exact: true })
.getByText('$11,825.00', { exact: true })
).toBeVisible();
await expect(accounts.getByRole('link', { name: /^Brokerage\b/ })).toBeVisible();
await expect(
accounts.getByRole('link', { name: /^assets:investments:retirement\b/ })
).toBeVisible();
// Overview uses the whole space beside the rail, not a centered reading column.
const main = page.locator('main');
const mainBox = await main.boundingBox();
const railBox = await accounts.boundingBox();
expect(mainBox).not.toBeNull();
expect(railBox).not.toBeNull();
expect(mainBox!.x).toBeCloseTo(0, 0);
expect(mainBox!.x + mainBox!.width).toBeCloseTo(railBox!.x, 0);
const header = page.getByRole('banner');
const headerBox = await header.boundingBox();
const accountsHeaderBox = await accounts.locator('header').boundingBox();
expect(headerBox!.height).toBeCloseTo(accountsHeaderBox!.height, 0);
expect(headerBox!.y + headerBox!.height).toBeCloseTo(
accountsHeaderBox!.y + accountsHeaderBox!.height,
0
);
await expect(
header.getByRole('link', { name: 'Overview home', exact: true }).locator('img')
).toBeVisible();
// The Accounts total opens the saved trend rather than another homepage card.
const netWorthLink = accounts.getByRole('link', { name: /^Net worth/ });
await expect(netWorthLink).toHaveAttribute('href', '/reports/networth');
await netWorthLink.click();
await expect(page).toHaveURL(/\/reports\/networth$/);
await expect(main.getByRole('heading', { name: 'Net worth', exact: true })).toBeVisible();
await expect(netWorthLink).toHaveAttribute('aria-current', 'page');
const period = page.getByRole('button', { name: 'Net worth period', exact: true });
const rows = page
.getByRole('table', { name: 'Net worth by valuation date', exact: true })
.locator('tbody tr');
await expect(period).toHaveText('3 months');
await expect(rows).toHaveCount(3);
await expect(rows.getByRole('rowheader')).toHaveText(['2026-02-28', '2026-03-31', '2026-04-16']);
for (const index of [0, 1]) {
await expect(rows.nth(index).getByRole('cell')).toHaveText([
'No reported amounts',
'No reported valuation'
]);
}
await expect(rows.last().getByRole('cell')).toHaveText([
'11825.00',
'USD',
'No unconverted units'
]);
const accountsText = await accounts.innerText();
const requests: string[] = [];
const observe = (request: Request) => {
if (
['fetch', 'xhr', 'document'].includes(request.resourceType()) ||
request.method() === 'POST'
) {
requests.push(`${request.method()} ${new URL(request.url()).pathname}`);
}
};
page.on('request', observe);
await page.evaluate(() => Object.assign(window, { netWorthNavigationToken: 'retained' }));
await period.click();
await page.getByRole('option', { name: '1 month', exact: true }).click();
await expect(page).toHaveURL(/\/reports\/networth\?months=1$/);
await expect(rows).toHaveCount(1);
await expect(netWorthLink).toHaveAttribute('aria-current', 'page');
await page.goBack();
await expect(period).toHaveText('3 months');
await expect(rows).toHaveCount(3);
expect(await accounts.innerText()).toBe(accountsText);
expect(await page.evaluate(() => Reflect.get(window, 'netWorthNavigationToken'))).toBe(
'retained'
);
expect(requests).toEqual([]);
page.off('request', observe);
await page.goto('/reports/networth?months=12');
await expect(period).toHaveText('12 months');
await expect(rows).toHaveCount(12);
await expect(rows.first().getByRole('rowheader')).toHaveText('2025-05-31');
await expect(rows.last().getByRole('rowheader')).toHaveText('2026-04-16');
await expect(netWorthLink).toHaveAttribute('aria-current', 'page');
await accounts.getByRole('link', { name: /^Everyday checking\b/ }).click();
await expect(main.getByRole('heading', { name: 'Everyday checking', exact: true })).toBeVisible();
// Investment registers still display the preparation timestamp used by this refresh check.
await accounts.getByRole('link', { name: /^Brokerage\b/ }).click();
await expect(main.getByRole('heading', { name: 'Brokerage', exact: true })).toBeVisible();
const registerStamp = main.getByText(/^Prepared at /).locator('time');
await expect(registerStamp).toHaveAttribute('datetime', savedAt);
// Explicit work happens in another authenticated page, not in the page left open.
const preparationPage = await context.newPage();
let laterAt: string;
try {
laterAt = await prepareReports(preparationPage);
} finally {
await preparationPage.close();
}
expect(laterAt).not.toBe(savedAt);
await expect(registerStamp).toHaveAttribute('datetime', savedAt);
// Query-only navigation retains component instances; the saved register must
// still refresh without a document reload.
const token = `account-navigation-${Date.now()}`;
await page.evaluate((value) => {
Object.assign(window, { accountNavigationToken: value });
}, token);
await main.getByRole('link', { name: 'Newest first', exact: true }).click();
await expect(page).toHaveURL(/\/registers\/investments\/[a-z0-9]{8}\?order=desc$/);
await expect(registerStamp).toHaveAttribute('datetime', laterAt);
expect(await page.evaluate(() => Reflect.get(window, 'accountNavigationToken'))).toBe(token);
expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(1440);
});
test('responsive Accounts supports phone, overlay drawer and desktop transitions in dark mode', async ({
page
}) => {
await page.setViewportSize({ width: 390, height: 844 });
await page.emulateMedia({ colorScheme: 'dark' });
await prepared(page);
const trigger = page.getByRole('button', { name: 'Accounts', exact: true });
const drawer = page.getByRole('dialog', { name: 'Accounts', exact: true });
await trigger.focus();
await page.keyboard.press('Enter');
await expect(drawer).toBeVisible();
const close = drawer.getByRole('button', { name: 'Close accounts', exact: true });
await expect(close).toBeFocused();
await expect(drawer).toBeInViewport({ ratio: 1 });
const phoneBox = await drawer.boundingBox();
expect(phoneBox!.x).toBeCloseTo(0, 0);
expect(phoneBox!.width).toBeCloseTo(390, 0);
await page.keyboard.press('Escape');
await expect(drawer).not.toBeVisible();
await expect(trigger).toBeFocused();
await page.keyboard.press('Enter');
await close.click();
await expect(drawer).not.toBeVisible();
await expect(trigger).toBeFocused();
await page.keyboard.press('Enter');
await expect(close).toBeFocused();
await page.setViewportSize({ width: 900, height: 844 });
await expect(drawer).toBeInViewport({ ratio: 1 });
const overlayBox = await drawer.boundingBox();
expect(overlayBox!.x).toBeGreaterThan(0);
expect(overlayBox!.x + overlayBox!.width).toBeCloseTo(900, 0);
await page.mouse.click(24, 200);
await expect(drawer).not.toBeVisible();
await expect(trigger).toBeFocused();
await trigger.click();
await drawer.getByRole('link', { name: /^Net worth/ }).click();
await expect(page).toHaveURL(/\/reports\/networth$/);
await expect(
page.locator('main').getByRole('heading', { name: 'Net worth', exact: true })
).toBeVisible();
await expect(drawer).not.toBeVisible();
await trigger.click();
await expect(drawer.getByRole('link', { name: /^Net worth/ })).toHaveAttribute(
'aria-current',
'page'
);
// The current destination still refreshes saved reports; closing must not cancel it.
const refreshed = page.waitForResponse(
(response) =>
response.request().method() === 'GET' &&
new URL(response.url()).pathname === '/reports/networth/__data.json'
);
await drawer.getByRole('link', { name: /^Net worth/ }).focus();
await page.keyboard.press('Enter');
const refresh = await refreshed;
expect(refresh.status()).toBe(200);
// The app's afterNavigate focus confirms completion; don't wait on transport drain.
await expect(page).toHaveURL(/\/reports\/networth$/);
await expect(drawer).not.toBeVisible();
await expect(page.locator('main')).toBeFocused();
await page
.getByRole('navigation', { name: 'Primary', exact: true })
.getByRole('link', { name: 'Overview', exact: true })
.click();
await expect(
page.locator('main').getByRole('heading', { name: 'Overview', exact: true })
).toBeVisible();
await trigger.click();
await drawer.getByRole('link', { name: 'Investments', exact: true }).click();
await expect(page).toHaveURL(/\/registers\/investments$/);
await expect(
page.locator('main').getByRole('heading', { name: 'Investment register', exact: true })
).toBeVisible();
await expect(drawer).not.toBeVisible();
await trigger.click();
await expect(close).toBeFocused();
await page.goBack();
await expect(
page.locator('main').getByRole('heading', { name: 'Overview', exact: true })
).toBeVisible();
await expect(drawer).not.toBeVisible();
await expect(trigger).not.toBeFocused();
await page.setViewportSize({ width: 390, height: 844 });
await trigger.click();
await expect(close).toBeFocused();
const checking = drawer.getByRole('link', { name: /^Everyday checking\b/ });
await checking.focus();
await page.evaluate(() => Object.assign(window, { drawerNavigationToken: 'retained' }));
await page.keyboard.press('Enter');
await expect(
page.locator('main').getByRole('heading', { name: 'Everyday checking', exact: true })
).toBeVisible();
await expect(drawer).not.toBeVisible();
await expect(trigger).not.toBeFocused();
// Closing the portal must not turn its link into a full-document navigation.
expect(await page.evaluate(() => Reflect.get(window, 'drawerNavigationToken'))).toBe('retained');
expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(390);
// The account drawer must not leave the register's existing dialog unusable.
const activity = page.getByRole('region', { name: 'Cash register activity', exact: true });
const opener = activity.getByRole('button', { name: /^View transaction details:/ }).first();
await opener.click();
const details = page.getByRole('dialog', { name: 'Transaction details', exact: true });
await expect(details).toBeVisible();
await details.getByRole('button', { name: 'Close transaction details', exact: true }).click();
await expect(opener).toBeFocused();
await trigger.click();
await expect(drawer).toBeVisible();
await page.setViewportSize({ width: 1440, height: 900 });
await expect(drawer).not.toBeVisible();
await expect(page.getByRole('complementary', { name: 'Accounts', exact: true })).toBeVisible();
// Real focus and scrolling detect stale modal inertness/scroll locks, not CSS classes.
const dateOrder = page.locator('main').getByRole('link', { name: /^Date:/ });
await dateOrder.focus();
await expect(dateOrder).toBeFocused();
// Cash history deliberately fills the viewport and scrolls internally. Use a
// normal document page to verify that closing the drawer released its body lock.
await page
.getByRole('navigation', { name: 'Primary', exact: true })
.getByRole('link', { name: 'Overview', exact: true })
.click();
await expect(
page.getByRole('heading', { name: 'Overview', exact: true, level: 1 })
).toBeVisible();
await page.setViewportSize({ width: 1440, height: 350 });
await page.evaluate(() => window.scrollTo(0, 0));
await page.getByRole('heading', { name: 'Overview', exact: true, level: 1 }).hover();
await page.mouse.wheel(0, 600);
await expect.poll(() => page.evaluate(() => window.scrollY)).toBeGreaterThan(0);
});