Files
opencode-manager/backend/test/services/repo-worktree.test.ts
Chris Scott dacde879d6 feat(opencode): migrate OpenCode Manager to OpenCode 2 (#359)
* feat(opencode): migrate OpenCode Manager to OpenCode 2

Port the backend lifecycle, generated plugins, event aggregation,
schedules, worktrees, provider credentials, MCP, config handling, the
frontend chat model, and the ocm CLI to OpenCode 2.

Adopt @opencode/client 2.0.15 as the single typed V2 API contract,
require OpenCode 2.0.0 or newer (pinned 2.0.15), and delete every
V1-only code path.

* refactor(opencode): reload config in place and drop v1 compatibility shims

Route config/skill/AGENTS.md changes through the OpenCode location reload
API instead of a full server restart, stop auto-resuming interrupted
sessions, remove the restart coordinator and v1 MCP/version shims, and pin
support to OpenCode >=2.0.15 <3.0.0. Adds integration-ready retries,
service-mode launch, ocm-cli repo proxy, and new frontend form/MCP helpers.

* refactor(opencode): consolidate v2 migration helpers and harden install, proxy, and session flows

- extract shared semver and model-ref utilities, reuse across version checks
- share the OpenCode release download script across Docker, entrypoint, dev setup
- unify install/upgrade failure recovery and guard externally managed OPENCODE_BIN
- scope the repo proxy location rewrite and surface ocm proxy errors
- rework frontend session projection, permission lookup, and cache invalidation

* fix(schedules): reject unavailable agents before creating a session

* feat(opencode): add ocm install, schedule agent fallback, and session move guards
2026-09-26 13:16:45 -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 "${originRepoPath}"`, { env })
// Clone origin to get a working base repo (default branch is "master" in bare repos)
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 })
// Rename default branch to "main" and push
execSync(`git -C "${baseRepoPath}" branch -m master 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)
})
})
})