Personal Telegram developer console for GitHub

phase2-20261005
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 196 lines 6.96 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(() => service.listPullRequests('Factor-easy'), /401|Bad credentials/i);
43 }
44 );
45 });
46
47 test('GitHubService repository listing uses the authenticated user endpoint', async () => {
48 let requestedUrl = '';
49 await withFetch(async (url) => {
50 requestedUrl = String(url);
51 if (requestedUrl.startsWith('https://api.github.com/user/repos?')) {
52 return json([
53 {
54 name: 'nexa-dev-bot',
55 full_name: 'davoodmehraban89/nexa-dev-bot',
56 private: true,
57 stargazers_count: 0,
58 forks_count: 0,
59 description: 'Personal Telegram developer console for GitHub'
60 }
61 ]);
62 }
63 return json({ message: 'Not Found' }, 404);
64 }, async () => {
65 const service = new GitHubService('token', 'davoodmehraban89');
66 const repos = await service.listRepositories();
67 assert.match(requestedUrl, /\/user\/repos\?/);
68 assert.equal(repos.length, 1);
69 assert.equal(repos[0].name, 'nexa-dev-bot');
70 assert.equal(repos[0].private, true);
71 });
72 });
73
74 test('GitHubService lists and maps workflow runs for a numeric repository id', async () => {
75 const calls = [];
76 await withFetch(async (url, options = {}) => {
77 const value = String(url);
78 calls.push({ url: value, method: options.method || 'GET' });
79 if (value === 'https://api.github.com/repositories/123') return json(repositoryContext());
80 if (value === 'https://api.github.com/repos/davoodmehraban89/nexa-dev-bot/actions/runs?per_page=10') {
81 return json({ workflow_runs: [{
82 id: 77,
83 name: 'CI',
84 status: 'completed',
85 conclusion: 'failure',
86 event: 'push',
87 head_branch: 'main',
88 head_sha: 'abcdef0123456789',
89 run_number: 12,
90 html_url: 'https://github.com/x/actions/runs/77',
91 created_at: '2026-10-03T00:00:00Z',
92 updated_at: '2026-10-03T00:01:00Z'
93 }] });
94 }
95 return json({ message: 'Not Found' }, 404);
96 }, async () => {
97 const service = new GitHubService('token');
98 const runs = await service.listWorkflowRuns(123);
99 assert.equal(runs.length, 1);
100 assert.deepEqual(runs[0], {
101 id: 77,
102 name: 'CI',
103 status: 'completed',
104 conclusion: 'failure',
105 event: 'push',
106 branch: 'main',
107 sha: 'abcdef0',
108 runNumber: 12,
109 url: 'https://github.com/x/actions/runs/77',
110 createdAt: '2026-10-03T00:00:00Z',
111 updatedAt: '2026-10-03T00:01:00Z'
112 });
113 assert.equal(calls.at(-1).method, 'GET');
114 });
115 });
116
117 test('GitHubService gets workflow run and lists jobs with bounded step details', async () => {
118 await withFetch(async (url) => {
119 const value = String(url);
120 if (value === 'https://api.github.com/repositories/123') return json(repositoryContext());
121 if (value.endsWith('/actions/runs/77')) {
122 return json({
123 id: 77,
124 name: 'CI',
125 status: 'completed',
126 conclusion: 'failure',
127 event: 'push',
128 head_branch: 'main',
129 head_sha: 'abcdef0123456789',
130 run_number: 12,
131 html_url: 'https://github.com/x/actions/runs/77',
132 created_at: '2026-10-03T00:00:00Z',
133 updated_at: '2026-10-03T00:01:00Z'
134 });
135 }
136 if (value.endsWith('/actions/runs/77/jobs?per_page=20')) {
137 return json({ jobs: [{
138 id: 88,
139 name: 'test',
140 status: 'completed',
141 conclusion: 'failure',
142 started_at: '2026-10-03T00:00:10Z',
143 completed_at: '2026-10-03T00:00:50Z',
144 html_url: 'https://github.com/x/actions/runs/77/job/88',
145 steps: [
146 { number: 1, name: 'checkout', status: 'completed', conclusion: 'success' },
147 { number: 2, name: 'test', status: 'completed', conclusion: 'failure' }
148 ]
149 }] });
150 }
151 return json({ message: 'Not Found' }, 404);
152 }, async () => {
153 const service = new GitHubService('token');
154 const run = await service.getWorkflowRun(123, 77);
155 assert.equal(run.id, 77);
156 assert.equal(run.sha, 'abcdef0');
157 const jobs = await service.listWorkflowJobs(123, 77);
158 assert.equal(jobs.length, 1);
159 assert.equal(jobs[0].id, 88);
160 assert.equal(jobs[0].steps[1].conclusion, 'failure');
161 });
162 });
163
164 test('GitHubService uses POST endpoints for rerun failed and cancel and accepts empty success bodies', async () => {
165 const calls = [];
166 await withFetch(async (url, options = {}) => {
167 const value = String(url);
168 calls.push({ url: value, method: options.method || 'GET', headers: options.headers });
169 if (value === 'https://api.github.com/repositories/123') return json(repositoryContext());
170 if (value.endsWith('/actions/runs/77/rerun-failed-jobs')) return new Response(null, { status: 201 });
171 if (value.endsWith('/actions/runs/77/cancel')) return new Response(null, { status: 202 });
172 return json({ message: 'Not Found' }, 404);
173 }, async () => {
174 const service = new GitHubService('token');
175 assert.deepEqual(await service.rerunFailedWorkflowRun(123, 77), { accepted: true, status: 201 });
176 assert.deepEqual(await service.cancelWorkflowRun(123, 77), { accepted: true, status: 202 });
177 assert.equal(calls.find(call => call.url.endsWith('/rerun-failed-jobs')).method, 'POST');
178 assert.equal(calls.find(call => call.url.endsWith('/cancel')).method, 'POST');
179 });
180 });
181
182 test('GitHubService write actions surface provider permission/state errors', async () => {
183 for (const [status, message] of [[403, 'Resource not accessible by personal access token'], [409, 'Conflict'], [422, 'Validation Failed']]) {
184 await withFetch(async (url) => {
185 const value = String(url);
186 if (value === 'https://api.github.com/repositories/123') return json(repositoryContext());
187 return json({ message }, status);
188 }, async () => {
189 const service = new GitHubService('token');
190 await assert.rejects(
191 () => service.rerunFailedWorkflowRun(123, 77),
192 new RegExp(`${status}|${message}`, 'i')
193 );
194 });
195 }
196 });