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.

32.8 KB
Raw Blame
#!/usr/bin/env node
/**
 * Notes CRUD Lifecycle Tests (Flask -> FastAPI migration)
 *
 * Drives a real browser through the full notes lifecycle a human would
 * perform from the Notes UI: create via the real modal -> appears in the
 * list -> open it via a click-through -> edit its title/content -> add a
 * tag (persisted) -> remove that tag (persisted) -> delete (guarded by a
 * confirm() dialog, dismissed then confirmed) -> gone from the list AND the
 * API. Mirrors the shape/rigor of test_collections_crud_ci.js (same repo,
 * same migration): every step is a real click/type against the rendered
 * page; 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 notes coverage (read before writing this
 * file) -- what is already covered and therefore NOT repeated here:
 *   - tests/ui_tests/playwright/tests/notes/*.spec.js is an EXTENSIVE
 *     Playwright suite (notes-crud, notes-editing, notes-tags,
 *     notes-detail-edit, notes-list*, notes-modal-close-paths,
 *     notes-wikilinks, notes-ai*, notes-versions, notes-collections, ...)
 *     that already covers this exact CRUD lifecycle in much finer detail
 *     (markdown toolbar, write/preview tabs, Ctrl+B, wikilink autocomplete,
 *     pin toggle, cancel-preserves-nothing, modal heading icon regression,
 *     etc.) -- but it is a DIFFERENT test runner (Playwright, not
 *     Puppeteer) and this repo's Puppeteer suite
 *     (tests/ui_tests/*_ci.js, run via `CI=true node <file>.js`) had ZERO
 *     notes coverage before this file (confirmed: no `test_notes*.js` /
 *     `*notes*_ci.js` existed under tests/ui_tests/ outside the playwright/
 *     subdirectory). This file is the Puppeteer-suite equivalent of the
 *     CRUD subset of that Playwright coverage, NOT a duplicate of it --
 *     the two suites run independently (different runner, different CI
 *     job) and this repo's stated goal here is Puppeteer parity for the
 *     surfaces that had none.
 *   - tests/js/pages/note-detail-*.test.js and notes-*.test.js (Vitest/Jest,
 *     tests/js/) unit-test note-detail.js / notes.js functions directly
 *     against jsdom mocks (saveNote's diff-body logic, tag chip rendering,
 *     wikilink parsing, version-history rendering, etc.) -- no real browser,
 *     no real HTTP round-trip, no real server. Not duplicated here either.
 *   - tests/notes/*.py and tests/database/test_migration_00{21,22}_*.py
 *     test the FastAPI router / NoteService / DB layer directly (no browser
 *     at all).
 *   None of the above is a real-Chromium, Puppeteer-driven, this-suite's
 *   own CI-shard exercise of the notes CRUD surface -- that gap is what
 *   this file fills.
 *
 * AI-backed note actions (summarize, suggest-tags, fact-check/verify,
 * key-concepts, similar-notes, ask-your-notes, synthesize) are
 * DELIBERATELY NOT exercised here -- they need a real LLM, which this
 * environment does not provide, and the task scope is the CRUD surface,
 * not the AI surface. (Existing coverage for those, for the record:
 * tests/notes/test_note_ai_service.py, tests/js/pages/note-detail-ai-*.js,
 * tests/ui_tests/playwright/tests/notes/notes-ai*.spec.js.)
 *
 * What "delete has a confirmation step" means here (surveyed, not
 * invented): note-detail.js's deleteNote() gates the DELETE call behind a
 * plain `window.confirm('Are you sure you want to delete this note? This
 * action cannot be undone.')` -- the exact same pattern
 * test_collections_crud_ci.js found and tested for collection deletion (a
 * plain confirm(), not the app's custom delete_confirmation_modal
 * component). Both directions are exercised below: dismissing the confirm()
 * must leave the note untouched (Test 7), and accepting it must actually
 * delete it (Test 8).
 *
 * 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 note is created, after the final delete, and on any assertion
 * failure.
 *
 * Registered in the `library` shard (tests/ui_tests/run_all_tests.js) --
 * same theme as test_collections_crud_ci.js / test_rag_index_search_ci.js
 * (a real-UI CRUD-lifecycle test for a content type owned by this
 * migration).
 *
 * Run: CI=true node test_notes_crud_ci.js
 *      LDR_UI_SCREENSHOTS=1 CI=true node test_notes_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 = 'notes_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 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/waitForFunction waits
 * used everywhere else in this file. Mirrors test_collections_crud_ci.js.
 */
