OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / test / unit / path-prefix.test.js

71 lines · javascript ref:
/**
 * Unit tests for path prefix injection in UI rendering.
 *
 * Verifies that when pathPrefix is set (e.g., '/git'), all rendered
 * HTML correctly uses the prefix in links, buttons, and script vars.
 * Without this, deploying behind a reverse proxy (e.g., 7rnx.net/git/)
 * breaks all navigation.
 */

import test from 'brittle'
import { renderRepoManager } from '../../lib/repo-manager-ui.js'
import { renderPage } from '../../lib/page-layout.js'

const MOCK_REPOS = [
  {
    name: 'test-repo',
    path: '/tmp/test.git',
    commits: 5,
    branches: 1,
    lastCommit: { hash: 'abc123', author: 'test', timestamp: Date.now() / 1000, message: 'test' },
    visibility: 'public',
    category: 'uncategorized'
  }
]

const MOCK_STATS = { totalRepos: 1, totalCommits: 5 }

// ---- Path prefix in repo manager ----

test('renderRepoManager - includes pathPrefix in __PATH_PREFIX__', (t) => {
  const html = renderRepoManager(MOCK_REPOS, MOCK_STATS, 'main', '/git')
  t.ok(html.includes("window.__PATH_PREFIX__ = '/git'"), 'PATH_PREFIX should be /git')
})

test('renderRepoManager - empty prefix when not set', (t) => {
  const html = renderRepoManager(MOCK_REPOS, MOCK_STATS, 'main', '')
  t.ok(html.includes("window.__PATH_PREFIX__ = ''"), 'PATH_PREFIX should be empty string')
})

test('renderRepoManager - repo links use getBasePath()', (t) => {
  const html = renderRepoManager(MOCK_REPOS, MOCK_STATS, 'main', '/git')
  t.ok(html.includes("getBasePath()"), 'links should use getBasePath() for prefix')
})

test('renderRepoManager - all static links include prefix', (t) => {
  const prefix = '/git'
  const html = renderRepoManager(MOCK_REPOS, MOCK_STATS, 'main', prefix)
  // Find all href="/..." links — they should all start with the prefix
  const allLinks = html.match(/href=["']\/[^"'\s]+/gi) || []
  const wrongPrefix = allLinks.filter(h => {
    const path = h.replace(/^href=["']/, '')
    // Skip vendor/favicon/external
    if (path.startsWith('/vendor') || path.startsWith('/favicon')) return false
    // Must start with prefix
    return !path.startsWith(prefix)
  })
  t.is(wrongPrefix.length, 0, `Found links without prefix: ${wrongPrefix.join(', ')}`)
})

// ---- Path prefix in renderPage ----

test('renderPage - passes pathPrefix to nav links', (t) => {
  const html = renderPage('Test', '<p>content</p>', { pathPrefix: '/git', activePage: 'repos' })
  t.ok(html.includes('/git'), 'rendered page should contain /git prefix')
})

test('renderPage - nav links work without prefix', (t) => {
  const html = renderPage('Test', '<p>content</p>', { pathPrefix: '', activePage: 'repos' })
  t.ok(html.includes("__PATH_PREFIX__"), 'should still have PATH_PREFIX variable')
})