|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | + |
| 3 | +import { expandBackupList, openBackupDashboard, requireBlobToken, waitForTaskDone } from './helpers' |
| 4 | + |
| 5 | +const MANUAL_DIALOG = 'dialog.backup-confirm-dialog--manual[open]' |
| 6 | +// One `<dialog>` is rendered per backup row — target the one that was just opened. |
| 7 | +const RESTORE_DIALOG = 'dialog.backup-confirm-dialog--restore[open]' |
| 8 | +// The delete confirm dialog has no `--variant` suffix. Use `:not()` to exclude the |
| 9 | +// variant dialogs and require `[open]` to pick the one associated with our row. |
| 10 | +const DELETE_DIALOG = 'dialog.backup-confirm-dialog:not([class*="--"])[open]' |
| 11 | + |
| 12 | +// Skip this whole file when no real Vercel Blob store is configured — the roundtrip needs |
| 13 | +// a writable backend. CI only runs the happy path when the optional secret is present. |
| 14 | +test.describe('Backup create + restore roundtrip (dev app)', () => { |
| 15 | + test.beforeEach(() => { |
| 16 | + test.skip( |
| 17 | + !requireBlobToken(), |
| 18 | + 'Needs BLOB_READ_WRITE_TOKEN to exercise the real @vercel/blob backend', |
| 19 | + ) |
| 20 | + }) |
| 21 | + |
| 22 | + // The roundtrip touches the real blob store and includes dialog polling, so give it |
| 23 | + // plenty of time: create (mongo dump + upload) + restore (download + upsert). |
| 24 | + test.setTimeout(300_000) |
| 25 | + |
| 26 | + test('create a manual backup via the UI and restore it from the backup list', async ({ |
| 27 | + page, |
| 28 | + }) => { |
| 29 | + const label = `e2e-roundtrip-${Date.now()}` |
| 30 | + |
| 31 | + await openBackupDashboard(page) |
| 32 | + |
| 33 | + // ---- CREATE ----------------------------------------------------------------- |
| 34 | + await page.getByRole('button', { name: /Create manual Backup/i }).click() |
| 35 | + const manualDialog = page.locator(MANUAL_DIALOG) |
| 36 | + await expect(manualDialog).toBeVisible() |
| 37 | + |
| 38 | + // Wait for the backup preview to load (the Start backup button is disabled until |
| 39 | + // the phase transitions to `ready` or `error`). |
| 40 | + await expect( |
| 41 | + manualDialog.locator('.restore-preview__sticky-heading', { |
| 42 | + hasText: /Collection selection/i, |
| 43 | + }), |
| 44 | + ).toBeVisible({ timeout: 30_000 }) |
| 45 | + |
| 46 | + await manualDialog.getByLabel('Optional backup label').fill(label) |
| 47 | + |
| 48 | + // Keep every collection checked by default — in dev the `media` collection is empty |
| 49 | + // so `includeMedia` is implicitly `false` (mediaBlobCandidates=0) and the archive |
| 50 | + // stays as a small json. |
| 51 | + const startButton = manualDialog.getByRole('button', { name: /Start backup/i }) |
| 52 | + await expect(startButton).toBeEnabled({ timeout: 30_000 }) |
| 53 | + await startButton.click() |
| 54 | + |
| 55 | + await waitForTaskDone(manualDialog, { timeout: 120_000 }) |
| 56 | + // After success the dialog auto-closes within ~1s (router.refresh() + close). |
| 57 | + await expect(manualDialog).toBeHidden({ timeout: 10_000 }) |
| 58 | + |
| 59 | + // The list is server-rendered and refreshes after create. Expand it and locate |
| 60 | + // the row by its unique label pill. |
| 61 | + await expandBackupList(page) |
| 62 | + const createdRow = page |
| 63 | + .locator('.backup-item') |
| 64 | + .filter({ has: page.locator('.backup-item__pill--label', { hasText: label }) }) |
| 65 | + await expect(createdRow).toHaveCount(1, { timeout: 30_000 }) |
| 66 | + await expect(createdRow.locator('.backup-item__pill--manual')).toBeVisible() |
| 67 | + |
| 68 | + // ---- RESTORE ---------------------------------------------------------------- |
| 69 | + await createdRow.getByRole('button', { name: /^Restore$/ }).click() |
| 70 | + const restoreDialog = page.locator(RESTORE_DIALOG) |
| 71 | + await expect(restoreDialog).toBeVisible() |
| 72 | + |
| 73 | + // Preview transitions from `loading` to `ready` once the blob is downloaded + |
| 74 | + // analysed. On a cold cache this can take 10–20s. |
| 75 | + await expect( |
| 76 | + restoreDialog.locator('.restore-preview__sticky-heading', { |
| 77 | + hasText: /Collection selection/i, |
| 78 | + }), |
| 79 | + ).toBeVisible({ timeout: 60_000 }) |
| 80 | + |
| 81 | + // Deselect the auth-session collections so the restore keeps our session intact. |
| 82 | + // The checkbox `aria-label` is `Restore collection ${displayTitle}` where |
| 83 | + // `displayTitle` is `"<Label> (<slug>)"`, so we match by the slug suffix to stay |
| 84 | + // resilient against localisation differences. |
| 85 | + for (const slug of ['users', 'roles', 'payload-preferences']) { |
| 86 | + const cb = restoreDialog.locator( |
| 87 | + `input[type="checkbox"][aria-label*="(${slug})" i][aria-label^="Restore collection" i]`, |
| 88 | + ) |
| 89 | + if ((await cb.count()) > 0 && (await cb.first().isChecked())) { |
| 90 | + await cb.first().click() |
| 91 | + await expect(cb.first()).not.toBeChecked() |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + await restoreDialog.getByRole('button', { name: /Yes, restore/i }).click() |
| 96 | + await waitForTaskDone(restoreDialog, { timeout: 180_000 }) |
| 97 | + await expect(restoreDialog).toBeHidden({ timeout: 10_000 }) |
| 98 | + |
| 99 | + // ---- CLEANUP: delete the just-created backup via the UI --------------------- |
| 100 | + // Exercises the delete flow AND keeps the blob store tidy for subsequent runs. |
| 101 | + await expandBackupList(page) |
| 102 | + await expect(createdRow).toHaveCount(1, { timeout: 15_000 }) |
| 103 | + await createdRow.getByRole('button', { name: /^Delete$/ }).click() |
| 104 | + |
| 105 | + const deleteDialog = page.locator(DELETE_DIALOG) |
| 106 | + await expect(deleteDialog).toBeVisible() |
| 107 | + await deleteDialog.getByRole('button', { name: /Yes, delete/i }).click() |
| 108 | + await waitForTaskDone(deleteDialog, { timeout: 60_000 }) |
| 109 | + |
| 110 | + // `TaskActionButton` with `refreshOnComplete` triggers a router.refresh; wait for |
| 111 | + // the row to disappear from the list so the test leaves a clean slate. |
| 112 | + await expect(createdRow).toHaveCount(0, { timeout: 30_000 }) |
| 113 | + }) |
| 114 | +}) |
0 commit comments