Personal Telegram developer console for GitHub

fix/cloudflare-fetch-binding
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 269 lines 9.12 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import worker from '../src/index.js';
4
5 const env = {
6 TELEGRAM_WEBHOOK_SECRET: 'right',
7 OWNER_TELEGRAM_ID: '123',
8 TELEGRAM_BOT_TOKEN: 'telegram-test-token',
9 GITHUB_TOKEN: 'github-test-token',
10 GITHUB_OWNER: 'davoodmehraban89'
11 };
12
13 function webhook(update) {
14 return new Request('https://example.test/telegram/webhook', {
15 method: 'POST',
16 headers: {
17 'content-type': 'application/json',
18 'X-Telegram-Bot-Api-Secret-Token': 'right'
19 },
20 body: JSON.stringify(update)
21 });
22 }
23
24 function json(data, status = 200) {
25 return new Response(JSON.stringify(data), {
26 status,
27 headers: { 'content-type': 'application/json' }
28 });
29 }
30
31 function installApiMock() {
32 const telegramCalls = [];
33 const githubCalls = [];
34 const originalFetch = globalThis.fetch;
35 const folderSha = 'a'.repeat(40);
36 const fileSha = 'b'.repeat(40);
37 const nestedFileSha = 'c'.repeat(40);
38
39 globalThis.fetch = async (url, options = {}) => {
40 const value = String(url);
41
42 if (value.startsWith('https://api.telegram.org/')) {
43 const method = value.split('/').at(-1);
44 const body = options.body ? JSON.parse(options.body) : {};
45 telegramCalls.push({ method, body });
46 return json({ ok: true, result: { message_id: body.message_id || 700, ...body } });
47 }
48
49 if (value.startsWith('https://api.github.com/')) {
50 githubCalls.push(value);
51
52 if (value.startsWith('https://api.github.com/user/repos?')) {
53 return json([{
54 id: 1001,
55 name: 'demo',
56 full_name: 'davoodmehraban89/demo',
57 private: true,
58 default_branch: 'main',
59 stargazers_count: 0,
60 forks_count: 0,
61 description: 'Demo repository'
62 }]);
63 }
64
65 if (value === 'https://api.github.com/repositories/1001' || value === 'https://api.github.com/repos/davoodmehraban89/demo') {
66 return json({
67 id: 1001,
68 name: 'demo',
69 full_name: 'davoodmehraban89/demo',
70 private: true,
71 default_branch: 'main',
72 archived: false,
73 description: 'Demo repository',
74 html_url: 'https://github.com/davoodmehraban89/demo'
75 });
76 }
77
78 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/contents') {
79 return json([
80 { name: 'src', path: 'src', type: 'dir', size: 0, sha: folderSha },
81 { name: 'README.md', path: 'README.md', type: 'file', size: 100, sha: fileSha }
82 ]);
83 }
84
85 if (value === `https://api.github.com/repos/davoodmehraban89/demo/git/trees/${folderSha}`) {
86 return json({ tree: [
87 { path: 'index.js', type: 'blob', size: 42, sha: nestedFileSha }
88 ] });
89 }
90
91 if (value === `https://api.github.com/repos/davoodmehraban89/demo/git/blobs/${nestedFileSha}`) {
92 return json({
93 encoding: 'base64',
94 size: 21,
95 content: btoa('console.log("hello")\n')
96 });
97 }
98
99 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/commits?per_page=5') {
100 return json([{
101 sha: '1234567890abcdef',
102 commit: { message: 'fix mobile menu', author: { name: 'Davood', date: '2026-10-02T10:00:00Z' } },
103 author: { login: 'davoodmehraban89' }
104 }]);
105 }
106
107 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/pulls?state=all&per_page=10') {
108 return json([{
109 number: 7,
110 title: 'Improve bot UI',
111 state: 'open',
112 merged_at: null,
113 created_at: '2026-10-02T10:00:00Z',
114 html_url: 'https://github.com/davoodmehraban89/demo/pull/7',
115 user: { login: 'davoodmehraban89' }
116 }]);
117 }
118
119 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=3') {
120 return json({
121 workflow_runs: [{
122 name: 'CI',
123 status: 'completed',
124 conclusion: 'success',
125 head_commit: { message: 'fix mobile menu' },
126 html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
127 }]
128 });
129 }
130
131 return json({ message: 'Not Found' }, 404);
132 }
133
134 throw new Error(`Unexpected URL: ${value}`);
135 };
136
137 return {
138 telegramCalls,
139 githubCalls,
140 folderSha,
141 nestedFileSha,
142 restore() {
143 globalThis.fetch = originalFetch;
144 }
145 };
146 }
147
148 async function sendUpdate(update) {
149 const response = await worker.fetch(webhook(update), env);
150 assert.equal(response.status, 200);
151 return response.json();
152 }
153
154 test('single-message navigation edits the same Telegram message across menu layers', async () => {
155 const mock = installApiMock();
156 try {
157 await sendUpdate({
158 message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/start' }
159 });
160
161 const initial = mock.telegramCalls.find(call => call.method === 'sendMessage');
162 assert.ok(initial);
163 assert.match(initial.body.text, /Nexa Dev Bot/);
164
165 const menuMessage = { message_id: 700, chat: { id: 123 } };
166
167 await sendUpdate({
168 callback_query: { id: 'repos', from: { id: 123 }, data: 'menu:repos', message: menuMessage }
169 });
170 await sendUpdate({
171 callback_query: { id: 'repo', from: { id: 123 }, data: 'repo:1001', message: menuMessage }
172 });
173 await sendUpdate({
174 callback_query: { id: 'back', from: { id: 123 }, data: 'menu:repos', message: menuMessage }
175 });
176 await sendUpdate({
177 callback_query: { id: 'home', from: { id: 123 }, data: 'menu:main', message: menuMessage }
178 });
179
180 const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
181 assert.equal(edits.length, 4);
182 assert.ok(edits.every(call => call.body.message_id === 700));
183 assert.match(edits[0].body.text, /فهرست مخزن‌ها/);
184 assert.match(edits[1].body.text, /پروژه: demo/);
185 assert.match(edits[3].body.text, /Nexa Dev Bot/);
186
187 const postStartSends = mock.telegramCalls.filter(call => call.method === 'sendMessage');
188 assert.equal(postStartSends.length, 1, 'callbacks must not create extra chat messages');
189 } finally {
190 mock.restore();
191 }
192 });
193
194 test('repository activity views use live GitHub responses and edit in place', async () => {
195 const mock = installApiMock();
196 try {
197 const menuMessage = { message_id: 701, chat: { id: 123 } };
198 for (const data of ['files:1001', 'commits:1001', 'pr:1001', 'status:1001']) {
199 await sendUpdate({
200 callback_query: { id: data, from: { id: 123 }, data, message: menuMessage }
201 });
202 }
203
204 const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
205 assert.equal(edits.length, 4);
206 assert.match(edits[0].body.text, /README\.md/);
207 assert.match(edits[1].body.text, /fix mobile menu/);
208 assert.match(edits[2].body.text, /#7/);
209 assert.match(edits[2].body.text, /Improve bot UI/);
210 assert.match(edits[3].body.text, /CI/);
211 } finally {
212 mock.restore();
213 }
214 });
215
216 test('/repos uses numeric repository ids in callbacks', async () => {
217 const mock = installApiMock();
218 try {
219 await sendUpdate({
220 message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/repos' }
221 });
222
223 const sent = mock.telegramCalls.find(call => call.method === 'sendMessage');
224 assert.ok(sent);
225 const buttons = sent.body.reply_markup.inline_keyboard.flat();
226 assert.ok(buttons.some(button => button.text.includes('demo')));
227 assert.ok(buttons.some(button => button.callback_data === 'repo:1001'));
228 assert.ok(mock.githubCalls.some(url => url.startsWith('https://api.github.com/user/repos?')));
229 } finally {
230 mock.restore();
231 }
232 });
233
234 test('deep file browser opens directory and file previews in the same Telegram message', async () => {
235 const mock = installApiMock();
236 try {
237 const menuMessage = { message_id: 702, chat: { id: 123 } };
238
239 await sendUpdate({
240 callback_query: { id: 'root', from: { id: 123 }, data: 'files:1001', message: menuMessage }
241 });
242
243 let edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
244 const rootButtons = edits.at(-1).body.reply_markup.inline_keyboard.flat();
245 assert.ok(rootButtons.some(button => button.callback_data === `tree:1001:${mock.folderSha}`));
246
247 await sendUpdate({
248 callback_query: { id: 'tree', from: { id: 123 }, data: `tree:1001:${mock.folderSha}`, message: menuMessage }
249 });
250
251 edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
252 const treeView = edits.at(-1);
253 assert.match(treeView.body.text, /index\.js/);
254 const treeButtons = treeView.body.reply_markup.inline_keyboard.flat();
255 assert.ok(treeButtons.some(button => button.callback_data === `blob:1001:${mock.nestedFileSha}`));
256
257 await sendUpdate({
258 callback_query: { id: 'blob', from: { id: 123 }, data: `blob:1001:${mock.nestedFileSha}`, message: menuMessage }
259 });
260
261 edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
262 const blobView = edits.at(-1);
263 assert.match(blobView.body.text, /console\.log/);
264 assert.equal(blobView.body.message_id, 702);
265 assert.ok(mock.telegramCalls.filter(call => call.method === 'sendMessage').length === 0);
266 } finally {
267 mock.restore();
268 }
269 });