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.

11.1 KB
Raw Blame
/**
 * Live results-page contract for the migrated ratings API.
 *
 * Existing results tests cover report loading, exports, and context overflow;
 * this keeps the independent GET/POST rating workflow tied to its browser DOM,
 * CSRF header, and optional-detail payload semantics.
 */

import '@js/config/urls.js';

const RESEARCH_ID = 'rating-migration-3299';
const REPORT_URL = `/api/report/${RESEARCH_ID}`;
const RATING_URL = `/metrics/api/ratings/${RESEARCH_ID}`;
const CONTEXT_URL = `/api/research/${RESEARCH_ID}/context-overflow`;
const originalReadyState = Object.getOwnPropertyDescriptor(
    document,
    'readyState',
);

function renderPage() {
    document.body.innerHTML = `
        <main id="results-content"></main>
        <button id="export-markdown-btn" disabled>Markdown</button>
        <button id="download-pdf-btn" disabled>PDF</button>
        <div id="research-rating">
            <button class="ldr-star">1</button>
            <button class="ldr-star">2</button>
            <button class="ldr-star">3</button>
            <button class="ldr-star">4</button>
            <button class="ldr-star">5</button>
        </div>
        <button id="ldr-detailed-rating-toggle" style="display: none"
                aria-expanded="false">Details ▾</button>
        <section id="ldr-detailed-rating" style="display: none">
            <input class="ldr-dimension-slider" data-dimension="accuracy"
                   value="3"><span>3</span>
            <input class="ldr-dimension-slider" data-dimension="relevance"
                   value="3"><span>3</span>
            <textarea id="ldr-rating-feedback"></textarea>
        </section>
    `;
}

function deferred() {
    let resolvePromise;
    const promise = new Promise(resolve => {
        resolvePromise = resolve;
    });
    return { promise, resolve: resolvePromise };
}

beforeAll(async () => {
    Object.defineProperty(document, 'readyState', {
        configurable: true,
        get: () => 'loading',
    });
    await import('@js/components/results.js');
});

beforeEach(() => {
    renderPage();
    window.history.replaceState({}, '', `/results/${RESEARCH_ID}`);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-rating') };
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    document.body.replaceChildren();
    window.history.replaceState({}, '', '/');
});

afterAll(() => {
    if (originalReadyState) {
        Object.defineProperty(document, 'readyState', originalReadyState);
    } else {
        delete document.readyState;
    }
});

