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.

10.9 KB
Raw Blame
/**
 * RAG collection: index + semantic-search — end-to-end UI test (library shard).
 *
 * Drives a real browser through the full library-RAG happy path against the
 * live server and real endpoints:
 *
 *   register/login
 *     -> create a collection (create form, UI)
 *     -> add a .txt document with known text (real upload endpoint)
 *     -> click "Index All Documents" and poll until indexing completes (UI)
 *     -> assert the collection reports >= 1 indexed document and >= 1 chunk (UI)
 *     -> run a semantic search and assert the document comes back (UI).
 *
 * This is the first browser-level test of the vector-store cutover's PRODUCTION
 * path: it exercises embed -> FAISS write -> int-id-keyed store -> query embed
 * -> rehydrate-by-id -> results, through the real routes and the RAG-specific UI
 * (the Index button + the semantic-search box). The document-upload FORM UI is
 * covered by other library-shard tests, so the upload step here hits the real
 * upload endpoint directly (with the session cookie + CSRF token) to keep the
 * setup robust and the assertions focused on indexing + search.
 *
 * Requires a working embedding provider on the server. The library CI shard
 * explicitly selects sentence-transformers + all-MiniLM-L6-v2 and prepares it
 * with scripts/ci/prepare_library_embeddings.py before seeding and startup.
 * The server then runs with local-only embeddings and the HuggingFace hub in
 * offline mode. Indexing has NO embedder-free code path, so success proves the
 * real cached model ran end-to-end. Local runs must prepare a model and select
 * the same provider/model in their disposable server's settings.
 *
 * Registered in the `library` shard (tests/ui_tests/run_all_tests.js). Server
 * base URL is overridable via LDR_TEST_URL (defaults to the shard default).
 */

const puppeteer = require("puppeteer");
const AuthHelper = require("./auth_helper");
const {
    getPuppeteerLaunchOptions,
    takeScreenshot,
} = require("./puppeteer_config");

const BASE_URL = process.env.LDR_TEST_URL || "http://127.0.0.1:5000";

// Distinctive, unambiguous content so the search assertion is meaningful.
const DOC_FILENAME = "zephyrine_lighthouse.txt";
const DOC_TEXT =
    "The Zephyrine coastal lighthouse was built in 1887 on a granite " +
    "headland. Its rotating lens guided fishing vessels safely through the " +
    "fog for over a century, and the keeper logged every storm in a " +
    "leather-bound journal.";
// A semantically related query (NOT a substring) — exercises real embeddings,
// not keyword matching.
const SEARCH_QUERY = "a lighthouse guiding boats along the coast";
// A word present in the indexed chunk text / title — confirms the returned
// result is actually our document.
const RESULT_NEEDLE = "lighthouse";

function delay(ms) {
    return new Promise((r) => setTimeout(r, ms));
}

/** Call the app's own API from inside the page (carries the session cookie). */
async function apiFetch(page, url, options) {
    return page.evaluate(
        async (u, opts) => {
            const resp = await fetch(u, opts || undefined);
            let body;
            try {
                body = await resp.json();
            } catch (_) {
                body = null;
            }
            return { ok: resp.ok, httpStatus: resp.status, body };
        },
        url,
        options || null,
    );
}

/**
 * Wait until a numeric stat element reads >= `min` (stats start as "-").
 * Uses only plain comparisons in the page — the app's CSP forbids
 * unsafe-eval, so no new Function()/eval() may run in page context.
 */
async function waitForStatAtLeast(page, selector, min, timeoutMs) {
    await page.waitForFunction(
        (sel, m) => {
            const el = document.querySelector(sel);
            if (!el) return false;
            const txt = (el.textContent || "").trim().replace(/,/g, "");
            return /^\d+$/.test(txt) && Number(txt) >= m;
        },
        { timeout: timeoutMs, polling: 500 },
        selector,
        min,
    );
}

/**
 * Poll the index status endpoint until the background indexing task reports the
 * SUCCESS terminal ("completed"). Fails fast on an error terminal. "idle" and
 * "processing" mean keep waiting ("idle" can appear briefly before the task row
 * exists), so they are NOT treated as done.
 */
async function waitForIndexingComplete(page, collectionId, timeoutMs) {
    const deadline = Date.now() + timeoutMs;
    let last = "unknown";
    while (Date.now() < deadline) {
        const res = await apiFetch(
            page,
            `/library/api/collections/${collectionId}/index/status`,
        );
        const status = (res.body && res.body.status) || `http_${res.httpStatus}`;
        last = status;
        if (status === "completed") return;
        if (["failed", "cancelled", "error"].includes(status)) {
            const msg = (res.body && res.body.progress_message) || "";
            throw new Error(`indexing ended in "${status}" ${msg}`);
        }
        await delay(2000);
    }
    throw new Error(
        `indexing did not complete within ${timeoutMs}ms (last status "${last}")`,
    );
}

