@aaronshaf/ger
Version:
Gerrit CLI and SDK - A modern CLI tool and TypeScript SDK for Gerrit Code Review, built with Effect-TS
83 lines (72 loc) • 3.11 kB
text/typescript
import { describe, test, expect } from 'bun:test'
import { Effect, Layer } from 'effect'
import { GitWorktreeService } from '@/services/git-worktree'
describe('Git Worktree Creation', () => {
test('should handle commit-based worktree creation in service interface', async () => {
// This test verifies that the GitWorktreeService creates worktrees using
// commit hashes to avoid branch conflicts (detached HEAD approach)
const mockGitService = {
validatePreconditions: () => Effect.succeed(undefined),
createWorktree: (changeId: string) => {
// Simulate commit-based worktree creation (detached HEAD)
return Effect.succeed({
path: `/tmp/test-worktree-${changeId}`,
changeId,
originalCwd: process.cwd(),
timestamp: Date.now(),
pid: process.pid,
})
},
fetchAndCheckoutPatchset: () => Effect.succeed(undefined),
cleanup: () => Effect.succeed(undefined),
getChangedFiles: () => Effect.succeed(['test.ts']),
}
const result = await Effect.runPromise(
Effect.gen(function* () {
const service = yield* GitWorktreeService
// This call should work without specifying a base branch
// The implementation will auto-detect main vs master vs other
const worktree = yield* service.createWorktree('12345')
return worktree
}).pipe(Effect.provide(Layer.succeed(GitWorktreeService, mockGitService))),
)
expect(result.changeId).toBe('12345')
expect(result.path).toContain('12345')
})
test('should demonstrate branch detection scenarios', () => {
// Test various branch detection patterns that the real implementation should handle
const testCases = [
{ input: 'refs/remotes/origin/main', expected: 'main' },
{ input: 'refs/remotes/origin/master', expected: 'master' },
{ input: 'refs/remotes/origin/develop', expected: 'develop' },
]
testCases.forEach(({ input, expected }) => {
// Simulate the regex pattern used in getDefaultBranch
const match = input.match(/refs\/remotes\/origin\/(.+)$/)
const result = match ? match[1] : 'main'
expect(result).toBe(expected)
})
})
test('should handle branch list parsing scenarios', () => {
// Test branch list parsing scenarios
const testCases = [
{ input: ' origin/main\n origin/feature-branch', expected: 'main' },
{ input: ' origin/master\n origin/develop', expected: 'master' },
{ input: ' origin/main\n origin/master', expected: 'main' }, // main takes precedence
{ input: ' origin/feature-only', expected: 'main' }, // fallback
{ input: '', expected: 'main' }, // empty fallback
]
testCases.forEach(({ input, expected }) => {
// Simulate the branch detection logic
let result: string
if (input.includes('origin/main')) {
result = 'main'
} else if (input.includes('origin/master')) {
result = 'master'
} else {
result = 'main' // fallback
}
expect(result).toBe(expected)
})
})
})