// The Audit page: deep-link redirect, server-evaluated filters, change-set and // runtime detail, revert, saved views, export, live tail and phone width. // // Rows come from the seeded stack's real feed. When a precondition is missing // (no revertable change set, no runtime task event) the check says so instead // of passing silently. const assert = require('node:assert/strict'); const fs = require('node:fs'); const DEFAULT_WINDOW_MS = 30 * 24 * 60 * 60 * 1000; function isAudit(u) { return u.pathname === '/api/audit'; } module.exports = async ({ page, go, shot, fail }) => { page.setDefaultTimeout(10000); const auditRequests = []; page.on('request', (request) => { try { const url = new URL(request.url()); if (isAudit(url) || url.pathname === '/api/audit/export') auditRequests.push(url); } catch { // A non-URL request cannot be an audit call. } }); let loads = 0; page.on('load', () => (loads += 1)); const requestMatching = async (action, predicate, timeout = 6000) => { const waiter = page .waitForRequest( (request) => { try { const url = new URL(request.url()); return (isAudit(url) || url.pathname === '/api/audit/export') && predicate(url); } catch { return false; } }, { timeout }, ) .catch(() => null); await action(); return waiter; }; const waitForUrl = (predicate, timeout = 8000) => page.waitForURL((url) => predicate(new URL(url)), { timeout }); const lastRequest = () => auditRequests[auditRequests.length - 1] || null; // ---- deep-link redirect ---------------------------------------------------- await go('/settings/audit'); const status = await page.evaluate(async () => (await fetch('/api/auth/status')).json()); const actor = status?.user?.id; assert.ok(actor, 'signed-in identity has no id'); const deepLink = `/settings/history?section=container-runtime-policies&actor=${encodeURIComponent(actor)}&since=30d#deep`; await go(deepLink); assert.match(page.url(), /\/settings\/audit\?/, 'history did not redirect to the audit route'); assert.match(page.url(), /section=container-runtime-policies/, 'redirect dropped section'); assert.match(page.url(), new RegExp(`actor=${encodeURIComponent(actor)}`), 'redirect dropped actor'); assert.match(page.url(), /since=30d/, 'redirect dropped since'); assert.match(page.url(), /#deep$/, 'redirect dropped the hash'); const redirected = lastRequest(); assert.ok(redirected, 'the redirect issued no /api/audit request'); assert.equal(redirected.searchParams.get('kind'), 'container-runtime-policies'); assert.equal(redirected.searchParams.get('actor'), actor); // The feed must be readable before anything below can mean anything. A 500 // here is the whole page: say what the server answered instead of timing out. const probe = await page.evaluate(async () => { const response = await fetch('/api/audit?limit=1'); return { status: response.status, body: await response.text() }; }); if (probe.status !== 200) { await shot('unavailable'); fail(`GET /api/audit returned ${probe.status}: ${probe.body.slice(0, 200)}`); return; } const kindChip = page.locator('ul[aria-label="Active filters"] li', { hasText: 'Kind:' }); await kindChip.first().waitFor({ timeout: 8000 }); assert.match(await kindChip.first().innerText(), /Container runtime policies/, 'kind chip missing its catalog title'); assert.match( await page.locator('ul[aria-label="Active filters"] li', { hasText: 'Actor:' }).first().innerText(), new RegExp(actor), 'actor chip missing', ); await shot('redirect'); await go('/settings/audit'); await page.locator('[data-row-key]').first().waitFor(); await shot('desktop'); // ---- filters --------------------------------------------------------------- await go('/settings/audit'); const search = page.getByLabel('Search events', { exact: true }); let request = await requestMatching( () => search.fill('repository'), (u) => u.searchParams.get('q') === 'repository', ); assert.ok(request, 'search issued no matching /api/audit request'); await waitForUrl((u) => u.searchParams.get('q') === 'repository'); assert.equal((await search.inputValue()), 'repository'); await go('/settings/audit'); request = await requestMatching( () => page.getByRole('button', { name: 'Runtime', exact: true }).click(), (u) => u.searchParams.get('categories') === 'runtime', ); assert.ok(request, 'category filter issued no matching /api/audit request'); await waitForUrl((u) => u.searchParams.get('categories') === 'runtime'); assert.equal( await page.getByRole('button', { name: 'Runtime', exact: true }).getAttribute('aria-pressed'), 'true', ); // Time: Any time drops the window from the request rather than guessing one. await page.getByRole('combobox', { name: 'Time range', exact: true }).click(); request = await requestMatching( () => page.getByRole('option', { name: 'Any time', exact: true }).click(), (u) => u.searchParams.get('from') === null && u.searchParams.get('categories') === 'runtime', ); assert.ok(request, 'Any time issued no matching /api/audit request'); await waitForUrl((u) => u.searchParams.get('since') === 'any'); assert.equal(await page.getByRole('combobox', { name: 'Time range', exact: true }).innerText(), 'Any time'); // Back/forward restore the controls the URL describes. const backRequest = page.waitForRequest( (r) => { const u = new URL(r.url()); return u.pathname === '/api/audit' && u.searchParams.get('categories') === 'runtime' && !!u.searchParams.get('from'); }, { timeout: 8000 }, ); await page.goBack(); await backRequest; await waitForUrl((u) => u.searchParams.get('categories') === 'runtime' && u.searchParams.get('since') === null); assert.equal(await page.getByRole('combobox', { name: 'Time range', exact: true }).innerText(), 'Last 30 days'); assert.equal( await page.getByRole('button', { name: 'Runtime', exact: true }).getAttribute('aria-pressed'), 'true', 'back did not restore the category toggle', ); const forwardRequest = page.waitForRequest( (r) => { const u = new URL(r.url()); return u.pathname === '/api/audit' && u.searchParams.get('categories') === 'runtime' && u.searchParams.get('from') === null; }, { timeout: 8000 }, ); await page.goForward(); await forwardRequest; await waitForUrl((u) => u.searchParams.get('since') === 'any'); assert.equal(await page.getByRole('combobox', { name: 'Time range', exact: true }).innerText(), 'Any time'); // Denied reaches the server through the same URL contract as the other // filters. It must both update the URL and issue a request. await go('/settings/audit'); const denied = page.getByRole('button', { name: 'Denied', exact: true }); request = await requestMatching( () => denied.click(), (u) => u.searchParams.get('outcome') === 'denied', 3000, ); const deniedUrl = new URL(page.url()); if (!request || deniedUrl.searchParams.get('outcome') !== 'denied') { await shot('denied'); fail( `Denied filter did not reach the server: URL ${page.url()} has outcome=${deniedUrl.searchParams.get('outcome') ?? 'absent'}, ` + `aria-pressed=${await denied.getAttribute('aria-pressed')}, no /api/audit request with outcome=denied`, ); } else { assert.equal(await denied.getAttribute('aria-pressed'), 'true'); } // Advanced actor filter: server-evaluated, in the URL, shown as a chip. await go('/settings/audit'); await page.getByRole('button', { name: 'Filters', exact: true }).click(); const actorInput = page.getByLabel('Actor', { exact: true }); request = await requestMatching( async () => { await actorInput.fill(actor); await actorInput.press('Enter'); }, (u) => u.searchParams.get('actor') === actor, ); assert.ok(request, 'advanced actor filter issued no matching /api/audit request'); await waitForUrl((u) => u.searchParams.get('actor') === actor); assert.match( await page.locator('ul[aria-label="Active filters"] li', { hasText: 'Actor:' }).first().innerText(), new RegExp(actor), ); await shot('filters'); await page.keyboard.press('Escape'); // ---- change set, revert dialog, keyboard -------------------------------- await go('/settings/audit'); await page.locator('[data-row-key]').first().waitFor(); const from = new Date(Date.now() - DEFAULT_WINDOW_MS).toISOString(); // The feed page size is 100: only rows in this response are rendered. const feed = await page.evaluate( async (iso) => (await fetch(`/api/audit?limit=100&from=${encodeURIComponent(iso)}`)).json(), from, ); const records = feed.records || []; assert.ok(records.length, 'the seeded feed is empty'); const changeSet = records.find( (row) => row.target?.kind === 'resource' && (row.detail?.fields || []).length > 0 && (row.detail?.version ?? 0) > 1, ); if (!changeSet) { fail('no revertable settings change set in the feed; cannot exercise the diff or Revert'); } else { const row = page.locator(`[data-row-key="${changeSet.id}"]`); await row.waitFor(); await row.scrollIntoViewIfNeeded(); await row.locator('td').first().click(); assert.equal(await row.getAttribute('aria-expanded'), 'true', 'click did not expand the change set'); const detail = row.locator('xpath=following-sibling::tr[1]'); await detail.getByText('Before:', { exact: false }).first().waitFor(); assert.match(await detail.innerText(), /After:/, 'expanded change set has no after value'); assert.equal( await detail.getByRole('button', { name: /^Revert \S/ }).count(), 0, 'the audit diff offers a per-field revert', ); const revertAll = page.getByRole('button', { name: 'Revert', exact: true }); assert.ok((await revertAll.count()) > 0, 'no Revert action on a revertable change set'); await shot('change-set'); await revertAll.first().click(); const dialog = page.getByRole('alertdialog'); await dialog.waitFor(); assert.match(await dialog.innerText(), /Revert this change\?/); assert.match(await dialog.innerText(), /previous version opens for review/i); await dialog.getByRole('button', { name: 'Cancel', exact: true }).click(); await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {}); assert.ok(!(await dialog.isVisible().catch(() => false)), 'Cancel left the revert dialog open'); assert.equal( await page.getByRole('region', { name: 'Unsaved changes' }).count(), 0, 'Cancel touched the save bar', ); // Enter is the keyboard equivalent of clicking the row. await row.click(); assert.equal(await row.getAttribute('aria-expanded'), 'false', 'the row did not collapse'); await row.focus(); await page.keyboard.press('Enter'); await page.waitForTimeout(200); assert.equal(await row.getAttribute('aria-expanded'), 'true', 'Enter did not expand the change set'); } // ---- runtime event detail ------------------------------------------------ await go('/settings/audit'); const runtime = records.find((row) => row.category === 'runtime' && row.target?.kind === 'task' && row.target?.id); if (!runtime) { fail('no runtime event with a task target in the feed; cannot exercise runtime detail'); } else { const row = page.locator(`[data-row-key="${runtime.id}"]`); await row.waitFor(); await row.scrollIntoViewIfNeeded(); await row.locator('td').first().click(); assert.equal(await row.getAttribute('aria-expanded'), 'true', 'runtime row did not expand'); const detail = row.locator('xpath=following-sibling::tr[1]'); await detail.locator('dl dt', { hasText: 'Target' }).first().waitFor(); assert.match(await detail.innerText(), /Table/, 'runtime metadata has no table row'); const link = detail.getByRole('link', { name: runtime.target.label || runtime.target.id, exact: true }); assert.equal(await link.count(), 1, 'runtime target is not a link'); assert.equal(await link.getAttribute('href'), `/tasks/${runtime.target.id}`); } // ---- saved views --------------------------------------------------------- await go('/settings/audit'); await page.getByRole('button', { name: 'Runtime', exact: true }).click(); await waitForUrl((u) => u.searchParams.get('categories') === 'runtime'); const viewName = `e2e-view-${Date.now()}`; await page.getByRole('button', { name: 'Audit actions', exact: true }).click(); await page.getByRole('menuitem', { name: 'Save view…', exact: true }).click(); const saveDialog = page.getByRole('dialog'); await saveDialog.waitFor(); await saveDialog.getByLabel('Name', { exact: true }).fill(viewName); await saveDialog.getByRole('button', { name: 'Save', exact: true }).click(); await page.waitForTimeout(400); const savedView = page.getByRole('combobox', { name: 'Saved view', exact: true }); await savedView.waitFor(); assert.equal((await savedView.innerText()).trim(), viewName); await shot('saved-view'); await page.getByRole('button', { name: 'Runtime', exact: true }).click(); await waitForUrl((u) => u.searchParams.get('categories') === null); await savedView.click(); await page.getByRole('option', { name: viewName, exact: true }).click(); await waitForUrl((u) => u.searchParams.get('categories') === 'runtime'); assert.equal((await savedView.innerText()).trim(), viewName, 'applying the saved view did not restore it'); await page.getByRole('button', { name: 'Audit actions', exact: true }).click(); await page.getByRole('menuitem', { name: 'Delete view', exact: true }).click(); await page.waitForTimeout(300); assert.equal(await savedView.count(), 0, 'deleting the saved view left it in the picker'); // ---- export -------------------------------------------------------------- // Export resolves its window at click time and opens a tab. A successful // export arrives as an attachment download; a failed one stays visible in // that tab, so the check accepts either and reads the response from it. await go('/settings/audit'); const exportOnce = async (label, extension) => { await page.getByRole('button', { name: 'Export', exact: true }).click(); const item = page.getByRole('menuitem', { name: label, exact: true }); await item.waitFor(); const downloadP = page.waitForEvent('download', { timeout: 15000 }).then((download) => ({ download })).catch(() => null); const popupP = page.waitForEvent('popup', { timeout: 15000 }).then((popup) => ({ popup })).catch(() => null); await item.click(); const settled = await Promise.race([downloadP, popupP]); if (!settled) { fail(`${label} export neither downloaded nor opened a tab`); return null; } if (settled.popup) { await settled.popup.waitForLoadState('load').catch(() => {}); const url = new URL(settled.popup.url()); assert.equal(url.pathname, '/api/audit/export', `${label} opened ${url.href}`); assert.equal(url.searchParams.get('format'), extension); assert.ok(url.searchParams.get('from'), `${label} export has no explicit start`); assert.ok(url.searchParams.get('to'), `${label} export has no explicit end`); const response = await page.request.get(url.href); const body = await response.text(); if (response.status() !== 200) { fail(`${label} export failed (${response.status()}): ${body.slice(0, 200)}`); return null; } if (!new RegExp(`attachment;.*\\.${extension}`).test(response.headers()['content-disposition'] || '')) { fail(`${label} export opened a tab without an attachment filename`); return null; } return body; } if (!new RegExp(`\\.${extension}$`).test(settled.download.suggestedFilename())) { fail(`${label} export did not download a .${extension} file`); return null; } const failure = await settled.download.failure(); if (failure) { fail(`${label} download failed: ${failure}`); return null; } const saved = await settled.download.path(); return saved ? fs.readFileSync(saved, 'utf8') : ''; }; await page.getByRole('button', { name: 'Export', exact: true }).click(); await page.getByRole('menuitem', { name: 'CSV', exact: true }).waitFor(); await shot('export'); await page.keyboard.press('Escape'); const csvBody = await exportOnce('CSV', 'csv'); if (csvBody !== null) assert.ok(csvBody.split('\n')[0].trim().length > 0, 'CSV export is empty'); const jsonBody = await exportOnce('JSON', 'json'); if (jsonBody !== null) assert.ok(jsonBody.trimStart().startsWith('['), 'JSON export is not an array'); // ---- retention: read and cancel ----------------------------------------- await go('/settings/audit'); const resourceResponse = await page.request.get(`${new URL(page.url()).origin}/api/control-plane/resources/audit-retention`); assert.equal(resourceResponse.status(), 200, 'audit-retention resource is unreadable'); const retentionResource = await resourceResponse.json(); const retentionHours = (value) => { const match = String(value || '').match(/^(\d+)h$/); return match ? Number(match[1]) : 0; }; const originalHours = retentionHours(retentionResource.resource.value.runtime_event_retention); const openRetention = async () => { await page.getByRole('button', { name: 'Audit actions', exact: true }).click(); await page.getByRole('menuitem', { name: 'Retention', exact: true }).click(); const dialog = page.getByRole('dialog'); await dialog.waitFor(); await dialog.getByLabel('Runtime event retention (hours)', { exact: true }).waitFor(); return dialog; }; let retentionDialog = await openRetention(); assert.match(await retentionDialog.innerText(), /Audit retention/); assert.equal( await retentionDialog.getByLabel('Runtime event retention (hours)', { exact: true }).inputValue(), String(originalHours), 'retention dialog did not read the stored resource', ); await retentionDialog.getByRole('button', { name: 'Cancel', exact: true }).click(); await retentionDialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {}); assert.ok(!(await retentionDialog.isVisible().catch(() => false)), 'Cancel left the retention dialog open'); // ---- live tail ----------------------------------------------------------- await go('/settings/audit'); await page.evaluate(() => window.scrollTo(0, 0)); await page.waitForTimeout(300); const topKey = await page.locator('[data-row-key]').first().getAttribute('data-row-key'); const loadsBeforeWrite = loads; let resource = await (await page.request.get(`${new URL(page.url()).origin}/api/control-plane/resources/audit-retention`)).json(); const toggleRetention = async () => { const current = retentionHours(resource.resource.value.runtime_event_retention); const next = current === 2160 ? 2161 : 2160; const written = await page.request.post( `${new URL(page.url()).origin}/api/control-plane/resources/audit-retention/commands/replace`, { headers: { 'X-Archie-CSRF': '1', Origin: new URL(page.url()).origin }, data: { value: { runtime_event_retention: `${next}h` }, expected_version: resource.resource.version }, }, ); const body = await written.text(); assert.equal(written.status(), 200, `retention write answered ${written.status()}: ${body.slice(0, 200)}`); resource = JSON.parse(body); }; await toggleRetention(); let liveKey = null; for (let attempt = 0; attempt < 40; attempt += 1) { const key = await page.locator('[data-row-key]').first().getAttribute('data-row-key'); if (key !== topKey) { liveKey = key; break; } await page.waitForTimeout(300); } if (!liveKey) { fail('live tail did not show a new row within 12s of a write'); await shot('live'); } else { assert.equal(loads, loadsBeforeWrite, 'the live row required a page reload'); assert.match( await page.locator(`[data-row-key="${liveKey}"]`).innerText(), /audit-retention/i, 'the live row is not the write that caused it', ); await shot('live'); } // Scrolled away, a new event is held behind a button instead of jumping the // list under the operator. await page.evaluate(() => window.scrollTo(0, 900)); await page.waitForTimeout(300); const scrolledTo = await page.evaluate(() => window.scrollY); assert.ok(scrolledTo > 80, 'the feed is too short to test held new events'); const heldTop = await page.locator('[data-row-key]').first().getAttribute('data-row-key'); await toggleRetention(); const heldButton = page.getByRole('button', { name: /^\d+ new events?$/ }).first(); let held = false; for (let attempt = 0; attempt < 40; attempt += 1) { if ((await heldButton.count()) && (await heldButton.isVisible().catch(() => false))) { held = true; break; } await page.waitForTimeout(300); } if (!held) { fail('held new events did not appear while scrolled away'); await shot('held'); } else { assert.equal( await page.locator('[data-row-key]').first().getAttribute('data-row-key'), heldTop, 'a new row was prepended while the operator was scrolled away', ); await shot('held'); await heldButton.click(); await page.waitForTimeout(500); assert.notEqual( await page.locator('[data-row-key]').first().getAttribute('data-row-key'), heldTop, 'showing held events did not prepend the new row', ); assert.ok((await page.evaluate(() => window.scrollY)) < 10, 'showing held events did not return to the top'); } // ---- retention: save ----------------------------------------------------- // Kept last: the page error this currently raises must not stop the rest. await go('/settings/audit'); retentionDialog = await openRetention(); const hoursField = retentionDialog.getByLabel('Runtime event retention (hours)', { exact: true }); const beforeSave = await (await page.request.get(`${new URL(page.url()).origin}/api/control-plane/resources/audit-retention`)).json(); const beforeHours = retentionHours(beforeSave.resource.value.runtime_event_retention); const wanted = beforeHours === 2160 ? 2161 : 2160; await hoursField.fill(String(wanted)); await retentionDialog.getByRole('button', { name: 'Save', exact: true }).click(); await retentionDialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {}); const stillOpen = await retentionDialog.isVisible().catch(() => false); const afterSave = await (await page.request.get(`${new URL(page.url()).origin}/api/control-plane/resources/audit-retention`)).json(); const afterHours = retentionHours(afterSave.resource.value.runtime_event_retention); if (stillOpen || afterHours !== wanted) { await shot('retention-save'); fail( `retention save did not complete: dialog ${stillOpen ? 'stayed open' : 'closed'}, ` + `stored retention is ${afterHours}h (wanted ${wanted}h), URL ${page.url()}`, ); } // ---- phone width --------------------------------------------------------- await page.setViewportSize({ width: 390, height: 844 }); await go('/settings/audit'); const assertNoOverflow = async (label) => { const { scrollWidth, innerWidth } = await page.evaluate(() => ({ scrollWidth: document.documentElement.scrollWidth, innerWidth, })); if (scrollWidth > innerWidth) { fail(`${label} overflows 390px: documentElement.scrollWidth=${scrollWidth} > innerWidth=${innerWidth}`); } }; await assertNoOverflow('audit list'); assert.equal( await page.locator('thead th', { hasText: 'Target' }).first().evaluate((el) => getComputedStyle(el).display), 'none', 'secondary columns are still rendered on the phone', ); await shot('390-list'); const phoneRow = page.locator('[data-row-key]').first(); await phoneRow.focus(); await page.keyboard.press('Enter'); await page.waitForTimeout(300); assert.equal(await phoneRow.getAttribute('aria-expanded'), 'true', 'phone row did not expand'); await assertNoOverflow('expanded change set'); await shot('390-expanded'); await page.getByRole('button', { name: 'Filters', exact: true }).click(); await page.getByRole('dialog').first().waitFor(); await page.waitForTimeout(300); await assertNoOverflow('advanced filters'); await shot('390-filters'); };