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 368 lines 10.3 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import worker from '../src/index.js';
4 import {
5 makeReplayNamespace,
6 withUpdateId
7 } from './helpers/replay-namespace.js';
8
9 const env = {
10 APPROVAL_REPLAY_GUARD: makeReplayNamespace(),
11 TELEGRAM_WEBHOOK_SECRET: 'right',
12 OWNER_TELEGRAM_ID: '123',
13 TELEGRAM_BOT_TOKEN: 'telegram-test-token',
14 GITHUB_TOKEN: 'github-test-token',
15 GITHUB_OWNER: 'davoodmehraban89'
16 };
17
18 function webhook(update) {
19 return new Request('https://example.test/telegram/webhook', {
20 method: 'POST',
21 headers: {
22 'content-type': 'application/json',
23 'X-Telegram-Bot-Api-Secret-Token': 'right'
24 },
25 body: JSON.stringify(withUpdateId(update))
26 });
27 }
28 const json = (data, status = 200) =>
29 new Response(JSON.stringify(data), {
30 status,
31 headers: { 'content-type': 'application/json' }
32 });
33
34 function installApiMock() {
35 const telegramCalls = [];
36 const githubCalls = [];
37 const originalFetch = globalThis.fetch;
38 const folderSha = 'a'.repeat(40);
39 const fileSha = 'b'.repeat(40);
40 const nestedFileSha = 'c'.repeat(40);
41
42 globalThis.fetch = async (url, options = {}) => {
43 const value = String(url);
44 if (value.startsWith('https://api.telegram.org/')) {
45 const method = value.split('/').at(-1);
46 const body = options.body ? JSON.parse(options.body) : {};
47 telegramCalls.push({ method, body });
48 return json({
49 ok: true,
50 result: { message_id: body.message_id || 700, ...body }
51 });
52 }
53 if (!value.startsWith('https://api.github.com/'))
54 throw new Error(`Unexpected URL: ${value}`);
55 githubCalls.push(value);
56
57 if (value.startsWith('https://api.github.com/user/repos?'))
58 return json([
59 {
60 id: 1001,
61 name: 'demo',
62 full_name: 'davoodmehraban89/demo',
63 private: true,
64 default_branch: 'main',
65 stargazers_count: 0,
66 forks_count: 0,
67 description: 'Demo repository'
68 }
69 ]);
70 if (
71 value === 'https://api.github.com/repositories/1001' ||
72 value === 'https://api.github.com/repos/davoodmehraban89/demo'
73 )
74 return json({
75 id: 1001,
76 name: 'demo',
77 full_name: 'davoodmehraban89/demo',
78 private: true,
79 default_branch: 'main',
80 archived: false,
81 description: 'Demo repository',
82 html_url: 'https://github.com/davoodmehraban89/demo'
83 });
84 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/contents')
85 return json([
86 { name: 'src', path: 'src', type: 'dir', size: 0, sha: folderSha },
87 {
88 name: 'README.md',
89 path: 'README.md',
90 type: 'file',
91 size: 100,
92 sha: fileSha
93 }
94 ]);
95 if (
96 value ===
97 `https://api.github.com/repos/davoodmehraban89/demo/git/trees/${folderSha}`
98 )
99 return json({
100 tree: [{ path: 'index.js', type: 'blob', size: 42, sha: nestedFileSha }]
101 });
102 if (
103 value ===
104 `https://api.github.com/repos/davoodmehraban89/demo/git/blobs/${nestedFileSha}`
105 )
106 return json({
107 encoding: 'base64',
108 size: 21,
109 content: btoa('console.log("hello")\n')
110 });
111 if (
112 value ===
113 'https://api.github.com/repos/davoodmehraban89/demo/commits?per_page=5'
114 )
115 return json([
116 {
117 sha: '1234567890abcdef',
118 commit: {
119 message: 'fix mobile menu',
120 author: { name: 'Davood', date: '2026-10-02T10:00:00Z' }
121 },
122 author: { login: 'davoodmehraban89' }
123 }
124 ]);
125 if (
126 value ===
127 'https://api.github.com/repos/davoodmehraban89/demo/pulls?state=all&per_page=10'
128 )
129 return json([
130 {
131 number: 7,
132 title: 'Improve bot UI',
133 state: 'open',
134 merged_at: null,
135 created_at: '2026-10-02T10:00:00Z',
136 html_url: 'https://github.com/davoodmehraban89/demo/pull/7',
137 user: { login: 'davoodmehraban89' }
138 }
139 ]);
140 if (
141 value ===
142 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=3'
143 )
144 return json({
145 workflow_runs: [
146 {
147 id: 1,
148 name: 'CI',
149 status: 'completed',
150 conclusion: 'success',
151 head_branch: 'main',
152 head_sha: '1234567890abcdef1234567890abcdef12345678',
153 run_number: 1,
154 head_commit: { message: 'fix mobile menu' },
155 html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
156 }
157 ]
158 });
159 if (
160 value ===
161 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=10'
162 )
163 return json({
164 workflow_runs: [
165 {
166 id: 1,
167 name: 'CI',
168 status: 'completed',
169 conclusion: 'success',
170 head_branch: 'main',
171 head_sha: '1234567890abcdef1234567890abcdef12345678',
172 run_number: 1,
173 event: 'push',
174 head_commit: { message: 'fix mobile menu' },
175 html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
176 }
177 ]
178 });
179 return json({ message: 'Not Found' }, 404);
180 };
181
182 return {
183 telegramCalls,
184 githubCalls,
185 folderSha,
186 nestedFileSha,
187 restore: () => {
188 globalThis.fetch = originalFetch;
189 }
190 };
191 }
192
193 async function sendUpdate(update) {
194 const response = await worker.fetch(webhook(update), env);
195 assert.equal(response.status, 200);
196 return response.json();
197 }
198
199 test('Studio-first bootstrap keeps legacy inline navigation on one Telegram message', async () => {
200 const mock = installApiMock();
201 try {
202 await sendUpdate({
203 message: {
204 message_id: 1,
205 from: { id: 123 },
206 chat: { id: 123, type: 'private' },
207 text: '/start'
208 }
209 });
210 const startSends = mock.telegramCalls.filter(
211 (call) => call.method === 'sendMessage'
212 );
213 assert.equal(startSends.length, 2);
214 assert.equal(startSends[0].body.reply_markup.remove_keyboard, true);
215 assert.match(startSends[1].body.text, /Nexa Studio/);
216 assert.equal(
217 startSends[1].body.reply_markup.inline_keyboard[0][0].web_app.url,
218 'https://example.test/studio/'
219 );
220 assert.equal(
221 mock.telegramCalls.filter((call) => call.method === 'setChatMenuButton')
222 .length,
223 1
224 );
225
226 const message = { message_id: 700, chat: { id: 123, type: 'private' } };
227 for (const data of ['menu:repos', 'repo:1001', 'menu:repos', 'menu:main']) {
228 await sendUpdate({
229 callback_query: { id: data, from: { id: 123 }, data, message }
230 });
231 }
232 const edits = mock.telegramCalls.filter(
233 (call) => call.method === 'editMessageText'
234 );
235 assert.equal(edits.length, 4);
236 assert.ok(edits.every((call) => call.body.message_id === 700));
237 assert.match(edits[0].body.text, /فهرست مخزن‌ها/);
238 assert.match(edits[1].body.text, /پروژه: demo/);
239 assert.match(edits[3].body.text, /Nexa Studio/);
240 assert.equal(
241 mock.telegramCalls.filter((call) => call.method === 'sendMessage').length,
242 2,
243 'callbacks must not create extra chat messages'
244 );
245 } finally {
246 mock.restore();
247 }
248 });
249
250 test('repository activity views use live GitHub responses and edit in place', async () => {
251 const mock = installApiMock();
252 try {
253 const message = { message_id: 701, chat: { id: 123 } };
254 for (const data of [
255 'files:1001',
256 'commits:1001',
257 'pr:1001',
258 'status:1001'
259 ]) {
260 await sendUpdate({
261 callback_query: { id: data, from: { id: 123 }, data, message }
262 });
263 }
264 const edits = mock.telegramCalls.filter(
265 (call) => call.method === 'editMessageText'
266 );
267 assert.equal(edits.length, 4);
268 assert.match(edits[0].body.text, /README\.md/);
269 assert.match(edits[1].body.text, /fix mobile menu/);
270 assert.match(edits[2].body.text, /#7/);
271 assert.match(edits[3].body.text, /CI/);
272 } finally {
273 mock.restore();
274 }
275 });
276
277 test('/repos uses numeric repository ids in callbacks', async () => {
278 const mock = installApiMock();
279 try {
280 await sendUpdate({
281 message: {
282 message_id: 1,
283 from: { id: 123 },
284 chat: { id: 123 },
285 text: '/repos'
286 }
287 });
288 const sent = mock.telegramCalls.find(
289 (call) => call.method === 'sendMessage'
290 );
291 const buttons = sent.body.reply_markup.inline_keyboard.flat();
292 assert.ok(buttons.some((button) => button.callback_data === 'repo:1001'));
293 assert.ok(
294 mock.githubCalls.some((url) =>
295 url.startsWith('https://api.github.com/user/repos?')
296 )
297 );
298 } finally {
299 mock.restore();
300 }
301 });
302
303 test('deep file browser opens directory and file previews in the same Telegram message', async () => {
304 const mock = installApiMock();
305 try {
306 const message = { message_id: 702, chat: { id: 123 } };
307 await sendUpdate({
308 callback_query: {
309 id: 'root',
310 from: { id: 123 },
311 data: 'files:1001',
312 message
313 }
314 });
315 let edits = mock.telegramCalls.filter(
316 (call) => call.method === 'editMessageText'
317 );
318 assert.ok(
319 edits
320 .at(-1)
321 .body.reply_markup.inline_keyboard.flat()
322 .some(
323 (button) => button.callback_data === `tree:1001:${mock.folderSha}`
324 )
325 );
326
327 await sendUpdate({
328 callback_query: {
329 id: 'tree',
330 from: { id: 123 },
331 data: `tree:1001:${mock.folderSha}`,
332 message
333 }
334 });
335 edits = mock.telegramCalls.filter(
336 (call) => call.method === 'editMessageText'
337 );
338 assert.ok(
339 edits
340 .at(-1)
341 .body.reply_markup.inline_keyboard.flat()
342 .some(
343 (button) => button.callback_data === `blob:1001:${mock.nestedFileSha}`
344 )
345 );
346
347 await sendUpdate({
348 callback_query: {
349 id: 'blob',
350 from: { id: 123 },
351 data: `blob:1001:${mock.nestedFileSha}`,
352 message
353 }
354 });
355 edits = mock.telegramCalls.filter(
356 (call) => call.method === 'editMessageText'
357 );
358 assert.doesNotMatch(edits.at(-1).body.text, /console\.log/);
359 assert.match(edits.at(-1).body.text, /Mini App/);
360 assert.equal(edits.at(-1).body.message_id, 702);
361 assert.equal(
362 mock.telegramCalls.filter((call) => call.method === 'sendMessage').length,
363 0
364 );
365 } finally {
366 mock.restore();
367 }
368 });