-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathaction-required.test.ts
More file actions
247 lines (212 loc) · 8.83 KB
/
Copy pathaction-required.test.ts
File metadata and controls
247 lines (212 loc) · 8.83 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const mockSendCodeReviewDisabledEmail = jest.fn();
jest.mock('@/lib/email', () => ({
sendCodeReviewDisabledEmail: (...args: unknown[]) => mockSendCodeReviewDisabledEmail(...args),
}));
import { db } from '@/lib/drizzle';
import { insertTestUser } from '@/tests/helpers/user.helper';
import { agent_configs, kilocode_users, type User } from '@kilocode/db/schema';
import { and, eq } from 'drizzle-orm';
import {
classifyCodeReviewActionRequiredFailure,
disableCodeReviewForActionRequiredFailure,
getCodeReviewActionRequiredRecoveryHref,
getCodeReviewActionRequiredState,
} from './action-required';
describe('classifyCodeReviewActionRequiredFailure', () => {
it('classifies GitHub installation, GitHub IP allow-list, BYOK invalid key, and selected model failures', () => {
expect(
classifyCodeReviewActionRequiredFailure(
'GitHub token or active app installation required for this repository (no_installation_found)'
)
).toBe('github_installation_required');
expect(
classifyCodeReviewActionRequiredFailure(
'Dispatch failed: GitHub token or active app installation required for this repository (no_installation_found)'
)
).toBe('github_installation_required');
expect(
classifyCodeReviewActionRequiredFailure(
'[BYOK] Your API key is invalid or has been revoked. Please check your API key configuration.'
)
).toBe('byok_invalid_key');
expect(
classifyCodeReviewActionRequiredFailure(
'Although you appear to have the correct authorization credentials, the `acme` organization has an IP allow list enabled, and 192.0.2.1 is not permitted.'
)
).toBe('github_ip_allow_list');
expect(
classifyCodeReviewActionRequiredFailure(
'Selected model is not available for this cloud agent session'
)
).toBe('selected_model_unavailable');
expect(
classifyCodeReviewActionRequiredFailure(
'prepareSession failed (400): {"error":{"message":"Selected model is not available for this cloud agent session","code":-32600,"data":{"code":"BAD_REQUEST","httpStatus":400,"path":"prepareSession"}}}'
)
).toBe('selected_model_unavailable');
expect(
classifyCodeReviewActionRequiredFailure(
'Not Found: The requested model is not allowed for your team.'
)
).toBe('selected_model_unavailable');
expect(
classifyCodeReviewActionRequiredFailure(
'prepareSession failed (400): {"error":{"message":"Not Found: The requested model is not allowed for your team.","code":-32600,"data":{"code":"BAD_REQUEST","httpStatus":400,"path":"prepareSession"}}}'
)
).toBe('selected_model_unavailable');
});
it('does not classify unrelated auth, rate-limit, or BYOK quota failures', () => {
expect(classifyCodeReviewActionRequiredFailure('GitHub returned 401 Unauthorized')).toBeNull();
expect(classifyCodeReviewActionRequiredFailure('GitHub returned 403 Forbidden')).toBeNull();
expect(classifyCodeReviewActionRequiredFailure('Rate limit exceeded: 429')).toBeNull();
expect(
classifyCodeReviewActionRequiredFailure('[BYOK] Your account quota is exhausted.')
).toBeNull();
});
it('routes selected model recovery to Code Reviewer settings', () => {
expect(getCodeReviewActionRequiredRecoveryHref('selected_model_unavailable')).toBe(
'/code-reviews'
);
expect(getCodeReviewActionRequiredRecoveryHref('selected_model_unavailable', 'org-1')).toBe(
'/organizations/org-1/code-reviews'
);
});
});
describe('disableCodeReviewForActionRequiredFailure', () => {
let testUser: User;
beforeAll(async () => {
testUser = await insertTestUser();
});
beforeEach(async () => {
mockSendCodeReviewDisabledEmail.mockResolvedValue({ sent: true });
await db.insert(agent_configs).values({
owned_by_user_id: testUser.id,
agent_type: 'code_review',
platform: 'github',
config: {},
is_enabled: true,
created_by: testUser.id,
});
});
afterEach(async () => {
await db
.delete(agent_configs)
.where(
and(
eq(agent_configs.owned_by_user_id, testUser.id),
eq(agent_configs.agent_type, 'code_review')
)
);
mockSendCodeReviewDisabledEmail.mockReset();
});
afterAll(async () => {
await db.delete(kilocode_users).where(eq(kilocode_users.id, testUser.id));
});
async function getStoredConfig() {
const [config] = await db
.select()
.from(agent_configs)
.where(
and(
eq(agent_configs.owned_by_user_id, testUser.id),
eq(agent_configs.agent_type, 'code_review'),
eq(agent_configs.platform, 'github')
)
)
.limit(1);
return config;
}
it('throws when the agent config is missing', async () => {
await db
.delete(agent_configs)
.where(
and(
eq(agent_configs.owned_by_user_id, testUser.id),
eq(agent_configs.agent_type, 'code_review')
)
);
await expect(
disableCodeReviewForActionRequiredFailure({
owner: { type: 'user', id: testUser.id, userId: testUser.id },
platform: 'github',
reason: 'github_installation_required',
errorMessage:
'GitHub token or active app installation required for this repository (no_installation_found)',
})
).rejects.toThrow('Code Review agent config not found');
expect(mockSendCodeReviewDisabledEmail).not.toHaveBeenCalled();
});
it('stores runtime state without recipient PII and sends one email for a repeated reason', async () => {
const owner = { type: 'user' as const, id: testUser.id, userId: testUser.id };
await disableCodeReviewForActionRequiredFailure({
owner,
platform: 'github',
reviewId: 'review-1',
reason: 'github_installation_required',
errorMessage:
'GitHub token or active app installation required for this repository (no_installation_found)',
});
await disableCodeReviewForActionRequiredFailure({
owner,
platform: 'github',
reviewId: 'review-2',
reason: 'github_installation_required',
errorMessage:
'Dispatch failed: GitHub token or active app installation required for this repository (no_installation_found)',
});
const config = await getStoredConfig();
const state = getCodeReviewActionRequiredState(config);
expect(config?.is_enabled).toBe(false);
expect(state?.reason).toBe('github_installation_required');
expect(state?.triggeringReviewId).toBe('review-2');
expect(state?.emailSentAt).toBeTruthy();
expect(JSON.stringify(config?.runtime_state)).not.toContain(testUser.google_user_email);
expect(mockSendCodeReviewDisabledEmail).toHaveBeenCalledTimes(1);
});
it('retries email when notification delivery fails', async () => {
const owner = { type: 'user' as const, id: testUser.id, userId: testUser.id };
mockSendCodeReviewDisabledEmail.mockResolvedValueOnce({ sent: false });
await disableCodeReviewForActionRequiredFailure({
owner,
platform: 'github',
reviewId: 'review-1',
reason: 'github_installation_required',
errorMessage:
'GitHub token or active app installation required for this repository (no_installation_found)',
});
let state = getCodeReviewActionRequiredState(await getStoredConfig());
expect(state?.emailSentAt).toBeUndefined();
mockSendCodeReviewDisabledEmail.mockResolvedValueOnce({ sent: true });
await disableCodeReviewForActionRequiredFailure({
owner,
platform: 'github',
reviewId: 'review-2',
reason: 'github_installation_required',
errorMessage:
'Dispatch failed: GitHub token or active app installation required for this repository (no_installation_found)',
});
state = getCodeReviewActionRequiredState(await getStoredConfig());
expect(state?.emailSentAt).toBeTruthy();
expect(mockSendCodeReviewDisabledEmail).toHaveBeenCalledTimes(2);
});
it('sends a new email when the action-required reason changes', async () => {
const owner = { type: 'user' as const, id: testUser.id, userId: testUser.id };
await disableCodeReviewForActionRequiredFailure({
owner,
platform: 'github',
reason: 'github_installation_required',
errorMessage:
'GitHub token or active app installation required for this repository (no_installation_found)',
});
await disableCodeReviewForActionRequiredFailure({
owner,
platform: 'github',
reason: 'github_ip_allow_list',
errorMessage:
'Although you appear to have the correct authorization credentials, the `acme` organization has an IP allow list enabled, and 192.0.2.1 is not permitted.',
});
const state = getCodeReviewActionRequiredState(await getStoredConfig());
expect(state?.reason).toBe('github_ip_allow_list');
expect(mockSendCodeReviewDisabledEmail).toHaveBeenCalledTimes(2);
});
});