Personal Telegram developer console for GitHub

phase2-spec-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 197 lines 7.29 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { NexaDevBot } from '../src/bot.js';
4 import { createGitHubPlugin } from '../src/plugins/github/plugin.js';
5
6 const OWNER = '42';
7 const WORKSPACE_ID = 'W000000001';
8
9 function makeMenu() {
10 return {
11 async getReposListView() { return { text: 'repos', reply_markup: { inline_keyboard: [] } }; },
12 async renderByCallback(data) { return { text: `legacy:${data}`, reply_markup: { inline_keyboard: [] } }; }
13 };
14 }
15
16 function makeWorkspaces() {
17 let pending = null;
18 const workspace = {
19 id: WORKSPACE_ID,
20 repoId: 123,
21 repoFullName: 'davood/demo',
22 baseBranch: 'main',
23 baseHeadSha: 'a'.repeat(40),
24 baseTreeSha: 'b'.repeat(40),
25 path: '.gitignore',
26 blobSha: 'c'.repeat(40),
27 originalText: 'node_modules/\n',
28 draftText: 'node_modules/\n',
29 commitMessage: '',
30 state: 'DRAFT'
31 };
32 return {
33 workspace,
34 async getWorkspace({ ownerId, workspaceId }) {
35 return ownerId === OWNER && workspaceId === WORKSPACE_ID ? structuredClone(workspace) : null;
36 },
37 async setPendingInput({ ownerId, workspaceId, mode }) {
38 assert.equal(ownerId, OWNER);
39 pending = { workspaceId, mode };
40 return structuredClone(pending);
41 },
42 async getPendingInput({ ownerId }) {
43 return ownerId === OWNER && pending ? structuredClone(pending) : null;
44 },
45 async consumePendingInput({ ownerId, workspaceId, mode }) {
46 if (ownerId !== OWNER || !pending || pending.workspaceId !== workspaceId || pending.mode !== mode) return false;
47 pending = null;
48 return true;
49 },
50 async updateDraft({ ownerId, workspaceId, text, digest }) {
51 assert.equal(ownerId, OWNER);
52 assert.equal(workspaceId, WORKSPACE_ID);
53 workspace.draftText = text;
54 workspace.draftDigest = digest;
55 workspace.state = 'DRAFT';
56 return structuredClone(workspace);
57 }
58 };
59 }
60
61 function makePlugin() {
62 const workspaces = makeWorkspaces();
63 const github = {
64 async getRepository(repoId) {
65 return { id: String(repoId), name: 'demo', fullName: 'davood/demo', defaultBranch: 'main', archived: false };
66 }
67 };
68 return { workspaces, plugin: createGitHubPlugin({ github, menu: makeMenu(), workspaces }) };
69 }
70
71 test('workspace keeps file upload as a secondary bounded draft input route', async () => {
72 const { workspaces, plugin } = makePlugin();
73 const workspaceView = await plugin.resolveCallback(`ws:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
74 const buttons = workspaceView.reply_markup.inline_keyboard.flat();
75 const upload = buttons.find(button => button.callback_data === `wfile:${WORKSPACE_ID}`);
76 assert.ok(upload);
77 assert.match(upload.text, /file|فایل|upload|بارگذاری/i);
78 assert.ok(new TextEncoder().encode(upload.callback_data).byteLength <= 64);
79
80 const route = plugin.resolveCallback(upload.callback_data, { ownerId: OWNER });
81 assert.equal(route.actionId, 'github.workspace.input.prepare');
82 const prompt = await route.execute();
83 assert.match(prompt.text, /65,?536|65536/);
84 assert.match(prompt.text, /20,?000|20000/);
85 assert.match(prompt.text, /UTF-?8|فایل/i);
86 assert.deepEqual(await workspaces.getPendingInput({ ownerId: OWNER }), {
87 workspaceId: WORKSPACE_ID,
88 mode: 'draft_file'
89 });
90 });
91
92 test('uploaded text file replaces only the local draft and preserves the repository target path', async () => {
93 const { workspaces, plugin } = makePlugin();
94 await plugin.resolveCallback(`wfile:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
95 const pending = await workspaces.getPendingInput({ ownerId: OWNER });
96 const route = plugin.resolveMessage({
97 pending,
98 file: { name: 'replacement.txt', text: 'node_modules/\n# smoke\n', byteLength: 22 }
99 }, { ownerId: OWNER });
100 assert.equal(route.actionId, 'github.workspace.input.apply');
101 const view = await route.execute();
102 assert.match(view.text, /Draft|ذخیره|فایل/i);
103 assert.equal(workspaces.workspace.path, '.gitignore');
104 assert.equal(workspaces.workspace.draftText, 'node_modules/\n# smoke\n');
105 assert.equal(await workspaces.getPendingInput({ ownerId: OWNER }), null);
106 });
107
108 test('bot downloads a pending workspace document using the expanded workspace byte ceiling', async () => {
109 const bot = new NexaDevBot({});
110 const calls = [];
111 bot.workspaceStore = {
112 isConfigured: () => true,
113 async getPendingInput({ ownerId }) {
114 calls.push(['getPendingInput', ownerId]);
115 return { workspaceId: WORKSPACE_ID, mode: 'draft_file' };
116 }
117 };
118 bot.telegram = {
119 async downloadTextFile(fileId, options) {
120 calls.push(['downloadTextFile', fileId, options]);
121 return { text: 'node_modules/\n# smoke\n', byteLength: 22, filePath: 'documents/replacement.txt' };
122 },
123 async sendMessage(chatId, text, replyMarkup) {
124 calls.push(['sendMessage', chatId, text, replyMarkup]);
125 return { ok: true };
126 }
127 };
128 bot.router = {
129 async dispatchMessage(payload, context) {
130 calls.push(['dispatchMessage', structuredClone(payload), structuredClone(context)]);
131 return { text: 'file accepted', reply_markup: { inline_keyboard: [] } };
132 }
133 };
134
135 const result = await bot.handleMessage({
136 chat: { id: 7 },
137 from: { id: 42 },
138 document: { file_id: 'FILE123', file_name: 'replacement.txt', file_size: 22 }
139 });
140
141 assert.equal(result.workspaceInput, true);
142 assert.deepEqual(calls.find(call => call[0] === 'downloadTextFile'), ['downloadTextFile', 'FILE123', { maxBytes: 65536 }]);
143 const dispatch = calls.find(call => call[0] === 'dispatchMessage');
144 assert.deepEqual(dispatch[1].file, {
145 name: 'replacement.txt',
146 text: 'node_modules/\n# smoke\n',
147 byteLength: 22
148 });
149 assert.deepEqual(dispatch[2], { isOwner: true, ownerId: OWNER });
150 });
151
152 test('wrong message type while file upload is pending does not dispatch or consume the workspace input', async () => {
153 const bot = new NexaDevBot({});
154 let dispatches = 0;
155 bot.workspaceStore = {
156 isConfigured: () => true,
157 async getPendingInput() { return { workspaceId: WORKSPACE_ID, mode: 'draft_file' }; }
158 };
159 bot.telegram = {
160 async sendMessage() { return { ok: true }; },
161 async downloadTextFile() { throw new Error('must not download'); }
162 };
163 bot.router = {
164 async dispatchMessage() { dispatches += 1; return null; }
165 };
166
167 const result = await bot.handleMessage({ chat: { id: 7 }, from: { id: 42 }, text: 'not a file' });
168 assert.equal(result.workspaceInput, true);
169 assert.equal(result.inputRequired, 'document');
170 assert.equal(dispatches, 0);
171 });
172
173 test('oversized Telegram document is rejected before download or workspace dispatch so owner can retry', async () => {
174 const bot = new NexaDevBot({});
175 let downloads = 0;
176 let dispatches = 0;
177 bot.workspaceStore = {
178 isConfigured: () => true,
179 async getPendingInput() { return { workspaceId: WORKSPACE_ID, mode: 'draft_file' }; }
180 };
181 bot.telegram = {
182 async sendMessage() { return { ok: true }; },
183 async downloadTextFile() { downloads += 1; return { text: 'unexpected', byteLength: 10 }; }
184 };
185 bot.router = {
186 async dispatchMessage() { dispatches += 1; return null; }
187 };
188
189 const result = await bot.handleMessage({
190 chat: { id: 7 },
191 from: { id: 42 },
192 document: { file_id: 'HUGE', file_name: 'huge.txt', file_size: 65537 }
193 });
194 assert.equal(result.inputRejected, true);
195 assert.equal(downloads, 0);
196 assert.equal(dispatches, 0);
197 });