Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

39.1 KB
Raw Blame
#!/usr/bin/env node
/**
 * Collections / Library CRUD Lifecycle Tests (Flask -> FastAPI migration)
 *
 * Drives a real browser through the full collection lifecycle a human would
 * perform from the Library UI: create -> appears in the list -> open it ->
 * edit the one field the UI actually lets you edit -> delete (guarded by a
 * confirmation, then confirmed) -> gone from the list AND the API. Every step
 * is a real click/type/select against the rendered page, not a fetch() used
 * as a shortcut for the action itself (fetch is only used afterward, from
 * page context, as a secondary server-side confirmation that a DOM-level
 * assertion already established).
 *
 * Overlap check against existing collections/library UI tests (read before
 * writing this file) -- what is already covered and therefore NOT repeated
 * here:
 *   - test_library_collections_ci.js (CollectionsPageTests) checks the
 *     collections page loads, the "Create Collection" anchor exists (href
 *     only -- never clicked), and the rendered card structure/markup for a
 *     collection seeded via fetch (test_lib/fixtures.js's seedCollection /
 *     deleteCollection, both plain fetch calls, not UI). It never fills the
 *     create FORM, never opens a collection via a real click-through, never
 *     touches the is_public/agent_enabled edit controls, and never drives
 *     the delete button or its confirmation dialog.
 *   - library/test_collections_page.js Test 4 only checks that SOME button
 *     matching a create-collection selector opens SOME modal -- it doesn't
 *     match this app's actual create page (a full navigation to
 *     /library/collections/create, not a modal) so it silently no-ops via
 *     its own "skip if not found" branch. Test 5 similarly best-effort
 *     clicks a collection link with a 2s race and never asserts on the
 *     result. Neither creates, edits, or deletes anything.
 *   - test_collections_auto_index.js drives a real control (#auto-index-toggle)
 *     but that toggle is a global per-user setting on the collections LIST
 *     page, not a property of any one collection -- unrelated to this file's
 *     per-collection lifecycle.
 *   - test_rag_index_search_ci.js DOES create a collection through the real
 *     create form (the only other file that does), but only as setup for an
 *     indexing/semantic-search flow: it never asserts the card appears in
 *     the list, never opens it via a list click-through, never edits it, and
 *     -- notably -- never deletes it, so every run of that file currently
 *     leaks one collection (+ its indexed document) into the test account.
 *     This file is the first to close the loop with an actual delete.
 *   - test_download_and_csrf_flows_ci.js creates/deletes a collection too,
 *     but purely as a fetch-level fixture to reach a document-download
 *     endpoint; it never touches the collections UI at all.
 *   None of the above exercises: the create FORM via real type()/click(),
 *   the card appearing in the list after creation, a click-through open from
 *   the list, the is_public edit toggle, or the delete button + its
 *   confirm()-gated flow (accepted and, separately, cancelled). Those gaps
 *   are what this file fills.
 *
 * What "edit" means here (surveyed, not invented): collection_details.js and
 * collection_details.html expose exactly two editable-after-creation fields
 * via real controls -- the "Public collection" (#collection-is-public) and
 * "Available to the research agent" (#collection-agent-enabled) checkboxes,
 * each wired to `PUT /library/api/collections/{id}` on change. There is NO
 * rename/edit control anywhere for a collection's name or description after
 * creation -- collection_details.js only ever assigns
 * collection-name/collection-description as read-only textContent, and no
 * PUT call in collection_details.js or collections_manager.js ever sends a
 * `name` or `description` field. So "rename or edit if the UI supports it"
 * is exercised here as the is_public toggle (the UI's actual edit surface),
 * not a rename -- there is no rename UI to test.
 *
 * What "guarded the way the UI intends" means here (surveyed, not invented):
 * collection_details.js's deleteCollection() gates the DELETE call behind a
 * plain `window.confirm(...)` (not the app's custom delete_confirmation_modal
 * component, which is included in this template for document-level deletes
 * but is NOT wired to the collection-level delete button). Both directions
 * are exercised: dismissing the confirm() must leave the collection
 * untouched (Test 5), and accepting it must actually delete it (Test 6).
 * showSuccess()/showError() in the same file are plain `alert(...)` calls,
 * which also fire on this page (from the is_public edit and from a
 * successful delete) -- a single page-wide dialog handler below accepts
 * confirm()/alert() by default and is switched to dismiss only for the one
 * assertion that needs it.
 *
 * ===========================================================================
 * TWO GENUINE DEFECTS FOUND while writing this file, both with evidence:
 * ===========================================================================
 *
 * DEFECT 1 (FIXED as part of this change, was a hard app-breaking bug):
 * GET /library/api/collections/{id}/documents (the collection DETAILS page's
 * data load) and PUT /library/api/collections/{id} (the is_public/
 * agent_enabled edit toggles) 500'd for EVERY collection, every time --
 * always, not intermittently. Root cause: src/local_deep_research/web/
 * routers/rag.py had two lazy imports one relative-import level too shallow:
 *   from ..deletion.services.collection_deletion import PROTECTED_COLLECTION_TYPES
 * (`_is_protected_collection()` at ~line 100, and again inside
 * `_update_collection_sync()` at ~line 1654) resolves to the nonexistent
 * `local_deep_research.web.deletion...`, raising ModuleNotFoundError --
 * caught generically by handle_api_error() and surfaced to the browser as an
 * opaque "An internal error occurred" 500, which is why the browser-visible
 * symptom was silent: the collection details page never left "Loading...",
 * and clicking Delete crashed the page with "Cannot read properties of null
 * (reading 'name')" because collectionData stayed null. The correct target,
 * `local_deep_research.research_library.deletion.services.collection_deletion`,
 * is imported correctly elsewhere in this SAME file (line ~2474) and in the
 * sibling router web/routers/library_delete.py (line ~21), both via
 * `from ...research_library.deletion...` (three dots) -- confirming this was
 * a stray one-off, not an intentional different path. Fixed by correcting
 * both call sites to the same three-dot import already proven correct
 * elsewhere in this file. Verified via an in-process repro (httpx
 * ASGITransport against the real FastAPI app object) that printed the raw
 * ModuleNotFoundError traceback before the fix and a clean 200 after, then
 * re-confirmed against the live dev server with a real browser.
 *
 * DEFECT 2 (NOT fixed -- a real, reproducible browser-facing UX bug, out of
 * scope for a test-writing task to redesign around):
 * Clicking the "Public collection" checkbox on the details page DOES persist
 * correctly server-side every time (confirmed via the PUT response body and,
 * separately, via GET immediately after -- both always report the new
 * value). But the checkbox's own on-page `.checked` state can revert to
 * unchecked immediately after the click -- reproduced with BOTH Puppeteer's
 * page.click() and a plain in-page `element.click()`, so this is not a
 * Puppeteer-only artifact -- and that wrong visual state was observed to
 * sometimes survive a same-page `location.reload()` too, even though a
 * completely fresh tab loading the same URL in the same session always shows
 * the correct (checked) value, ONCE that fresh tab's own async render has had
 * time to finish (see "CI-OBSERVED RACE" note below the DEFECT 2 writeup --
 * a separate bug, in this test, not in the app). Isolated experimentally: stubbing
 * `window.alert` to a no-op before the click makes the checkbox behave
 * correctly every time. Root cause is very likely collection_details.js's
 * `showSuccess()` calling a blocking `alert(...)` synchronously from inside
 * the checkbox's own 'change' handler -- interrupting the browser's
 * in-flight default action (the checkbox toggle) for that event with a
 * modal dialog appears to cause Chromium to revert it once the dialog is
 * dismissed, a known hazard of using alert()/confirm() for feedback from
 * form-control event handlers. This is a real, user-facing bug (a real
 * person clicking this checkbox and reading "Success: Collection marked
 * public." would very plausibly see the box itself uncheck itself right in
 * front of them), but redesigning collection_details.js's feedback mechanism
 * (replacing alert()/confirm() with a non-blocking toast) is a UX/behavior
 * change well beyond this task's scope. Test 4 below therefore verifies
 * persistence from a FRESH TAB rather than trusting the interacted-with
 * page's own checkbox state or a same-page reload -- see that test's own
 * comment for why that is the only non-flaky way to assert the real
 * (correct) server-side outcome, not a workaround for a testing artifact.
 *
 * CI-OBSERVED RACE (was in THIS test, fixed here -- not DEFECT 2 above):
 * GitHub Actions run 32619888018 failed Test 4 with exactly DEFECT 2's own
 * "server confirms is_public=true ... but the checkbox rendered by a
 * brand-new tab's own page load still shows unchecked" message -- but from
 * the fresh-tab path, which DEFECT 2 argues cannot be affected by the
 * alert()-revert mechanism (a fresh tab never clicks the checkbox, so no
 * 'change' handler and no alert() ever fires on it). Root cause was a
 * different, mundane bug in this test: collection_details.html ships
 * `#collection-is-public` with no `checked` attribute in the raw HTML --
 * `.checked` is only set once collection_details.js's loadCollectionDetails()
 * async fetch resolves. `waitForSelector('#collection-is-public')` after
 * `reload()` only proves the element EXISTS (true at domcontentloaded,
 * before that fetch necessarily resolves), so reading `.checked`
 * immediately afterward raced the page's own async render -- reliably wins
 * on an idle machine, loses often enough under CI load to explain the
 * intermittent failure (this test failed 1 of 3 attempts in that run; a
 * genuine server-persistence defect would fail every attempt, not 1 of 3).
 * Fixed by waiting for `.checked === true` (via waitForFunction, timing out
 * the same as every other async-render wait in this file) before reading it,
 * instead of reading right after a bare existence check.
 *
 * Screenshots: opt-in only via tests/ui_tests/screenshot_helper.js (no-op
 * unless LDR_UI_SCREENSHOTS is set -- see that file's header). Captured
 * after the collection is created, after it is deleted, and on any
 * assertion failure.
 *
 * Registered in the `library` shard (tests/ui_tests/run_all_tests.js) --
 * same theme as test_library_collections_ci.js / test_rag_index_search_ci.js.
 *
 * Run: CI=true node test_collections_crud_ci.js
 *      LDR_UI_SCREENSHOTS=1 CI=true node test_collections_crud_ci.js
 */

