// Audit retention reads the stored resource, the live tail prepends new rows // without a reload and holds them behind a button while scrolled away, and a // retention save persists. The save stays last: it must not stop the rest. const assert = require('node:assert/strict'); const { trackAudit } = require('./helpers/audit-helpers.cjs'); module.exports = async ({ page, go, shot, fail, settled }) => { const { loadCount } = trackAudit(page); 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 settled(); const topKey = await page.locator('[data-row-key]').first().getAttribute('data-row-key'); const loadsBeforeWrite = loadCount(); 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(loadCount(), 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 settled(); 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.waitForFunction( (top) => document.querySelector('[data-row-key]')?.getAttribute('data-row-key') !== top, heldTop, { timeout: 8000 }, ).catch(() => {}); await settled(); 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()}`, ); } };