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 261 lines 7.85 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 function repositoryContext(id = 123) {
23 return {
24 id,
25 name: 'nexa-dev-bot',
26 full_name: 'davoodmehraban89/nexa-dev-bot',
27 default_branch: 'main',
28 private: true
29 };
30 }
31
32 test('GitHubService fails closed when GITHUB_TOKEN is missing', async () => {
33 const service = new GitHubService('', 'davoodmehraban89');
34 await assert.rejects(() => service.listRepositories(), /GITHUB_TOKEN/i);
35 });
36
37 test('GitHubService surfaces GitHub API failures instead of returning sample data', async () => {
38 await withFetch(
39 async () => json({ message: 'Bad credentials' }, 401),
40 async () => {
41 const service = new GitHubService('bad-token', 'davoodmehraban89');
42 await assert.rejects(
43 () => service.listPullRequests('Factor-easy'),
44 /401|Bad credentials/i
45 );
46 }
47 );
48 });
49
50 test('GitHubService repository listing uses the authenticated user endpoint', async () => {
51 let requestedUrl = '';
52 await withFetch(
53 async (url) => {
54 requestedUrl = String(url);
55 if (requestedUrl.startsWith('https://api.github.com/user/repos?')) {
56 return json([
57 {
58 name: 'nexa-dev-bot',
59 full_name: 'davoodmehraban89/nexa-dev-bot',
60 private: true,
61 stargazers_count: 0,
62 forks_count: 0,
63 description: 'Personal Telegram developer console for GitHub'
64 }
65 ]);
66 }
67 return json({ message: 'Not Found' }, 404);
68 },
69 async () => {
70 const service = new GitHubService('token', 'davoodmehraban89');
71 const repos = await service.listRepositories();
72 assert.match(requestedUrl, /\/user\/repos\?/);
73 assert.equal(repos.length, 1);
74 assert.equal(repos[0].name, 'nexa-dev-bot');
75 assert.equal(repos[0].private, true);
76 }
77 );
78 });
79
80 test('GitHubService lists and maps workflow runs for a numeric repository id', async () => {
81 const calls = [];
82 await withFetch(
83 async (url, options = {}) => {
84 const value = String(url);
85 calls.push({ url: value, method: options.method || 'GET' });
86 if (value === 'https://api.github.com/repositories/123')
87 return json(repositoryContext());
88 if (
89 value ===
90 'https://api.github.com/repos/davoodmehraban89/nexa-dev-bot/actions/runs?per_page=10'
91 ) {
92 return json({
93 workflow_runs: [
94 {
95 id: 77,
96 name: 'CI',
97 status: 'completed',
98 conclusion: 'failure',
99 event: 'push',
100 head_branch: 'main',
101 head_sha: 'abcdef0123456789',
102 run_number: 12,
103 html_url: 'https://github.com/x/actions/runs/77',
104 created_at: '2026-10-03T00:00:00Z',
105 updated_at: '2026-10-03T00:01:00Z'
106 }
107 ]
108 });
109 }
110 return json({ message: 'Not Found' }, 404);
111 },
112 async () => {
113 const service = new GitHubService('token');
114 const runs = await service.listWorkflowRuns(123);
115 assert.equal(runs.length, 1);
116 assert.deepEqual(runs[0], {
117 id: 77,
118 name: 'CI',
119 status: 'completed',
120 conclusion: 'failure',
121 event: 'push',
122 branch: 'main',
123 sha: 'abcdef0',
124 runNumber: 12,
125 url: 'https://github.com/x/actions/runs/77',
126 createdAt: '2026-10-03T00:00:00Z',
127 updatedAt: '2026-10-03T00:01:00Z'
128 });
129 assert.equal(calls.at(-1).method, 'GET');
130 }
131 );
132 });
133
134 test('GitHubService gets workflow run and lists jobs with bounded step details', async () => {
135 await withFetch(
136 async (url) => {
137 const value = String(url);
138 if (value === 'https://api.github.com/repositories/123')
139 return json(repositoryContext());
140 if (value.endsWith('/actions/runs/77')) {
141 return json({
142 id: 77,
143 name: 'CI',
144 status: 'completed',
145 conclusion: 'failure',
146 event: 'push',
147 head_branch: 'main',
148 head_sha: 'abcdef0123456789',
149 run_number: 12,
150 html_url: 'https://github.com/x/actions/runs/77',
151 created_at: '2026-10-03T00:00:00Z',
152 updated_at: '2026-10-03T00:01:00Z'
153 });
154 }
155 if (value.endsWith('/actions/runs/77/jobs?per_page=20')) {
156 return json({
157 jobs: [
158 {
159 id: 88,
160 name: 'test',
161 status: 'completed',
162 conclusion: 'failure',
163 started_at: '2026-10-03T00:00:10Z',
164 completed_at: '2026-10-03T00:00:50Z',
165 html_url: 'https://github.com/x/actions/runs/77/job/88',
166 steps: [
167 {
168 number: 1,
169 name: 'checkout',
170 status: 'completed',
171 conclusion: 'success'
172 },
173 {
174 number: 2,
175 name: 'test',
176 status: 'completed',
177 conclusion: 'failure'
178 }
179 ]
180 }
181 ]
182 });
183 }
184 return json({ message: 'Not Found' }, 404);
185 },
186 async () => {
187 const service = new GitHubService('token');
188 const run = await service.getWorkflowRun(123, 77);
189 assert.equal(run.id, 77);
190 assert.equal(run.sha, 'abcdef0');
191 const jobs = await service.listWorkflowJobs(123, 77);
192 assert.equal(jobs.length, 1);
193 assert.equal(jobs[0].id, 88);
194 assert.equal(jobs[0].steps[1].conclusion, 'failure');
195 }
196 );
197 });
198
199 test('GitHubService uses POST endpoints for rerun failed and cancel and accepts empty success bodies', async () => {
200 const calls = [];
201 await withFetch(
202 async (url, options = {}) => {
203 const value = String(url);
204 calls.push({
205 url: value,
206 method: options.method || 'GET',
207 headers: options.headers
208 });
209 if (value === 'https://api.github.com/repositories/123')
210 return json(repositoryContext());
211 if (value.endsWith('/actions/runs/77/rerun-failed-jobs'))
212 return new Response(null, { status: 201 });
213 if (value.endsWith('/actions/runs/77/cancel'))
214 return new Response(null, { status: 202 });
215 return json({ message: 'Not Found' }, 404);
216 },
217 async () => {
218 const service = new GitHubService('token');
219 assert.deepEqual(await service.rerunFailedWorkflowRun(123, 77), {
220 accepted: true,
221 status: 201
222 });
223 assert.deepEqual(await service.cancelWorkflowRun(123, 77), {
224 accepted: true,
225 status: 202
226 });
227 assert.equal(
228 calls.find((call) => call.url.endsWith('/rerun-failed-jobs')).method,
229 'POST'
230 );
231 assert.equal(
232 calls.find((call) => call.url.endsWith('/cancel')).method,
233 'POST'
234 );
235 }
236 );
237 });
238
239 test('GitHubService write actions surface provider permission/state errors', async () => {
240 for (const [status, message] of [
241 [403, 'Resource not accessible by personal access token'],
242 [409, 'Conflict'],
243 [422, 'Validation Failed']
244 ]) {
245 await withFetch(
246 async (url) => {
247 const value = String(url);
248 if (value === 'https://api.github.com/repositories/123')
249 return json(repositoryContext());
250 return json({ message }, status);
251 },
252 async () => {
253 const service = new GitHubService('token');
254 await assert.rejects(
255 () => service.rerunFailedWorkflowRun(123, 77),
256 new RegExp(`${status}|${message}`, 'i')
257 );
258 }
259 );
260 }
261 });