-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathspawn-cloud-agent-session.test.ts
More file actions
209 lines (186 loc) · 7.64 KB
/
spawn-cloud-agent-session.test.ts
File metadata and controls
209 lines (186 loc) · 7.64 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { beforeAll, beforeEach, describe, expect, it, jest } from '@jest/globals';
import type { PlatformIntegration } from '@kilocode/db';
import type { CloudAgentAttachments } from '@/lib/cloud-agent/constants';
import type { createCloudAgentNextClient as CreateCloudAgentNextClient } from '@/lib/cloud-agent-next/cloud-agent-client';
import type { getGitHubTokenForUser as GetGitHubTokenForUser } from '@/lib/cloud-agent/github-integration-helpers';
import type {
buildGitLabCloneUrl as BuildGitLabCloneUrl,
getGitLabInstanceUrlForUser as GetGitLabInstanceUrlForUser,
getGitLabTokenForUser as GetGitLabTokenForUser,
} from '@/lib/cloud-agent/gitlab-integration-helpers';
import type { resolveBotSessionProfile as ResolveBotSessionProfile } from './resolve-bot-session-profile';
import type SpawnCloudAgentSession from './spawn-cloud-agent-session';
jest.mock('@/lib/config.server', () => ({
CALLBACK_TOKEN_SECRET: 'callback-secret',
}));
jest.mock('@/lib/constants', () => ({
APP_URL: 'https://app.example.test',
}));
jest.mock('@/lib/cloud-agent-next/cloud-agent-client', () => ({
createCloudAgentNextClient: jest.fn(),
}));
jest.mock('@/lib/cloud-agent/github-integration-helpers', () => ({
getGitHubTokenForOrganization: jest.fn(),
getGitHubTokenForUser: jest.fn(),
}));
jest.mock('@/lib/cloud-agent/gitlab-integration-helpers', () => ({
getGitLabTokenForOrganization: jest.fn(),
getGitLabTokenForUser: jest.fn(),
getGitLabInstanceUrlForOrganization: jest.fn(),
getGitLabInstanceUrlForUser: jest.fn(),
buildGitLabCloneUrl: jest.fn(),
}));
jest.mock('./resolve-bot-session-profile', () => ({
resolveBotSessionProfile: jest.fn(),
}));
jest.mock('@kilocode/cloud-agent-profile', () => ({
profileMcpServersToClientRecord: jest.fn(() => undefined),
}));
jest.mock('@sentry/nextjs', () => ({
captureException: jest.fn(),
}));
const platformIntegration = {
owned_by_organization_id: null,
owned_by_user_id: 'owner-1',
} as PlatformIntegration;
const attachments: CloudAgentAttachments = {
path: 'message-attachments',
files: ['image.png', 'requirements.md'],
};
const mockPrepareSession =
jest.fn<(input: unknown) => Promise<{ cloudAgentSessionId: string; kiloSessionId: string }>>();
const mockInitiateFromPreparedSession = jest.fn<(input: unknown) => Promise<unknown>>();
let spawnCloudAgentSession: typeof SpawnCloudAgentSession;
let mockCreateCloudAgentNextClient: jest.MockedFunction<typeof CreateCloudAgentNextClient>;
let mockGetGitHubTokenForUser: jest.MockedFunction<typeof GetGitHubTokenForUser>;
let mockGetGitLabTokenForUser: jest.MockedFunction<typeof GetGitLabTokenForUser>;
let mockGetGitLabInstanceUrlForUser: jest.MockedFunction<typeof GetGitLabInstanceUrlForUser>;
let mockBuildGitLabCloneUrl: jest.MockedFunction<typeof BuildGitLabCloneUrl>;
let mockResolveBotSessionProfile: jest.MockedFunction<typeof ResolveBotSessionProfile>;
describe('spawnCloudAgentSession', () => {
beforeAll(async () => {
const client = await import('@/lib/cloud-agent-next/cloud-agent-client');
const github = await import('@/lib/cloud-agent/github-integration-helpers');
const gitlab = await import('@/lib/cloud-agent/gitlab-integration-helpers');
const profile = await import('./resolve-bot-session-profile');
const spawn = await import('./spawn-cloud-agent-session');
mockCreateCloudAgentNextClient = jest.mocked(client.createCloudAgentNextClient);
mockGetGitHubTokenForUser = jest.mocked(github.getGitHubTokenForUser);
mockGetGitLabTokenForUser = jest.mocked(gitlab.getGitLabTokenForUser);
mockGetGitLabInstanceUrlForUser = jest.mocked(gitlab.getGitLabInstanceUrlForUser);
mockBuildGitLabCloneUrl = jest.mocked(gitlab.buildGitLabCloneUrl);
mockResolveBotSessionProfile = jest.mocked(profile.resolveBotSessionProfile);
spawnCloudAgentSession = spawn.default;
});
beforeEach(() => {
jest.clearAllMocks();
mockCreateCloudAgentNextClient.mockReturnValue({
prepareSession: mockPrepareSession,
initiateFromPreparedSession: mockInitiateFromPreparedSession,
} as never);
mockPrepareSession.mockResolvedValue({
cloudAgentSessionId: 'cloud-session-1',
kiloSessionId: 'kilo-session-1',
});
mockInitiateFromPreparedSession.mockResolvedValue({});
mockResolveBotSessionProfile.mockResolvedValue({});
mockGetGitHubTokenForUser.mockResolvedValue('github-token');
mockGetGitLabTokenForUser.mockResolvedValue('gitlab-token');
mockGetGitLabInstanceUrlForUser.mockResolvedValue('https://gitlab.com');
mockBuildGitLabCloneUrl.mockReturnValue('https://gitlab.com/group/repo.git');
});
it('passes only canonical attachments when preparing a GitHub session', async () => {
await spawnCloudAgentSession(
{ githubRepo: 'owner/repo', prompt: 'Use the files', mode: 'code' },
'model',
platformIntegration,
'auth-token',
'ticket-user',
'request-1',
undefined,
{ attachments }
);
const prepareInput = mockPrepareSession.mock.calls[0]?.[0];
expect(prepareInput).toEqual(expect.objectContaining({ attachments }));
expect(prepareInput).not.toHaveProperty('images');
});
it('passes only canonical attachments when preparing a GitLab session', async () => {
await spawnCloudAgentSession(
{ gitlabProject: 'group/repo', prompt: 'Use the files', mode: 'ask' },
'model',
platformIntegration,
'auth-token',
'ticket-user',
'request-2',
undefined,
{ attachments }
);
const prepareInput = mockPrepareSession.mock.calls[0]?.[0];
expect(prepareInput).toEqual(expect.objectContaining({ attachments }));
expect(prepareInput).not.toHaveProperty('images');
});
it('adds separate PR strategy guidance when preparing a requested GitHub session', async () => {
await spawnCloudAgentSession(
{
githubRepo: 'owner/repo',
githubPullRequestStrategy: 'separate_pull_request',
prompt: 'Update REVIEW.md',
mode: 'code',
},
'model',
platformIntegration,
'auth-token',
'ticket-user',
'request-3'
);
const prepareInput = mockPrepareSession.mock.calls[0]?.[0];
expect(prepareInput).toEqual(
expect.objectContaining({
prompt: expect.stringContaining('create a fresh branch'),
})
);
expect(prepareInput).toEqual(
expect.objectContaining({
prompt: expect.stringContaining("must not push to or modify the current PR's head branch"),
})
);
});
it('adds current PR branch guidance when preparing a current-branch GitHub session', async () => {
await spawnCloudAgentSession(
{
githubRepo: 'owner/repo',
githubPullRequestStrategy: 'current_pr_branch',
prompt: 'Update REVIEW.md',
mode: 'code',
},
'model',
platformIntegration,
'auth-token',
'ticket-user',
'request-4'
);
const prepareInput = mockPrepareSession.mock.calls[0]?.[0];
expect(prepareInput).toEqual(
expect.objectContaining({
prompt: expect.stringContaining('Work on the current PR head branch'),
})
);
expect(prepareInput).toEqual(
expect.objectContaining({
prompt: expect.not.stringContaining('create a fresh branch'),
})
);
});
it('leaves the prompt unchanged when separate PR strategy is not requested', async () => {
await spawnCloudAgentSession(
{ githubRepo: 'owner/repo', prompt: 'Update REVIEW.md', mode: 'code' },
'model',
platformIntegration,
'auth-token',
'ticket-user',
'request-5'
);
const prepareInput = mockPrepareSession.mock.calls[0]?.[0];
expect(prepareInput).toEqual(expect.objectContaining({ prompt: 'Update REVIEW.md' }));
});
});