OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git commits / 0977893

Add path prefix tests to prevent broken nav on reverse proxy
When pear-git runs behind a reverse proxy (e.g., 7rnx.net/git/), --prefix=/git must be set. Without it, all buttons/links point to the root instead of /git/, breaking navigation. Tests verify: - __PATH_PREFIX__ is injected correctly into rendered HTML - All static links include the configured prefix - getBasePath() is used for dynamic links - renderPage passes prefix through to nav Caught by: production buttons broken after server restart without --prefix=/git flag. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
097789327298e4502b8d47bb1d9c726bf4ed4066
by plankatron <fleeky@gmail.com> · 2026-04-07T13:14:44.000Z

Changed files (1) +70 -0

test/unit/path-prefix.test.js +70 -0
……@@ -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
10import test from 'brittle'
11import { renderRepoManager } from '../../lib/repo-manager-ui.js'
12import { renderPage } from '../../lib/page-layout.js'
13
14const 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
26const MOCK_STATS = { totalRepos: 1, totalCommits: 5 }
27
28// ---- Path prefix in repo manager ----
29
30test('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
35test('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
40test('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
45test('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
62test('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
67test('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})