-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgitOperationHandlers.test.ts
More file actions
190 lines (142 loc) · 6.94 KB
/
Copy pathgitOperationHandlers.test.ts
File metadata and controls
190 lines (142 loc) · 6.94 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { describe, expect, it } from 'bun:test';
import { handleGitOperation, type GitRunner } from './gitHandlers.js';
import type { HandlerContext } from './handlerContext.js';
import type { BridgeMessage } from './types.js';
function makeCtx() {
const sent: any[] = [];
const ctx = {
sendToSaas: (payload: any) => { sent.push(payload); },
syncAgents: () => {},
provider: {} as any,
} as HandlerContext;
return { ctx, sent };
}
function recordingRunner(overrides: (args: string[]) => { ok?: boolean; stdout?: string; stderr?: string } = () => ({})) {
const calls: string[][] = [];
const run: GitRunner = (args) => {
calls.push(args);
if (args[0] === 'rev-parse') return { ok: true, stdout: 'C:/workspace/proj\n', stderr: '' };
const r = overrides(args);
return { ok: r.ok ?? true, stdout: r.stdout ?? '', stderr: r.stderr ?? '' };
};
return { run, calls };
}
const msg = (over: Partial<BridgeMessage> = {}): BridgeMessage => ({
action: 'git_operation', requestId: 'r1', path: 'proj', useBasePath: true, ...over,
});
describe('handleGitOperation — commit', () => {
it('stages everything and commits with the given message', () => {
const { ctx, sent } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'commit', message: 'my change' }), ctx, run);
expect(calls.some((c) => c[0] === 'add' && c.includes('-A'))).toBe(true);
const commit = calls.find((c) => c[0] === 'commit');
expect(commit).toEqual(['commit', '-m', 'my change']);
expect(sent[0]).toMatchObject({ action: 'git_operation_ack', requestId: 'r1', success: true });
});
it('refuses to commit without a message', () => {
const { ctx, sent } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'commit', message: ' ' }), ctx, run);
expect(calls.some((c) => c[0] === 'commit')).toBe(false);
expect(sent[0]).toMatchObject({ success: false, error: 'MISSING_COMMIT_MESSAGE' });
});
it('never passes the message as a shell string', () => {
const { ctx } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'commit', message: 'oops"; rm -rf /' }), ctx, run);
// argv form keeps the whole message a single argument, no shell involved
expect(calls.find((c) => c[0] === 'commit')?.[2]).toBe('oops"; rm -rf /');
});
it('reports git stderr when the commit fails', () => {
const { ctx, sent } = makeCtx();
const { run } = recordingRunner((args) =>
args[0] === 'commit' ? { ok: false, stderr: 'nothing to commit, working tree clean' } : {});
handleGitOperation(msg({ operation: 'commit', message: 'x' }), ctx, run);
expect(sent[0]).toMatchObject({ success: false });
expect(sent[0].error).toContain('nothing to commit');
});
it('stops before committing when staging fails', () => {
const { ctx, sent } = makeCtx();
const { run, calls } = recordingRunner((args) =>
args[0] === 'add' ? { ok: false, stderr: 'permission denied' } : {});
handleGitOperation(msg({ operation: 'commit', message: 'x' }), ctx, run);
expect(calls.some((c) => c[0] === 'commit')).toBe(false);
expect(sent[0]).toMatchObject({ success: false });
});
});
describe('handleGitOperation — remote operations', () => {
it('reuses positive repository detection across operations', () => {
const { ctx } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'fetch' }), ctx, run);
handleGitOperation(msg({ requestId: 'r2', operation: 'push' }), ctx, run);
expect(calls.filter((c) => c[0] === 'rev-parse')).toHaveLength(1);
expect(calls.filter((c) => c[0] === 'fetch')).toHaveLength(1);
expect(calls.filter((c) => c[0] === 'push')).toHaveLength(1);
});
it('fetches with prune', () => {
const { ctx, sent } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'fetch' }), ctx, run);
expect(calls.some((c) => c[0] === 'fetch')).toBe(true);
expect(sent[0]).toMatchObject({ success: true, operation: 'fetch' });
});
it('pulls without creating a merge commit behind the user back', () => {
const { ctx } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'pull' }), ctx, run);
expect(calls.find((c) => c[0] === 'pull')).toEqual(['pull', '--ff-only']);
});
it('pushes to the tracked upstream', () => {
const { ctx, sent } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'push' }), ctx, run);
expect(calls.some((c) => c[0] === 'push')).toBe(true);
expect(sent[0]).toMatchObject({ success: true, operation: 'push' });
});
it('never force-pushes', () => {
const { ctx } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'push' }), ctx, run);
const push = calls.find((c) => c[0] === 'push') ?? [];
expect(push.some((a) => a === '--force' || a === '-f' || a === '--force-with-lease')).toBe(false);
});
it('surfaces an authentication failure instead of reporting success', () => {
const { ctx, sent } = makeCtx();
const { run } = recordingRunner((args) =>
args[0] === 'push' ? { ok: false, stderr: 'fatal: Authentication failed' } : {});
handleGitOperation(msg({ operation: 'push' }), ctx, run);
expect(sent[0]).toMatchObject({ success: false });
expect(sent[0].error).toContain('Authentication failed');
});
it('rejects an unknown operation rather than running anything', () => {
const { ctx, sent } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'reset-hard' as never }), ctx, run);
expect(calls.filter((c) => c[0] !== 'rev-parse')).toHaveLength(0);
expect(sent[0]).toMatchObject({ success: false, error: 'UNSUPPORTED_OPERATION' });
});
it('refuses to operate outside the Bridge base path', () => {
const { ctx, sent } = makeCtx();
const { run, calls } = recordingRunner();
handleGitOperation(msg({ operation: 'fetch', path: '../elsewhere' }), ctx, run);
expect(calls).toHaveLength(0);
expect(sent[0]).toMatchObject({ success: false, error: 'INVALID_PATH' });
});
it('reports a folder that is not a repository', () => {
const { ctx, sent } = makeCtx();
const run: GitRunner = (args) =>
args[0] === 'rev-parse'
? { ok: false, stdout: '', stderr: 'not a git repository' }
: { ok: true, stdout: '', stderr: '' };
handleGitOperation(msg({ operation: 'fetch' }), ctx, run);
expect(sent[0]).toMatchObject({ success: false, error: 'NOT_A_REPOSITORY' });
});
it('reports a missing git binary', () => {
const { ctx, sent } = makeCtx();
const run: GitRunner = () => { throw new Error('spawn git ENOENT'); };
handleGitOperation(msg({ operation: 'fetch' }), ctx, run);
expect(sent[0]).toMatchObject({ success: false, error: 'GIT_NOT_AVAILABLE' });
});
});