Files
opencode-manager/backend/test/services/repo-worktree.test.ts
T
ioplex 4ec62cd4e7 fix: address findings from the full Docker evaluation of main
Critical and high:
- repos: unlinking a local repo removes only the Manager reference and alias,
  never the repository files; confirm button reads "Unlink"
- auth: rate-limit by the socket address; trust proxy headers only when
  AUTH_TRUSTED_PROXY_HEADERS is set (documented and forwarded in compose)
- composer: per-session in-memory drafts (text, bash mode, attachments);
  Escape no longer clears the prompt
- url params: compose same-tick updates so schedule dialogs stay closed after
  create/delete; delete dialog names the job

Medium:
- tool rows show failures (non-zero shell exit, failed exploration members)
- voice conversation shows the specific error and a starting state
- settings: Escape in dropdowns and the shortcut recorder no longer closes
  the dialog; mobile deep links open the requested section; one content tree
  per platform
- closing pushed sheets, settings, tool panels and schedule dialogs goes back
  in history
- login returns to the requested page; signed-out pages make no
  authenticated requests
- action terminals keep their output (Manager-side output capture and replay)
- unpushed-commit detection, commit message kept on failure
- binary files with unknown extensions are detected and not editable
- tool panel resizes when docked and floats as a dismissible sheet when the
  page is too narrow
- Docker image runs tini as PID 1

Low: not-found states, add-repo and branch validation errors (4xx),
search-aware empty states, mobile repo sort/filter/select cues, sidebar
resize affordance and collapse shortcut, mobile unread badge, preview port
filtering, terminal close handling, walkthrough empty state and helper
session filtering, placeholder contrast, permission labels and focus,
TTS task-list sanitizing, git identity email validation, quieter git
logging, hermetic worktree tests, and accessible names for icon-only
controls.
2026-10-23 11:42:37 -04:00

97 lines
3.6 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { execSync } from 'child_process'
import { mkdtempSync, existsSync } from 'fs'
import { tmpdir } from 'os'
import path from 'path'
import { rm } from 'fs/promises'
import { resolveDefaultBranch, createWorktreeSafely, removeWorktree } from '../../src/services/repo'
describe('repo worktree helpers', () => {
let baseRepoPath: string
let originRepoPath: string
let worktreePath: string
let tmpDir: string
const env = process.env as Record<string, string>
beforeAll(() => {
tmpDir = mkdtempSync(path.join(tmpdir(), 'repo-worktree-test-'))
originRepoPath = path.join(tmpDir, 'origin.git')
baseRepoPath = path.join(tmpDir, 'base')
worktreePath = path.join(tmpDir, 'feature-x')
// Init bare origin
execSync(`git init --bare -b main "${originRepoPath}"`, { env })
// Clone origin to get a working base repo (default branch is "main")
execSync(`git clone "${originRepoPath}" "${baseRepoPath}"`, { env })
// Set git config for commits
execSync(`git -C "${baseRepoPath}" config user.email test@test.com`, { env })
execSync(`git -C "${baseRepoPath}" config user.name Test`, { env })
// Ensure the branch is "main" and push
execSync(`git -C "${baseRepoPath}" branch -M main`, { env })
execSync(`git -C "${baseRepoPath}" commit --allow-empty -m "Initial commit"`, { env })
execSync(`git -C "${baseRepoPath}" push origin main`, { env })
// Set bare repo HEAD to main so origin/HEAD is resolvable
execSync(`git -C "${originRepoPath}" symbolic-ref HEAD refs/heads/main`, { env })
execSync(`git -C "${baseRepoPath}" remote set-head origin --auto`, { env })
})
afterAll(async () => {
await rm(tmpDir, { recursive: true, force: true })
})
describe('resolveDefaultBranch', () => {
it('resolves default branch from origin/HEAD', async () => {
const branch = await resolveDefaultBranch(baseRepoPath, env)
expect(branch).toBe('main')
})
it('falls back to main when origin/HEAD cannot be resolved', async () => {
const branch = await resolveDefaultBranch('/nonexistent/path', env)
expect(branch).toBe('main')
})
})
describe('createWorktreeSafely', () => {
it('creates a worktree for a new branch and checks it out', async () => {
await createWorktreeSafely(baseRepoPath, worktreePath, 'feature/x', env)
expect(existsSync(worktreePath)).toBe(true)
const branch = execSync(`git -C "${worktreePath}" rev-parse --abbrev-ref HEAD`, { encoding: 'utf-8' }).trim()
expect(branch).toBe('feature/x')
})
})
describe('removeWorktree', () => {
it('removes a worktree directory and prunes the worktree entry', async () => {
expect(existsSync(worktreePath)).toBe(true)
await removeWorktree(baseRepoPath, worktreePath)
expect(existsSync(worktreePath)).toBe(false)
// Verify pruning — the removed worktree should not appear in the list
const worktreeList = execSync(`git -C "${baseRepoPath}" worktree list`, { encoding: 'utf-8' })
expect(worktreeList).not.toContain(worktreePath)
})
it('removes a linked worktree created with git worktree add', async () => {
const linkedPath = path.join(tmpDir, 'linked-manual')
execSync(`git -C "${baseRepoPath}" worktree add -b manual/linked "${linkedPath}"`, { env })
expect(existsSync(linkedPath)).toBe(true)
await removeWorktree(baseRepoPath, linkedPath)
expect(existsSync(linkedPath)).toBe(false)
const worktreeList = execSync(`git -C "${baseRepoPath}" worktree list`, { encoding: 'utf-8' })
expect(worktreeList).not.toContain(linkedPath)
})
})
})