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.2 KB
Raw Blame
/**
 * Tests for config/urls.js — URLBuilder behavior
 *
 * Focuses on the URLBuilder logic (placeholder substitution, ID extraction,
 * page-type detection). Does NOT test that URL constants equal specific
 * literals — those tests would just assert the source against itself.
 */

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

const { URLS, URLBuilder } = window;

describe('URLBuilder', () => {
    describe('build', () => {
        it('replaces {id} placeholder', () => {
            expect(URLBuilder.build('/api/research/{id}', 42)).toBe('/api/research/42');
        });

        it('handles string IDs', () => {
            expect(URLBuilder.build('/api/research/{id}', 'abc-123')).toBe('/api/research/abc-123');
        });

        it('only replaces first {id} occurrence', () => {
            expect(URLBuilder.build('/api/{id}/{id}', 5)).toBe('/api/5/{id}');
        });

        it('returns template unchanged when no {id} placeholder', () => {
            expect(URLBuilder.build('/api/static', 5)).toBe('/api/static');
        });
    });

    describe('buildWithReplacements', () => {
        it('replaces multiple distinct placeholders', () => {
            const result = URLBuilder.buildWithReplacements(
                '/api/collection/{collectionId}/document/{documentId}',
                { collectionId: '10', documentId: '20' }
            );
            expect(result).toBe('/api/collection/10/document/20');
        });

        it('handles missing placeholder gracefully', () => {
            const result = URLBuilder.buildWithReplacements(
                '/api/{a}/{b}',
                { a: '1' }
            );
            expect(result).toBe('/api/1/{b}');
        });

        it('handles empty replacements object', () => {
            expect(URLBuilder.buildWithReplacements('/api/{id}', {})).toBe('/api/{id}');
        });
    });

    describe('convenience methods build URLs from URLS constants', () => {
        // These test that the convenience methods correctly compose URLBuilder.build
        // with the right URLS constant — not what those constants are.

        it('progressPage substitutes ID into PAGES.PROGRESS', () => {
            expect(URLBuilder.progressPage(42)).toBe(URLS.PAGES.PROGRESS.replace('{id}', '42'));
        });

        it('researchStatus substitutes ID into API.RESEARCH_STATUS', () => {
            expect(URLBuilder.researchStatus(42)).toBe(URLS.API.RESEARCH_STATUS.replace('{id}', '42'));
        });

        it('terminateResearch substitutes ID into API.TERMINATE_RESEARCH', () => {
            expect(URLBuilder.terminateResearch(42)).toBe(URLS.API.TERMINATE_RESEARCH.replace('{id}', '42'));
        });

        it('deleteResearch substitutes ID into API.DELETE_RESEARCH', () => {
            expect(URLBuilder.deleteResearch(42)).toBe(URLS.API.DELETE_RESEARCH.replace('{id}', '42'));
        });

        it('historyStatus substitutes ID into HISTORY_API.STATUS', () => {
            expect(URLBuilder.historyStatus(42)).toBe(URLS.HISTORY_API.STATUS.replace('{id}', '42'));
        });

        it('researchLogs returns the bare URL when no limit passed', () => {
            const url = URLBuilder.researchLogs(42);
            expect(url).toBe(URLS.API.RESEARCH_LOGS.replace('{id}', '42'));
            expect(url).not.toContain('?');
        });

        it('researchLogs appends ?limit=N when limit passed', () => {
            const url = URLBuilder.researchLogs(42, 500);
            expect(url).toContain('limit=500');
        });

        it('researchLogs encodes the limit value', () => {
            // Sanity: limit should be safe to pass directly, but encodeURIComponent
            // means anything unexpected stays escaped.
            const url = URLBuilder.researchLogs('abc-123', 5000);
            expect(url).toBe(`${URLS.API.RESEARCH_LOGS.replace('{id}', 'abc-123')}?limit=5000`);
        });

        it('getSetting substitutes key into SETTINGS_API.GET_SETTING', () => {
            const url = URLBuilder.getSetting('llm.model');
            expect(url).toContain('llm.model');
            expect(url).toBe(URLS.SETTINGS_API.GET_SETTING.replace('{key}', 'llm.model'));
        });

        it('researchMetrics substitutes ID into METRICS_API.RESEARCH', () => {
            expect(URLBuilder.researchMetrics(42)).toBe(URLS.METRICS_API.RESEARCH.replace('{id}', '42'));
        });

        it('journalQualityPage scopes the dashboard to an encoded research ID', () => {
            expect(URLBuilder.journalQualityPage('abc 123')).toBe(
                `${URLS.PAGES.JOURNAL_QUALITY}?research_id=abc%20123`
            );
        });

        it.each([
            ['resultsPage', ['research-1'], '/results/research-1'],
            ['detailsPage', ['research-1'], '/details/research-1'],
            ['documentPage', ['doc-1'], '/library/document/doc-1'],
            ['researchDetails', ['research-1'], '/api/research/research-1'],
            ['researchLogsExport', ['research-1'], '/api/research/research-1/logs/export'],
            ['researchReport', ['research-1'], '/api/report/research-1'],
            ['historyDetails', ['research-1'], '/history/details/research-1'],
            ['historyLogs', ['research-1'], '/history/logs/research-1'],
            ['markdownExport', ['research-1'], '/history/markdown/research-1'],
            ['historyReport', ['research-1'], '/history/report/research-1'],
            ['historyMarkdown', ['research-1'], '/history/markdown/research-1'],
            ['historyLogCount', ['research-1'], '/history/log_count/research-1'],
            ['updateSetting', ['llm.model'], '/settings/api/llm.model'],
            ['deleteSetting', ['llm.model'], '/settings/api/llm.model'],
            ['researchTimelineMetrics', ['research-1'], '/metrics/api/metrics/research/research-1/timeline'],
            ['researchSearchMetrics', ['research-1'], '/metrics/api/metrics/research/research-1/search'],
            ['getRating', ['research-1'], '/metrics/api/ratings/research-1'],
            ['saveRating', ['research-1'], '/metrics/api/ratings/research-1'],
            ['researchCosts', ['research-1'], '/metrics/api/research-costs/research-1'],
        ])('%s maps to its canonical migrated route', (method, args, expected) => {
            expect(URLBuilder[method](...args)).toBe(expected);
        });
    });

    describe('extractResearchIdFromPattern', () => {
        const originalLocation = window.location;

        function setPath(pathname) {
            Object.defineProperty(window, 'location', {
                configurable: true,
                value: { ...originalLocation, pathname },
            });
        }

        afterEach(() => {
            Object.defineProperty(window, 'location', {
                configurable: true,
                value: originalLocation,
            });
        });

        it.each([
            ['/results/8f98e166-bf06-4de4-ae65-fb8e790a16e4', 'results', '8f98e166-bf06-4de4-ae65-fb8e790a16e4'],
            ['/details/research-abc-123', 'details', 'research-abc-123'],
            ['/progress/42', 'progress', '42'],
        ])('extracts migrated string IDs from %s', (pathname, pattern, expected) => {
            setPath(pathname);

            expect(URLBuilder.extractResearchIdFromPattern(pattern)).toBe(expected);
        });

        it('does not extract an ID for a different page route', () => {
            setPath('/history/research-abc-123');

            expect(URLBuilder.extractResearchIdFromPattern('results')).toBeNull();
        });
    });

    describe('extractResearchId', () => {
        const originalLocation = window.location;

        afterEach(() => {
            Object.defineProperty(window, 'location', {
                configurable: true,
                value: originalLocation,
            });
        });

        it.each([
            ['/results/101', '101'],
            ['/details/202', '202'],
            ['/progress/303', '303'],
        ])('extracts a numeric ID from %s', (pathname, expected) => {
            Object.defineProperty(window, 'location', {
                configurable: true,
                value: { ...originalLocation, pathname },
            });

            expect(URLBuilder.extractResearchId()).toBe(expected);
        });

        it('returns null when no ID pattern matches current path', () => {
            // Default happy-dom path is "/"
            expect(URLBuilder.extractResearchId()).toBeNull();
        });
    });

    describe('getCurrentPageType', () => {
        const originalLocation = window.location;

        afterEach(() => {
            Object.defineProperty(window, 'location', {
                configurable: true,
                value: originalLocation,
            });
        });

        it.each([
            ['/', 'home'],
            ['/index', 'home'],
            ['/home', 'home'],
            ['/results/research-1', 'results'],
            ['/details/research-1', 'details'],
            ['/progress/research-1', 'progress'],
            ['/history/', 'history'],
            ['/settings/', 'settings'],
            ['/metrics/costs', 'metrics'],
            ['/library/', 'unknown'],
        ])('classifies %s as %s', (pathname, expected) => {
            Object.defineProperty(window, 'location', {
                configurable: true,
                value: { ...originalLocation, pathname },
            });

            expect(URLBuilder.getCurrentPageType()).toBe(expected);
        });
    });
});