const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
const { capture, captureOnFailure, screenshotsEnabled } = require('./screenshot_helper');

const BASE_URL = process.env.BASE_URL || process.env.LDR_BASE_URL || 'http://127.0.0.1:5000';
const isCI = !!process.env.CI;

const TIMEOUTS = {
    navigation: isCI ? 60000 : 30000,
    selector: isCI ? 30000 : 10000,
};

const SCREENSHOT_PREFIX = 'collections_crud';

/** True for a real, attributable console error -- not the browser's own favicon probe noise. */
function isRealConsoleError(msg) {
    return msg.type() === 'error' && !msg.text().startsWith('Failed to load resource');
}

/**
 * Poll a synchronous Node-side condition (e.g. "did the dialog handler fire
 * yet") until it's true or the timeout elapses. Used only for state that
 * lives outside the page (Puppeteer's dialog event is delivered
 * asynchronously relative to the click that triggered window.confirm()),
 * never as a substitute for the page-level waitForSelector/waitForResponse/
 * waitForNavigation waits used everywhere else in this file.
 */
async function waitForNodeCondition(conditionFn, { timeout = 5000, interval = 100 } = {}) {
    const deadline = Date.now() + timeout;
    while (Date.now() < deadline) {
        // `await` here matters even though most callers pass a synchronous
        // predicate: one call site passes an async predicate, and
        // `if (conditionFn())` on an un-awaited Promise is always truthy
        // (a Promise object, not its resolved value), which would make this
        // loop exit "successfully" on its very first iteration regardless
        // of the real condition. `await` on a plain boolean is a no-op, so
        // this is safe for sync predicates too.
        if (await conditionFn()) return true;
        await new Promise((resolve) => setTimeout(resolve, interval));
    }
    return await conditionFn();
}

