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 268 lines 8.41 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { PluginRegistry } from '../src/core/plugin-registry.js';
4 import { createGitHubPlugin } from '../src/plugins/github/plugin.js';
5
6 function makeMenu() {
7 const calls = [];
8 const reposView = { text: 'repos', reply_markup: { inline_keyboard: [] } };
9 return {
10 calls,
11 reposView,
12 async getReposListView() {
13 calls.push('/repos');
14 return reposView;
15 },
16 async renderByCallback(data) {
17 calls.push(data);
18 return {
19 text: data,
20 reply_markup: {
21 inline_keyboard: data.startsWith('repo:')
22 ? [[{ text: '🔙 بازگشت', callback_data: 'menu:repos' }]]
23 : []
24 }
25 };
26 }
27 };
28 }
29
30 function makeGitHub() {
31 const calls = [];
32 return {
33 calls,
34 async getRepository(repoId) {
35 calls.push(`repo:${repoId}`);
36 return { id: String(repoId), name: 'demo', fullName: 'davood/demo' };
37 },
38 async listPullRequests(repoId) {
39 calls.push(`pr:${repoId}`);
40 return [
41 {
42 number: 19,
43 title: 'Feature',
44 author: 'dev',
45 state: 'open',
46 merged: false
47 }
48 ];
49 },
50 async listWorkflowRuns(repoId) {
51 calls.push(`runs:${repoId}`);
52 return [
53 {
54 id: 77,
55 name: 'CI',
56 status: 'completed',
57 conclusion: 'failure',
58 branch: 'main',
59 sha: 'abcdef0',
60 runNumber: 12
61 }
62 ];
63 },
64 async getWorkflowRun(repoId, runId) {
65 calls.push(`run:${repoId}:${runId}`);
66 return {
67 id: Number(runId),
68 name: 'CI',
69 status: 'completed',
70 conclusion: 'failure',
71 branch: 'main',
72 sha: 'abcdef0',
73 runNumber: 12
74 };
75 },
76 async listWorkflowJobs(repoId, runId) {
77 calls.push(`jobs:${repoId}:${runId}`);
78 return [
79 {
80 id: 88,
81 name: 'test',
82 status: 'completed',
83 conclusion: 'failure',
84 steps: [
85 {
86 number: 1,
87 name: 'checkout',
88 status: 'completed',
89 conclusion: 'success'
90 },
91 {
92 number: 2,
93 name: 'test',
94 status: 'completed',
95 conclusion: 'failure'
96 }
97 ]
98 }
99 ];
100 },
101 async rerunFailedWorkflowRun(repoId, runId) {
102 calls.push(`rerun:${repoId}:${runId}`);
103 return { accepted: true, status: 201 };
104 },
105 async cancelWorkflowRun(repoId, runId) {
106 calls.push(`cancel:${repoId}:${runId}`);
107 return { accepted: true, status: 202 };
108 }
109 };
110 }
111
112 test('GitHub plugin manifest registers read and write workflow actions with explicit risk', () => {
113 const plugin = createGitHubPlugin({ github: makeGitHub(), menu: makeMenu() });
114 const registry = new PluginRegistry([plugin]);
115
116 const readActions = [
117 'github.repos.list',
118 'github.repo.detail',
119 'github.files.list',
120 'github.tree.read',
121 'github.blob.read',
122 'github.pr.list',
123 'github.commits.list',
124 'github.status.read',
125 'github.workflow.runs.list',
126 'github.workflow.run.detail',
127 'github.workflow.jobs.list'
128 ];
129 for (const actionId of readActions) {
130 assert.equal(registry.getAction(actionId)?.risk, 'READ');
131 }
132
133 assert.equal(
134 registry.getAction('github.workflow.run.rerun_failed')?.risk,
135 'WRITE'
136 );
137 assert.equal(registry.getAction('github.workflow.run.cancel')?.risk, 'WRITE');
138 });
139
140 test('GitHub plugin resolves /repos without executing the menu before policy', async () => {
141 const menu = makeMenu();
142 const plugin = createGitHubPlugin({ github: makeGitHub(), menu });
143
144 const route = plugin.resolveCommand('/repos');
145
146 assert.equal(route.actionId, 'github.repos.list');
147 assert.deepEqual(menu.calls, []);
148 assert.equal(await route.execute(), menu.reposView);
149 assert.deepEqual(menu.calls, ['/repos']);
150 assert.equal(plugin.resolveCommand('/status'), null);
151 });
152
153 test('GitHub plugin maps read callback families to declared actions without eager execution', async () => {
154 const menu = makeMenu();
155 const github = makeGitHub();
156 const plugin = createGitHubPlugin({ github, menu });
157 const cases = [
158 ['menu:repos', 'github.repos.list'],
159 ['repo:123', 'github.repo.detail'],
160 ['files:123', 'github.files.list'],
161 ['tree:123:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'github.tree.read'],
162 ['blob:123:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'github.blob.read'],
163 ['pr:123', 'github.pr.advanced.list'],
164 ['commits:123', 'github.commits.list'],
165 ['status:123', 'github.workflow.runs.list'],
166 ['runs:123', 'github.workflow.runs.list'],
167 ['run:123:77', 'github.workflow.run.detail'],
168 ['jobs:123:77', 'github.workflow.jobs.list']
169 ];
170
171 for (const [callbackData, actionId] of cases) {
172 const beforeMenu = menu.calls.length;
173 const beforeGitHub = github.calls.length;
174 const route = plugin.resolveCallback(callbackData);
175 assert.equal(route.actionId, actionId);
176 assert.equal(
177 menu.calls.length,
178 beforeMenu,
179 'route resolution must be side-effect-free'
180 );
181 assert.equal(
182 github.calls.length,
183 beforeGitHub,
184 'route resolution must not call GitHub'
185 );
186 const view = await route.execute();
187 assert.ok(view?.text);
188 }
189 });
190
191 test('repository detail view keeps its existing status entry without duplicate CI navigation', async () => {
192 const menu = makeMenu();
193 const plugin = createGitHubPlugin({ github: makeGitHub(), menu });
194 const view = await plugin.resolveCallback('repo:123').execute();
195 const buttons = view.reply_markup.inline_keyboard.flat();
196 assert.ok(!buttons.some((button) => button.callback_data === 'runs:123'));
197 });
198
199 test('GitHub plugin write callbacks expose compact approval metadata and defer provider calls', async () => {
200 const menu = makeMenu();
201 const github = makeGitHub();
202 const plugin = createGitHubPlugin({ github, menu });
203
204 const rerun = plugin.resolveCallback('rerun:123:77');
205 assert.equal(rerun.actionId, 'github.workflow.run.rerun_failed');
206 assert.deepEqual(rerun.approval, {
207 input: { repoId: 123, runId: 77 },
208 label: 'اجرای مجدد jobهای ناموفق'
209 });
210 assert.deepEqual(github.calls, []);
211 const rerunView = await rerun.execute();
212 assert.deepEqual(github.calls, ['rerun:123:77']);
213 assert.match(rerunView.text, /اجرا.*مجدد|دوباره/i);
214 assert.ok(
215 rerunView.reply_markup.inline_keyboard
216 .flat()
217 .some((button) => button.callback_data === 'run:123:77')
218 );
219
220 github.calls.length = 0;
221 const cancel = plugin.resolveCallback('cancelrun:123:77');
222 assert.equal(cancel.actionId, 'github.workflow.run.cancel');
223 assert.deepEqual(cancel.approval, {
224 input: { repoId: 123, runId: 77 },
225 label: 'لغو Workflow در حال اجرا'
226 });
227 assert.deepEqual(github.calls, []);
228 const cancelView = await cancel.execute();
229 assert.deepEqual(github.calls, ['cancel:123:77']);
230 assert.match(cancelView.text, /لغو/);
231 });
232
233 test('GitHub workflow views expose bounded navigation and write-action callbacks', async () => {
234 const plugin = createGitHubPlugin({ github: makeGitHub(), menu: makeMenu() });
235
236 const runsView = await plugin.resolveCallback('runs:123').execute();
237 assert.match(runsView.text, /CI/);
238 assert.ok(
239 runsView.reply_markup.inline_keyboard
240 .flat()
241 .some((button) => button.callback_data === 'run:123:77')
242 );
243
244 const runView = await plugin.resolveCallback('run:123:77').execute();
245 const runButtons = runView.reply_markup.inline_keyboard.flat();
246 assert.ok(
247 runButtons.some((button) => button.callback_data === 'jobs:123:77')
248 );
249 assert.ok(
250 runButtons.some((button) => button.callback_data === 'rerun:123:77')
251 );
252
253 const jobsView = await plugin.resolveCallback('jobs:123:77').execute();
254 assert.match(jobsView.text, /checkout/);
255 assert.match(jobsView.text, /test/);
256 });
257
258 test('GitHub plugin rejects malformed workflow callback ids and unrelated callbacks', () => {
259 const plugin = createGitHubPlugin({ github: makeGitHub(), menu: makeMenu() });
260
261 assert.equal(plugin.resolveCommand('/start'), null);
262 assert.equal(plugin.resolveCallback('run:abc:77'), null);
263 assert.equal(plugin.resolveCallback('rerun:123:nope'), null);
264 assert.equal(plugin.resolveCallback('menu:main'), null);
265 assert.equal(plugin.resolveCallback('menu:status'), null);
266 assert.equal(plugin.resolveCallback('menu:tools'), null);
267 assert.equal(plugin.resolveCallback('tool:ping'), null);
268 });