// A revertable change set expands to its Before/After diff, offers one Revert // action whose dialog cancels cleanly, and toggles from the keyboard. const assert = require('node:assert/strict'); const { DEFAULT_WINDOW_MS, trackAudit } = require('./helpers/audit-helpers.cjs'); module.exports = async ({ page, go, shot, fail, settled }) => { trackAudit(page); 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'); return; } 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 settled(); assert.equal(await row.getAttribute('aria-expanded'), 'true', 'Enter did not expand the change set'); };