Personal Telegram developer console for GitHub

feat/studio-g0-foundation
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 305 lines 10 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { GuidedNexaDevBot } from '../src/guide-bot.js';
4 import { KillSwitchGuidedNexaDevBot } from '../src/kill-switch-bot.js';
5 import * as config from '../src/config.js';
6 import { TelegramClient } from '../src/telegram.js';
7
8 const STUDIO_URL = 'https://acceptance.test/studio/';
9 const ownerMessage = (text, extra = {}) => ({
10 message_id: 1,
11 from: { id: 42 },
12 chat: { id: 42, type: 'private' },
13 text,
14 ...extra
15 });
16
17 function studioBot(env = {}, { failMenu = false } = {}) {
18 const bot = new KillSwitchGuidedNexaDevBot({
19 OWNER_TELEGRAM_ID: '42',
20 TELEGRAM_BOT_TOKEN: 'synthetic-bot-credential',
21 STUDIO_URL,
22 ...env
23 });
24 const effects = [];
25 bot.telegram.fetchFn = async (url, options) => {
26 const method = new URL(url).pathname.split('/').at(-1);
27 const payload = JSON.parse(options.body);
28 effects.push({ method, payload });
29 if (method === 'setChatMenuButton' && failMenu)
30 throw new Error('PRIVATE_PROVIDER_ERROR_SENTINEL');
31 return Response.json({
32 ok: true,
33 result:
34 method === 'sendMessage' || method === 'editMessageText'
35 ? { message_id: 10, chat: { id: 42, type: 'private' } }
36 : true
37 });
38 };
39 return { bot, effects };
40 }
41
42 function assertStudioButton(payload) {
43 assert.deepEqual(payload.reply_markup, {
44 inline_keyboard: [
45 [{ text: 'باز کردن Nexa Studio', web_app: { url: STUDIO_URL } }]
46 ]
47 });
48 assert.match(payload.text, /Nexa Studio/);
49 }
50
51 test('/start clears the old keyboard and offers a single Studio launch action', async () => {
52 const { bot, effects } = studioBot();
53 const result = await bot.handleUpdate({ message: ownerMessage('/start') });
54 const sent = effects.filter(({ method }) => method === 'sendMessage');
55
56 assert.equal(result.ok, true);
57 assert.equal(result.command, 'start');
58 assert.equal(result.studioEntry, true);
59 assert.equal(result.menuButtonConfigured, true);
60 assert.equal(sent.length, 2);
61 assert.deepEqual(sent[0].payload.reply_markup, { remove_keyboard: true });
62 assertStudioButton(sent[1].payload);
63 assert.deepEqual(
64 effects.find(({ method }) => method === 'setChatMenuButton')?.payload,
65 {
66 chat_id: 42,
67 menu_button: {
68 type: 'web_app',
69 text: 'Nexa Studio',
70 web_app: { url: STUDIO_URL }
71 }
72 }
73 );
74 assert.equal(
75 effects.some(({ payload }) => payload.reply_markup?.keyboard),
76 false
77 );
78 });
79
80 test('/menu and the stale main quick-menu text launch Studio before pending input', async (t) => {
81 for (const text of ['/menu', '🏠 منوی اصلی']) {
82 await t.test(text, async () => {
83 const { bot, effects } = studioBot();
84 bot.workspaceStore = {
85 isConfigured: () => true,
86 async getPendingInput() {
87 assert.fail('main navigation must precede stale workspace input');
88 }
89 };
90 const result = await bot.handleMessage(ownerMessage(text));
91
92 assert.equal(result.ok, true);
93 assert.equal(result.studioEntry, true);
94 assertStudioButton(effects.at(-1).payload);
95 assert.equal(
96 effects.some(({ method }) => method === 'setChatMenuButton'),
97 false
98 );
99 });
100 }
101 });
102
103 test('start payloads launch Studio but unrelated start-like commands do not configure a menu', async () => {
104 const { bot, effects } = studioBot();
105 await bot.handleMessage(ownerMessage('/start@NexaBot launch'));
106 assertStudioButton(effects.at(-1).payload);
107 assert.equal(
108 effects.filter(({ method }) => method === 'setChatMenuButton').length,
109 1
110 );
111
112 effects.length = 0;
113 await bot.handleMessage(ownerMessage('/starter'));
114 assert.equal(
115 effects.some(({ method }) => method === 'setChatMenuButton'),
116 false
117 );
118 });
119
120 test('menu setup failure keeps the primary launch action and returns metadata only', async () => {
121 const { bot, effects } = studioBot({}, { failMenu: true });
122 const result = await bot.handleMessage(ownerMessage('/start'));
123
124 assert.equal(result.ok, true);
125 assert.equal(result.menuButtonConfigured, false);
126 assertStudioButton(effects.at(-1).payload);
127 assert.equal(
128 JSON.stringify(result).includes('PRIVATE_PROVIDER_ERROR_SENTINEL'),
129 false
130 );
131 });
132
133 test('direct entry handlers require the configured owner and that owner’s private chat before effects', async (t) => {
134 for (const message of [
135 ownerMessage('/start', { from: { id: 7 } }),
136 ownerMessage('/start', { from: undefined }),
137 ownerMessage('/start', { chat: { id: -42, type: 'group' } }),
138 ownerMessage('/start', { chat: { id: 99, type: 'private' } }),
139 ownerMessage('/start', { chat: { id: 42 } }),
140 ownerMessage('/menu', { chat: { id: 42, type: 'supergroup' } })
141 ]) {
142 await t.test(JSON.stringify(message), async () => {
143 const { bot, effects } = studioBot();
144 const result = await bot.handleMessage(message);
145 assert.equal(result.ok, false);
146 assert.equal(result.reason, 'owner_private_chat_required');
147 assert.deepEqual(effects, []);
148 });
149 }
150 const { bot, effects } = studioBot({ OWNER_TELEGRAM_ID: '' });
151 assert.equal((await bot.handleMessage(ownerMessage('/start'))).ok, false);
152 assert.deepEqual(effects, []);
153 });
154
155 test('main-menu callback edits the primary Studio entry without setting chat configuration', async () => {
156 const { bot, effects } = studioBot();
157 const result = await bot.handleCallbackQuery({
158 id: 'main',
159 data: 'menu:main',
160 from: { id: 42 },
161 message: ownerMessage(undefined)
162 });
163
164 assert.equal(result.ok, true);
165 assert.equal(result.edited, true);
166 assert.deepEqual(
167 effects.map(({ method }) => method),
168 ['answerCallbackQuery', 'editMessageText']
169 );
170 assertStudioButton(effects.at(-1).payload);
171 });
172
173 test('main-menu callback fails closed outside the owner private chat', async () => {
174 const { bot, effects } = studioBot();
175 const result = await bot.handleCallbackQuery({
176 id: 'main',
177 data: 'menu:main',
178 from: { id: 7 },
179 message: ownerMessage(undefined)
180 });
181 assert.equal(result.ok, false);
182 assert.deepEqual(effects, []);
183 });
184
185 test('invalid explicit Studio URLs produce a fixed Persian error without exposing URL input', async (t) => {
186 for (const value of [
187 '',
188 null,
189 'http://acceptance.test/studio/',
190 'https://PRIVATE_URL_SENTINEL:secret@acceptance.test/studio/',
191 'https://acceptance.test/studio/?token=PRIVATE_URL_SENTINEL',
192 'https://acceptance.test/studio/?',
193 'https://acceptance.test/studio/#PRIVATE_URL_SENTINEL',
194 'https://acceptance.test/studio/#',
195 'https://acceptance.test/workspace/editor',
196 'PRIVATE_URL_SENTINEL'
197 ]) {
198 await t.test(String(value), async () => {
199 const { bot, effects } = studioBot({ STUDIO_URL: value });
200 const result = await bot.handleMessage(ownerMessage('/start'));
201 assert.equal(result.ok, false);
202 assert.equal(result.invalidStudioUrl, true);
203 assert.equal(effects.length, 1);
204 assert.equal(effects[0].method, 'sendMessage');
205 assert.equal(
206 effects[0].payload.text,
207 '⚠️ نشانی Nexa Studio پیکربندی نشده است. با مدیر ربات تماس بگیرید.'
208 );
209 assert.deepEqual(effects[0].payload.reply_markup, {
210 remove_keyboard: true
211 });
212 assert.equal(
213 JSON.stringify({ effects, result }).includes('PRIVATE_URL_SENTINEL'),
214 false
215 );
216 });
217 }
218 });
219
220 test('Studio URLs normalize the exact HTTPS route and default only when unspecified', async () => {
221 assert.equal(typeof config.studioAppUrl, 'function');
222 assert.equal(
223 config.studioAppUrl('https://acceptance.test/studio'),
224 STUDIO_URL
225 );
226 assert.equal(config.studioAppUrl(STUDIO_URL), STUDIO_URL);
227 assert.equal(
228 config.studioAppUrl(),
229 'https://nexa-dev-bot.davoodmehraban89.workers.dev/studio/'
230 );
231 for (const value of [
232 '',
233 null,
234 42,
235 'https://acceptance.test/studio/child',
236 'https://acceptance.test/studio//'
237 ])
238 assert.equal(config.studioAppUrl(value), '');
239 });
240
241 test('TelegramClient sends a scoped web_app chat menu payload through the Bot API', async () => {
242 const calls = [];
243 const client = new TelegramClient('synthetic-bot-credential', {
244 fetch: async (url, options) => {
245 calls.push({ url, payload: JSON.parse(options.body) });
246 return Response.json({ ok: true, result: true });
247 }
248 });
249 assert.equal(typeof client.setChatMenuButton, 'function');
250 await client.setChatMenuButton(42, {
251 type: 'web_app',
252 text: 'Nexa Studio',
253 web_app: { url: STUDIO_URL }
254 });
255 assert.equal(
256 new URL(calls[0].url).pathname.split('/').at(-1),
257 'setChatMenuButton'
258 );
259 assert.deepEqual(calls[0].payload, {
260 chat_id: 42,
261 menu_button: {
262 type: 'web_app',
263 text: 'Nexa Studio',
264 web_app: { url: STUDIO_URL }
265 }
266 });
267 });
268
269 test('the existing secret guard intercepts token messages before any Studio launch effects', async () => {
270 const { bot, effects } = studioBot();
271 const result = await bot.handleUpdate({
272 message: ownerMessage('github_pat_synthetic_noncredential_fixture')
273 });
274 assert.equal(result.secretRejected, true);
275 assert.deepEqual(
276 effects.map(({ method }) => method),
277 ['deleteMessage', 'sendMessage']
278 );
279 });
280
281 test('direct start handling rejects a token caption before configuring the chat menu', async () => {
282 const { bot, effects } = studioBot();
283 const result = await bot.handleMessage(
284 ownerMessage('/start', {
285 caption: 'github_pat_synthetic_noncredential_fixture'
286 })
287 );
288 assert.equal(result.secretRejected, true);
289 assert.deepEqual(
290 effects.map(({ method }) => method),
291 ['deleteMessage', 'sendMessage']
292 );
293 });
294
295 test('legacy guide routing stays available without restoring a persistent keyboard', async () => {
296 const bot = new GuidedNexaDevBot({ OWNER_TELEGRAM_ID: '42' });
297 const sent = [];
298 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
299 sent.push({ chatId, text, replyMarkup });
300 return { ok: true };
301 };
302 const result = await bot.handleMessage(ownerMessage('📘 راهنما'));
303 assert.equal(result.quickMenu, 'help');
304 assert.match(sent[0].text, /راهنمای Nexa Dev/);
305 });