Files
opencode-manager/backend/src/services/project-id-resolver.test.ts
T
Chris Scott 53238a6324 feat(git): extend source control with worktrees, stashes, conflicts and identities
- Create a worktree from a new or existing branch
- Integrate a worktree branch into a target branch by merge commit or cherry-pick, with continue/abort on conflicts
- Delete a worktree with keep, delete-local, or delete-local-and-remote branch options, using safe branch deletion for unmerged branches
- Add a stash tab (list, push, apply, pop, drop) shared across a repository and its worktrees
- Rename and delete branches, optionally deleting the remote branch and updating scheduled base branches
- Detect in-progress merge, rebase, cherry-pick and revert operations and hand conflicts to an agent session
- Generate commit messages from staged changes with the default model
- Add a default git identity and saved per-repository identities written to git config
- Unify git error mapping behind a typed error and one route helper; consolidate git mutations and cache invalidation
- Update repository docs
2026-10-03 21:59:00 -04:00

76 lines
2.6 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { mkdtempSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import path from 'path'
import { execSync } from 'child_process'
import { gitRemoteProjectId } from '@opencode-manager/shared/project-id'
import { isGitMainCheckout, resolveProjectId } from './project-id-resolver'
const COMMIT_IDENTITY_ARGS = ['-c', 'user.name=t', '-c', 'user.email=t@t']
describe('resolveProjectId', () => {
let base: string
beforeAll(() => {
base = mkdtempSync(path.join(tmpdir(), 'oc-resolve-project-'))
})
afterAll(() => {
rmSync(base, { recursive: true, force: true })
})
it('returns null for a non-git directory', async () => {
const dir = mkdtempSync(path.join(base, 'non-git-'))
expect(await resolveProjectId(dir)).toBeNull()
})
it('prefers the normalized origin remote hash', async () => {
const dir = mkdtempSync(path.join(base, 'remote-'))
execSync(`git init -q "${dir}"`)
execSync(`git -C "${dir}" ${COMMIT_IDENTITY_ARGS.join(' ')} commit -q --allow-empty -m init`)
execSync(`git -C "${dir}" remote add origin git@github.com:Acme/App.git`)
expect(await resolveProjectId(dir)).toBe(gitRemoteProjectId('git@github.com:Acme/App.git'))
})
it('falls back to the sorted first root commit when there is no remote', async () => {
const dir = mkdtempSync(path.join(base, 'root-'))
execSync(`git init -q "${dir}"`)
execSync(`git -C "${dir}" ${COMMIT_IDENTITY_ARGS.join(' ')} commit -q --allow-empty -m init`)
const rootCommit = execSync(`git -C "${dir}" rev-list --max-parents=0 HEAD`).toString().trim()
expect(await resolveProjectId(dir)).toBe(rootCommit)
})
})
describe('isGitMainCheckout', () => {
let base: string
let mainRepo: string
let worktree: string
beforeAll(() => {
base = mkdtempSync(path.join(tmpdir(), 'oc-main-checkout-'))
mainRepo = path.join(base, 'main')
worktree = path.join(base, 'wt')
execSync(`git init -q "${mainRepo}"`)
execSync(`git -C "${mainRepo}" ${COMMIT_IDENTITY_ARGS.join(' ')} commit -q --allow-empty -m init`)
execSync(`git -C "${mainRepo}" worktree add -q "${worktree}" -b feature`)
})
afterAll(() => {
rmSync(base, { recursive: true, force: true })
})
it('returns true for the main checkout', async () => {
expect(await isGitMainCheckout(mainRepo)).toBe(true)
})
it('returns false for a linked worktree', async () => {
expect(await isGitMainCheckout(worktree)).toBe(false)
})
it('returns false for a non-git directory', async () => {
expect(await isGitMainCheckout(base)).toBe(false)
})
})