| … | … | @@ -0,0 +1,70 @@ |
| 1 | /** |
| 2 | * Unit tests for path prefix injection in UI rendering. |
| 3 | * |
| 4 | * Verifies that when pathPrefix is set (e.g., '/git'), all rendered |
| 5 | * HTML correctly uses the prefix in links, buttons, and script vars. |
| 6 | * Without this, deploying behind a reverse proxy (e.g., 7rnx.net/git/) |
| 7 | * breaks all navigation. |
| 8 | */ |
| 9 | |
| 10 | import test from 'brittle' |
| 11 | import { renderRepoManager } from '../../lib/repo-manager-ui.js' |
| 12 | import { renderPage } from '../../lib/page-layout.js' |
| 13 | |
| 14 | const MOCK_REPOS = [ |
| 15 | { |
| 16 | name: 'test-repo', |
| 17 | path: '/tmp/test.git', |
| 18 | commits: 5, |
| 19 | branches: 1, |
| 20 | lastCommit: { hash: 'abc123', author: 'test', timestamp: Date.now() / 1000, message: 'test' }, |
| 21 | visibility: 'public', |
| 22 | category: 'uncategorized' |
| 23 | } |
| 24 | ] |
| 25 | |
| 26 | const MOCK_STATS = { totalRepos: 1, totalCommits: 5 } |
| 27 | |
| 28 | // ---- Path prefix in repo manager ---- |
| 29 | |
| 30 | test('renderRepoManager - includes pathPrefix in __PATH_PREFIX__', (t) => { |
| 31 | const html = renderRepoManager(MOCK_REPOS, MOCK_STATS, 'main', '/git') |
| 32 | t.ok(html.includes("window.__PATH_PREFIX__ = '/git'"), 'PATH_PREFIX should be /git') |
| 33 | }) |
| 34 | |
| 35 | test('renderRepoManager - empty prefix when not set', (t) => { |
| 36 | const html = renderRepoManager(MOCK_REPOS, MOCK_STATS, 'main', '') |
| 37 | t.ok(html.includes("window.__PATH_PREFIX__ = ''"), 'PATH_PREFIX should be empty string') |
| 38 | }) |
| 39 | |
| 40 | test('renderRepoManager - repo links use getBasePath()', (t) => { |
| 41 | const html = renderRepoManager(MOCK_REPOS, MOCK_STATS, 'main', '/git') |
| 42 | t.ok(html.includes("getBasePath()"), 'links should use getBasePath() for prefix') |
| 43 | }) |
| 44 | |
| 45 | test('renderRepoManager - all static links include prefix', (t) => { |
| 46 | const prefix = '/git' |
| 47 | const html = renderRepoManager(MOCK_REPOS, MOCK_STATS, 'main', prefix) |
| 48 | // Find all href="/..." links — they should all start with the prefix |
| 49 | const allLinks = html.match(/href=["']\/[^"'\s]+/gi) || [] |
| 50 | const wrongPrefix = allLinks.filter(h => { |
| 51 | const path = h.replace(/^href=["']/, '') |
| 52 | // Skip vendor/favicon/external |
| 53 | if (path.startsWith('/vendor') || path.startsWith('/favicon')) return false |
| 54 | // Must start with prefix |
| 55 | return !path.startsWith(prefix) |
| 56 | }) |
| 57 | t.is(wrongPrefix.length, 0, `Found links without prefix: ${wrongPrefix.join(', ')}`) |
| 58 | }) |
| 59 | |
| 60 | // ---- Path prefix in renderPage ---- |
| 61 | |
| 62 | test('renderPage - passes pathPrefix to nav links', (t) => { |
| 63 | const html = renderPage('Test', '<p>content</p>', { pathPrefix: '/git', activePage: 'repos' }) |
| 64 | t.ok(html.includes('/git'), 'rendered page should contain /git prefix') |
| 65 | }) |
| 66 | |
| 67 | test('renderPage - nav links work without prefix', (t) => { |
| 68 | const html = renderPage('Test', '<p>content</p>', { pathPrefix: '', activePage: 'repos' }) |
| 69 | t.ok(html.includes("__PATH_PREFIX__"), 'should still have PATH_PREFIX variable') |
| 70 | }) |