mirror of
https://github.com/Zakkaus/doona.git
synced 2026-12-05 16:02:03 +00:00
A route replacement during a form submit no longer starts a transition, which threw "Failed to construct 'FormData'". Hidden rule and trace tabs stop polling, verdicts of sources in use are never evicted, and tests assert that retained hidden pages make no requests. The startup budgets follow the size CI measures for this tree plus 0.3%: login 213,700 and Activity 326,000 bytes gzip. CI builds about 0.7 KB and 1 KB larger than a local build.
328 lines
19 KiB
TypeScript
328 lines
19 KiB
TypeScript
import {createMockApi} from '../mock';
|
|
import {ApiError} from '../src/api/error';
|
|
import {expect, expectTextInside, mockBackend, settleFrames, test, box} from './fixtures';
|
|
|
|
// Without the backend's rule dictionary the list falls back to the flows grouped by rule.
|
|
test('the rule list filters by source without accumulating polls, sorted in config order', async ({page}) => {
|
|
const api = createMockApi();
|
|
const capabilities = await api.capabilities();
|
|
capabilities.resources.events.available = false;
|
|
capabilities.resources.rules.available = false;
|
|
const flows = await api.flows({detail: 'full', limit: 1000});
|
|
await page.clock.install();
|
|
await page.addInitScript(() => localStorage.setItem('doona-api', location.origin));
|
|
await page.route('**/api/v1/capabilities', route => route.fulfill({json: capabilities}));
|
|
await page.route('**/api/v1/version', async route => route.fulfill({json: await api.version()}));
|
|
await page.route('**/api/v1/runtime/memory', async route => route.fulfill({json: await api.runtimeMemory()}));
|
|
await page.route('**/api/v1/runtime/traffic/history?*', async route => route.fulfill({json: await api.trafficHistory()}));
|
|
await page.route('**/api/v1/config', async route => route.fulfill({json: await api.config()}));
|
|
await page.route('**/api/v1/groups', async route => route.fulfill({json: await api.groups()}));
|
|
await page.route('**/api/v1/nodes?*', async route => route.fulfill({json: await api.nodes({limit: 1000})}));
|
|
await page.route('**/api/v1/runtime', async route => route.fulfill({json: await api.runtime()}));
|
|
await page.route('**/api/v1/runtime/settings', async route => route.fulfill({json: await api.runtimeSettings()}));
|
|
await page.route('**/api/v1/flows?*', route => route.fulfill({json: flows}));
|
|
await page.goto('/#/rules?tab=list&view=advanced');
|
|
const panel = page.getByRole('tabpanel', {name: 'Routing rules'});
|
|
const rows = panel.locator('.rp-table [role=rowgroup]:last-child [role=row][data-key]');
|
|
await expect(rows.first()).toBeVisible();
|
|
const allCount = await rows.count();
|
|
const snapshot = await rows.allTextContents();
|
|
const ids = await panel.getByRole('rowheader').allTextContents();
|
|
expect(ids.length).toBe(allCount);
|
|
const sourceRows = await Promise.all(
|
|
['Kernel', 'Userspace', 'Unknown'].map(async source => ({
|
|
source,
|
|
texts: await rows.filter({has: page.locator('.rp-badge', {hasText: new RegExp(`^${source}$`)})}).allTextContents()
|
|
}))
|
|
);
|
|
const filters = panel.getByRole('radiogroup', {name: 'Rule source'});
|
|
for (const {source, texts} of sourceRows) {
|
|
await filters.getByRole('radio', {name: source, exact: true}).click();
|
|
await expect(rows).toHaveCount(texts.length);
|
|
expect(await rows.allTextContents()).toEqual(texts);
|
|
}
|
|
expect(sourceRows.some(({texts}) => texts.length < allCount)).toBe(true);
|
|
await filters.getByRole('radio', {name: 'All', exact: true}).click();
|
|
await expect(rows).toHaveCount(allCount);
|
|
await page.clock.fastForward(6000);
|
|
expect(await rows.allTextContents()).toEqual(snapshot);
|
|
});
|
|
|
|
test('the rule list keeps exact loss counts and replaces an empty snapshot', async ({page}) => {
|
|
const api = createMockApi();
|
|
const capabilities = await api.capabilities();
|
|
capabilities.resources.events.available = false;
|
|
capabilities.resources.rules.available = false;
|
|
const snapshot = await api.flows({detail: 'full', limit: 1000});
|
|
const flow = snapshot.flows[0];
|
|
snapshot.flows = Array.from({length: 13}, (_, i) => ({
|
|
...flow,
|
|
id: `flow-${i}`,
|
|
rule_id: `rule-${i}`,
|
|
rule_expression: `domain(full: rule-${i}.example)`,
|
|
rule_source: 'kernel'
|
|
}));
|
|
snapshot.flows.push(
|
|
{...flow, id: 'null-recomputed', rule_id: null, rule_expression: null, rule_source: 'recomputed'},
|
|
{...flow, id: 'null-unknown', rule_id: null, rule_expression: null, rule_source: 'unknown'}
|
|
);
|
|
snapshot.dropped_records = '18446744073709551615';
|
|
await page.clock.install();
|
|
await page.addInitScript(() => localStorage.setItem('doona-api', location.origin));
|
|
await page.route('**/api/v1/capabilities', route => route.fulfill({json: capabilities}));
|
|
await page.route('**/api/v1/version', async route => route.fulfill({json: await api.version()}));
|
|
await page.route('**/api/v1/runtime/memory', async route => route.fulfill({json: await api.runtimeMemory()}));
|
|
await page.route('**/api/v1/runtime/traffic/history?*', async route => route.fulfill({json: await api.trafficHistory()}));
|
|
await page.route('**/api/v1/config', async route => route.fulfill({json: await api.config()}));
|
|
await page.route('**/api/v1/groups', async route => route.fulfill({json: await api.groups()}));
|
|
await page.route('**/api/v1/nodes?*', async route => route.fulfill({json: await api.nodes({limit: 1000})}));
|
|
await page.route('**/api/v1/runtime', async route => route.fulfill({json: await api.runtime()}));
|
|
await page.route('**/api/v1/runtime/settings', async route => route.fulfill({json: await api.runtimeSettings()}));
|
|
await page.route('**/api/v1/flows?*', route => route.fulfill({json: snapshot}));
|
|
await page.goto('/#/rules?tab=list&view=advanced');
|
|
const panel = page.getByRole('tabpanel', {name: 'Routing rules'});
|
|
const rows = panel.locator('.rp-table [role=rowgroup]:last-child [role=row][data-key]');
|
|
await expect(rows).toHaveCount(15);
|
|
const ids = await panel.getByRole('rowheader').allTextContents();
|
|
expect(ids.slice(0, 3)).toEqual(['domain(full: rule-0.example)', 'domain(full: rule-1.example)', 'domain(full: rule-2.example)']);
|
|
expect(ids.slice(-2)).toEqual(['Unknown rule', 'Unknown rule']);
|
|
await expect(rows.first()).toContainText('6.7%');
|
|
await expect(panel.getByText('18,446,744,073,709,551,615 dropped records', {exact: true})).toBeVisible();
|
|
await panel.getByRole('radio', {name: 'Unknown', exact: true}).click();
|
|
await expect(rows).toHaveCount(1);
|
|
snapshot.flows = [];
|
|
snapshot.dropped_records = null;
|
|
await page.clock.fastForward(16000);
|
|
await expect(rows).toHaveCount(0);
|
|
await expect(panel.getByText('No flows from this source', {exact: true})).toBeVisible();
|
|
await expect(panel.getByText('Dropped record count unknown', {exact: true})).toBeVisible();
|
|
});
|
|
|
|
test.describe('without flow capability', () => {
|
|
test.use({storage: {'doona-mock-profile': 'base'}});
|
|
|
|
test('the page leaves the navigation and a deep link says so', async ({page}) => {
|
|
await page.goto('/#/rules');
|
|
await expect(page.locator('.rp-nav[href="#/rules"]')).toHaveAttribute('data-unavailable', '');
|
|
await expect(page.locator('.rp-content')).toContainText('This backend does not provide this page');
|
|
await expect(page.getByRole('tab')).toHaveCount(0);
|
|
await expect(page).toHaveURL(/#\/rules$/);
|
|
});
|
|
});
|
|
|
|
test('trace query mode validates ports and shows evaluations for both DNS address families', async ({page}) => {
|
|
const api = createMockApi();
|
|
const capabilities = await api.capabilities();
|
|
for (const resource of Object.values(capabilities.resources)) resource.available = false;
|
|
capabilities.resources.routing_trace.available = true;
|
|
capabilities.resources.routing_trace.resolve_modes = ['none'];
|
|
capabilities.resources.dns_query.available = true;
|
|
capabilities.resources.dns_query.limits!.max_types_per_request = 1;
|
|
const requested: string[][] = [];
|
|
await page.addInitScript(() => localStorage.setItem('doona-api', location.origin));
|
|
await page.route('**/api/v1/capabilities', route => route.fulfill({json: capabilities}));
|
|
await page.route('**/api/v1/version', async route => route.fulfill({json: await api.version()}));
|
|
await page.route('**/api/v1/dns/query?*', async route => {
|
|
const body = route.request().postDataJSON();
|
|
const types = body.type;
|
|
requested.push(types);
|
|
expect(types).toHaveLength(1);
|
|
await route.fulfill({json: await api.dnsQuery(body.domain, types)});
|
|
});
|
|
await page.route('**/api/v1/routing/trace', async route => {
|
|
await route.fulfill({json: await api.routingTrace(route.request().postDataJSON())});
|
|
});
|
|
await page.goto('/#/rules?tab=trace');
|
|
await expect(page.getByLabel('Domain', {exact: true})).toHaveValue('');
|
|
await expect(page.getByRole('textbox', {name: 'Destination port', exact: true})).toHaveValue('');
|
|
const run = page.getByRole('button', {name: 'Run trace', exact: true});
|
|
// A disabled button sits in its tooltip wrapper and still lines up with the input boxes beside it.
|
|
await page.setViewportSize({width: 1600, height: 900});
|
|
await expect(run).toBeDisabled();
|
|
const [button, input] = [await box(run), await box(page.locator('.rp-input', {has: page.getByLabel('Domain', {exact: true})}))];
|
|
expect(Math.abs(button.y - input.y)).toBeLessThanOrEqual(1);
|
|
expect(Math.abs(button.y + button.height - (input.y + input.height))).toBeLessThanOrEqual(1);
|
|
expect(requested).toEqual([]);
|
|
await page.getByLabel('Domain', {exact: true}).fill('trace.example');
|
|
// The number field keeps a port above its maximum while it is typed and snaps it into range on leaving.
|
|
await page.getByRole('textbox', {name: 'Destination port', exact: true}).fill('65536');
|
|
await page.getByRole('textbox', {name: 'Destination port', exact: true}).blur();
|
|
await expect(page.getByRole('textbox', {name: 'Destination port', exact: true})).toHaveValue('65535');
|
|
await page.getByRole('textbox', {name: 'Destination port', exact: true}).fill('443');
|
|
await run.click();
|
|
await expect(page.getByRole('heading', {name: '192.0.2.14', exact: true})).toBeVisible();
|
|
await expect(page.getByRole('heading', {name: '2001:db8::14', exact: true})).toBeVisible();
|
|
await expect(page.getByRole('grid', {name: 'Rule evaluation 1', exact: true})).toBeVisible();
|
|
await expect(page.getByRole('grid', {name: 'Rule evaluation 2', exact: true})).toBeVisible();
|
|
await expect(page.getByRole('heading', {name: 'DNS query: trace.example', exact: true})).toHaveCount(1);
|
|
await expect(page.getByRole('heading', {name: /^DNS: /})).toHaveCount(0);
|
|
expect(requested).toEqual([['A'], ['AAAA']]);
|
|
});
|
|
|
|
test('without a rule dictionary, Retry refetches the flows the distribution is built from', async ({page}) => {
|
|
const {api, capabilities, handlers} = await mockBackend(page);
|
|
capabilities.resources.rules.available = false;
|
|
let fail = true;
|
|
handlers['GET flows'] = async () => {
|
|
if (fail) throw new ApiError(500, 'internal', 'Flows unavailable');
|
|
return api.flows({detail: 'full', limit: 1000});
|
|
};
|
|
// Frozen timers: only the retry, not the next poll, can bring the flows back.
|
|
await page.clock.install();
|
|
await page.goto('/#/rules?tab=list&view=advanced');
|
|
const panel = page.getByRole('tabpanel', {name: 'Routing rules'});
|
|
const alert = panel.getByRole('alert').filter({hasText: 'Flows unavailable'});
|
|
await expect(alert).toBeVisible();
|
|
fail = false;
|
|
await alert.getByRole('button', {name: 'Retry', exact: true}).click();
|
|
await expect(alert).toHaveCount(0);
|
|
await expect(panel.locator('.rp-table [role=rowgroup]:last-child [role=row][data-key]').first()).toBeVisible();
|
|
});
|
|
|
|
test('Cancel on a rule write that has landed reads the sources again, so the next write starts from it', async ({page}) => {
|
|
const {api} = await mockBackend(page);
|
|
// The page's polls of the accepted write never answer; only the backend finishes it.
|
|
await page.route('**/api/v1/operations/**', () => {});
|
|
await page.goto('/#/rules?tab=list&view=advanced');
|
|
const add = page.getByRole('button', {name: 'Add rule', exact: true});
|
|
const dialog = page.getByRole('dialog');
|
|
const write = async (value: string) => {
|
|
await add.click();
|
|
await dialog.getByRole('textbox', {name: 'Values', exact: true}).fill(value);
|
|
const written = page.waitForResponse(response => response.request().method() === 'PUT');
|
|
await dialog.getByRole('button', {name: 'Add rule', exact: true}).click();
|
|
return written;
|
|
};
|
|
const first = await write('example.org');
|
|
expect(first.status()).toBe(202);
|
|
// Take the operation from Location: CI has seen Chromium discard this response's body before json() read it.
|
|
const operation_id = new URL(first.headers().location, first.url()).pathname.split('/').pop()!;
|
|
await expect.poll(async () => (await api.operation(operation_id)).status).toBe('succeeded');
|
|
await dialog.getByRole('button', {name: 'Cancel', exact: true}).click();
|
|
await expect(dialog).toHaveCount(0);
|
|
expect((await write('example.net')).status()).toBe(202);
|
|
});
|
|
|
|
test('the rule list fits its rows instead of holding a page of empty space', async ({page}) => {
|
|
const {api, handlers} = await mockBackend(page);
|
|
const dictionary = await api.rules();
|
|
handlers['GET rules'] = async () => ({...dictionary, rules: dictionary.rules.slice(0, 3)});
|
|
await page.goto('/#/rules?tab=list&view=advanced');
|
|
const table = page.locator('.rp-table', {has: page.getByRole('grid', {name: 'Routing rules'})});
|
|
await expect(table.locator('[role=row][data-key]')).toHaveCount(3);
|
|
// Wrapped rows grow with their text; the container ends at the final row without a fill-height gap.
|
|
const geometry = await table.evaluate(el => {
|
|
const rows = el.querySelectorAll('[role=row][data-key]');
|
|
return {bottom: el.getBoundingClientRect().bottom, rowBottom: rows[rows.length - 1].getBoundingClientRect().bottom};
|
|
});
|
|
expect(geometry.bottom - geometry.rowBottom).toBeGreaterThanOrEqual(0);
|
|
expect(geometry.bottom - geometry.rowBottom).toBeLessThanOrEqual(2);
|
|
});
|
|
|
|
for (const extra of [0, 25]) {
|
|
test(`wrapped rule cells share their row edges in a ${extra ? 'virtualised' : 'native'} table`, async ({page}) => {
|
|
await page.setViewportSize({width: 1280, height: 900});
|
|
const {api} = await mockBackend(page);
|
|
const source = (await api.config()).sources.find(source => source.id === 'src-main')!;
|
|
const expression = 'domain(suffix: ghcr.io, suffix: brew.sh, suffix: nix.org, suffix: nixos.org, suffix: iriswo.com)';
|
|
const added = [expression, ...Array.from({length: extra}, (_, i) => `domain(full: row-${i}.example)`)];
|
|
const content = source.content!.replace(/^routing\s*\{/m, `routing {\n${added.map(rule => ` ${rule} -> proxy`).join('\n')}`);
|
|
await api.pollOperation(await api.replaceConfigSource(source.id, content, `"${source.content_sha256}"`));
|
|
await page.goto('/#/rules?view=advanced');
|
|
const cell = page.locator('.rp-cell-wrap').filter({hasText: expression});
|
|
await expect(cell).toBeVisible();
|
|
await page.evaluate(() => document.fonts.ready);
|
|
await settleFrames(page);
|
|
const row = cell.locator('xpath=ancestor::*[@role="row"][1]');
|
|
const grid = page.getByRole('grid', {name: 'Routing rules'});
|
|
expect(await grid.evaluate(el => el.tagName)).toBe(extra ? 'DIV' : 'TABLE');
|
|
const bounds = await row.evaluate(el => {
|
|
const row = el.getBoundingClientRect();
|
|
return {
|
|
height: row.height,
|
|
edges: [...el.querySelectorAll('[role=gridcell], [role=rowheader]')].map(cell => {
|
|
const rect = cell.getBoundingClientRect();
|
|
return {top: rect.top - row.top, bottom: rect.bottom - row.bottom};
|
|
})
|
|
};
|
|
});
|
|
expect(bounds.edges.length).toBeGreaterThan(1);
|
|
for (const edge of bounds.edges) {
|
|
expect(Math.abs(edge.top)).toBeLessThanOrEqual(1);
|
|
expect(Math.abs(edge.bottom)).toBeLessThanOrEqual(1);
|
|
}
|
|
expect(bounds.height).toBeGreaterThan(40);
|
|
const short = grid.locator('[role=row][data-key]').nth(1);
|
|
expect((await box(short)).height).toBe(40);
|
|
});
|
|
}
|
|
|
|
for (const width of [1440, 768, 390]) {
|
|
test(`rule text and controls follow kit geometry at ${width}px`, async ({page}) => {
|
|
await page.setViewportSize({width, height: 1000});
|
|
const {api} = await mockBackend(page);
|
|
const source = (await api.config()).sources.find(source => source.id === 'src-main')!;
|
|
const expression = `domain(full: '${'a'.repeat(63)}.example')`;
|
|
const content = source.content!.replace(/^routing\s*\{/m, `routing {\n ${expression} -> proxy`);
|
|
expect(content).toContain(expression);
|
|
await api.pollOperation(await api.replaceConfigSource(source.id, content, `"${source.content_sha256}"`));
|
|
await page.goto('/#/rules?view=advanced');
|
|
const longCell = page.locator('.rp-cell-wrap').filter({hasText: expression});
|
|
await expectTextInside(longCell);
|
|
await expect(longCell.locator('.rp-code')).toHaveText(expression);
|
|
const add = page.getByRole('button', {name: 'Add rule', exact: true});
|
|
await expect(add).toBeVisible();
|
|
const bar = page.locator('.rp-toolbar').filter({has: add});
|
|
const segment = await bar.locator('.rp-seg').boundingBox();
|
|
expect((await box(add)).height).toBe(segment!.height);
|
|
const cells = page.locator('.rp-cell-wrap');
|
|
await expect(cells.first()).toBeVisible();
|
|
expect(
|
|
await cells.evaluateAll(elements => elements.every(el => el.scrollWidth <= el.clientWidth + 1 && getComputedStyle(el).whiteSpace === 'normal'))
|
|
).toBe(true);
|
|
await page.goto('/#/rules?view=simple');
|
|
const labels = page.locator('.rp-switch');
|
|
await expect(labels).toHaveCount(3);
|
|
for (const label of await labels.all()) await expect(label).toHaveCSS('font-size', '14px');
|
|
for (const description of await page.locator('.rp-radio-text .rp-label, .rp-field > .rp-label').all()) {
|
|
await expect(description).toHaveCSS('font-size', '12px');
|
|
await expect(description).toHaveCSS('line-height', '16px');
|
|
}
|
|
});
|
|
}
|
|
|
|
test('long template explanations remain available in contextual help', async ({page}) => {
|
|
await page.goto('/#/rules?view=simple');
|
|
const more = page.getByRole('button', {name: 'More templates', exact: true});
|
|
await expect(more).toBeVisible();
|
|
if ((await more.getAttribute('aria-expanded')) === 'false') await more.click();
|
|
const help = page.locator('.rp-radios .rp-help').first();
|
|
await expect(help).toBeVisible();
|
|
await help.click();
|
|
await expect(page.locator('.rp-popover')).toContainText('GFW');
|
|
await page.keyboard.press('Escape');
|
|
await expect(help).toBeFocused();
|
|
});
|
|
|
|
test('hidden rule and trace tabs stop fetching flows and inventory', async ({page}) => {
|
|
const {requests, capabilities} = await mockBackend(page);
|
|
capabilities.resources.events.available = false;
|
|
await page.clock.install();
|
|
await page.goto('/#/rules?view=advanced');
|
|
await expect(page.getByRole('tabpanel', {name: 'Routing rules'}).getByRole('rowheader').first()).toBeVisible();
|
|
await page.getByRole('tab', {name: 'Trace simulation', exact: true}).click();
|
|
await expect(page.getByRole('tabpanel', {name: 'Trace simulation'})).toBeVisible();
|
|
await expect.poll(() => requests.filter(request => new URL(request.url()).pathname === '/api/v1/nodes').length).toBeGreaterThan(0);
|
|
await page.getByRole('tab', {name: 'DNS rules', exact: true}).click();
|
|
await expect(page.getByRole('tabpanel', {name: 'DNS rules'})).toBeVisible();
|
|
const reads = () => requests.filter(request => /\/api\/v1\/(flows|nodes|groups)(?:\?|$)/.test(request.url())).length;
|
|
await settleFrames(page);
|
|
const before = reads();
|
|
expect(before).toBeGreaterThan(0);
|
|
await page.clock.fastForward(60_000);
|
|
await settleFrames(page);
|
|
expect(reads()).toBe(before);
|
|
await page.getByRole('tab', {name: 'Routing rules', exact: true}).click();
|
|
await expect.poll(reads).toBeGreaterThan(before);
|
|
});
|