forked from stablyai/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-object-ref.test.ts
More file actions
63 lines (48 loc) · 1.99 KB
/
Copy pathcommit-object-ref.test.ts
File metadata and controls
63 lines (48 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { beforeEach, describe, expect, it, vi } from 'vitest'
const gitExecFileAsyncMock = vi.hoisted(() => vi.fn())
vi.mock('./runner', () => ({
gitExecFileAsync: gitExecFileAsyncMock
}))
import {
hasCommitObjectViaGitExec,
hasLocalCommitObject,
isFullGitObjectId
} from './commit-object-ref'
describe('commit object refs', () => {
beforeEach(() => {
gitExecFileAsyncMock.mockReset()
})
it('recognizes only complete git object IDs', () => {
expect(isFullGitObjectId('a'.repeat(40))).toBe(true)
expect(isFullGitObjectId('A'.repeat(40))).toBe(true)
expect(isFullGitObjectId('abc123')).toBe(false)
expect(isFullGitObjectId('origin/main')).toBe(false)
expect(isFullGitObjectId('g'.repeat(40))).toBe(false)
})
it('verifies full commit objects and rejects missing objects', async () => {
const gitExec = vi.fn().mockResolvedValue({ stdout: 'a'.repeat(40), stderr: '' })
await expect(hasCommitObjectViaGitExec(gitExec, 'a'.repeat(40))).resolves.toBe(true)
expect(gitExec).toHaveBeenCalledWith([
'rev-parse',
'--verify',
'--quiet',
`${'a'.repeat(40)}^{commit}`
])
gitExec.mockRejectedValueOnce(new Error('missing'))
await expect(hasCommitObjectViaGitExec(gitExec, 'b'.repeat(40))).resolves.toBe(false)
})
it('does not shell out for branch names or short SHAs', async () => {
const gitExec = vi.fn()
await expect(hasCommitObjectViaGitExec(gitExec, 'abc123')).resolves.toBe(false)
await expect(hasCommitObjectViaGitExec(gitExec, 'origin/main')).resolves.toBe(false)
expect(gitExec).not.toHaveBeenCalled()
})
it('checks local commit objects in the target repo path', async () => {
gitExecFileAsyncMock.mockResolvedValue({ stdout: 'a'.repeat(40), stderr: '' })
await expect(hasLocalCommitObject('/repo', 'a'.repeat(40))).resolves.toBe(true)
expect(gitExecFileAsyncMock).toHaveBeenCalledWith(
['rev-parse', '--verify', '--quiet', `${'a'.repeat(40)}^{commit}`],
{ cwd: '/repo' }
)
})
})