Personal Telegram developer console for GitHub

feat/plugin-foundation-v1
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 227 lines 7.05 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 - Webhook Dispatcher & In-Place Editing', () => {
48 it('/start sends one new message with Main Menu', async () => {
49 const sentMessages = [];
50 const bot = makeBot();
51 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
52 sentMessages.push({ chatId, text, replyMarkup });
53 return { ok: true, result: { message_id: 501 } };
54 };
55
56 const res = await bot.handleUpdate({
57 message: { message_id: 100, chat: { id: 123456 }, text: '/start' }
58 });
59
60 assert.equal(res.ok, true);
61 assert.equal(res.command, 'start');
62 assert.equal(sentMessages.length, 1);
63 assert.equal(sentMessages[0].chatId, 123456);
64 assert.match(sentMessages[0].text, /Nexa Dev Bot/);
65 assert.ok(sentMessages[0].replyMarkup.inline_keyboard);
66 });
67
68 it('/repos is dispatched through the plugin router', async () => {
69 const sentMessages = [];
70 const bot = makeBot();
71 const routedView = {
72 text: 'routed repositories',
73 reply_markup: { inline_keyboard: [] }
74 };
75 let dispatchCount = 0;
76
77 bot.menu.getReposListView = async () => {
78 throw new Error('direct repository path must not be used');
79 };
80 bot.router.dispatchCommand = async (command, context) => {
81 dispatchCount += 1;
82 assert.equal(command, '/repos');
83 assert.equal(context.isOwner, true);
84 return routedView;
85 };
86 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
87 sentMessages.push({ chatId, text, replyMarkup });
88 return { ok: true };
89 };
90
91 const res = await bot.handleUpdate({
92 message: { message_id: 101, chat: { id: 123456 }, text: '/repos' }
93 });
94
95 assert.equal(res.ok, true);
96 assert.equal(res.command, 'repos');
97 assert.equal(dispatchCount, 1);
98 assert.equal(sentMessages.length, 1);
99 assert.equal(sentMessages[0].text, 'routed repositories');
100 });
101
102 it('callback answers Telegram and edits the existing message in place', async () => {
103 const answeredCallbacks = [];
104 const editedMessages = [];
105 const sentMessages = [];
106 const bot = makeBot();
107
108 bot.telegram.answerCallbackQuery = async (id) => {
109 answeredCallbacks.push(id);
110 return { ok: true };
111 };
112 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
113 editedMessages.push({ chatId, messageId, text, replyMarkup });
114 return { ok: true };
115 };
116 bot.telegram.sendMessage = async () => {
117 sentMessages.push(true);
118 return { ok: true };
119 };
120
121 const res = await bot.handleUpdate({
122 callback_query: {
123 id: 'cb_999',
124 data: 'menu:repos',
125 message: { message_id: 501, chat: { id: 123456 } }
126 }
127 });
128
129 assert.equal(res.ok, true);
130 assert.equal(res.edited, true);
131 assert.deepEqual(answeredCallbacks, ['cb_999']);
132 assert.equal(editedMessages.length, 1);
133 assert.equal(sentMessages.length, 0);
134 assert.equal(editedMessages[0].messageId, 501);
135 assert.match(editedMessages[0].text, /فهرست مخزن‌ها/);
136 });
137
138 it('github callbacks are dispatched through the plugin router before editing', async () => {
139 const bot = makeBot();
140 const edits = [];
141 let dispatchCount = 0;
142
143 bot.telegram.answerCallbackQuery = async () => ({ ok: true });
144 bot.menu.renderByCallback = async () => {
145 throw new Error('direct github callback path must not be used');
146 };
147 bot.router.dispatchCallback = async (data, context) => {
148 dispatchCount += 1;
149 assert.equal(data, 'repo:Factor-easy');
150 assert.equal(context.isOwner, true);
151 return {
152 text: 'routed project',
153 reply_markup: { inline_keyboard: [] }
154 };
155 };
156 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
157 edits.push({ chatId, messageId, text, replyMarkup });
158 return { ok: true };
159 };
160
161 const res = await bot.handleUpdate({
162 callback_query: {
163 id: 'cb_route',
164 data: 'repo:Factor-easy',
165 message: { message_id: 501, chat: { id: 123456 } }
166 }
167 });
168
169 assert.equal(res.ok, true);
170 assert.equal(res.edited, true);
171 assert.equal(dispatchCount, 1);
172 assert.equal(edits.length, 1);
173 assert.equal(edits[0].text, 'routed project');
174 });
175
176 it('callback navigation still edits when answerCallbackQuery fails', async () => {
177 const editedMessages = [];
178 const bot = makeBot();
179
180 bot.telegram.answerCallbackQuery = async () => {
181 throw new Error('Telegram API answerCallbackQuery failed: query is too old');
182 };
183 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
184 editedMessages.push({ chatId, messageId, text, replyMarkup });
185 return { ok: true };
186 };
187
188 const res = await bot.handleUpdate({
189 callback_query: {
190 id: 'cb_stale',
191 data: 'menu:main',
192 message: { message_id: 501, chat: { id: 123456 } }
193 }
194 });
195
196 assert.equal(res.ok, true);
197 assert.equal(res.edited, true);
198 assert.equal(editedMessages.length, 1);
199 assert.equal(editedMessages[0].messageId, 501);
200 assert.match(editedMessages[0].text, /Nexa Dev Bot/);
201 });
202
203 it('multi-tier back navigation stays on the same Telegram message', async () => {
204 const edits = [];
205 const bot = makeBot();
206 bot.telegram.answerCallbackQuery = async () => ({ ok: true });
207 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
208 edits.push({ chatId, messageId, text, replyMarkup });
209 return { ok: true };
210 };
211
212 const message = { message_id: 501, chat: { id: 123456 } };
213 for (const data of ['repo:Factor-easy', 'pr:Factor-easy', 'repo:Factor-easy', 'menu:repos', 'menu:main']) {
214 const result = await bot.handleUpdate({
215 callback_query: { id: data, data, message }
216 });
217 assert.equal(result.ok, true);
218 }
219
220 assert.equal(edits.length, 5);
221 assert.ok(edits.every(edit => edit.messageId === 501));
222 assert.match(edits[0].text, /پروژه: Factor-easy/);
223 assert.match(edits[1].text, /#43/);
224 assert.match(edits[3].text, /فهرست مخزن‌ها/);
225 assert.match(edits[4].text, /Nexa Dev Bot/);
226 });
227 });