Personal Telegram developer console for GitHub

feat/studio-g0-foundation
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
js 338 lines 9.9 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { GitHubService } from '../src/github.js';
4
5 async function withFetch(mock, fn) {
6 const originalFetch = globalThis.fetch;
7 globalThis.fetch = mock;
8 try {
9 return await fn();
10 } finally {
11 globalThis.fetch = originalFetch;
12 }
13 }
14
15 function json(data, status = 200) {
16 return new Response(JSON.stringify(data), {
17 status,
18 headers: { 'content-type': 'application/json' }
19 });
20 }
21
22 const repo = {
23 id: 123,
24 name: 'nexa-dev-bot',
25 full_name: 'davoodmehraban89/nexa-dev-bot',
26 default_branch: 'main',
27 private: true
28 };
29
30 const repoUrl = 'https://api.github.com/repos/davoodmehraban89/nexa-dev-bot';
31
32 test('GitHub Advanced reads expose job detail and bounded raw job logs', async () => {
33 const calls = [];
34 await withFetch(
35 async (url) => {
36 const value = String(url);
37 calls.push(value);
38 if (value === 'https://api.github.com/repositories/123')
39 return json(repo);
40 if (value === `${repoUrl}/actions/jobs/88`)
41 return json({
42 id: 88,
43 name: 'test',
44 status: 'completed',
45 conclusion: 'failure',
46 steps: []
47 });
48 if (value === `${repoUrl}/actions/jobs/88/logs`)
49 return new Response('0123456789ABCDEFGHIJ', { status: 200 });
50 return json({ message: 'Not Found' }, 404);
51 },
52 async () => {
53 const service = new GitHubService('token');
54 const job = await service.getWorkflowJob(123, 88);
55 assert.equal(job.id, 88);
56 const log = await service.getWorkflowJobLog(123, 88, { maxChars: 10 });
57 assert.deepEqual(log, { text: 'ABCDEFGHIJ', truncated: true });
58 assert.ok(calls.includes(`${repoUrl}/actions/jobs/88/logs`));
59 }
60 );
61 });
62
63 test('GitHub Advanced reads list bounded workflow artifacts and branches', async () => {
64 const calls = [];
65 await withFetch(
66 async (url) => {
67 const value = String(url);
68 calls.push(value);
69 if (value === 'https://api.github.com/repositories/123')
70 return json(repo);
71 if (value === `${repoUrl}/actions/runs/77/artifacts?per_page=20&page=100`)
72 return json({
73 artifacts: [
74 {
75 id: 9,
76 name: 'coverage',
77 size_in_bytes: 1234,
78 expired: false,
79 created_at: '2026-10-03T00:00:00Z',
80 expires_at: '2026-10-10T00:00:00Z',
81 archive_download_url: 'https://api.github.com/a'
82 }
83 ]
84 });
85 if (value === `${repoUrl}/branches?per_page=20&page=100`)
86 return json([
87 {
88 name: 'feature/x',
89 commit: { sha: 'a'.repeat(40) },
90 protected: false
91 }
92 ]);
93 return json({ message: 'Not Found' }, 404);
94 },
95 async () => {
96 const service = new GitHubService('token');
97 const artifacts = await service.listWorkflowArtifacts(123, 77, {
98 perPage: 999,
99 page: 999
100 });
101 assert.deepEqual(artifacts[0], {
102 id: 9,
103 name: 'coverage',
104 size: 1234,
105 expired: false,
106 createdAt: '2026-10-03T00:00:00Z',
107 expiresAt: '2026-10-10T00:00:00Z',
108 downloadUrl: 'https://api.github.com/a'
109 });
110 const branches = await service.listBranches(123, {
111 perPage: 999,
112 page: 999
113 });
114 assert.deepEqual(branches[0], {
115 name: 'feature/x',
116 sha: 'a'.repeat(40),
117 protected: false
118 });
119 }
120 );
121 });
122
123 test('GitHub compare validates a 40-hex head and returns bounded file summaries', async () => {
124 const head = 'b'.repeat(40);
125 await withFetch(
126 async (url) => {
127 const value = String(url);
128 if (value === 'https://api.github.com/repositories/123')
129 return json(repo);
130 if (value === `${repoUrl}/compare/main...${head}`)
131 return json({
132 status: 'ahead',
133 ahead_by: 2,
134 behind_by: 0,
135 total_commits: 2,
136 base_commit: { sha: 'a'.repeat(40) },
137 merge_base_commit: { sha: 'c'.repeat(40) },
138 files: [
139 {
140 filename: 'src/x.js',
141 status: 'modified',
142 additions: 3,
143 deletions: 1,
144 changes: 4,
145 patch: '+'.repeat(2000)
146 }
147 ]
148 });
149 return json({ message: 'Not Found' }, 404);
150 },
151 async () => {
152 const service = new GitHubService('token');
153 await assert.rejects(
154 () => service.compareWithDefault(123, 'not-a-sha'),
155 /SHA/i
156 );
157 const result = await service.compareWithDefault(123, head);
158 assert.equal(result.base, 'main');
159 assert.equal(result.head, head);
160 assert.equal(result.aheadBy, 2);
161 assert.ok(result.files[0].patch.length <= 1200);
162 }
163 );
164 });
165
166 test('GitHub PR detail exposes files and checks with bounded pagination', async () => {
167 const sha = 'd'.repeat(40);
168 await withFetch(
169 async (url) => {
170 const value = String(url);
171 if (value === 'https://api.github.com/repositories/123')
172 return json(repo);
173 if (value === `${repoUrl}/pulls/18`)
174 return json({
175 number: 18,
176 title: 'feat',
177 state: 'open',
178 merged: false,
179 user: { login: 'dev' },
180 body: 'body',
181 html_url: 'https://github.com/x/pull/18',
182 head: { ref: 'feat/x', sha },
183 base: { ref: 'main' },
184 additions: 10,
185 deletions: 2,
186 changed_files: 1
187 });
188 if (value === `${repoUrl}/pulls/18/files?per_page=20&page=100`)
189 return json([
190 {
191 filename: 'src/x.js',
192 status: 'modified',
193 additions: 10,
194 deletions: 2,
195 changes: 12,
196 patch: '+'.repeat(2000)
197 }
198 ]);
199 if (value === `${repoUrl}/commits/${sha}/check-runs?per_page=20&page=100`)
200 return json({
201 check_runs: [
202 {
203 id: 7,
204 name: 'CI',
205 status: 'completed',
206 conclusion: 'success',
207 html_url: 'https://github.com/check/7'
208 }
209 ]
210 });
211 return json({ message: 'Not Found' }, 404);
212 },
213 async () => {
214 const service = new GitHubService('token');
215 const pr = await service.getPullRequest(123, 18);
216 assert.equal(pr.headSha, sha);
217 const files = await service.listPullRequestFiles(123, 18, {
218 perPage: 999,
219 page: 999
220 });
221 assert.ok(files[0].patch.length <= 1200);
222 const checks = await service.listCommitChecks(123, sha, {
223 perPage: 999,
224 page: 999
225 });
226 assert.deepEqual(checks[0], {
227 id: 7,
228 name: 'CI',
229 status: 'completed',
230 conclusion: 'success',
231 url: 'https://github.com/check/7'
232 });
233 }
234 );
235 });
236
237 test('GitHub issues exclude pull requests and releases are source-backed', async () => {
238 await withFetch(
239 async (url) => {
240 const value = String(url);
241 if (value === 'https://api.github.com/repositories/123')
242 return json(repo);
243 if (value === `${repoUrl}/issues?state=all&per_page=10&page=2`)
244 return json([
245 {
246 number: 4,
247 title: 'bug',
248 state: 'open',
249 user: { login: 'u' },
250 comments: 2,
251 html_url: 'https://github.com/i/4'
252 },
253 { number: 5, title: 'pr', pull_request: {}, state: 'open' }
254 ]);
255 if (value === `${repoUrl}/releases?per_page=10&page=2`)
256 return json([
257 {
258 id: 6,
259 tag_name: 'v1.2.0',
260 name: 'Release 1.2',
261 draft: false,
262 prerelease: false,
263 published_at: '2026-10-03T00:00:00Z',
264 html_url: 'https://github.com/r/6'
265 }
266 ]);
267 return json({ message: 'Not Found' }, 404);
268 },
269 async () => {
270 const service = new GitHubService('token');
271 const issues = await service.listIssues(123, { page: 2 });
272 assert.equal(issues.length, 1);
273 assert.equal(issues[0].number, 4);
274 const releases = await service.listReleases(123, { page: 2 });
275 assert.equal(releases[0].tag, 'v1.2.0');
276 }
277 );
278 });
279
280 test('GitHub commit detail is source-backed and returns bounded file summaries', async () => {
281 const sha = 'e'.repeat(40);
282 await withFetch(
283 async (url) => {
284 const value = String(url);
285 if (value === 'https://api.github.com/repositories/123')
286 return json(repo);
287 if (value === `${repoUrl}/commits?per_page=5`)
288 return json([
289 {
290 sha,
291 commit: {
292 message: 'subject\n\nbody',
293 author: { name: 'Dev', date: '2026-10-04T12:00:00Z' }
294 },
295 author: { login: 'dev' }
296 }
297 ]);
298 if (value === `${repoUrl}/commits/${sha}`)
299 return json({
300 sha,
301 commit: {
302 message: 'subject\n\nbody',
303 author: { name: 'Dev', date: '2026-10-04T12:00:00Z' }
304 },
305 author: { login: 'dev' },
306 stats: { additions: 12, deletions: 4 },
307 files: [
308 {
309 filename: 'src/menu.js',
310 status: 'modified',
311 additions: 8,
312 deletions: 2,
313 changes: 10
314 }
315 ]
316 });
317 return json({ message: 'Not Found' }, 404);
318 },
319 async () => {
320 const service = new GitHubService('token');
321 const list = await service.listCommits(123);
322 assert.equal(list[0].sha, sha.slice(0, 7));
323 assert.equal(list[0].fullSha, sha);
324 const detail = await service.getCommit(123, sha);
325 assert.equal(detail.sha, sha);
326 assert.equal(detail.message, 'subject\n\nbody');
327 assert.equal(detail.additions, 12);
328 assert.equal(detail.deletions, 4);
329 assert.deepEqual(detail.files[0], {
330 filename: 'src/menu.js',
331 status: 'modified',
332 additions: 8,
333 deletions: 2,
334 changes: 10
335 });
336 }
337 );
338 });