describe('URLS constants — structural sanity (not literal values)', () => {
    // Lightweight invariants that catch typos without asserting exact strings.

    it('all URL templates start with /', () => {
        const collectGroup = (group) => Object.values(group);
        const allUrls = [
            ...collectGroup(URLS.API),
            ...collectGroup(URLS.PAGES),
            ...collectGroup(URLS.HISTORY_API),
            ...collectGroup(URLS.SETTINGS_API),
            ...collectGroup(URLS.METRICS_API),
            ...collectGroup(URLS.LIBRARY_API),
        ];
        for (const url of allUrls) {
            expect(url).toMatch(/^\//);
        }
    });

    it('no URLs contain accidental double slashes', () => {
        const collectGroup = (group) => Object.values(group);
        const allUrls = [
            ...collectGroup(URLS.API),
            ...collectGroup(URLS.PAGES),
            ...collectGroup(URLS.HISTORY_API),
            ...collectGroup(URLS.SETTINGS_API),
        ];
        for (const url of allUrls) {
            expect(url).not.toMatch(/\/\//);
        }
    });

    it('settings API routes are namespaced under /settings/', () => {
        for (const [key, url] of Object.entries(URLS.SETTINGS_API)) {
            expect(url, `SETTINGS_API.${key}`).toMatch(/^\/settings\//);
        }
    });

    it('metrics API routes are namespaced under /metrics/', () => {
        for (const [key, url] of Object.entries(URLS.METRICS_API)) {
            expect(url, `METRICS_API.${key}`).toMatch(/^\/metrics\//);
        }
    });

    it('journal quality page is served from the metrics blueprint', () => {
        expect(URLS.PAGES.JOURNAL_QUALITY).toMatch(/^\/metrics\//);
    });

    it('library API routes are namespaced under /library/', () => {
        for (const [key, url] of Object.entries(URLS.LIBRARY_API)) {
            expect(url, `LIBRARY_API.${key}`).toMatch(/^\/library\//);
        }
    });
});