async function run() {
    console.log(`Running collections CRUD lifecycle tests (CI mode: ${isCI})`);
    console.log(`Screenshots: ${screenshotsEnabled() ? 'ENABLED (LDR_UI_SCREENSHOTS set)' : 'disabled (default)'}`);

    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();
    await page.setViewport({ width: 1280, height: 900 });
    if (isCI) {
        page.setDefaultTimeout(60000);
        page.setDefaultNavigationTimeout(60000);
    }

    page.on('pageerror', (e) => console.log('PAGE ERROR:', e.message));
    page.on('console', (m) => {
        if (isRealConsoleError(m)) console.log('BROWSER ERROR:', m.text());
    });

    // ------------------------------------------------------------------
    // Single page-wide dialog handler. deleteCollection() gates on a plain
    // confirm(); updateCollectionIsPublic()/showSuccess()/showError() use
    // plain alert(). Default to accepting everything (the happy path); the
    // one test that needs the *other* outcome (delete cancelled) flips
    // `confirmAction` to 'dismiss' just for that click, then flips it back.
    // ------------------------------------------------------------------
    let confirmAction = 'accept';
    const dialogEvents = [];
    page.on('dialog', async (dialog) => {
        const info = { type: dialog.type(), message: dialog.message() };
        dialogEvents.push(info);
        try {
            if (dialog.type() === 'confirm' && confirmAction === 'dismiss') {
                await dialog.dismiss();
            } else {
                await dialog.accept();
            }
        } catch (_) {
            /* dialog already handled (e.g. navigation raced it away) */
        }
    });

    const uniqueName = `ldr-ui-crud-${Date.now()}-${Math.floor(Math.random() * 1e6)}`;
    const uniqueDescription = `Puppeteer CRUD-lifecycle fixture (${uniqueName}). Safe to delete.`;

    let passed = 0;
    let failed = 0;
    let collectionId = null;
    let collectionDeletedConfirmed = false;

    try {
        const auth = new AuthHelper(page, BASE_URL);
        await auth.ensureAuthenticatedWithTimeout();

        // ---------------------------------------------------------------
        // Test 1: create a collection through the real create FORM, reached
        // by clicking the list page's "Create Collection" link (not a
        // direct page.goto to the create URL).
        // ---------------------------------------------------------------
        console.log(`Test 1: create collection "${uniqueName}" via the real UI form`);
        try {
            await page.goto(`${BASE_URL}/library/collections`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            await page.waitForSelector('#create-collection-btn', { timeout: TIMEOUTS.selector });

            await Promise.all([
                page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation }),
                page.click('#create-collection-btn'),
            ]);
            if (!page.url().endsWith('/library/collections/create')) {
                throw new Error(`Expected to land on the create-collection page, got: ${page.url()}`);
            }

            await page.waitForSelector('#collection-name', { visible: true, timeout: TIMEOUTS.selector });
            await page.type('#collection-name', uniqueName);
            await page.type('#collection-description', uniqueDescription);

            await Promise.all([
                page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation }),
                page.click('#create-collection-btn'),
            ]);

            const match = page.url().match(/\/library\/collections\/([^/]+)$/);
            if (!match) {
                throw new Error(`Expected redirect to /library/collections/<id>, got: ${page.url()}`);
            }
            collectionId = decodeURIComponent(match[1]);

            await page.waitForFunction(
                (expected) => document.getElementById('collection-name')?.textContent === expected,
                { timeout: TIMEOUTS.selector },
                uniqueName
            );
            const descText = await page.$eval('#collection-description', (el) => el.textContent);
            if (descText !== uniqueDescription) {
                throw new Error(`Description mismatch after create: expected "${uniqueDescription}", got "${descText}"`);
            }

            console.log(`PASSED (collection id=${collectionId})`);
            passed++;
            await capture(page, SCREENSHOT_PREFIX, 'created', { fullPage: true });
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'create', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 2: the new collection appears in the collections list.
        // ---------------------------------------------------------------
        console.log('Test 2: new collection appears in the collections list');
        try {
            if (!collectionId) throw new Error('Skipped: no collection id (Test 1 did not create one)');

            await Promise.all([
                page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation }),
                page.click('.ldr-rag-header a[href="/library/collections"]'),
            ]);
            const cardSelector = `.ldr-collection-card-wrapper[data-id="${collectionId}"]`;
            await page.waitForSelector(cardSelector, { timeout: TIMEOUTS.selector });
            const cardName = await page.$eval(`${cardSelector} .ldr-collection-header h3`, (el) => el.textContent.trim());
            if (cardName !== uniqueName) {
                throw new Error(`List card name mismatch: expected "${uniqueName}", got "${cardName}"`);
            }
            console.log('PASSED (card present with matching name)');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'appears_in_list', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 3: open it via a real click-through from the list (not a
        // direct URL navigation) and land on its details page.
        // ---------------------------------------------------------------
        console.log('Test 3: open the collection via a click-through from the list');
        try {
            if (!collectionId) throw new Error('Skipped: no collection id (Test 1 did not create one)');

            const cardSelector = `.ldr-collection-card-wrapper[data-id="${collectionId}"] a.ldr-collection-card`;
            await page.waitForSelector(cardSelector, { timeout: TIMEOUTS.selector });
            await Promise.all([
                page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation }),
                page.click(cardSelector),
            ]);

            if (!page.url().endsWith(`/library/collections/${collectionId}`)) {
                throw new Error(`Expected details page for ${collectionId}, got: ${page.url()}`);
            }
            await page.waitForFunction(
                (expected) => document.getElementById('collection-name')?.textContent === expected,
                { timeout: TIMEOUTS.selector },
                uniqueName
            );
            console.log('PASSED (details page shows the created collection)');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'open_details', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 4: edit -- the UI's real edit surface is the is_public
        // checkbox (see file header for the survey backing this). Toggle
        // it, wait for the app's own PUT, then verify persistence from a
        // FRESH TAB rather than a same-page reload.
        //
        // That "fresh tab, not reload" choice is deliberate, not stylistic:
        // see the "KNOWN DEFECT" paragraph in the file header. Toggling this
        // checkbox correctly persists server-side every time (asserted
        // directly on the PUT response below), but the checkbox's own DOM
        // `.checked` state can revert to unchecked immediately afterward,
        // and that corrupted visual state was observed to sometimes survive
        // a same-page reload too -- because the revert happens on the PAGE
        // itself (the live DOM node / in-memory render), not the server, a
        // brand-new tab in the same session is unaffected and reliably
        // reflects the true saved value. This is the non-flaky way to prove
        // persistence; it is not a workaround for a testing artifact.
        // ---------------------------------------------------------------
        console.log('Test 4: edit -- toggle "Public collection" and confirm it persists');
        try {
            if (!collectionId) throw new Error('Skipped: no collection id (Test 1 did not create one)');

            await page.waitForSelector('#collection-is-public', { timeout: TIMEOUTS.selector });
            const before = await page.$eval('#collection-is-public', (el) => el.checked);
            if (before !== false) {
                throw new Error(`Expected a freshly created collection to default is_public=false, got ${before}`);
            }

            const isPutForCollection = (r) =>
                r.url().endsWith(`/library/api/collections/${collectionId}`) && r.request().method() === 'PUT';
            const dialogsBeforeEdit = dialogEvents.length;
            const [putResp] = await Promise.all([
                page.waitForResponse(isPutForCollection, { timeout: TIMEOUTS.navigation }),
                page.click('#collection-is-public'),
            ]);
            const putBody = await putResp.json().catch(() => null);
            if (putResp.status() !== 200 || !putBody?.success || putBody.collection?.is_public !== true) {
                throw new Error(`Edit PUT did not succeed: status=${putResp.status()} body=${JSON.stringify(putBody)}`);
            }

            // updateCollectionIsPublic() also fires a blocking alert() on
            // success (showSuccess()), which our page-wide dialog handler
            // auto-accepts -- wait for that to be observed so it can never
            // leak an open dialog into the next test.
            await waitForNodeCondition(() => dialogEvents.length > dialogsBeforeEdit, { timeout: 5000 });

            const freshPage = await browser.newPage();
            try {
                if (isCI) {
                    freshPage.setDefaultTimeout(60000);
                    freshPage.setDefaultNavigationTimeout(60000);
                }
                await freshPage.goto(`${BASE_URL}/library/collections/${collectionId}`, {
                    waitUntil: 'domcontentloaded',
                    timeout: TIMEOUTS.navigation,
                });
                await freshPage.waitForSelector('#collection-is-public', { timeout: TIMEOUTS.selector });

                // A fresh tab is the reliable check (see the file-header
                // DEFECT 2 writeup) but was observed, rarely, to catch the
                // very first read a moment before the write is visible to a
                // brand-new page load. Confirm against the same API the
                // checkbox itself renders from, polling briefly, before
                // trusting a single read; server truth is already proven by
                // the PUT response above in every run this file has ever
                // seen, so this loop only guards a possible propagation
                // delay, not the checkbox-revert defect itself (a fresh tab
                // never interacted with the checkbox, so that defect cannot
                // apply here).
                let freshTabValue = null;
                let apiValue = null;
                for (let attempt = 0; attempt < 10; attempt++) {
                    apiValue = await freshPage.evaluate(async (id) => {
                        const r = await fetch(`/library/api/collections/${id}/documents`, { credentials: 'same-origin' });
                        const body = await r.json().catch(() => null);
                        return body?.collection?.is_public ?? null;
                    }, collectionId);
                    if (apiValue === true) break;
                    await new Promise((resolve) => setTimeout(resolve, 300));
                }
                if (apiValue !== true) {
                    throw new Error(
                        `GENUINE DEFECT: is_public edit did not persist -- polling the collection API for up to 3s ` +
                        `from a brand-new tab still reads is_public=${apiValue}, even though the PUT response ` +
                        `confirmed is_public=true was saved`
                    );
                }
                // Reload so the rendered checkbox reflects the state just
                // confirmed above, in case the poll loop needed more than
                // its first attempt (the checkbox was set at this tab's
                // initial page load, which could otherwise predate that).
                await freshPage.reload({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation });

                // waitForSelector only proves the checkbox EXISTS in the DOM --
                // collection_details.html ships it with no `checked` attribute
                // (always unchecked in the raw, server-rendered markup), and it
                // only gets `.checked` set once collection_details.js's
                // loadCollectionDetails() -> safeFetchWithAuth() -> `.checked =
                // !!collectionData.is_public` async chain completes. domcontent-
                // loaded (and therefore waitForSelector's presence check) fires
                // long before that fetch necessarily resolves, so reading
                // `.checked` right after waitForSelector races the page's own
                // JS -- reliably wins locally, but not guaranteed under CI load.
                // Wait for the checked state itself instead (mirrors the
                // waitForFunction-on-async-rendered-text pattern Test 1/3 above
                // already use for #collection-name), then read the settled
                // value. A genuine persistence defect still fails this, just
                // with a real render deadline instead of a single early read.
                try {
                    await freshPage.waitForFunction(
                        () => document.getElementById('collection-is-public')?.checked === true,
                        { timeout: TIMEOUTS.selector }
                    );
                } catch (_) {
                    // Let the read below report the actual settled state.
                }
                freshTabValue = await freshPage.$eval('#collection-is-public', (el) => el.checked);
                if (freshTabValue !== true) {
                    throw new Error(
                        `GENUINE DEFECT: server confirms is_public=true (via both the PUT response and a polled ` +
                        `re-fetch), but the checkbox rendered by a brand-new tab's own page load still shows ` +
                        `unchecked (${freshTabValue}) even after waiting up to ${TIMEOUTS.selector}ms for ` +
                        `loadCollectionDetails() to finish rendering it`
                    );
                }
            } finally {
                await freshPage.close();
            }

            console.log('PASSED (is_public: false -> true, confirmed saved + visible from a fresh tab)');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'edit_is_public', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 5: delete is guarded by a confirmation -- dismissing it must
        // leave the collection completely untouched (no DELETE fires, page
        // stays put, collection still exists).
        // ---------------------------------------------------------------
        console.log('Test 5: delete confirmation dismissed leaves the collection intact');
        try {
            if (!collectionId) throw new Error('Skipped: no collection id (Test 1 did not create one)');

            await page.waitForSelector('#delete-collection-btn', { timeout: TIMEOUTS.selector });

            const deleteRequests = [];
            const onRequest = (req) => {
                if (req.method() === 'DELETE' && req.url().endsWith(`/library/api/collections/${collectionId}`)) {
                    deleteRequests.push(req.url());
                }
            };
            page.on('request', onRequest);

            confirmAction = 'dismiss';
            const dialogsBefore = dialogEvents.length;
            await page.click('#delete-collection-btn');
            const dialogFired = await waitForNodeCondition(() => dialogEvents.length > dialogsBefore, { timeout: 5000 });
            confirmAction = 'accept';
            page.off('request', onRequest);

            if (!dialogFired) {
                throw new Error('No confirm() dialog was observed after clicking Delete Collection');
            }
            const confirmDialog = dialogEvents[dialogEvents.length - 1];
            if (confirmDialog.type !== 'confirm' || !/delete/i.test(confirmDialog.message) || !confirmDialog.message.includes(uniqueName)) {
                throw new Error(`Delete confirmation dialog did not look like a guard: ${JSON.stringify(confirmDialog)}`);
            }
            if (deleteRequests.length !== 0) {
                throw new Error(`GENUINE DEFECT: DELETE request fired despite the confirmation being dismissed: ${deleteRequests.join(', ')}`);
            }
            if (!page.url().endsWith(`/library/collections/${collectionId}`)) {
                throw new Error(`GENUINE DEFECT: page navigated away after a dismissed confirmation: ${page.url()}`);
            }
            const stillThere = await page.$eval('#collection-name', (el) => el.textContent).catch(() => null);
            if (stillThere !== uniqueName) {
                throw new Error(`GENUINE DEFECT: collection no longer shows on its own details page after a dismissed delete confirmation (name="${stillThere}")`);
            }

            console.log(`PASSED (confirm() dialog "${confirmDialog.message.slice(0, 60)}..." dismissed, nothing deleted)`);
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'delete_guard', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 6: accepting the same confirmation actually deletes it, and
        // the app's own redirect lands back on the list with the card gone.
        // ---------------------------------------------------------------
        console.log('Test 6: delete confirmation accepted actually deletes the collection');
        try {
            if (!collectionId) throw new Error('Skipped: no collection id (Test 1 did not create one)');

            confirmAction = 'accept';
            const isDeleteForCollection = (r) =>
                r.url().endsWith(`/library/api/collections/${collectionId}`) && r.request().method() === 'DELETE';
            const isCollectionsListGet = (r) =>
                r.url().endsWith('/library/api/collections') && r.request().method() === 'GET';

            // Register BOTH response listeners (and the list-refresh one, which
            // only fires ~1s later after the redirect) before the click so
            // neither event can be missed, but read the delete response's body
            // as soon as IT resolves rather than after both have — the app
            // navigates away ~1s later (alert -> redirect), and Chrome can evict
            // a torn-down frame's buffered response body, so deferring
            // deleteResp.json() until after the second wait risks reading an
            // already-gone body.
            const deleteRespPromise = page.waitForResponse(isDeleteForCollection, { timeout: TIMEOUTS.navigation });
            const listRespPromise = page
                .waitForResponse(isCollectionsListGet, { timeout: TIMEOUTS.navigation })
                .catch(() => null);
            await page.click('#delete-collection-btn');

            const deleteResp = await deleteRespPromise;
            const deleteBody = await deleteResp.json().catch(() => null);
            if (deleteResp.status() !== 200 || !deleteBody?.success || !deleteBody?.deleted) {
                throw new Error(`Delete did not succeed: status=${deleteResp.status()} body=${JSON.stringify(deleteBody)}`);
            }
            collectionDeletedConfirmed = true;

            // Best-effort: confirms a fresh list fetch happened post-redirect.
            // Not fatal on its own — the DOM/URL poll below is the hard check.
            await listRespPromise;
            const cardSelector = `.ldr-collection-card-wrapper[data-id="${collectionId}"]`;
            const gone = await waitForNodeCondition(
                async () => {
                    if (!page.url().endsWith('/library/collections')) return false;
                    const el = await page.$(cardSelector);
                    return el === null;
                },
                { timeout: TIMEOUTS.navigation, interval: 250 }
            );
            if (!gone) {
                throw new Error(`Expected redirect to /library/collections with the card gone; url=${page.url()}`);
            }

            console.log('PASSED (DELETE succeeded, redirected to list, card no longer rendered)');
            passed++;
            await capture(page, SCREENSHOT_PREFIX, 'deleted', { fullPage: true });
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'delete_confirmed', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 7: gone server-side too, not just hidden client-side --
        // absent from the collections API list, and its own detail
        // endpoint 404s.
        // ---------------------------------------------------------------
        console.log('Test 7: deleted collection is gone from the API, not just the DOM');
        try {
            if (!collectionId) throw new Error('Skipped: no collection id (Test 1 did not create one)');
            if (!collectionDeletedConfirmed) throw new Error('Skipped: Test 6 did not confirm deletion');

            const apiCheck = await page.evaluate(async (id) => {
                const listResp = await fetch('/library/api/collections', { credentials: 'same-origin' });
                const listBody = await listResp.json().catch(() => null);
                const stillInList = Array.isArray(listBody?.collections)
                    && listBody.collections.some((c) => c.id === id);

                const detailsResp = await fetch(`/library/api/collections/${id}/documents`, { credentials: 'same-origin' });
                const detailsBody = await detailsResp.json().catch(() => null);

                return { stillInList, detailsStatus: detailsResp.status, detailsBody };
            }, collectionId);

            if (apiCheck.stillInList) {
                throw new Error('GENUINE DEFECT: deleted collection is still present in GET /library/api/collections');
            }
            if (apiCheck.detailsStatus !== 404) {
                throw new Error(
                    `GENUINE DEFECT: GET .../collections/${collectionId}/documents expected 404 after delete, ` +
                    `got ${apiCheck.detailsStatus} (${JSON.stringify(apiCheck.detailsBody)})`
                );
            }

            console.log('PASSED (absent from collections API, detail endpoint 404s)');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'gone_via_api', false);
            failed++;
        }
    } catch (e) {
        console.log(`Test suite error: ${e.message}`);
        failed++;
    } finally {
        // -----------------------------------------------------------------
        // Idempotency net: if anything above threw before Test 6 confirmed
        // the delete, best-effort delete the collection via fetch (from
        // whatever page is currently live) so a failed run never leaves a
        // leftover collection for the next run to trip over. Never throws;
        // never masks a test result already recorded above.
        // -----------------------------------------------------------------
        if (collectionId && !collectionDeletedConfirmed) {
            try {
                const cleanup = await page.evaluate(async (id) => {
                    const csrf = document.querySelector('meta[name="csrf-token"]')?.content || '';
                    try {
                        const resp = await fetch(`/library/api/collections/${id}`, {
                            method: 'DELETE',
                            credentials: 'same-origin',
                            headers: { 'X-CSRFToken': csrf },
                        });
                        return { status: resp.status };
                    } catch (err) {
                        return { status: 0, error: String(err) };
                    }
                }, collectionId);
                console.log(`Cleanup: best-effort delete of leftover collection ${collectionId} -> ${JSON.stringify(cleanup)}`);
            } catch (cleanupError) {
                console.log(`Cleanup: could not remove leftover collection ${collectionId}: ${cleanupError.message}`);
            }
        }

        await browser.close();
    }

    console.log('-'.repeat(50));
    console.log(`Collections CRUD Lifecycle Tests — passed: ${passed}, failed: ${failed}`);
    console.log('-'.repeat(50));
    if (failed > 0) process.exit(1);
}

run().catch((e) => {
    console.error('Test runner error:', e);
    process.exit(1);
});