Personal Telegram developer console for GitHub

hardening/ci-zero-test-telegram-token
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 212 lines 7 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
36 globalThis.fetch = async (url, options = {}) => {
37 const value = String(url);
38
39 if (value.startsWith('https://api.telegram.org/')) {
40 const method = value.split('/').at(-1);
41 const body = options.body ? JSON.parse(options.body) : {};
42 telegramCalls.push({ method, body });
43 return json({ ok: true, result: { message_id: body.message_id || 700, ...body } });
44 }
45
46 if (value.startsWith('https://api.github.com/')) {
47 githubCalls.push(value);
48
49 if (value.startsWith('https://api.github.com/user/repos?')) {
50 return json([{
51 name: 'demo',
52 full_name: 'davoodmehraban89/demo',
53 private: true,
54 default_branch: 'main',
55 stargazers_count: 0,
56 forks_count: 0,
57 description: 'Demo repository'
58 }]);
59 }
60
61 if (value === 'https://api.github.com/repos/davoodmehraban89/demo') {
62 return json({
63 name: 'demo',
64 full_name: 'davoodmehraban89/demo',
65 private: true,
66 default_branch: 'main',
67 archived: false,
68 description: 'Demo repository',
69 html_url: 'https://github.com/davoodmehraban89/demo'
70 });
71 }
72
73 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/contents') {
74 return json([
75 { name: 'src', path: 'src', type: 'dir', size: 0, sha: 'a'.repeat(40) },
76 { name: 'README.md', path: 'README.md', type: 'file', size: 100, sha: 'b'.repeat(40) }
77 ]);
78 }
79
80 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/commits?per_page=5') {
81 return json([{
82 sha: '1234567890abcdef',
83 commit: { message: 'fix mobile menu', author: { name: 'Davood', date: '2026-10-02T10:00:00Z' } },
84 author: { login: 'davoodmehraban89' }
85 }]);
86 }
87
88 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/pulls?state=all&per_page=10') {
89 return json([{
90 number: 7,
91 title: 'Improve bot UI',
92 state: 'open',
93 merged_at: null,
94 created_at: '2026-10-02T10:00:00Z',
95 html_url: 'https://github.com/davoodmehraban89/demo/pull/7',
96 user: { login: 'davoodmehraban89' }
97 }]);
98 }
99
100 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=3') {
101 return json({
102 workflow_runs: [{
103 name: 'CI',
104 status: 'completed',
105 conclusion: 'success',
106 head_commit: { message: 'fix mobile menu' },
107 html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
108 }]
109 });
110 }
111
112 return json({ message: 'Not Found' }, 404);
113 }
114
115 throw new Error(`Unexpected URL: ${value}`);
116 };
117
118 return {
119 telegramCalls,
120 githubCalls,
121 restore() {
122 globalThis.fetch = originalFetch;
123 }
124 };
125 }
126
127 async function sendUpdate(update) {
128 const response = await worker.fetch(webhook(update), env);
129 assert.equal(response.status, 200);
130 return response.json();
131 }
132
133 test('single-message navigation edits the same Telegram message across menu layers', async () => {
134 const mock = installApiMock();
135 try {
136 await sendUpdate({
137 message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/start' }
138 });
139
140 const initial = mock.telegramCalls.find(call => call.method === 'sendMessage');
141 assert.ok(initial);
142 assert.match(initial.body.text, /Nexa Dev Bot/);
143
144 const menuMessage = { message_id: 700, chat: { id: 123 } };
145
146 await sendUpdate({
147 callback_query: { id: 'repos', from: { id: 123 }, data: 'menu:repos', message: menuMessage }
148 });
149 await sendUpdate({
150 callback_query: { id: 'repo', from: { id: 123 }, data: 'repo:demo', message: menuMessage }
151 });
152 await sendUpdate({
153 callback_query: { id: 'back', from: { id: 123 }, data: 'menu:repos', message: menuMessage }
154 });
155 await sendUpdate({
156 callback_query: { id: 'home', from: { id: 123 }, data: 'menu:main', message: menuMessage }
157 });
158
159 const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
160 assert.equal(edits.length, 4);
161 assert.ok(edits.every(call => call.body.message_id === 700));
162 assert.match(edits[0].body.text, /فهرست مخزن‌ها/);
163 assert.match(edits[1].body.text, /پروژه: demo/);
164 assert.match(edits[3].body.text, /Nexa Dev Bot/);
165
166 const postStartSends = mock.telegramCalls.filter(call => call.method === 'sendMessage');
167 assert.equal(postStartSends.length, 1, 'callbacks must not create extra chat messages');
168 } finally {
169 mock.restore();
170 }
171 });
172
173 test('repository activity views use live GitHub responses and edit in place', async () => {
174 const mock = installApiMock();
175 try {
176 const menuMessage = { message_id: 701, chat: { id: 123 } };
177 for (const data of ['files:demo', 'commits:demo', 'pr:demo', 'status:demo']) {
178 await sendUpdate({
179 callback_query: { id: data, from: { id: 123 }, data, message: menuMessage }
180 });
181 }
182
183 const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
184 assert.equal(edits.length, 4);
185 assert.match(edits[0].body.text, /README\.md/);
186 assert.match(edits[1].body.text, /fix mobile menu/);
187 assert.match(edits[2].body.text, /#7/);
188 assert.match(edits[2].body.text, /Improve bot UI/);
189 assert.match(edits[3].body.text, /CI/);
190 assert.ok(mock.githubCalls.some(url => url.startsWith('https://api.github.com/user/repos?')) === false);
191 } finally {
192 mock.restore();
193 }
194 });
195
196 test('/repos uses authenticated repository discovery and renders the API result', async () => {
197 const mock = installApiMock();
198 try {
199 await sendUpdate({
200 message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/repos' }
201 });
202
203 const sent = mock.telegramCalls.find(call => call.method === 'sendMessage');
204 assert.ok(sent);
205 const buttons = sent.body.reply_markup.inline_keyboard.flat();
206 assert.ok(buttons.some(button => button.text.includes('demo')));
207 assert.ok(buttons.some(button => button.callback_data === 'repo:demo'));
208 assert.ok(mock.githubCalls.some(url => url.startsWith('https://api.github.com/user/repos?')));
209 } finally {
210 mock.restore();
211 }
212 });