forked from github/copilot-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-github-write-preview-apply.js
More file actions
303 lines (277 loc) · 11.4 KB
/
Copy pathtest-github-write-preview-apply.js
File metadata and controls
303 lines (277 loc) · 11.4 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env node
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'liku-github-write-'));
process.env.LIKU_HOME_OVERRIDE = path.join(tempRoot, '.liku');
process.env.LIKU_HOME_OLD_OVERRIDE = path.join(tempRoot, '.liku-cli-old');
const {
createGitHubCommandExecutor,
} = require(path.join(__dirname, '..', 'src', 'main', 'github', 'command-executor.js'));
const {
readGitHubWriteApplyResultArtifact,
readGitHubWriteApprovalArtifact,
readGitHubWriteEventLog,
readGitHubWritePreviewArtifact,
} = require(path.join(__dirname, '..', 'src', 'main', 'github', 'write-artifacts.js'));
let pass = 0;
async function test(name, fn) {
await fn();
pass += 1;
console.log(`PASS ${name}`);
}
function createHeaders(values = {}) {
return {
get(name) {
return values[String(name || '').toLowerCase()] || values[name] || null;
},
};
}
function createJsonResponse(status, payload, headers = {}) {
return {
ok: status >= 200 && status < 300,
status,
url: 'https://api.github.com/repos/example/project/issues/42/comments',
headers: createHeaders(headers),
async text() {
return JSON.stringify(payload);
},
};
}
(async () => {
try {
const executor = createGitHubCommandExecutor({
env: {
GH_TOKEN: 'github_pat_test_preview_apply_1234567890',
},
cwd: path.join(__dirname, '..'),
});
await test('draft preview writes reviewed sanitized artifacts and approval state', async () => {
const report = await executor.execute({
source: 'cli',
area: 'issues',
action: 'comment',
positionals: ['issues', 'comment', 'draft', '42'],
options: {
slug: 'example/project',
body: 'Authorization: Bearer ghp_secret_token_12345678901234567890\nThanks for the detailed repro.',
featureFlags: {
enableGitHub: true,
enableGitHubWrites: true,
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(report.success, true);
assert.strictEqual(report.schemaVersion, 'github.issue-comment-draft.v1');
assert.strictEqual(report.capability.key, 'issues.comment.draft');
assert.strictEqual(report.policy.allowed, true);
assert.strictEqual(report.policy.state, 'preview-allowed');
assert.ok(report.previewId);
assert.ok(report.review.reviewRequired);
assert.ok(report.approval.applyToken);
assert.ok(fs.existsSync(report.previewArtifact.filePath));
assert.ok(fs.existsSync(report.approvalArtifact.filePath));
const previewArtifact = readGitHubWritePreviewArtifact({ previewId: report.previewId });
const approvalArtifact = readGitHubWriteApprovalArtifact({ previewId: report.previewId });
const eventLog = readGitHubWriteEventLog({ previewId: report.previewId });
assert.strictEqual(previewArtifact.review.exportKind, 'github-write-preview');
assert.strictEqual(approvalArtifact.status, 'requested');
assert.ok(previewArtifact.input.body.includes('[redacted token]'));
assert.ok(!previewArtifact.input.body.includes('ghp_secret_token_12345678901234567890'));
assert.ok(Array.isArray(eventLog.events));
assert.ok(eventLog.events.some((entry) => entry.eventName === 'preview.created'));
assert.ok(eventLog.events.some((entry) => entry.eventName === 'approval.requested'));
});
await test('apply posts the comment once and replays the terminal result on duplicate apply', async () => {
let fetchCalls = 0;
const preview = await executor.execute({
source: 'cli',
area: 'issues',
action: 'comment',
positionals: ['issues', 'comment', 'draft', '42'],
options: {
slug: 'example/project',
body: 'Please verify against 0.0.16 before closing.',
featureFlags: {
enableGitHub: true,
enableGitHubWrites: true,
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
const applyReport = await executor.execute({
source: 'cli',
area: 'apply',
action: preview.previewId,
positionals: ['apply', preview.previewId],
options: {
approve: true,
approvalFile: preview.approvalArtifact.filePath,
featureFlags: {
enableGitHub: true,
enableGitHubWrites: true,
},
fetchImpl: async (_url, init) => {
fetchCalls += 1;
const body = JSON.parse(String(init.body || '{}'));
assert.strictEqual(body.body, 'Please verify against 0.0.16 before closing.');
return createJsonResponse(201, {
id: 9001,
node_id: 'IC_kwDOTest',
body: body.body,
html_url: 'https://github.com/example/project/issues/42#issuecomment-9001',
created_at: '2026-05-26T00:00:00.000Z',
updated_at: '2026-05-26T00:00:00.000Z',
user: {
login: 'octocat',
type: 'User',
html_url: 'https://github.com/octocat',
},
}, {
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4999',
'x-ratelimit-reset': '1767225600',
});
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(applyReport.success, true);
assert.strictEqual(applyReport.schemaVersion, 'github.write-apply.v1');
assert.strictEqual(applyReport.capability.key, 'github.apply');
assert.strictEqual(applyReport.policy.state, 'apply-allowed');
assert.strictEqual(applyReport.approval.status, 'applied');
assert.strictEqual(applyReport.result.comment.id, 9001);
assert.strictEqual(fetchCalls, 1);
const approvalArtifact = readGitHubWriteApprovalArtifact({ previewId: preview.previewId });
const resultArtifact = readGitHubWriteApplyResultArtifact({ previewId: preview.previewId });
const eventLog = readGitHubWriteEventLog({ previewId: preview.previewId });
assert.strictEqual(approvalArtifact.status, 'applied');
assert.strictEqual(resultArtifact.success, true);
assert.ok(eventLog.events.some((entry) => entry.eventName === 'approval.approved'));
assert.ok(eventLog.events.some((entry) => entry.eventName === 'apply.started'));
assert.ok(eventLog.events.some((entry) => entry.eventName === 'apply.succeeded'));
const replayReport = await executor.execute({
source: 'cli',
area: 'apply',
action: preview.previewId,
positionals: ['apply', preview.previewId],
options: {
approve: true,
approvalFile: preview.approvalArtifact.filePath,
featureFlags: {
enableGitHub: true,
enableGitHubWrites: true,
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(replayReport.success, true);
assert.strictEqual(replayReport.execution.status, 'replayed-terminal-result');
assert.strictEqual(replayReport.execution.alreadyApplied, true);
assert.strictEqual(replayReport.result.comment.id, 9001);
assert.strictEqual(fetchCalls, 1);
});
await test('apply rejects invalid tokens and expired previews', async () => {
const invalidPreview = await executor.execute({
source: 'cli',
area: 'issues',
action: 'comment',
positionals: ['issues', 'comment', 'draft', '42'],
options: {
slug: 'example/project',
body: 'Token validation proof.',
featureFlags: {
enableGitHub: true,
enableGitHubWrites: true,
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
const invalidApply = await executor.execute({
source: 'cli',
area: 'apply',
action: invalidPreview.previewId,
positionals: ['apply', invalidPreview.previewId],
options: {
approve: true,
'apply-token': 'ghwa_invalid',
featureFlags: {
enableGitHub: true,
enableGitHubWrites: true,
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(invalidApply.success, false);
assert.strictEqual(invalidApply.error, 'INVALID_APPLY_TOKEN');
const expiredPreview = await executor.execute({
source: 'cli',
area: 'issues',
action: 'comment',
positionals: ['issues', 'comment', 'draft', '42'],
options: {
slug: 'example/project',
body: 'Expiration proof.',
featureFlags: {
enableGitHub: true,
enableGitHubWrites: true,
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
const expiredPreviewArtifact = readGitHubWritePreviewArtifact({ previewId: expiredPreview.previewId });
const expiredApprovalArtifact = readGitHubWriteApprovalArtifact({ previewId: expiredPreview.previewId });
expiredPreviewArtifact.expiresAt = '2000-01-01T00:00:00.000Z';
expiredApprovalArtifact.expiresAt = '2000-01-01T00:00:00.000Z';
fs.writeFileSync(expiredPreviewArtifact.filePath, JSON.stringify(expiredPreviewArtifact, null, 2));
fs.writeFileSync(expiredApprovalArtifact.filePath, JSON.stringify(expiredApprovalArtifact, null, 2));
const expiredApply = await executor.execute({
source: 'cli',
area: 'apply',
action: expiredPreview.previewId,
positionals: ['apply', expiredPreview.previewId],
options: {
approve: true,
approvalFile: expiredPreview.approvalArtifact.filePath,
featureFlags: {
enableGitHub: true,
enableGitHubWrites: true,
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(expiredApply.success, false);
assert.strictEqual(expiredApply.error, 'EXPIRED_PREVIEW');
const updatedApproval = readGitHubWriteApprovalArtifact({ previewId: expiredPreview.previewId });
const eventLog = readGitHubWriteEventLog({ previewId: expiredPreview.previewId });
assert.strictEqual(updatedApproval.status, 'expired');
assert.ok(eventLog.events.some((entry) => entry.eventName === 'preview.expired'));
});
console.log(`PASS github write preview/apply (${pass} assertions)`);
} finally {
fs.rmSync(tempRoot, { recursive: true, force: true });
}
})().catch((error) => {
console.error('FAIL github write preview/apply');
console.error(error.stack || error.message);
process.exit(1);
});