Personal Telegram developer console for GitHub

phase2-20261005-a
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 246 lines 7.95 KB
Raw
1 import { describe, it } from 'node:test';
2 import assert from 'node:assert/strict';
3 import { NexaDevBot } from '../src/bot.js';
4
5 const fakeGitHub = {
6 owner: 'davoodmehraban89',
7 async listRepositories() {
8 return [{ id: 'Factor-easy', name: 'Factor-easy', private: true }];
9 },
10 async getRepository(repo) {
11 return {
12 id: repo,
13 name: repo,
14 fullName: `davoodmehraban89/${repo}`,
15 private: true,
16 description: 'Factor-easy project',
17 defaultBranch: 'main',
18 archived: false
19 };
20 },
21 async listPullRequests() {
22 return [{
23 number: 43,
24 title: 'Ops: strengthen long-running execution heartbeat rule',
25 author: 'davoodmehraban89',
26 state: 'open',
27 merged: false
28 }];
29 },
30 async listCommits() {
31 return [];
32 },
33 async listFiles() {
34 return [];
35 },
36 async getRepoStatus() {
37 return { defaultBranch: 'main', lastRuns: [] };
38 }
39 };
40
41 function makeBot() {
42 const bot = new NexaDevBot();
43 bot.menu.github = fakeGitHub;
44 return bot;
45 }
46
47 describe('Nexa Dev Bot - Cloudflare plugin registration', () => {
48 it('registers the Cloudflare READ plugin and fails closed when provider config is absent', async () => {
49 const bot = new NexaDevBot();
50 assert.ok(bot.registry.getPlugin('cloudflare'), 'cloudflare plugin must be registered');
51 assert.deepEqual(bot.registry.getAction('cloudflare.worker.status'), {
52 pluginId: 'cloudflare',
53 actionId: 'cloudflare.worker.status',
54 risk: 'READ'
55 });
56
57 const view = await bot.router.dispatchCallback('cf:status', { isOwner: true, ownerId: '123' });
58 assert.match(view.text, /پیکربندی نشده|not configured/i);
59 });
60 });
61
62 describe('Nexa Dev Bot - Webhook Dispatcher & In-Place Editing', () => {
63 it('/start installs the bottom quick menu and sends the inline Main Menu', async () => {
64 const sentMessages = [];
65 const bot = makeBot();
66 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
67 sentMessages.push({ chatId, text, replyMarkup });
68 return { ok: true, result: { message_id: 500 + sentMessages.length } };
69 };
70
71 const res = await bot.handleUpdate({
72 message: { message_id: 100, chat: { id: 123456 }, text: '/start' }
73 });
74
75 assert.equal(res.ok, true);
76 assert.equal(res.command, 'start');
77 assert.equal(res.persistentMenu, true);
78 assert.equal(sentMessages.length, 2);
79 assert.equal(sentMessages[0].chatId, 123456);
80 assert.equal(sentMessages[0].replyMarkup.is_persistent, true);
81 assert.ok(sentMessages[0].replyMarkup.keyboard);
82 assert.equal(sentMessages[1].chatId, 123456);
83 assert.match(sentMessages[1].text, /Nexa Dev Bot/);
84 assert.ok(sentMessages[1].replyMarkup.inline_keyboard);
85 });
86
87 it('/repos is dispatched through the plugin router', async () => {
88 const sentMessages = [];
89 const bot = makeBot();
90 const routedView = {
91 text: 'routed repositories',
92 reply_markup: { inline_keyboard: [] }
93 };
94 let dispatchCount = 0;
95
96 bot.menu.getReposListView = async () => {
97 throw new Error('direct repository path must not be used');
98 };
99 bot.router.dispatchCommand = async (command, context) => {
100 dispatchCount += 1;
101 assert.equal(command, '/repos');
102 assert.equal(context.isOwner, true);
103 return routedView;
104 };
105 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
106 sentMessages.push({ chatId, text, replyMarkup });
107 return { ok: true };
108 };
109
110 const res = await bot.handleUpdate({
111 message: { message_id: 101, chat: { id: 123456 }, text: '/repos' }
112 });
113
114 assert.equal(res.ok, true);
115 assert.equal(res.command, 'repos');
116 assert.equal(dispatchCount, 1);
117 assert.equal(sentMessages.length, 1);
118 assert.equal(sentMessages[0].text, 'routed repositories');
119 });
120
121 it('callback answers Telegram and edits the existing message in place', async () => {
122 const answeredCallbacks = [];
123 const editedMessages = [];
124 const sentMessages = [];
125 const bot = makeBot();
126
127 bot.telegram.answerCallbackQuery = async (id) => {
128 answeredCallbacks.push(id);
129 return { ok: true };
130 };
131 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
132 editedMessages.push({ chatId, messageId, text, replyMarkup });
133 return { ok: true };
134 };
135 bot.telegram.sendMessage = async () => {
136 sentMessages.push(true);
137 return { ok: true };
138 };
139
140 const res = await bot.handleUpdate({
141 callback_query: {
142 id: 'cb_999',
143 data: 'menu:repos',
144 message: { message_id: 501, chat: { id: 123456 } }
145 }
146 });
147
148 assert.equal(res.ok, true);
149 assert.equal(res.edited, true);
150 assert.deepEqual(answeredCallbacks, ['cb_999']);
151 assert.equal(editedMessages.length, 1);
152 assert.equal(sentMessages.length, 0);
153 assert.equal(editedMessages[0].messageId, 501);
154 assert.match(editedMessages[0].text, /فهرست مخزن‌ها/);
155 });
156
157 it('github callbacks are dispatched through the plugin router before editing', async () => {
158 const bot = makeBot();
159 const edits = [];
160 let dispatchCount = 0;
161
162 bot.telegram.answerCallbackQuery = async () => ({ ok: true });
163 bot.menu.renderByCallback = async () => {
164 throw new Error('direct github callback path must not be used');
165 };
166 bot.router.dispatchCallback = async (data, context) => {
167 dispatchCount += 1;
168 assert.equal(data, 'repo:Factor-easy');
169 assert.equal(context.isOwner, true);
170 return {
171 text: 'routed project',
172 reply_markup: { inline_keyboard: [] }
173 };
174 };
175 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
176 edits.push({ chatId, messageId, text, replyMarkup });
177 return { ok: true };
178 };
179
180 const res = await bot.handleUpdate({
181 callback_query: {
182 id: 'cb_route',
183 data: 'repo:Factor-easy',
184 message: { message_id: 501, chat: { id: 123456 } }
185 }
186 });
187
188 assert.equal(res.ok, true);
189 assert.equal(res.edited, true);
190 assert.equal(dispatchCount, 1);
191 assert.equal(edits.length, 1);
192 assert.equal(edits[0].text, 'routed project');
193 });
194
195 it('callback navigation still edits when answerCallbackQuery fails', async () => {
196 const editedMessages = [];
197 const bot = makeBot();
198
199 bot.telegram.answerCallbackQuery = async () => {
200 throw new Error('Telegram API answerCallbackQuery failed: query is too old');
201 };
202 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
203 editedMessages.push({ chatId, messageId, text, replyMarkup });
204 return { ok: true };
205 };
206
207 const res = await bot.handleUpdate({
208 callback_query: {
209 id: 'cb_stale',
210 data: 'menu:main',
211 message: { message_id: 501, chat: { id: 123456 } }
212 }
213 });
214
215 assert.equal(res.ok, true);
216 assert.equal(res.edited, true);
217 assert.equal(editedMessages.length, 1);
218 assert.equal(editedMessages[0].messageId, 501);
219 assert.match(editedMessages[0].text, /Nexa Dev Bot/);
220 });
221
222 it('multi-tier back navigation stays on the same Telegram message', async () => {
223 const edits = [];
224 const bot = makeBot();
225 bot.telegram.answerCallbackQuery = async () => ({ ok: true });
226 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
227 edits.push({ chatId, messageId, text, replyMarkup });
228 return { ok: true };
229 };
230
231 const message = { message_id: 501, chat: { id: 123456 } };
232 for (const data of ['repo:Factor-easy', 'pr:Factor-easy', 'repo:Factor-easy', 'menu:repos', 'menu:main']) {
233 const result = await bot.handleUpdate({
234 callback_query: { id: data, data, message }
235 });
236 assert.equal(result.ok, true);
237 }
238
239 assert.equal(edits.length, 5);
240 assert.ok(edits.every(edit => edit.messageId === 501));
241 assert.match(edits[0].text, /پروژه: Factor-easy/);
242 assert.match(edits[1].text, /#43/);
243 assert.match(edits[3].text, /فهرست مخزن‌ها/);
244 assert.match(edits[4].text, /Nexa Dev Bot/);
245 });
246 });