async function waitForNodeCondition(conditionFn, { timeout = 5000, interval = 100 } = {}) {
    const deadline = Date.now() + timeout;
    while (Date.now() < deadline) {
        if (await conditionFn()) return true;
        await new Promise((resolve) => setTimeout(resolve, interval));
    }
    return await conditionFn();
}

/** Select-all-then-type into a text input/textarea via real keystrokes (not a .value assignment). */
async function clearAndType(page, selector, text) {
    await page.click(selector);
    await page.evaluate((sel) => {
        const el = document.querySelector(sel);
        el.focus();
        el.select();
    }, selector);
    if (text) await page.type(selector, text);
}

/** Open the note detail page's "more actions" dropdown if it isn't already open. */
async function openMoreMenu(page, timeout) {
    const alreadyOpen = await page.$eval('#more-menu', (el) => el.classList.contains('ldr-open')).catch(() => false);
    if (alreadyOpen) return;
    await page.click('#more-btn');
    await page.waitForFunction(
        () => document.getElementById('more-menu')?.classList.contains('ldr-open') === true,
        { timeout }
    );
}

async function run() {
    console.log(`Running notes 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. deleteNote() gates on a plain
    // confirm(); default to accepting (the happy path), flip to 'dismiss'
    // for the one test that needs the other outcome. Same pattern as
    // test_collections_crud_ci.js.
    // ------------------------------------------------------------------
    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 uniqueSuffix = `${Date.now()}-${Math.floor(Math.random() * 1e6)}`;
    const originalTitle = `ldr-ui-note-crud-${uniqueSuffix}`;
    const updatedTitle = `${originalTitle}-updated`;
    const originalContent = `Puppeteer CRUD-lifecycle fixture body (${uniqueSuffix}). Safe to delete.`;
    const updatedContent = `Updated body via Puppeteer CRUD test (${uniqueSuffix}).`;
    const tagName = `puppeteer-crud-${uniqueSuffix.slice(-6)}`;

    let passed = 0;
    let failed = 0;
    let noteId = null;
    let noteDeletedConfirmed = false;

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

        // ---------------------------------------------------------------
        // Test 1: create a note through the real modal, reached by
        // clicking the list page's "New Note" button (not a direct
        // page.goto to a create URL -- the notes UI creates via a modal,
        // not a dedicated page).
        // ---------------------------------------------------------------
        console.log(`Test 1: create note "${originalTitle}" via the real UI modal`);
        try {
            await page.goto(`${BASE_URL}/notes/`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            await page.waitForSelector('[data-action="create-new-note"]', { timeout: TIMEOUTS.selector });
            await page.click('[data-action="create-new-note"]');

            await page.waitForSelector('#note-title', { visible: true, timeout: TIMEOUTS.selector });
            await page.type('#note-title', originalTitle);
            await page.type('#note-content', originalContent);

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

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

            await page.waitForFunction(
                (expected) => document.getElementById('note-title-display')?.textContent === expected,
                { timeout: TIMEOUTS.selector },
                originalTitle
            );
            const renderedContent = await page.$eval('#note-content-rendered', (el) => el.textContent);
            if (!renderedContent.includes(originalContent)) {
                throw new Error(`Rendered content missing the created body. Got: "${renderedContent.slice(0, 200)}"`);
            }

            console.log(`PASSED (note id=${noteId})`);
            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 note appears in the notes list.
        // ---------------------------------------------------------------
        console.log('Test 2: new note appears in the notes list');
        try {
            if (!noteId) throw new Error('Skipped: no note id (Test 1 did not create one)');

            await page.goto(`${BASE_URL}/notes/`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            const cardSelector = `.ldr-note-card[data-note-id="${noteId}"]`;
            await page.waitForSelector(cardSelector, { timeout: TIMEOUTS.selector });
            const cardTitle = await page.$eval(`${cardSelector} .ldr-note-card-title`, (el) => el.textContent.trim());
            if (cardTitle !== originalTitle) {
                throw new Error(`List card title mismatch: expected "${originalTitle}", got "${cardTitle}"`);
            }
            console.log('PASSED (card present with matching title)');
            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 detail page.
        // ---------------------------------------------------------------
        console.log('Test 3: open the note via a click-through from the list');
        try {
            if (!noteId) throw new Error('Skipped: no note id (Test 1 did not create one)');

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

            if (!page.url().endsWith(`/notes/${noteId}`)) {
                throw new Error(`Expected detail page for ${noteId}, got: ${page.url()}`);
            }
            await page.waitForFunction(
                (expected) => document.getElementById('note-title-display')?.textContent === expected,
                { timeout: TIMEOUTS.selector },
                originalTitle
            );
            console.log('PASSED (detail page shows the created note)');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'open_detail', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 4: edit title + content via edit mode, save, and confirm
        // read mode reflects the update -- plus a server-side check via
        // the API (not trusting the client's own re-render alone).
        // ---------------------------------------------------------------
        console.log('Test 4: edit title + content, save, read mode + API reflect the update');
        try {
            if (!noteId) throw new Error('Skipped: no note id (Test 1 did not create one)');

            await page.waitForSelector('[data-action="enter-edit-mode"]', { timeout: TIMEOUTS.selector });
            await page.click('[data-action="enter-edit-mode"]');
            await page.waitForSelector('#edit-mode', { visible: true, timeout: TIMEOUTS.selector });

            await clearAndType(page, '#ldr-note-title', updatedTitle);
            await clearAndType(page, '#note-content', updatedContent);

            await page.click('#save-btn');
            await page.waitForFunction(
                (expected) => document.getElementById('note-title-display')?.textContent === expected,
                { timeout: TIMEOUTS.selector },
                updatedTitle
            );
            await page.waitForSelector('#read-mode', { visible: true, timeout: TIMEOUTS.selector });

            const renderedContent = await page.$eval('#note-content-rendered', (el) => el.textContent);
            if (!renderedContent.includes(updatedContent)) {
                throw new Error(`Rendered content missing the updated body. Got: "${renderedContent.slice(0, 200)}"`);
            }

            const apiCheck = await page.evaluate(async (id) => {
                const r = await fetch(`/notes/api/notes/${id}`, { credentials: 'same-origin' });
                const body = await r.json().catch(() => null);
                return { status: r.status, note: body?.note };
            }, noteId);
            if (apiCheck.status !== 200 || apiCheck.note?.title !== updatedTitle || apiCheck.note?.content !== updatedContent) {
                throw new Error(`API did not reflect the edit: ${JSON.stringify(apiCheck)}`);
            }

            console.log('PASSED (title + content updated, confirmed in read mode and via API)');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'edit_content', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 5: add a tag, save, and confirm it persists (read mode,
        // edit mode's chip list, and the API).
        // ---------------------------------------------------------------
        console.log(`Test 5: add tag "${tagName}", save, confirm persistence`);
        try {
            if (!noteId) throw new Error('Skipped: no note id (Test 1 did not create one)');

            await page.waitForSelector('[data-action="enter-edit-mode"]', { timeout: TIMEOUTS.selector });
            await page.click('[data-action="enter-edit-mode"]');
            await page.waitForSelector('#edit-mode', { visible: true, timeout: TIMEOUTS.selector });

            await page.click('#add-tag-input');
            await page.type('#add-tag-input', tagName);
            await page.keyboard.press('Enter');
            await page.waitForSelector(
                `#ldr-note-tags .ldr-note-tag .ldr-remove-tag[data-tag="${tagName}"]`,
                { timeout: TIMEOUTS.selector }
            );

            await page.click('#save-btn');
            await page.waitForSelector('#read-mode', { visible: true, timeout: TIMEOUTS.selector });
            await page.waitForSelector('#note-tags-display .ldr-tag', { timeout: TIMEOUTS.selector });

            const readModeTags = await page.$$eval('#note-tags-display .ldr-tag', (els) => els.map((el) => el.textContent.trim()));
            if (!readModeTags.includes(`#${tagName}`)) {
                throw new Error(`Read-mode tags do not include "#${tagName}": ${JSON.stringify(readModeTags)}`);
            }

            const apiCheck = await page.evaluate(async (id) => {
                const r = await fetch(`/notes/api/notes/${id}`, { credentials: 'same-origin' });
                const body = await r.json().catch(() => null);
                return { status: r.status, tags: body?.note?.tags || [] };
            }, noteId);
            if (apiCheck.status !== 200 || !apiCheck.tags.includes(tagName)) {
                throw new Error(`API tags do not include "${tagName}": ${JSON.stringify(apiCheck)}`);
            }

            console.log('PASSED (tag added, visible in read mode, confirmed via API)');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'add_tag', false);
            failed++;
        }

        // ---------------------------------------------------------------
        // Test 6: remove that same tag, save, and confirm it is gone --
        // not just from the edit-mode DOM (which removeTag() alone would
        // show even if the save silently failed), but from read mode
        // AND the API after a real save round-trip.
        // ---------------------------------------------------------------
        console.log(`Test 6: remove tag "${tagName}", save, confirm removal persists`);
        try {
            if (!noteId) throw new Error('Skipped: no note id (Test 1 did not create one)');

            await page.waitForSelector('[data-action="enter-edit-mode"]', { timeout: TIMEOUTS.selector });
            await page.click('[data-action="enter-edit-mode"]');
            await page.waitForSelector('#edit-mode', { visible: true, timeout: TIMEOUTS.selector });

            const removeSelector = `#ldr-note-tags .ldr-note-tag .ldr-remove-tag[data-tag="${tagName}"]`;
            await page.waitForSelector(removeSelector, { timeout: TIMEOUTS.selector });
            await page.click(removeSelector);
            const goneFromEditor = await waitForNodeCondition(
                () => page.$(removeSelector).then((el) => el === null),
                { timeout: 3000 }
            );
            if (!goneFromEditor) {
                throw new Error('Tag chip still present in edit mode immediately after clicking remove');
            }

            await page.click('#save-btn');
            await page.waitForSelector('#read-mode', { visible: true, timeout: TIMEOUTS.selector });

            // What a real user sees: renderReadModeTags() hides the whole
            // #note-tags-display container (display:none) when note.tags is
            // empty. Minor DOM-hygiene note (not asserted as a failure --
            // see below): that function's `tags.length === 0` early return
            // sets display:none WITHOUT first removing existing `.ldr-tag`
            // children (unlike its own non-empty branch, which does), so a
            // stale, hidden `.ldr-tag` node containing the removed tag's
            // text can be left behind in the DOM until the next tag is
            // added (which does clear it, via that same removal line).
            // Invisible to a user -- the container itself is display:none --
            // so this checks the user-visible signal (container hidden),
            // not raw node presence.
            const tagsDisplayState = await page.$eval('#note-tags-display', (el) => ({
                display: getComputedStyle(el).display,
                inlineDisplay: el.style.display,
            }));
            if (tagsDisplayState.inlineDisplay !== 'none' || tagsDisplayState.display !== 'none') {
                throw new Error(
                    `GENUINE DEFECT: #note-tags-display is not hidden after removing the only tag ` +
                    `(inline display="${tagsDisplayState.inlineDisplay}", computed="${tagsDisplayState.display}") -- ` +
                    'a user would still see a tags row'
                );
            }

            const apiCheck = await page.evaluate(async (id) => {
                const r = await fetch(`/notes/api/notes/${id}`, { credentials: 'same-origin' });
                const body = await r.json().catch(() => null);
                return { status: r.status, tags: body?.note?.tags || [] };
            }, noteId);
            if (apiCheck.status !== 200 || apiCheck.tags.includes(tagName)) {
                throw new Error(`GENUINE DEFECT: API tags still include "${tagName}" after removal + save: ${JSON.stringify(apiCheck)}`);
            }

            console.log('PASSED (tag removed, gone from read mode, confirmed via API)');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await captureOnFailure(page, SCREENSHOT_PREFIX, 'remove_tag', false);
            failed++;
        }

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

            await openMoreMenu(page, TIMEOUTS.selector);
            await page.waitForSelector('[data-action="delete-note"]', { visible: true, timeout: TIMEOUTS.selector });

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

            confirmAction = 'dismiss';
            const dialogsBefore = dialogEvents.length;
            await page.click('[data-action="delete-note"]');
            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 Note');
            }
            const confirmDialog = dialogEvents[dialogEvents.length - 1];
            if (confirmDialog.type !== 'confirm' || !/delete/i.test(confirmDialog.message)) {
                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(`/notes/${noteId}`)) {
                throw new Error(`GENUINE DEFECT: page navigated away after a dismissed confirmation: ${page.url()}`);
            }
            const stillThere = await page.$eval('#note-title-display', (el) => el.textContent).catch(() => null);
            if (stillThere !== updatedTitle) {
                throw new Error(`GENUINE DEFECT: note title no longer shows on its own detail page after a dismissed delete confirmation (title="${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 8: accepting the same confirmation actually deletes it, and
        // the app's own redirect lands back on the list.
        // ---------------------------------------------------------------
        console.log('Test 8: delete confirmation accepted actually deletes the note');
        try {
            if (!noteId) throw new Error('Skipped: no note id (Test 1 did not create one)');

            confirmAction = 'accept';
            await openMoreMenu(page, TIMEOUTS.selector);
            await page.waitForSelector('[data-action="delete-note"]', { visible: true, timeout: TIMEOUTS.selector });

            const isDeleteForNote = (r) =>
                r.url().endsWith(`/notes/api/notes/${noteId}`) && r.request().method() === 'DELETE';
            const deleteRespPromise = page.waitForResponse(isDeleteForNote, { timeout: TIMEOUTS.navigation });
            // .catch() attached immediately (not "awaited later") so a
            // rejection here (e.g. the frame tearing down mid-navigation)
            // can never become an unhandled rejection that crashes the
            // process, regardless of what throws below before this is
            // awaited.
            const navPromise = page
                .waitForNavigation({ waitUntil: 'domcontentloaded', timeout: TIMEOUTS.navigation })
                .catch(() => null);
            await page.click('[data-action="delete-note"]');

            const deleteResp = await deleteRespPromise;
            if (deleteResp.status() !== 200) {
                throw new Error(`Delete request did not return 200: status=${deleteResp.status()}`);
            }
            // Proof of success is the redirect itself, not the DELETE
            // response body: deleteNote() in note-detail.js only navigates
            // to /notes/ when `data.success` is true, and it does so
            // IMMEDIATELY (no alert()/confirm() pause, unlike
            // test_collections_crud_ci.js's collection-delete flow) --
            // there is no safe window to read the response body from
            // Puppeteer's side afterward; Chrome can (and, observed while
            // writing this test, reliably does) evict the buffered body
            // once the frame starts navigating away, making
            // deleteResp.json() resolve to null/throw even on a genuine
            // success.
            await navPromise;
            if (!page.url().endsWith('/notes/') && !page.url().endsWith('/notes')) {
                throw new Error(`Expected redirect to /notes/ after delete (proves the app's own JS saw success), got: ${page.url()}`);
            }
            noteDeletedConfirmed = true;

            const cardSelector = `.ldr-note-card[data-note-id="${noteId}"]`;
            const gone = await waitForNodeCondition(
                async () => (await page.$(cardSelector)) === null,
                { timeout: TIMEOUTS.navigation, interval: 250 }
            );
            if (!gone) {
                throw new Error('Deleted note card is still rendered in the notes list');
            }

            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 9: gone server-side too, not just hidden client-side --
        // absent from the notes list API, and its own GET endpoint 404s.
        // ---------------------------------------------------------------
        console.log('Test 9: deleted note is gone from the API, not just the DOM');
        try {
            if (!noteId) throw new Error('Skipped: no note id (Test 1 did not create one)');
            if (!noteDeletedConfirmed) throw new Error('Skipped: Test 8 did not confirm deletion');

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

                const getResp = await fetch(`/notes/api/notes/${id}`, { credentials: 'same-origin' });
                const getBody = await getResp.json().catch(() => null);

                return { stillInList, getStatus: getResp.status, getBody };
            }, noteId);

            if (apiCheck.stillInList) {
                throw new Error('GENUINE DEFECT: deleted note is still present in GET /notes/api/notes');
            }
            if (apiCheck.getStatus !== 404) {
                throw new Error(
                    `GENUINE DEFECT: GET /notes/api/notes/${noteId} expected 404 after delete, ` +
                    `got ${apiCheck.getStatus} (${JSON.stringify(apiCheck.getBody)})`
                );
            }

            console.log('PASSED (absent from notes list API, single-note 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 8 confirmed
        // the delete, best-effort delete the note via fetch (from whatever
        // page is currently live) so a failed run never leaves a leftover
        // note for the next run to trip over. Never throws; never masks a
        // test result already recorded above.
        // -----------------------------------------------------------------
        if (noteId && !noteDeletedConfirmed) {
            try {
                const cleanup = await page.evaluate(async (id) => {
                    const csrf = document.querySelector('meta[name="csrf-token"]')?.content || '';
                    try {
                        const resp = await fetch(`/notes/api/notes/${id}`, {
                            method: 'DELETE',
                            credentials: 'same-origin',
                            headers: { 'X-CSRFToken': csrf },
                        });
                        return { status: resp.status };
                    } catch (err) {
                        return { status: 0, error: String(err) };
                    }
                }, noteId);
                console.log(`Cleanup: best-effort delete of leftover note ${noteId} -> ${JSON.stringify(cleanup)}`);
            } catch (cleanupError) {
                console.log(`Cleanup: could not remove leftover note ${noteId}: ${cleanupError.message}`);
            }
        }

        await browser.close();
    }

    console.log('-'.repeat(50));
    console.log(`Notes 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);
});