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 341 lines 9.7 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { GitHubService } from '../src/github.js';
4
5 const REPO = 123;
6 const HEAD = 'a'.repeat(40);
7 const TREE = 'b'.repeat(40);
8 const BLOB1 = 'c'.repeat(40);
9 const BLOB2 = 'd'.repeat(40);
10 const COMMIT = 'e'.repeat(40);
11 const TIMESTAMP = '2026-10-03T09:00:00.000Z';
12
13 function jsonBody(options = {}) {
14 return options.body ? JSON.parse(options.body) : null;
15 }
16
17 function withFetch(handler, run) {
18 const original = globalThis.fetch;
19 globalThis.fetch = handler;
20 return Promise.resolve()
21 .then(run)
22 .finally(() => {
23 globalThis.fetch = original;
24 });
25 }
26
27 function repoResponse(url) {
28 if (String(url).endsWith('/repositories/123')) {
29 return Response.json({
30 id: REPO,
31 name: 'demo',
32 full_name: 'davood/demo',
33 default_branch: 'main'
34 });
35 }
36 return null;
37 }
38
39 test('getGitRef encodes slash branch names and only 404 may become an allowed missing ref', async () => {
40 const seen = [];
41 await withFetch(
42 async (url, options = {}) => {
43 const repo = repoResponse(url);
44 if (repo) return repo;
45 seen.push([String(url), options.method || 'GET']);
46 if (String(url).endsWith('/git/ref/heads/feature/mobile')) {
47 return Response.json({
48 ref: 'refs/heads/feature/mobile',
49 object: { type: 'commit', sha: HEAD }
50 });
51 }
52 return new Response(JSON.stringify({ message: 'Not Found' }), {
53 status: 404,
54 headers: { 'content-type': 'application/json' }
55 });
56 },
57 async () => {
58 const github = new GitHubService('token', 'davood');
59 assert.deepEqual(await github.getGitRef(REPO, 'feature/mobile'), {
60 branch: 'feature/mobile',
61 sha: HEAD
62 });
63 assert.equal(
64 await github.getGitRef(REPO, 'missing/branch', { allowMissing: true }),
65 null
66 );
67 await assert.rejects(
68 () => github.getGitRef(REPO, 'missing/branch'),
69 /404/
70 );
71 }
72 );
73 assert.ok(
74 seen.some(
75 ([url, method]) =>
76 url.endsWith('/git/ref/heads/feature/mobile') && method === 'GET'
77 )
78 );
79 });
80
81 test('getGitRef still propagates non-404 provider errors and validates returned full SHA', async () => {
82 await withFetch(
83 async (url) => {
84 const repo = repoResponse(url);
85 if (repo) return repo;
86 return new Response(JSON.stringify({ message: 'Forbidden' }), {
87 status: 403,
88 headers: { 'content-type': 'application/json' }
89 });
90 },
91 async () => {
92 const github = new GitHubService('token', 'davood');
93 await assert.rejects(
94 () => github.getGitRef(REPO, 'main', { allowMissing: true }),
95 /403 Forbidden/
96 );
97 }
98 );
99
100 await withFetch(
101 async (url) => {
102 const repo = repoResponse(url);
103 if (repo) return repo;
104 return Response.json({
105 ref: 'refs/heads/main',
106 object: { sha: 'short' }
107 });
108 },
109 async () => {
110 const github = new GitHubService('token', 'davood');
111 await assert.rejects(() => github.getGitRef(REPO, 'main'), /SHA/);
112 }
113 );
114 });
115
116 test('Git Data primitives send exact blob tree commit and branch-ref requests', async () => {
117 const requests = [];
118 await withFetch(
119 async (url, options = {}) => {
120 const repo = repoResponse(url);
121 if (repo) return repo;
122 requests.push({
123 url: String(url),
124 method: options.method || 'GET',
125 body: jsonBody(options)
126 });
127 const value = String(url);
128 if (value.endsWith('/git/blobs'))
129 return Response.json({ sha: BLOB1 }, { status: 201 });
130 if (value.endsWith('/git/trees'))
131 return Response.json({ sha: TREE }, { status: 201 });
132 if (value.endsWith('/git/commits'))
133 return Response.json({ sha: COMMIT }, { status: 201 });
134 if (value.endsWith('/git/refs'))
135 return Response.json(
136 { ref: 'refs/heads/nexa/edit-W123456789', object: { sha: COMMIT } },
137 { status: 201 }
138 );
139 return new Response('unexpected', { status: 500 });
140 },
141 async () => {
142 const github = new GitHubService('token', 'davood');
143 assert.deepEqual(await github.createGitBlob(REPO, 'hello\n'), {
144 sha: BLOB1
145 });
146 assert.deepEqual(
147 await github.createGitTree(REPO, {
148 baseTreeSha: HEAD,
149 entries: [
150 { path: 'src/app.js', mode: '100644', type: 'blob', sha: BLOB1 },
151 { path: 'src/other.js', mode: '100644', type: 'blob', sha: BLOB2 }
152 ]
153 }),
154 { sha: TREE }
155 );
156 assert.deepEqual(
157 await github.createGitCommit(REPO, {
158 message: 'edit app',
159 treeSha: TREE,
160 parentSha: HEAD,
161 timestamp: TIMESTAMP
162 }),
163 { sha: COMMIT }
164 );
165 assert.deepEqual(
166 await github.createGitBranchRef(REPO, {
167 branch: 'nexa/edit-W123456789',
168 sha: COMMIT
169 }),
170 { ref: 'refs/heads/nexa/edit-W123456789', sha: COMMIT }
171 );
172 }
173 );
174
175 assert.deepEqual(requests, [
176 {
177 url: 'https://api.github.com/repos/davood/demo/git/blobs',
178 method: 'POST',
179 body: { content: 'hello\n', encoding: 'utf-8' }
180 },
181 {
182 url: 'https://api.github.com/repos/davood/demo/git/trees',
183 method: 'POST',
184 body: {
185 base_tree: HEAD,
186 tree: [
187 { path: 'src/app.js', mode: '100644', type: 'blob', sha: BLOB1 },
188 { path: 'src/other.js', mode: '100644', type: 'blob', sha: BLOB2 }
189 ]
190 }
191 },
192 {
193 url: 'https://api.github.com/repos/davood/demo/git/commits',
194 method: 'POST',
195 body: {
196 message: 'edit app',
197 tree: TREE,
198 parents: [HEAD],
199 author: {
200 name: 'Nexa Dev Bot',
201 email: 'nexa-dev-bot@users.noreply.github.com',
202 date: TIMESTAMP
203 },
204 committer: {
205 name: 'Nexa Dev Bot',
206 email: 'nexa-dev-bot@users.noreply.github.com',
207 date: TIMESTAMP
208 }
209 }
210 },
211 {
212 url: 'https://api.github.com/repos/davood/demo/git/refs',
213 method: 'POST',
214 body: { ref: 'refs/heads/nexa/edit-W123456789', sha: COMMIT }
215 }
216 ]);
217 });
218
219 test('Git Data primitives validate all full SHAs and preserve deterministic retry bodies', async () => {
220 const commitBodies = [];
221 await withFetch(
222 async (url, options = {}) => {
223 const repo = repoResponse(url);
224 if (repo) return repo;
225 if (String(url).endsWith('/git/commits')) {
226 commitBodies.push(jsonBody(options));
227 return Response.json({ sha: COMMIT }, { status: 201 });
228 }
229 return Response.json({ sha: TREE }, { status: 201 });
230 },
231 async () => {
232 const github = new GitHubService('token', 'davood');
233 await assert.rejects(
234 () => github.createGitTree(REPO, { baseTreeSha: 'short', entries: [] }),
235 /SHA/
236 );
237 await assert.rejects(
238 () =>
239 github.createGitTree(REPO, {
240 baseTreeSha: TREE,
241 entries: [
242 { path: 'a.js', mode: '100644', type: 'blob', sha: 'short' }
243 ]
244 }),
245 /SHA/
246 );
247 await assert.rejects(
248 () =>
249 github.createGitCommit(REPO, {
250 message: 'x',
251 treeSha: 'short',
252 parentSha: HEAD,
253 timestamp: TIMESTAMP
254 }),
255 /SHA/
256 );
257 await assert.rejects(
258 () =>
259 github.createGitCommit(REPO, {
260 message: 'x',
261 treeSha: TREE,
262 parentSha: 'short',
263 timestamp: TIMESTAMP
264 }),
265 /SHA/
266 );
267 await assert.rejects(
268 () =>
269 github.createGitBranchRef(REPO, {
270 branch: 'nexa/edit-W123456789',
271 sha: 'short'
272 }),
273 /SHA/
274 );
275
276 const input = {
277 message: 'same material',
278 treeSha: TREE,
279 parentSha: HEAD,
280 timestamp: TIMESTAMP
281 };
282 await github.createGitCommit(REPO, input);
283 await github.createGitCommit(REPO, input);
284 }
285 );
286 assert.equal(commitBodies.length, 2);
287 assert.deepEqual(commitBodies[0], commitBodies[1]);
288 });
289
290 test('provider 409 and 422 remain explicit and an existing different target ref never triggers an update or force endpoint', async () => {
291 for (const status of [409, 422]) {
292 await withFetch(
293 async (url, options = {}) => {
294 const repo = repoResponse(url);
295 if (repo) return repo;
296 return new Response(JSON.stringify({ message: 'Reference conflict' }), {
297 status,
298 headers: { 'content-type': 'application/json' }
299 });
300 },
301 async () => {
302 const github = new GitHubService('token', 'davood');
303 await assert.rejects(
304 () =>
305 github.createGitBranchRef(REPO, {
306 branch: 'nexa/edit-W123456789',
307 sha: COMMIT
308 }),
309 new RegExp(String(status))
310 );
311 }
312 );
313 }
314
315 const calls = [];
316 await withFetch(
317 async (url, options = {}) => {
318 const repo = repoResponse(url);
319 if (repo) return repo;
320 calls.push({ url: String(url), method: options.method || 'GET' });
321 if (String(url).endsWith('/git/ref/heads/nexa/edit-W123456789')) {
322 return Response.json({ object: { sha: HEAD } });
323 }
324 return new Response('unexpected', { status: 500 });
325 },
326 async () => {
327 const github = new GitHubService('token', 'davood');
328 const existing = await github.getGitRef(REPO, 'nexa/edit-W123456789', {
329 allowMissing: true
330 });
331 assert.deepEqual(existing, { branch: 'nexa/edit-W123456789', sha: HEAD });
332 assert.notEqual(existing.sha, COMMIT);
333 }
334 );
335 assert.deepEqual(calls, [
336 {
337 url: 'https://api.github.com/repos/davood/demo/git/ref/heads/nexa/edit-W123456789',
338 method: 'GET'
339 }
340 ]);
341 });