it('hydrates an existing rating and POSTs only user-touched details with CSRF', async () => {
    const fetchMock = vi.fn((input, options = {}) => {
        const url = String(input);
        if (url === REPORT_URL) {
            return Promise.resolve({
                ok: true,
                json: vi.fn().mockResolvedValue({
                    content: '# Rated migration report',
                    metadata: { query: 'Rating migration coverage' },
                }),
            });
        }
        if (url === CONTEXT_URL) {
            return Promise.resolve({ ok: false, status: 404 });
        }
        if (url === RATING_URL && !options.method) {
            return Promise.resolve({
                ok: true,
                json: vi.fn().mockResolvedValue({ rating: 2 }),
            });
        }
        if (url === RATING_URL && options.method === 'POST') {
            return Promise.resolve({ ok: true });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    document.dispatchEvent(new Event('DOMContentLoaded'));

    await vi.waitFor(() => {
        expect(document.querySelectorAll('.ldr-star.active')).toHaveLength(2);
    });
    expect(fetchMock).toHaveBeenCalledWith(RATING_URL);
    expect(document.getElementById('ldr-detailed-rating-toggle').style.display)
        .toBe('inline');

    document.getElementById('ldr-detailed-rating-toggle').click();
    const accuracy = document.querySelector(
        '[data-dimension="accuracy"]',
    );
    accuracy.value = '5';
    accuracy.dispatchEvent(new Event('input'));
    document.getElementById('ldr-rating-feedback').value = '  Useful report  ';
    document.querySelectorAll('.ldr-star')[3].click();

    await vi.waitFor(() => {
        expect(fetchMock).toHaveBeenCalledWith(RATING_URL, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-rating',
            },
            body: JSON.stringify({
                rating: 4,
                accuracy: 5,
                feedback: 'Useful report',
            }),
        });
    });
    expect(window.api.getCsrfToken).toHaveBeenCalledOnce();
    expect(document.querySelectorAll('.ldr-star.active')).toHaveLength(4);
    expect(JSON.parse(fetchMock.mock.calls.find(
        ([url, options = {}]) => (
            url === RATING_URL && options.method === 'POST'
        ),
    )[1].body)).not.toHaveProperty('relevance');
});

it('does not let a pending initial rating overwrite a newer user selection', async () => {
    const initialRating = deferred();
    const savedRating = deferred();
    const fetchMock = vi.fn((input, options = {}) => {
        const url = String(input);
        if (url === REPORT_URL) {
            return Promise.resolve({
                ok: true,
                json: vi.fn().mockResolvedValue({
                    content: '# Rating ownership report',
                    metadata: { query: 'Rating ownership' },
                }),
            });
        }
        if (url === CONTEXT_URL) {
            return Promise.resolve({ ok: false, status: 404 });
        }
        if (url === RATING_URL && !options.method) {
            return initialRating.promise;
        }
        if (url === RATING_URL && options.method === 'POST') {
            return savedRating.promise;
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    document.dispatchEvent(new Event('DOMContentLoaded'));
    document.querySelectorAll('.ldr-star')[3].click();

    expect(document.querySelectorAll('.ldr-star.active')).toHaveLength(4);
    expect(fetchMock).toHaveBeenCalledWith(
        RATING_URL,
        expect.objectContaining({
            method: 'POST',
            body: JSON.stringify({ rating: 4 }),
        }),
    );

    savedRating.resolve({ ok: true });
    await Promise.resolve();
    const initialRatingJson = vi.fn().mockResolvedValue({ rating: 2 });
    initialRating.resolve({
        ok: true,
        json: initialRatingJson,
    });
    await vi.waitFor(() => {
        expect(initialRatingJson).toHaveBeenCalledOnce();
    });
    expect(document.querySelectorAll('.ldr-star.active')).toHaveLength(4);
});

it('serializes rapid rating writes so the latest click persists last', async () => {
    const firstSave = deferred();
    const secondSave = deferred();
    const postBodies = [];
    const fetchMock = vi.fn((input, options = {}) => {
        const url = String(input);
        if (url === REPORT_URL) {
            return Promise.resolve({
                ok: true,
                json: vi.fn().mockResolvedValue({
                    content: '# Rapid rating report',
                    metadata: { query: 'Rating ordering' },
                }),
            });
        }
        if (url === CONTEXT_URL) {
            return Promise.resolve({ ok: false, status: 404 });
        }
        if (url === RATING_URL && !options.method) {
            return Promise.resolve({
                ok: true,
                json: vi.fn().mockResolvedValue({ rating: null }),
            });
        }
        if (url === RATING_URL && options.method === 'POST') {
            postBodies.push(JSON.parse(options.body));
            return postBodies.length === 1 ? firstSave.promise : secondSave.promise;
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    document.dispatchEvent(new Event('DOMContentLoaded'));
    const stars = document.querySelectorAll('.ldr-star');
    stars[1].click();
    stars[4].click();

    expect(postBodies).toEqual([{ rating: 2 }]);
    expect(document.querySelectorAll('.ldr-star.active')).toHaveLength(5);

    firstSave.resolve({ ok: true });
    await vi.waitFor(() => {
        expect(postBodies).toEqual([{ rating: 2 }, { rating: 5 }]);
    });
    secondSave.resolve({ ok: true });
    await vi.waitFor(() => {
        expect(fetchMock.mock.calls.filter(([, options = {}]) => (
            options.method === 'POST'
        ))).toHaveLength(2);
    });

    expect(postBodies.at(-1)).toEqual({ rating: 5 });
    expect(document.querySelectorAll('.ldr-star.active')).toHaveLength(5);
});

it.each(['non-ok response', 'network rejection'])(
    'still persists the latest queued rating after a first %s',
    async (failureKind) => {
        const firstSave = deferred();
        const secondSave = deferred();
        const postBodies = [];
        const fetchMock = vi.fn((input, options = {}) => {
            const url = String(input);
            if (url === REPORT_URL) {
                return Promise.resolve({
                    ok: true,
                    json: vi.fn().mockResolvedValue({
                        content: '# Rating retry report',
                        metadata: { query: 'Rating retry ordering' },
                    }),
                });
            }
            if (url === CONTEXT_URL) {
                return Promise.resolve({ ok: false, status: 404 });
            }
            if (url === RATING_URL && !options.method) {
                return Promise.resolve({
                    ok: true,
                    json: vi.fn().mockResolvedValue({ rating: null }),
                });
            }
            if (url === RATING_URL && options.method === 'POST') {
                postBodies.push(JSON.parse(options.body));
                return postBodies.length === 1
                    ? firstSave.promise
                    : secondSave.promise;
            }
            throw new Error(`Unexpected request: ${url}`);
        });
        vi.stubGlobal('fetch', fetchMock);
        vi.spyOn(console, 'error').mockImplementation(() => {});

        document.dispatchEvent(new Event('DOMContentLoaded'));
        const stars = document.querySelectorAll('.ldr-star');
        stars[0].click();
        stars[3].click();
        expect(postBodies).toEqual([{ rating: 1 }]);

        if (failureKind === 'non-ok response') {
            firstSave.resolve({ ok: false, status: 503 });
        } else {
            firstSave.resolve(Promise.reject(new Error('rating service offline')));
        }
        await vi.waitFor(() => {
            expect(postBodies).toEqual([{ rating: 1 }, { rating: 4 }]);
        });

        secondSave.resolve({ ok: true });
        await vi.waitFor(() => {
            expect(fetchMock.mock.calls.filter(([, request = {}]) => (
                request.method === 'POST'
            ))).toHaveLength(2);
        });

        expect(postBodies.at(-1)).toEqual({ rating: 4 });
        expect(document.querySelectorAll('.ldr-star.active')).toHaveLength(4);
    },
);