Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { commitAll, headRemoteUrl, pushHead, type GitRunner } from './git'
3
4type GitResult = { stdout: string; stderr: string; code: number }
5
6const CWD = '/tmp/review'
7const IDENTITY = { name: 'TanStack AI Bot', email: 'bot@tanstack.com' }
8const TOKEN = 'ghs_abc'
9const ORIGIN = 'TanStack/ai'
10const FORK = 'alice/ai'
11const REMOTE = `https://x-access-token:tok@github.com/${ORIGIN}.git`
12
13function createFakeRunner(
14 impl?: (args: Array<string>, cwd: string) => GitResult,
15) {
16 const calls: Array<{ args: Array<string>; cwd: string }> = []
17 const runner: GitRunner = async (args, cwd) => {
18 calls.push({ args: [...args], cwd })
19 if (impl) {
20 return impl(args, cwd)
21 }
22 return { stdout: '', stderr: '', code: 0 }
23 }
24 return { runner, calls }
25}
26
27describe('commitAll', () => {
28 it('adds all files then commits with the given identity', async () => {
29 const { runner, calls } = createFakeRunner()
30
31 expect(await commitAll(CWD, 'polish: fix nits', runner, IDENTITY)).toEqual({
32 committed: true,
33 })
34 expect(calls).toEqual([
35 { args: ['add', '-A'], cwd: CWD },
36 {
37 args: [
38 '-c',
39 'user.name=TanStack AI Bot',
40 '-c',
41 'user.email=bot@tanstack.com',
42 'commit',
43 '-m',
44 'polish: fix nits',
45 ],
46 cwd: CWD,
47 },
48 ])
49 })
50
51 it('returns committed false when git reports nothing to commit', async () => {
52 const { runner } = createFakeRunner((args) => {
53 if (args.includes('commit')) {
54 return {
55 stdout: 'On branch main\nnothing to commit, working tree clean\n',
56 stderr: '',
57 code: 1,
58 }
59 }
60 return { stdout: '', stderr: '', code: 0 }
61 })
62
63 expect(await commitAll(CWD, 'nope', runner, IDENTITY)).toEqual({
64 committed: false,
65 })
66 })
67
68 it('throws when add exits non-zero', async () => {
69 const { runner } = createFakeRunner((args) => {
70 if (args[0] === 'add') {
71 return { stdout: '', stderr: 'index.lock exists', code: 128 }
72 }
73 return { stdout: '', stderr: '', code: 0 }
74 })
75
76 await expect(commitAll(CWD, 'nope', runner, IDENTITY)).rejects.toThrow(
77 /index.lock exists/,
78 )
79 })
80
81 it('throws when commit fails for a reason other than an empty tree', async () => {
82 const { runner } = createFakeRunner((args) => {
83 if (args.includes('commit')) {
84 return { stdout: '', stderr: 'hook declined', code: 1 }
85 }
86 return { stdout: '', stderr: '', code: 0 }
87 })
88
89 await expect(commitAll(CWD, 'nope', runner, IDENTITY)).rejects.toThrow(
90 /hook declined/,
91 )
92 })
93})
94
95describe('pushHead', () => {
96 it('pushes HEAD to the dest ref with --force-with-lease and no bare --force', async () => {
97 const { runner, calls } = createFakeRunner()
98
99 await pushHead(CWD, runner, { remoteUrl: REMOTE, ref: 'heads/fix-nits' })
100
101 const args = calls[0]?.args ?? []
102 expect(calls).toEqual([
103 {
104 args: ['push', '--force-with-lease', REMOTE, 'HEAD:heads/fix-nits'],
105 cwd: CWD,
106 },
107 ])
108 expect(args).toContain('--force-with-lease')
109 expect(args).not.toContain('--force')
110 })
111
112 it('throws when push exits non-zero', async () => {
113 const { runner } = createFakeRunner(() => ({
114 stdout: '',
115 stderr: 'stale info',
116 code: 1,
117 }))
118
119 await expect(
120 pushHead(CWD, runner, { remoteUrl: REMOTE, ref: 'main' }),
121 ).rejects.toThrow(/stale info/)
122 })
123})
124
125describe('headRemoteUrl', () => {
126 it('uses originRepo for a same-repo PR', () => {
127 expect(
128 headRemoteUrl({
129 isFork: false,
130 originRepo: ORIGIN,
131 headRepo: FORK,
132 token: TOKEN,
133 }),
134 ).toBe(`https://x-access-token:${TOKEN}@github.com/${ORIGIN}.git`)
135 })
136
137 it('uses headRepo for a fork PR', () => {
138 expect(
139 headRemoteUrl({
140 isFork: true,
141 originRepo: ORIGIN,
142 headRepo: FORK,
143 token: TOKEN,
144 }),
145 ).toBe(`https://x-access-token:${TOKEN}@github.com/${FORK}.git`)
146 })
147})