async function runTests() {
    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();
    await page.setViewport({ width: 1400, height: 900 });

    // The index button uses confirm(); success/error use alert(). Auto-accept so
    // a click never blocks on a tab-modal dialog (see collection_details.js).
    page.on("dialog", async (d) => {
        try {
            await d.accept();
        } catch (_) {
            /* dialog already handled */
        }
    });
    page.on("console", (m) => {
        if (m.type() === "error") {
            console.log("  [browser console error]", m.text());
        }
    });

    let passed = 0;
    let failed = 0;
    const ok = (label) => {
        passed += 1;
        console.log(`  ✅ ${label}`);
    };

    try {
        // ---- 0. Authenticate ----
        const authHelper = new AuthHelper(page, BASE_URL);
        await authHelper.ensureAuthenticated();
        ok("authenticated");

        // ---- 1. Create a collection via the create form ----
        await page.goto(`${BASE_URL}/library/collections/create`, {
            waitUntil: "domcontentloaded",
            timeout: 30000,
        });
        await page.waitForSelector("#collection-name", {
            visible: true,
            timeout: 15000,
        });
        await page.type("#collection-name", `RAG E2E ${Date.now()}`);
        await Promise.all([
            // create posts JSON then redirects to /library/collections/<uuid>
            page.waitForFunction(
                () =>
                    /\/library\/collections\/[0-9a-fA-F-]{16,}$/.test(
                        location.pathname,
                    ),
                { timeout: 30000 },
            ),
            page.click("#create-collection-btn"),
        ]);
        const collectionId = await page.evaluate(() =>
            location.pathname.split("/").filter(Boolean).pop(),
        );
        if (!collectionId || collectionId === "create") {
            throw new Error(`no collection id after create (got "${collectionId}")`);
        }
        ok(`created collection ${collectionId}`);

        // ---- 2. Add a document via the real upload endpoint (session + CSRF) ----
        // We're on the collection details page (base.html -> csrf meta present).
        const uploadRes = await page.evaluate(
            async (cid, filename, text) => {
                const meta = document.querySelector('meta[name="csrf-token"]');
                const csrf = meta ? meta.getAttribute("content") : "";
                const fd = new FormData();
                fd.append(
                    "files",
                    new File([text], filename, { type: "text/plain" }),
                );
                const r = await fetch(`/library/api/collections/${cid}/upload`, {
                    method: "POST",
                    headers: { "X-CSRFToken": csrf },
                    body: fd,
                });
                let body;
                try {
                    body = await r.json();
                } catch (_) {
                    body = null;
                }
                return { ok: r.ok, httpStatus: r.status, body };
            },
            collectionId,
            DOC_FILENAME,
            DOC_TEXT,
        );
        if (!uploadRes.ok) {
            throw new Error(
                `upload failed (http ${uploadRes.httpStatus}): ${JSON.stringify(
                    uploadRes.body,
                )}`,
            );
        }
        await page.reload({ waitUntil: "domcontentloaded", timeout: 30000 });
        await waitForStatAtLeast(page, "#stat-total-docs", 1, 30000);
        ok("document added (total docs >= 1)");

        // ---- 3. Index the collection (UI button) + wait for completion ----
        await page.waitForSelector("#index-collection-btn", {
            visible: true,
            timeout: 15000,
        });
        await page.click("#index-collection-btn"); // confirm() auto-accepted
        await waitForIndexingComplete(page, collectionId, 180000);
        ok("indexing completed");

        // Stats reflect a real index build (chunk + embed + FAISS write).
        await page.reload({ waitUntil: "domcontentloaded", timeout: 30000 });
        await waitForStatAtLeast(page, "#stat-indexed-docs", 1, 30000);
        await waitForStatAtLeast(page, "#stat-total-chunks", 1, 30000);
        ok("collection reports >= 1 indexed document and >= 1 chunk");

        // ---- 4. Semantic search returns the document (UI) ----
        // The search section is revealed once an indexed item exists.
        await page.waitForSelector("#collection-search-section", {
            visible: true,
            timeout: 20000,
        });
        await page.click("#collection-search-input");
        await page.type("#collection-search-input", SEARCH_QUERY);
        await page.click("#collection-search-btn");
        // Results render async after the query embed + FAISS search + rehydrate.
        await page.waitForFunction(
            (needle) => {
                const box = document.querySelector("#collection-search-results");
                if (!box) return false;
                return (box.innerText || "")
                    .toLowerCase()
                    .includes(needle.toLowerCase());
            },
            { timeout: 30000, polling: 500 },
            RESULT_NEEDLE,
        );
        ok(`semantic search returned the document (matched "${RESULT_NEEDLE}")`);

        console.log(`\n📊 RAG index+search UI: ${passed} passed, ${failed} failed`);
    } catch (err) {
        failed += 1;
        console.log(`  ❌ ${err && err.message ? err.message : err}`);
        await takeScreenshot(page, "/tmp/rag_index_search_error.png");
        console.log(`\n📊 RAG index+search UI: ${passed} passed, ${failed} failed`);
    } finally {
        await browser.close();
    }

    process.exit(failed > 0 ? 1 : 0);
}

runTests();