import {
cleanup,
fireEvent,
render as renderRaw,
screen,
within,
} from '@testing-library/react';
import { TooltipProvider } from '@workflow/web-shared';
import {
buildRunUsage,
formatLocalDataWrittenNote,
type RunUsage,
type RunUsageAggregates,
USAGE_DOCS_URL,
WORKFLOW_PRICING_DOCS_URL,
} from '@workflow/world';
import type { ReactNode } from 'react';
import { MemoryRouter } from 'react-router';
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
dataWrittenLabel,
RunUsageHeaderStats,
RunUsagePage,
RunUsagePageSkeleton,
runUsagePath,
STORAGE_DOCS_URL,
USAGE_TABLES_LAYOUT,
} from './run-usage';
/** The app's root provides tooltips and the router; so does each test. */
function Providers({ children }: { children: ReactNode }) {
return (
{children}
);
}
const render = (ui: React.ReactElement) =>
renderRaw(ui, { wrapper: Providers });
afterEach(cleanup);
const WORKFLOW = 'workflow//./src/workflows/order//processOrder';
const step = (name: string, file = 'order') =>
`step//./src/workflows/${file}//${name}`;
const stepGroup = (
stepName: string,
steps: number,
{ retries = 0, restarts }: { retries?: number; restarts?: number } = {}
) => {
const r = restarts ?? retries;
return {
stepName,
steps,
events: steps * 3 + retries + r,
created: steps,
started: steps + r,
completed: steps,
failed: 0,
retries,
restarts: r,
bytes: steps * 100,
};
};
const runOf = (
input: Partial &
Pick
) =>
buildRunUsage({
runId: 'wrun_1',
status: 'completed',
workflowName: WORKFLOW,
streamBytes: 0,
...input,
});
/**
* 1,145 events: nine step functions (one retried once), two hooks, an
* attribute write, a sleep, and the run's own events.
*/
function bigRun(overrides: Partial = {}): RunUsage {
return {
...runOf({
eventGroups: [
{ eventType: 'run_created', events: 1, bytes: 400 },
{ eventType: 'run_started', events: 1, bytes: 100 },
{ eventType: 'run_completed', events: 1, bytes: 300 },
{ eventType: 'attr_set', events: 1, bytes: 50 },
{ eventType: 'wait_created', events: 1, bytes: 20 },
{ eventType: 'wait_completed', events: 1, bytes: 20 },
{ eventType: 'hook_created', events: 2, bytes: 120 },
{ eventType: 'hook_received', events: 1, bytes: 80 },
],
hookGroups: [
{ hookId: 'hook_a', token: 'approval:order-1', events: 2 },
{ hookId: 'hook_b', token: 'refund:order-1', events: 1 },
],
stepGroups: [
stepGroup(step('chargeCard'), 300, { retries: 1 }),
stepGroup(step('sendEmail'), 50),
...['a', 'b', 'c', 'd', 'e', 'f', 'g'].map((n, i) =>
stepGroup(step(n), 7 - i)
),
],
streamBytes: 2500,
}),
...overrides,
};
}
const groups = () => screen.getAllByTestId('run-usage-group');
const typeLabels = () =>
screen.getAllByTestId('run-usage-type-label').map((el) => el.textContent);
const childrenOf = (type: string) =>
within(
document.querySelector(
`[data-testid="run-usage-children"][data-type="${type}"]`
) as HTMLElement
);
const childLabels = (type: string) =>
childrenOf(type)
.queryAllByTestId('run-usage-child-label')
.map((el) => el.textContent);
describe('RunUsageHeaderStats', () => {
it("shows the whole run's events, without the retry count", () => {
const usage = bigRun();
expect(usage.retries.count).toBe(1);
render(
);
expect(screen.getByTestId('run-usage-events').textContent).toBe(
usage.events.toLocaleString('en-US')
);
// Retries are on the step rows, not in the header.
expect(document.body.textContent).not.toMatch(/retr/i);
expect(document.body.textContent).not.toMatch(/billable/i);
});
it('links both figures to the run’s Usage page', () => {
const usage = bigRun();
render(
);
const events = screen.getByRole('link', {
name: `${usage.events.toLocaleString('en-US')} events, open usage`,
});
const storage = screen.getByRole('link', {
name: '41.4 KB storage (local), open usage',
});
expect(events.getAttribute('href')).toBe('/run/wrun_1/usage');
expect(storage.getAttribute('href')).toBe('/run/wrun_1/usage');
expect(storage.textContent).toBe('41.4 KB');
});
it('shows plain figures on the Usage page itself', () => {
render(
);
// The figures aren't links to the page they're on; only the storage
// help link (to the docs) is.
expect(
screen.queryAllByRole('link').map((a) => a.getAttribute('href'))
).toEqual([STORAGE_DOCS_URL]);
expect(screen.getByTestId('run-usage-data-written').textContent).toBe(
'41.4 KB'
);
});
it('labels a local run’s figure “Storage (local)”', () => {
render(
);
expect(screen.getByTestId('run-usage-data-label').textContent).toBe(
'Storage (local)'
);
expect(document.body.textContent).not.toMatch(/data written/i);
});
it('labels a billed run’s figure plain “Storage”, with no local note', () => {
render(
);
expect(screen.getByTestId('run-usage-data-label').textContent).toBe(
'Storage'
);
expect(screen.queryByTestId('run-usage-data-help')).toBeNull();
});
it('keeps “(local)” while the first read is pending on the Local World', () => {
render(
);
expect(screen.getByTestId('run-usage-data-label').textContent).toBe(
'Storage (local)'
);
expect(screen.queryByTestId('run-usage-events')).toBeNull();
expect(screen.queryByRole('link')).toBeNull();
expect(dataWrittenLabel(undefined, false)).toBe('Storage');
});
it('says what storage counts in a tooltip, and links the docs section', async () => {
// Radix measures the tooltip; jsdom has no ResizeObserver.
vi.stubGlobal(
'ResizeObserver',
class {
observe() {}
unobserve() {}
disconnect() {}
}
);
render(
);
const help = screen.getByRole('link', {
name: 'How Storage (local) is counted (opens in a new tab)',
});
// The Usage page's storage section, in a new tab.
expect(help.getAttribute('href')).toBe(STORAGE_DOCS_URL);
expect(STORAGE_DOCS_URL).toBe(
'https://workflow-sdk.dev/docs/observability/usage#storage'
);
expect(help.getAttribute('target')).toBe('_blank');
fireEvent.focus(help);
const tooltip = (await screen.findByRole('tooltip')).textContent ?? '';
// The shared note, the same sentence the other surfaces read.
expect(tooltip).toBe(formatLocalDataWrittenNote(bigRun()));
expect(tooltip).toContain('QuickJS VM snapshots');
expect(tooltip).toContain("dynamic workflow's code");
});
});
describe('the usage table', () => {
it('heads its first column “Type”, never “Span”', () => {
render();
const headers = within(screen.getByTestId('run-usage-types'))
.getAllByRole('columnheader')
.map((th) => th.textContent);
expect(headers[0]).toBe('Type');
expect(document.body.textContent).not.toMatch(/\bSpan\b/);
});
it('lists the types in the CLI’s order, flat, with no root row', () => {
render();
expect(typeLabels()).toEqual([
'Steps',
'Hooks',
'Attributes',
'Sleeps',
'Run',
]);
// No workflow row above them, and no "Built-in" group.
const table = screen.getByTestId('run-usage-types');
expect(table.textContent).not.toContain('processOrder');
expect(table.textContent).not.toContain('Built-in');
expect(screen.queryByTestId('run-usage-span')).toBeNull();
});
it('says what each type counts after its name', () => {
render();
const detail = (type: string) =>
groups()
.find((row) => row.getAttribute('data-type') === type)
?.querySelector('[data-testid="run-usage-group-detail"]')?.textContent;
expect(detail('steps')).toBe('378 calls');
expect(detail('hooks')).toBe('2 hooks');
expect(detail('attributes')).toBe('1 write');
expect(detail('sleeps')).toBe('1 sleep');
// Run: just "Run"; its event types are in its tooltip.
expect(detail('run')).toBeUndefined();
expect(document.body.textContent).not.toContain('×');
expect(document.body.textContent).not.toContain('1 created');
});
it('shows the Run row’s event types in a tooltip, on focus', async () => {
// Radix measures the tooltip; jsdom has no ResizeObserver.
vi.stubGlobal(
'ResizeObserver',
class {
observe() {}
unobserve() {}
disconnect() {}
}
);
render();
const run = groups().find((row) => row.getAttribute('data-type') === 'run');
const label = within(run as HTMLElement).getByTestId(
'run-usage-type-label'
);
expect(label.textContent).toBe('Run');
// Reachable by keyboard, not only on hover.
expect(label.tabIndex).toBe(0);
fireEvent.focus(label);
expect((await screen.findByRole('tooltip')).textContent).toBe(
'1 created, 1 started, 1 completed'
);
});
it('puts the retries badge under the name until the table has room for the bars and its column', () => {
render();
// The table's own width decides, not the viewport's: its wrapper is a
// size container, and the bars and the Retries column share one size.
const wrapper = screen.getByTestId('run-usage-types')
.parentElement as HTMLElement;
expect(wrapper.className.split(/\s+/)).toContain('@container');
const WIDE = '@min-[520px]:table-cell';
const charge = childrenOf('steps').getAllByTestId('run-usage-child')[0];
const [nameCell, ...cells] = within(charge).getAllByRole('cell');
// jsdom has no layout: check the container classes each copy sits in.
const inline = within(nameCell).getByTestId('run-usage-retries-inline');
expect(inline.textContent).toBe('1 retry');
expect(inline.parentElement?.className.split(/\s+/)).toContain(
'@min-[520px]:hidden'
);
// The share bar's cell appears at the same size as the Retries column.
const bar = cells.at(1) as HTMLElement;
expect(bar.className.split(/\s+/)).toEqual(
expect.arrayContaining(['hidden', WIDE])
);
const column = cells.at(-1) as HTMLElement;
expect(within(column).getByTestId('run-usage-retries').textContent).toBe(
'1 retry'
);
expect(column.className.split(/\s+/)).toEqual(
expect.arrayContaining(['hidden', WIDE])
);
// The header's bar and Retries columns hide with them.
const headers = within(screen.getByTestId('run-usage-types')).getAllByRole(
'columnheader'
);
for (const header of [headers.at(2), headers.at(-1)]) {
expect((header as HTMLElement).className.split(/\s+/)).toEqual(
expect.arrayContaining(['hidden', WIDE])
);
}
// The Steps row carries its retries too.
const steps = groups().find(
(row) => row.getAttribute('data-type') === 'steps'
);
expect(
within(steps as HTMLElement).getByTestId('run-usage-retries-inline')
.textContent
).toBe('1 retry');
// A row with no retries has no badge.
const sendEmail = childrenOf('steps').getAllByTestId('run-usage-child')[1];
expect(
within(sendEmail).queryByTestId('run-usage-retries-inline')
).toBeNull();
});
it('makes Steps and Hooks toggles, expanded by default', () => {
render();
const toggles = screen.getAllByTestId('run-usage-toggle');
expect(toggles.map((t) => t.textContent)).toEqual([
expect.stringContaining('Steps'),
expect.stringContaining('Hooks'),
]);
for (const toggle of toggles) {
// A native button: Enter and Space work without extra handlers.
expect(toggle.tagName).toBe('BUTTON');
expect(toggle.getAttribute('type')).toBe('button');
expect(toggle.getAttribute('aria-expanded')).toBe('true');
const controlled = document.getElementById(
toggle.getAttribute('aria-controls') ?? ''
);
expect(controlled?.getAttribute('data-testid')).toBe(
'run-usage-children'
);
expect(controlled?.hidden).toBe(false);
}
// Attributes, Sleeps and Run don't expand.
for (const row of groups().slice(2)) {
expect(row.querySelector('button')).toBeNull();
}
});
it('collapses and expands a group', () => {
render();
const steps = screen.getByRole('button', { name: /^Steps/ });
const children = document.getElementById(
steps.getAttribute('aria-controls') ?? ''
) as HTMLElement;
fireEvent.click(steps);
expect(steps.getAttribute('aria-expanded')).toBe('false');
expect(children.hidden).toBe(true);
// The other toggle keeps its own state.
expect(
screen
.getByRole('button', { name: /^Hooks/ })
.getAttribute('aria-expanded')
).toBe('true');
fireEvent.click(steps);
expect(steps.getAttribute('aria-expanded')).toBe('true');
expect(children.hidden).toBe(false);
});
it('lists each step function with its calls and retries, biggest first', () => {
render();
expect(childLabels('steps')).toEqual([
'chargeCard',
'sendEmail',
'a',
'b',
'c',
]);
const charge = childrenOf('steps').getAllByTestId('run-usage-child')[0];
expect(within(charge).getByTestId('run-usage-calls').textContent).toBe(
'300 calls'
);
expect(within(charge).getByTestId('run-usage-retries').textContent).toBe(
'1 retry'
);
// Past the limit, the rest fold into one row that opens them.
fireEvent.click(screen.getByRole('button', { name: '+ 4 other steps' }));
expect(childLabels('steps')).toEqual([
'chargeCard',
'sendEmail',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
]);
expect(screen.queryByRole('button', { name: /other steps/ })).toBeNull();
});
it('lists each hook by its token, without calls', () => {
render();
expect(childLabels('hooks')).toEqual([
'approval:order-1',
'refund:order-1',
]);
expect(childrenOf('hooks').queryByTestId('run-usage-calls')).toBeNull();
});
it('marks each type with the trace’s icon', () => {
render();
for (const row of groups()) {
expect(row.querySelectorAll('svg').length).toBeGreaterThanOrEqual(1);
}
});
it('keys step rows on the full name, and names the module when labels match', () => {
const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
// A live run with two `send` steps in different files that swap places
// between two reads.
const read = (a: number, b: number) =>
runOf({
status: 'running',
eventGroups: [{ eventType: 'run_created', events: 1, bytes: 1 }],
stepGroups: [
stepGroup(step('send', 'email'), a),
stepGroup(step('send', 'sms'), b),
],
});
const first = read(3, 1);
const { rerender } = render(
);
const labels = childLabels('steps');
expect(labels).toHaveLength(2);
expect(new Set(labels).size).toBe(2);
expect(labels[0]).toContain('send');
expect(labels[0]).toContain('email');
expect(labels[1]).toContain('sms');
const rowFor = (file: string) =>
childrenOf('steps')
.getAllByTestId('run-usage-child')
.find((row) => row.textContent?.includes(file));
const smsRow = rowFor('sms');
// Keyed on usageRows()' key: the step's full name.
expect(smsRow?.getAttribute('data-key')).toBe(step('send', 'sms'));
// The sms step overtakes: the rows re-sort, each keeping its element.
rerender();
expect(childLabels('steps')[0]).toContain('sms');
expect(rowFor('sms')).toBe(smsRow);
expect(
errors.mock.calls.some((call) => String(call[0]).includes('same key'))
).toBe(false);
errors.mockRestore();
});
it('keys hook rows on the hook id, so a reused token stays two rows', () => {
const usage = runOf({
eventGroups: [{ eventType: 'hook_created', events: 2, bytes: 1 }],
hookGroups: [
{ hookId: 'hook_1', token: 'approval', events: 1 },
{ hookId: 'hook_2', token: 'approval', events: 1 },
],
stepGroups: [],
});
render();
expect(childLabels('hooks')).toHaveLength(2);
expect(
childrenOf('hooks')
.getAllByTestId('run-usage-child')
.map((row) => row.getAttribute('data-key'))
).toEqual(['hook_1', 'hook_2']);
});
it('shows a long hook token in full, and lets it wrap at its separators', () => {
const token = 'approval:order-1042/region.us-east_1';
const usage = runOf({
eventGroups: [{ eventType: 'hook_created', events: 1, bytes: 1 }],
hookGroups: [{ hookId: 'hook_1', token, events: 1 }],
stepGroups: [],
});
render();
const label = childrenOf('hooks').getByTestId('run-usage-child-label');
// Nothing is cut: the whole token is the text, split only by .
expect(label.textContent).toBe(token);
// After each of : - / . - _
expect(label.querySelectorAll('wbr')).toHaveLength(6);
// And only there: nothing lets the browser break inside a word.
expect(label.className).not.toMatch(/overflow-wrap:anywhere|break-all/);
});
it('keeps the removed content off the page', () => {
const usage = bigRun({ status: 'running', inProgress: true });
render();
const text = document.body.textContent ?? '';
expect(screen.queryByTestId('run-usage-summary')).toBeNull();
expect(text).not.toMatch(/events · \d+ retr/);
expect(text).not.toContain('can still grow');
expect(text).not.toContain('On Vercel');
expect(text).not.toContain('Where the events come from');
expect(text).not.toContain('drives');
expect(document.querySelector('details')).toBeNull();
expect(text).not.toContain('steps wrote');
});
it('puts the two tables side by side from lg, stacked below it', () => {
render();
const tables = screen.getByTestId('run-usage-tables');
// jsdom has no layout: check the grid the breakpoints come from.
const classes = tables.className.split(/\s+/);
expect(classes).toContain('grid-cols-1');
expect(
classes.filter((c) => /^(sm|md|lg|xl|2xl):grid-cols-/.test(c))
).toEqual(['lg:grid-cols-[minmax(0,3fr)_minmax(0,2fr)]']);
const columns = [...tables.children];
expect(columns).toHaveLength(2);
expect(
columns[0].querySelector('[data-testid="run-usage-types"]')
).not.toBeNull();
expect(
columns[1].querySelector('[data-testid="run-usage-data"]')
).not.toBeNull();
});
it('gives the skeleton the same two-column layout', () => {
render();
const tables = screen.getByTestId('run-usage-tables');
expect(tables.className).toBe(USAGE_TABLES_LAYOUT);
expect(tables.children).toHaveLength(2);
});
});
describe('the storage table', () => {
it('leaves out the entities that wrote nothing, like the CLI', () => {
const usage = runOf({
eventGroups: [{ eventType: 'run_created', events: 1, bytes: 400 }],
stepGroups: [stepGroup(step('a'), 1)],
});
render();
expect(
screen.getAllByTestId('run-usage-entity').map((row) => row.textContent)
).toEqual(['Run400 B', 'Steps100 B']);
expect(screen.getByTestId('run-usage-total').textContent).toBe('500 B');
});
it('heads it “Storage (local)” and lists it by entity with a total', () => {
render();
expect(screen.getByTestId('run-usage-data-heading').textContent).toBe(
'Storage (local)'
);
expect(
screen.getAllByTestId('run-usage-entity').map((row) => row.textContent)
).toEqual(['Run890 B', 'Steps37.8 KB', 'Hooks200 B', 'Streams2.5 KB']);
expect(screen.getByTestId('run-usage-total').textContent).toBe('41.4 KB');
expect(screen.queryByTestId('run-usage-retained')).toBeNull();
});
it('heads it plain “Storage” on a billed run, with Data retained', () => {
render(
);
expect(screen.getByTestId('run-usage-data-heading').textContent).toBe(
'Storage'
);
expect(screen.getByTestId('run-usage-retained').textContent).toContain(
'41.4 KB-months'
);
});
it('says when a run’s data was deleted and its sizes are what it wrote', () => {
const usage = runOf({
eventGroups: [{ eventType: 'run_created', events: 1, bytes: 40 }],
stepGroups: [],
dataPurged: true,
savedDataWritten: { run: 300_000, step: 300_000, hook: 0, stream: 0 },
});
render();
expect(screen.getByTestId('run-usage-purged-note').textContent).toBe(
"This run's data was deleted; sizes show what it wrote."
);
expect(screen.getByTestId('run-usage-total').textContent).toBe('600 KB');
});
it('has no purge note for a run that kept its data', () => {
render();
expect(screen.queryByTestId('run-usage-purged-note')).toBeNull();
});
});
describe('the footer', () => {
it('says local runs are not billed, and links how usage is counted', () => {
render();
expect(screen.getByTestId('run-usage-footer').textContent).toBe(
"Local runs aren't billed. Counted from this run's log in your local data directory."
);
const link = screen.getByRole('link', {
name: 'How usage is counted (opens in a new tab)',
});
expect(link.getAttribute('href')).toBe(USAGE_DOCS_URL);
expect(USAGE_DOCS_URL).toBe(
'https://workflow-sdk.dev/docs/observability/usage'
);
expect(link.getAttribute('target')).toBe('_blank');
// The new tab is marked with the Docs button's icon, hidden from readers.
expect(link.querySelector('svg')?.getAttribute('aria-hidden')).toBe('true');
// One link: the docs page.
const footer = screen.getByTestId('run-usage-footer-line');
expect(footer.querySelectorAll('a')).toHaveLength(1);
});
it('never links pricing, even on a billed run', () => {
// Usage is counted on the Local World only; the page never shows on
// Vercel, so there is no pricing link to show.
render(
);
expect(screen.queryByRole('link', { name: /pricing/i })).toBeNull();
expect(document.body.innerHTML).not.toContain(WORKFLOW_PRICING_DOCS_URL);
const footer = screen.getByTestId('run-usage-footer-line');
expect(
[...footer.querySelectorAll('a')].map((a) => a.getAttribute('href'))
).toEqual([USAGE_DOCS_URL]);
});
it('puts the link inline, right after its sentence, with no arrow', () => {
render();
const line = screen.getByTestId('run-usage-footer-line');
expect(line.tagName).toBe('P');
expect(line.textContent).toBe(
"Local runs aren't billed. Counted from this run's log in your local data directory. How usage is counted (opens in a new tab)"
);
expect(line.textContent).not.toContain('→');
const footer = line.closest('footer');
expect(footer?.className ?? '').not.toMatch(
/justify-between|text-right|\bborder/
);
for (const a of line.querySelectorAll('a')) {
expect(a.className).toContain('whitespace-nowrap');
}
});
});
describe('RunUsagePage states', () => {
it('keeps the last numbers with a note when a refresh fails', () => {
render(
);
expect(screen.getByRole('status').textContent).toBe(
'Showing the last numbers read. Failed to refresh: locked'
);
expect(screen.getByTestId('run-usage-types')).toBeTruthy();
});
it('shows a failed first read with “Try again”, busy while it reads', () => {
const onRetry = vi.fn();
const { rerender } = render(
);
expect(screen.getByTestId('run-usage-error').textContent).toContain(
'Failed to read this run’s usage: locked'
);
fireEvent.click(screen.getByRole('button', { name: 'Try again' }));
expect(onRetry).toHaveBeenCalledTimes(1);
rerender(
);
const busy = screen.getByRole('button', { name: 'Try again' });
expect(busy.getAttribute('aria-busy')).toBe('true');
expect((busy as HTMLButtonElement).disabled).toBe(true);
});
it('shows the skeleton until the first read answers', () => {
render();
expect(screen.getByTestId('run-usage-skeleton')).toBeTruthy();
expect(screen.queryByTestId('run-usage-types')).toBeNull();
});
it('says the page isn’t available on a backend without usage, and links back', () => {
render();
expect(screen.getByTestId('run-usage-unsupported').textContent).toBe(
'This page isn’t available for this backend.Back to run'
);
expect(
screen.getByRole('link', { name: 'Back to run' }).getAttribute('href')
).toBe('/run/wrun_1');
});
it('renders a run with no events', () => {
const empty = runOf({ status: 'pending', eventGroups: [], stepGroups: [] });
render();
expect(screen.getByText('This run has no events yet.')).toBeTruthy();
expect(screen.getByTestId('run-usage-total').textContent).toBe('0 B');
});
});
describe('runUsagePath', () => {
it('escapes the run ID', () => {
expect(runUsagePath('wrun_1')).toBe('/run/wrun_1/usage');
expect(runUsagePath('a/b')).toBe('/run/a%2Fb/usage');
});
});