Personal Telegram developer console for GitHub

feat/cp3-github-write-final
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 154 lines 8.36 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', OWNER_TELEGRAM_ID: '123',
7 TELEGRAM_BOT_TOKEN: 'telegram-test-token', GITHUB_TOKEN: 'github-test-token',
8 GITHUB_OWNER: 'davoodmehraban89'
9 };
10
11 function webhook(update) {
12 return new Request('https://example.test/telegram/webhook', {
13 method: 'POST',
14 headers: { 'content-type': 'application/json', 'X-Telegram-Bot-Api-Secret-Token': 'right' },
15 body: JSON.stringify(update)
16 });
17 }
18 const json = (data, status = 200) => new Response(JSON.stringify(data), { status, headers: { 'content-type': 'application/json' } });
19
20 function installApiMock() {
21 const telegramCalls = [];
22 const githubCalls = [];
23 const originalFetch = globalThis.fetch;
24 const folderSha = 'a'.repeat(40);
25 const fileSha = 'b'.repeat(40);
26 const nestedFileSha = 'c'.repeat(40);
27
28 globalThis.fetch = async (url, options = {}) => {
29 const value = String(url);
30 if (value.startsWith('https://api.telegram.org/')) {
31 const method = value.split('/').at(-1);
32 const body = options.body ? JSON.parse(options.body) : {};
33 telegramCalls.push({ method, body });
34 return json({ ok: true, result: { message_id: body.message_id || 700, ...body } });
35 }
36 if (!value.startsWith('https://api.github.com/')) throw new Error(`Unexpected URL: ${value}`);
37 githubCalls.push(value);
38
39 if (value.startsWith('https://api.github.com/user/repos?')) return json([{
40 id: 1001, name: 'demo', full_name: 'davoodmehraban89/demo', private: true,
41 default_branch: 'main', stargazers_count: 0, forks_count: 0, description: 'Demo repository'
42 }]);
43 if (value === 'https://api.github.com/repositories/1001' || value === 'https://api.github.com/repos/davoodmehraban89/demo') return json({
44 id: 1001, name: 'demo', full_name: 'davoodmehraban89/demo', private: true,
45 default_branch: 'main', archived: false, description: 'Demo repository', html_url: 'https://github.com/davoodmehraban89/demo'
46 });
47 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/contents') return json([
48 { name: 'src', path: 'src', type: 'dir', size: 0, sha: folderSha },
49 { name: 'README.md', path: 'README.md', type: 'file', size: 100, sha: fileSha }
50 ]);
51 if (value === `https://api.github.com/repos/davoodmehraban89/demo/git/trees/${folderSha}`) return json({ tree: [
52 { path: 'index.js', type: 'blob', size: 42, sha: nestedFileSha }
53 ] });
54 if (value === `https://api.github.com/repos/davoodmehraban89/demo/git/blobs/${nestedFileSha}`) return json({
55 encoding: 'base64', size: 21, content: btoa('console.log("hello")\n')
56 });
57 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/commits?per_page=5') return json([{
58 sha: '1234567890abcdef', commit: { message: 'fix mobile menu', author: { name: 'Davood', date: '2026-10-02T10:00:00Z' } },
59 author: { login: 'davoodmehraban89' }
60 }]);
61 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/pulls?state=all&per_page=10') return json([{
62 number: 7, title: 'Improve bot UI', state: 'open', merged_at: null, created_at: '2026-10-02T10:00:00Z',
63 html_url: 'https://github.com/davoodmehraban89/demo/pull/7', user: { login: 'davoodmehraban89' }
64 }]);
65 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=3') return json({ workflow_runs: [{
66 id: 1, name: 'CI', status: 'completed', conclusion: 'success', head_branch: 'main', head_sha: '1234567890abcdef1234567890abcdef12345678', run_number: 1,
67 head_commit: { message: 'fix mobile menu' }, html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
68 }] });
69 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=10') return json({ workflow_runs: [{
70 id: 1, name: 'CI', status: 'completed', conclusion: 'success', head_branch: 'main', head_sha: '1234567890abcdef1234567890abcdef12345678', run_number: 1,
71 event: 'push', head_commit: { message: 'fix mobile menu' }, html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
72 }] });
73 return json({ message: 'Not Found' }, 404);
74 };
75
76 return { telegramCalls, githubCalls, folderSha, nestedFileSha, restore: () => { globalThis.fetch = originalFetch; } };
77 }
78
79 async function sendUpdate(update) {
80 const response = await worker.fetch(webhook(update), env);
81 assert.equal(response.status, 200);
82 return response.json();
83 }
84
85 test('persistent quick menu bootstrap keeps later inline navigation on one Telegram message', async () => {
86 const mock = installApiMock();
87 try {
88 await sendUpdate({ message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/start' } });
89 const startSends = mock.telegramCalls.filter(call => call.method === 'sendMessage');
90 assert.equal(startSends.length, 2);
91 assert.equal(startSends[0].body.reply_markup.is_persistent, true);
92 assert.ok(startSends[0].body.reply_markup.keyboard);
93 assert.match(startSends[1].body.text, /Nexa Dev Bot/);
94
95 const message = { message_id: 700, chat: { id: 123 } };
96 for (const data of ['menu:repos', 'repo:1001', 'menu:repos', 'menu:main']) {
97 await sendUpdate({ callback_query: { id: data, from: { id: 123 }, data, message } });
98 }
99 const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
100 assert.equal(edits.length, 4);
101 assert.ok(edits.every(call => call.body.message_id === 700));
102 assert.match(edits[0].body.text, /فهرست مخزن‌ها/);
103 assert.match(edits[1].body.text, /پروژه: demo/);
104 assert.match(edits[3].body.text, /Nexa Dev Bot/);
105 assert.equal(mock.telegramCalls.filter(call => call.method === 'sendMessage').length, 2, 'callbacks must not create extra chat messages');
106 } finally { mock.restore(); }
107 });
108
109 test('repository activity views use live GitHub responses and edit in place', async () => {
110 const mock = installApiMock();
111 try {
112 const message = { message_id: 701, chat: { id: 123 } };
113 for (const data of ['files:1001', 'commits:1001', 'pr:1001', 'status:1001']) {
114 await sendUpdate({ callback_query: { id: data, from: { id: 123 }, data, message } });
115 }
116 const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
117 assert.equal(edits.length, 4);
118 assert.match(edits[0].body.text, /README\.md/);
119 assert.match(edits[1].body.text, /fix mobile menu/);
120 assert.match(edits[2].body.text, /#7/);
121 assert.match(edits[3].body.text, /CI/);
122 } finally { mock.restore(); }
123 });
124
125 test('/repos uses numeric repository ids in callbacks', async () => {
126 const mock = installApiMock();
127 try {
128 await sendUpdate({ message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/repos' } });
129 const sent = mock.telegramCalls.find(call => call.method === 'sendMessage');
130 const buttons = sent.body.reply_markup.inline_keyboard.flat();
131 assert.ok(buttons.some(button => button.callback_data === 'repo:1001'));
132 assert.ok(mock.githubCalls.some(url => url.startsWith('https://api.github.com/user/repos?')));
133 } finally { mock.restore(); }
134 });
135
136 test('deep file browser opens directory and file previews in the same Telegram message', async () => {
137 const mock = installApiMock();
138 try {
139 const message = { message_id: 702, chat: { id: 123 } };
140 await sendUpdate({ callback_query: { id: 'root', from: { id: 123 }, data: 'files:1001', message } });
141 let edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
142 assert.ok(edits.at(-1).body.reply_markup.inline_keyboard.flat().some(button => button.callback_data === `tree:1001:${mock.folderSha}`));
143
144 await sendUpdate({ callback_query: { id: 'tree', from: { id: 123 }, data: `tree:1001:${mock.folderSha}`, message } });
145 edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
146 assert.ok(edits.at(-1).body.reply_markup.inline_keyboard.flat().some(button => button.callback_data === `blob:1001:${mock.nestedFileSha}`));
147
148 await sendUpdate({ callback_query: { id: 'blob', from: { id: 123 }, data: `blob:1001:${mock.nestedFileSha}`, message } });
149 edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
150 assert.match(edits.at(-1).body.text, /console\.log/);
151 assert.equal(edits.at(-1).body.message_id, 702);
152 assert.equal(mock.telegramCalls.filter(call => call.method === 'sendMessage').length, 0);
153 } finally { mock.restore(); }
154 });