spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / tests / git / probes / branch-base-of.test.ts
119 lines · 3.3 KBRaw
1import type { ProcessRunResult } from 'claude-code'
2import { describe, expect, test, tier } from 'claude-code/testing'
3
4import Git from '../../../hooks/git'
5import Fixtures from '../../fixtures'
6
7tier('builtin')
8
9describe('branch-base-of', () => {
10 type Script = Record<string, ProcessRunResult>
11
12 const runOf = (script: Script) => Fixtures.scriptedGitOf(script).run
13
14 const onFeature = (): Script => ({
15 'rev-parse --abbrev-ref HEAD': Fixtures.ok('feature\n'),
16 'symbolic-ref --short refs/remotes/origin/HEAD':
17 Fixtures.ok('origin/main\n'),
18 'show-ref --verify --quiet refs/remotes/origin/main': Fixtures.ok(),
19 })
20
21 test("origin's merge-base wins unless the local descends", async () => {
22 expect(
23 await Git.branchBaseOf(
24 runOf({
25 ...onFeature(),
26 'merge-base HEAD origin/main': Fixtures.ok(
27 `${Fixtures.ORIGIN_BASE}\n`,
28 ),
29 'merge-base HEAD main': Fixtures.ok(`${Fixtures.LOCAL_BASE}\n`),
30 'rev-parse HEAD': Fixtures.ok(`${Fixtures.FEATURE_HEAD}\n`),
31 }),
32 ),
33 ).toEqual({
34 kind: 'merge-base',
35 mergeBase: Fixtures.ORIGIN_BASE,
36 baseBranch: 'main',
37 })
38
39 expect(
40 await Git.branchBaseOf(
41 runOf({
42 ...onFeature(),
43 'merge-base HEAD origin/main': Fixtures.ok(
44 `${Fixtures.ORIGIN_BASE}\n`,
45 ),
46 'merge-base HEAD main': Fixtures.ok(`${Fixtures.LOCAL_BASE}\n`),
47 [`merge-base --is-ancestor ${Fixtures.ORIGIN_BASE} ` +
48 Fixtures.LOCAL_BASE]: Fixtures.ok(),
49 'rev-parse HEAD': Fixtures.ok(`${Fixtures.FEATURE_HEAD}\n`),
50 }),
51 ),
52 ).toEqual({
53 kind: 'merge-base',
54 mergeBase: Fixtures.LOCAL_BASE,
55 baseBranch: 'main',
56 })
57 })
58
59 test('the default branch, or HEAD at the base: head-is-base', async () => {
60 expect(
61 await Git.branchBaseOf(
62 runOf({
63 ...onFeature(),
64 'rev-parse --abbrev-ref HEAD': Fixtures.ok('main\n'),
65 }),
66 ),
67 ).toEqual({ kind: 'head-is-base', baseBranch: 'main' })
68
69 expect(
70 await Git.branchBaseOf(
71 runOf({
72 ...onFeature(),
73 'merge-base HEAD origin/main': Fixtures.ok(
74 `${Fixtures.FEATURE_HEAD}\n`,
75 ),
76 'rev-parse HEAD': Fixtures.ok(`${Fixtures.FEATURE_HEAD}\n`),
77 }),
78 ),
79 ).toEqual({ kind: 'head-is-base', baseBranch: 'main' })
80 })
81
82 test('a detached HEAD, or no common ancestor, has no base', async () => {
83 expect(
84 await Git.branchBaseOf(
85 runOf({
86 ...onFeature(),
87 'rev-parse --abbrev-ref HEAD': Fixtures.ok('HEAD\n'),
88 }),
89 ),
90 ).toEqual({ kind: 'none' })
91
92 expect(
93 await Git.branchBaseOf(
94 runOf({
95 ...onFeature(),
96 'merge-base HEAD origin/main': Fixtures.PROBE_MISSED,
97 'merge-base HEAD main': Fixtures.PROBE_MISSED,
98 }),
99 ),
100 ).toEqual({ kind: 'none' })
101 })
102
103 test('a failed merge-base while the ref exists: error', async () => {
104 expect(await Git.branchBaseOf(runOf(onFeature()))).toEqual({
105 kind: 'error',
106 reason: 'merge_base_failed',
107 })
108 })
109
110 test('no default ref anywhere: none', async () => {
111 expect(
112 await Git.branchBaseOf(
113 runOf({
114 'rev-parse --abbrev-ref HEAD': Fixtures.ok('feature\n'),
115 }),
116 ),
117 ).toEqual({ kind: 'none' })
118 })
119})