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 198 lines 5.69 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { NexaDevBot } from '../src/bot.js';
4 import { GuidedNexaDevBot } from '../src/guide-bot.js';
5
6 const WORKSPACE_ID = 'W000000001';
7
8 test('/start replaces the legacy persistent keyboard with the Studio launch button', async () => {
9 const bot = new GuidedNexaDevBot({ OWNER_TELEGRAM_ID: '7' });
10 const sent = [];
11 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
12 sent.push({ chatId, text, replyMarkup });
13 return { ok: true, result: { message_id: 100 + sent.length } };
14 };
15 bot.telegram.setChatMenuButton = async () => ({ ok: true });
16
17 const result = await bot.handleMessage({
18 chat: { id: 7, type: 'private' },
19 from: { id: 7 },
20 text: '/start'
21 });
22
23 assert.equal(result.ok, true);
24 assert.equal(sent.length, 2);
25 assert.deepEqual(sent[0].replyMarkup, { remove_keyboard: true });
26 assert.deepEqual(sent[1].replyMarkup, {
27 inline_keyboard: [
28 [
29 {
30 text: 'باز کردن Nexa Studio',
31 web_app: {
32 url: 'https://nexa-dev-bot.davoodmehraban89.workers.dev/studio/'
33 }
34 }
35 ]
36 ]
37 });
38 });
39
40 test('persistent menu text has precedence over pending draft input', async () => {
41 const bot = new NexaDevBot({});
42 let pendingReads = 0;
43 let workspaceDispatches = 0;
44 const sent = [];
45
46 bot.workspaceStore = {
47 isConfigured: () => true,
48 async getPendingInput() {
49 pendingReads += 1;
50 return { workspaceId: WORKSPACE_ID, mode: 'draft' };
51 }
52 };
53 bot.router = {
54 async dispatchMessage() {
55 workspaceDispatches += 1;
56 return {
57 text: 'should not happen',
58 reply_markup: { inline_keyboard: [] }
59 };
60 }
61 };
62 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
63 sent.push({ chatId, text, replyMarkup });
64 return { ok: true };
65 };
66
67 const result = await bot.handleMessage({
68 chat: { id: 7 },
69 from: { id: 42 },
70 text: '🏠 منوی اصلی'
71 });
72
73 assert.equal(result.ok, true);
74 assert.equal(result.quickMenu, 'main');
75 assert.equal(pendingReads, 0);
76 assert.equal(workspaceDispatches, 0);
77 assert.match(sent.at(-1).text, /Nexa Dev Bot/);
78 });
79
80 test('legacy Paste callbacks replace the workspace message with the Mini App redirect', async () => {
81 const bot = new NexaDevBot({});
82 const sent = [];
83 const edited = [];
84
85 bot.telegram.answerCallbackQuery = async () => ({ ok: true });
86 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
87 sent.push({ chatId, text, replyMarkup });
88 return { ok: true };
89 };
90 bot.telegram.editMessageText = async (...args) => {
91 edited.push(args);
92 return { ok: true };
93 };
94 bot.router.dispatchCallback = async (data) => {
95 assert.equal(data, `wtext:${WORKSPACE_ID}`);
96 return {
97 text: 'متن را فقط در Mini App ویرایش کنید',
98 reply_markup: {
99 inline_keyboard: [
100 [{ text: 'Editor', web_app: { url: 'https://example.test/editor' } }]
101 ]
102 }
103 };
104 };
105
106 const result = await bot.handleCallbackQuery({
107 id: 'cb-paste',
108 data: `wtext:${WORKSPACE_ID}`,
109 from: { id: 42 },
110 message: { message_id: 55, chat: { id: 7 } }
111 });
112
113 assert.equal(result.ok, true);
114 assert.equal(result.edited, true);
115 assert.equal(result.inputPrompt, undefined);
116 assert.equal(sent.length, 0);
117 assert.equal(edited.length, 1);
118 assert.equal(edited[0][1], 55);
119 assert.equal(edited[0][3].force_reply, undefined);
120 assert.equal(
121 edited[0][3].inline_keyboard[0][0].web_app.url,
122 'https://example.test/editor'
123 );
124 });
125
126 test('stale draft input sends only pending metadata to the Mini App redirect route', async () => {
127 const bot = new NexaDevBot({});
128 const pending = { workspaceId: WORKSPACE_ID, mode: 'draft' };
129 const payloadKeys = [];
130 const sent = [];
131 bot.workspaceStore = {
132 isConfigured: () => true,
133 async getPendingInput() {
134 return pending;
135 }
136 };
137 bot.router.dispatchMessage = async (payload, context) => {
138 payloadKeys.push(Object.keys(payload));
139 assert.deepEqual(payload.pending, pending);
140 assert.equal(context.ownerId, '42');
141 return { text: 'ویرایش فقط در Mini App' };
142 };
143 bot.telegram.sendMessage = async (chatId, text) => {
144 sent.push({ chatId, text });
145 return { ok: true };
146 };
147
148 const result = await bot.handleMessage({
149 message_id: 55,
150 chat: { id: 7 },
151 from: { id: 42 },
152 text: 'SOURCE_BODY_MUST_NOT_REACH_CONTROLLER'
153 });
154
155 assert.deepEqual(payloadKeys, [['pending']]);
156 assert.equal(result.workspaceInput, true);
157 assert.equal(
158 JSON.stringify({ sent, result }).includes('SOURCE_BODY_'),
159 false
160 );
161 });
162
163 test('stale upload input redirects before downloading or requesting another file', async () => {
164 for (const document of [
165 undefined,
166 { file_id: 'synthetic-file', file_size: 10 }
167 ]) {
168 const bot = new NexaDevBot({});
169 let downloads = 0;
170 const payloadKeys = [];
171 bot.workspaceStore = {
172 isConfigured: () => true,
173 async getPendingInput() {
174 return { workspaceId: WORKSPACE_ID, mode: 'draft_file' };
175 }
176 };
177 bot.telegram.downloadTextFile = async () => {
178 downloads++;
179 return { text: 'SOURCE_BODY', byteLength: 11 };
180 };
181 bot.router.dispatchMessage = async (payload) => {
182 payloadKeys.push(Object.keys(payload));
183 return { text: 'ویرایش فقط در Mini App' };
184 };
185 bot.telegram.sendMessage = async () => ({ ok: true });
186
187 const result = await bot.handleMessage({
188 chat: { id: 7 },
189 from: { id: 42 },
190 document
191 });
192
193 assert.equal(downloads, 0);
194 assert.deepEqual(payloadKeys, [['pending']]);
195 assert.equal(result.workspaceInput, true);
196 assert.equal(result.inputRequired, undefined);
197 }
198 });