| 1 | import assert from "node:assert/strict"; |
| 2 | import path from "node:path"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { createServer } from "vite"; |
| 5 | |
| 6 | const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 7 | process.env.PLAYWRIGHT_BROWSERS_PATH ??= path.join(root, ".pw-browsers"); |
| 8 | const { chromium } = await import("playwright"); |
| 9 | const port = Number(process.env.REASONIX_HISTORICAL_IMPORT_PORT ?? 4691); |
| 10 | const server = await createServer({ root, logLevel: "error", server: { host: "127.0.0.1", port, strictPort: true, hmr: false, watch: { ignored: ["**"] } } }); |
| 11 | await server.listen(); |
| 12 | const browser = await chromium.launch({ headless: true }); |
| 13 | const page = await browser.newPage({ viewport: { width: 1100, height: 700 }, locale: "en-US" }); |
| 14 | const errors = []; |
| 15 | page.on("pageerror", error => errors.push(error.message)); |
| 16 | try { |
| 17 | await page.goto(`http://127.0.0.1:${port}/bench/historical-import.html`); |
| 18 | await page.getByRole("heading", { name: "Historical sessions", exact: true }).waitFor(); |
| 19 | const banner = page.getByTestId("source-update-banner"); |
| 20 | await banner.getByText("Historical source changed. Import as a separate branch.", { exact: true }).waitFor(); |
| 21 | assert.equal(await banner.getByText("Not imported", { exact: true }).count(), 0); |
| 22 | const branchButton = banner.getByRole("button", { name: "Import and open · Branch", exact: true }); |
| 23 | await branchButton.click(); |
| 24 | await banner.getByRole("alert").waitFor(); |
| 25 | assert.match(await banner.innerText(), /Import failed/); |
| 26 | assert.equal(await branchButton.isEnabled(), true, "failed branch imports allow retry"); |
| 27 | await branchButton.click(); |
| 28 | await page.waitForFunction(() => document.body.dataset.openedBranch === "branch-v2"); |
| 29 | assert.equal(await banner.getByRole("alert").count(), 0); |
| 30 | const rows = page.locator(".archived-sessions__row"); |
| 31 | assert.equal(await rows.count(), 2, "listing exposes both sources without importing"); |
| 32 | await rows.nth(0).getByRole("button", { name: "Import and open", exact: true }).click(); |
| 33 | await page.getByRole("alert").waitFor(); |
| 34 | assert.equal(await rows.nth(0).getByRole("button", { name: "Import and open", exact: true }).count(), 1, "busy source remains retryable"); |
| 35 | await rows.nth(0).getByRole("button", { name: "Import and open", exact: true }).click(); |
| 36 | await page.waitForTimeout(50); |
| 37 | await page.getByRole("button", { name: "Import all", exact: true }).click(); |
| 38 | await page.getByRole("button", { name: "Pause after current item", exact: true }).click(); |
| 39 | await page.getByRole("button", { name: "Continue", exact: true }).click(); |
| 40 | await page.getByRole("button", { name: "Cancel import", exact: true }).click(); |
| 41 | assert.deepEqual(errors, []); |
| 42 | await page.screenshot({ path: process.env.REASONIX_HISTORICAL_IMPORT_EVIDENCE ?? path.join(root, "..", "..", "artifacts", "historical-import-browser.png") }); |
| 43 | console.log("PASS historical import browser: source update failure/retry/open, listing, blocked retry and batch controls"); |
| 44 | } finally { await browser.close(); await server.close(); } |
| 45 |