Personal Telegram developer console for GitHub

fix/phase2-file-editor-selection
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 232 lines 9.46 KB
Raw
1 import { describe, it } from 'node:test';
2 import assert from 'node:assert/strict';
3 import { readFileSync } from 'node:fs';
4 import { RuntimeDiagnostics } from '../src/core/runtime-diagnostics.js';
5 import { OpsMenuManager } from '../src/ops-menu.js';
6
7 function fakeRegistry() {
8 return {
9 getPlugins() {
10 return [{
11 manifest: {
12 id: 'github',
13 actions: {
14 'github.repos.list': { risk: 'READ' },
15 'github.workspace.commit_branch': { risk: 'WRITE' }
16 }
17 }
18 }];
19 }
20 };
21 }
22
23 function fullEnv() {
24 return {
25 TELEGRAM_BOT_TOKEN: 'tg-secret-value',
26 TELEGRAM_WEBHOOK_SECRET: 'webhook-secret-value',
27 GITHUB_TOKEN: 'github-secret-value',
28 OWNER_TELEGRAM_ID: '123',
29 APPROVAL_SIGNING_SECRET: 'approval-secret-value',
30 GITHUB_OWNER: 'davoodmehraban89'
31 };
32 }
33
34 function diagnosticsForMenus({ githubFailure = false } = {}) {
35 const github = {
36 owner: 'davoodmehraban89',
37 async listRepositories() {
38 if (githubFailure) throw new Error('GitHub 403 permission denied');
39 return [{ id: 1 }, { id: 2 }];
40 }
41 };
42 const diagnostics = new RuntimeDiagnostics({
43 env: fullEnv(),
44 github,
45 registry: fakeRegistry(),
46 replayStore: { isConfigured: () => true },
47 workspaceStore: { isConfigured: () => true },
48 approvals: { isReplayProtected: () => true }
49 });
50 return { github, diagnostics };
51 }
52
53 function callbacks(view) {
54 return (view.reply_markup?.inline_keyboard || []).flat().map(button => button.callback_data).filter(Boolean);
55 }
56
57 describe('Nexa Dev Bot - operational diagnostics', () => {
58 it('reports configuration presence and plugin counts without exposing secret values', () => {
59 const env = fullEnv();
60 const diagnostics = new RuntimeDiagnostics({
61 env,
62 github: { owner: 'davoodmehraban89' },
63 registry: fakeRegistry(),
64 replayStore: { isConfigured: () => true },
65 workspaceStore: { isConfigured: () => true },
66 approvals: { isReplayProtected: () => true }
67 });
68
69 const snapshot = diagnostics.getSnapshot();
70 assert.equal(snapshot.telegramConfigured, true);
71 assert.equal(snapshot.webhookSecretConfigured, true);
72 assert.equal(snapshot.ownerConfigured, true);
73 assert.equal(snapshot.githubConfigured, true);
74 assert.equal(snapshot.replayGuardConfigured, true);
75 assert.equal(snapshot.workspaceStoreConfigured, true);
76 assert.equal(snapshot.approvalSignerConfigured, true);
77 assert.equal(snapshot.githubOwner, 'davoodmehraban89');
78 assert.equal(snapshot.pluginCount, 1);
79 assert.equal(snapshot.actionCount, 2);
80
81 const serialized = JSON.stringify(snapshot);
82 for (const secret of Object.values(env).filter(value => String(value).includes('secret'))) {
83 assert.ok(!serialized.includes(secret), `snapshot leaked ${secret}`);
84 }
85 });
86
87 it('performs a bounded live GitHub READ probe and reports provider failure explicitly', async () => {
88 const success = diagnosticsForMenus().diagnostics;
89 assert.deepEqual(await success.probeGitHub(), {
90 ok: true,
91 owner: 'davoodmehraban89',
92 repositoryCount: 2
93 });
94
95 const failed = diagnosticsForMenus({ githubFailure: true }).diagnostics;
96 const failure = await failed.probeGitHub();
97 assert.equal(failure.ok, false);
98 assert.match(failure.error, /403/);
99 assert.doesNotMatch(failure.error, /github-secret-value/);
100 });
101
102 it('self-check is fail-closed when required bindings are missing', async () => {
103 const diagnostics = new RuntimeDiagnostics({
104 env: {},
105 github: {
106 owner: 'davoodmehraban89',
107 async listRepositories() { throw new Error('GITHUB_TOKEN is missing'); }
108 },
109 registry: fakeRegistry(),
110 replayStore: { isConfigured: () => false },
111 workspaceStore: { isConfigured: () => false },
112 approvals: { isReplayProtected: () => false }
113 });
114
115 const report = await diagnostics.runSelfCheck();
116 assert.equal(report.ok, false);
117 const byId = Object.fromEntries(report.checks.map(check => [check.id, check]));
118 for (const id of ['telegram', 'webhook', 'owner', 'github-token', 'replay-guard', 'workspace-store', 'approval-signer', 'github-live']) {
119 assert.equal(byId[id]?.ok, false, `${id} should fail closed`);
120 }
121 });
122
123 it('configures Cloudflare Worker Previews with isolated Durable Object bindings', () => {
124 const config = JSON.parse(readFileSync(new URL('../wrangler.jsonc', import.meta.url), 'utf8'));
125 const production = config.durable_objects?.bindings || [];
126 const preview = config.previews?.durable_objects?.bindings || [];
127 assert.ok(config.previews, 'wrangler previews block is required');
128 for (const binding of production) {
129 assert.ok(
130 preview.some(candidate => candidate.name === binding.name && candidate.class_name === binding.class_name),
131 `preview missing durable binding ${binding.name}`
132 );
133 }
134 });
135 });
136
137 describe('Nexa Dev Bot - operational menu surfaces', () => {
138 it('turns Development Tools into useful READ-only diagnostics', async () => {
139 const { github, diagnostics } = diagnosticsForMenus();
140 const menu = new OpsMenuManager(github, diagnostics);
141 const view = menu.getToolsView();
142 const values = callbacks(view);
143 for (const callback of ['ops:selfcheck', 'ops:github', 'ops:runtime', 'ops:plugins', 'tool:ping']) {
144 assert.ok(values.includes(callback), `missing ${callback}`);
145 }
146 assert.match(view.text, /تشخیص|Diagnostics|عیب.?یابی/);
147 });
148
149 it('renders source-backed live service status and explicit provider failure', async () => {
150 const okContext = diagnosticsForMenus();
151 const okMenu = new OpsMenuManager(okContext.github, okContext.diagnostics);
152 const okView = await okMenu.getServiceStatusView();
153 assert.match(okView.text, /GitHub.*✅|✅.*GitHub/s);
154 assert.match(okView.text, /2/);
155 assert.match(okView.text, /Workspace|Durable/);
156 assert.doesNotMatch(okView.text, /99\.9|100% SLA|ms latency/i);
157
158 const badContext = diagnosticsForMenus({ githubFailure: true });
159 const badMenu = new OpsMenuManager(badContext.github, badContext.diagnostics);
160 const badView = await badMenu.getServiceStatusView();
161 assert.match(badView.text, /GitHub.*❌|❌.*GitHub/s);
162 assert.match(badView.text, /403/);
163 });
164
165 it('provides a real Security Center and read-only Configuration Center', async () => {
166 const { github, diagnostics } = diagnosticsForMenus();
167 const menu = new OpsMenuManager(github, diagnostics);
168
169 const security = menu.getSecurityView();
170 for (const callback of ['security:selfcheck', 'security:policy', 'security:approval']) {
171 assert.ok(callbacks(security).includes(callback), `missing ${callback}`);
172 }
173 assert.match(security.text, /مرکز امنیت/);
174
175 const config = menu.getSettingsView();
176 assert.match(config.text, /پیکربندی/);
177 assert.doesNotMatch(config.text, /تنظیمات Nexa Dev/);
178 for (const callback of ['config:runtime', 'config:providers', 'config:editor', 'config:plugins']) {
179 assert.ok(callbacks(config).includes(callback), `missing ${callback}`);
180 }
181 });
182
183 it('routes every operational drill-down and keeps Telegram payloads bounded', async () => {
184 const { github, diagnostics } = diagnosticsForMenus();
185 const menu = new OpsMenuManager(github, diagnostics);
186 const encoder = new TextEncoder();
187 const routes = [
188 'menu:status', 'menu:tools', 'menu:security', 'menu:settings',
189 'ops:selfcheck', 'ops:github', 'ops:runtime', 'ops:plugins',
190 'security:selfcheck', 'security:policy', 'security:approval',
191 'config:runtime', 'config:providers', 'config:editor', 'config:plugins'
192 ];
193
194 for (const route of routes) {
195 const view = await menu.renderByCallback(route);
196 assert.ok(view?.text, `${route} returned no text`);
197 assert.ok(view.text.length <= 4096, `${route} exceeds Telegram message limit`);
198 for (const callback of callbacks(view)) {
199 assert.ok(encoder.encode(callback).length <= 64, `${callback} exceeds callback limit`);
200 }
201 }
202 });
203
204 it('configuration provider report offers an in-context live GitHub probe with configuration recovery', async () => {
205 const { github, diagnostics } = diagnosticsForMenus();
206 const menu = new OpsMenuManager(github, diagnostics);
207
208 const providers = await menu.renderByCallback('config:providers');
209 assert.ok(callbacks(providers).includes('config:github'), 'provider report must offer a direct live GitHub probe');
210
211 const probe = await menu.renderByCallback('config:github');
212 assert.match(probe.text, /READ زنده موفق/);
213 assert.ok(callbacks(probe).includes('config:github'), 'live probe must support refresh in the same context');
214 assert.ok(callbacks(probe).includes('menu:settings'), 'live probe must recover to configuration, not strand the owner in tools');
215 });
216
217 it('never renders configured secrets in operational pages', async () => {
218 const { github, diagnostics } = diagnosticsForMenus();
219 const menu = new OpsMenuManager(github, diagnostics);
220 const pages = await Promise.all([
221 menu.getServiceStatusView(),
222 menu.renderByCallback('ops:selfcheck'),
223 menu.renderByCallback('ops:runtime'),
224 menu.renderByCallback('security:selfcheck'),
225 menu.renderByCallback('config:providers')
226 ]);
227 const combined = pages.map(page => page.text).join('\n');
228 for (const secret of ['tg-secret-value', 'webhook-secret-value', 'github-secret-value', 'approval-secret-value']) {
229 assert.ok(!combined.includes(secret), `operational page leaked ${secret}`);
230 }
231 });
232 });