Personal Telegram developer console for GitHub

fix/phase2-workspace-ref-persistence
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 133 lines 4.51 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import worker from '../src/index.js';
4
5 function makeReplayNamespace() {
6 const used = new Set();
7 return {
8 idFromName(name) { return String(name); },
9 get(id) {
10 return {
11 async fetch(request) {
12 const body = await request.json();
13 const key = String(id);
14 if (used.has(key)) return Response.json({ accepted: false, reason: 'already_used' }, { status: 409 });
15 used.add(key);
16 return Response.json({ accepted: true, expiresAt: body.expiresAt });
17 }
18 };
19 }
20 };
21 }
22
23 const env = {
24 TELEGRAM_WEBHOOK_SECRET: 'right',
25 OWNER_TELEGRAM_ID: '123',
26 TELEGRAM_BOT_TOKEN: 'telegram-test-token',
27 GITHUB_TOKEN: 'github-test-token',
28 GITHUB_OWNER: 'davoodmehraban89',
29 APPROVAL_SIGNING_SECRET: 'approval-test-secret',
30 APPROVAL_REPLAY_GUARD: makeReplayNamespace()
31 };
32
33 function webhook(update) {
34 return new Request('https://example.test/telegram/webhook', {
35 method: 'POST',
36 headers: {
37 'content-type': 'application/json',
38 'X-Telegram-Bot-Api-Secret-Token': 'right'
39 },
40 body: JSON.stringify(update)
41 });
42 }
43
44 function json(data, status = 200) {
45 return new Response(JSON.stringify(data), {
46 status,
47 headers: { 'content-type': 'application/json' }
48 });
49 }
50
51 function installMock() {
52 const telegramCalls = [];
53 const githubCalls = [];
54 const originalFetch = globalThis.fetch;
55
56 globalThis.fetch = async (url, options = {}) => {
57 const value = String(url);
58 if (value.startsWith('https://api.telegram.org/')) {
59 const method = value.split('/').at(-1);
60 const body = options.body ? JSON.parse(options.body) : {};
61 telegramCalls.push({ method, body });
62 return json({ ok: true, result: { message_id: body.message_id || 950, ...body } });
63 }
64 if (value === 'https://api.github.com/repositories/1001') {
65 githubCalls.push({ url: value, method: options.method || 'GET' });
66 return json({ id: 1001, name: 'demo', full_name: 'davoodmehraban89/demo' });
67 }
68 if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs/55/rerun-failed-jobs') {
69 githubCalls.push({ url: value, method: options.method || 'GET' });
70 return json({ message: 'Resource not accessible by personal access token' }, 403);
71 }
72 throw new Error(`Unexpected URL: ${value}`);
73 };
74
75 return { telegramCalls, githubCalls, restore() { globalThis.fetch = originalFetch; } };
76 }
77
78 async function sendUpdate(update) {
79 const response = await worker.fetch(webhook(update), env);
80 assert.equal(response.status, 200);
81 return response.json();
82 }
83
84 test('approved GitHub WRITE provider failure visibly updates Telegram and does not claim success', async () => {
85 const mock = installMock();
86 try {
87 const menuMessage = { message_id: 950, chat: { id: 123 } };
88 const issued = await sendUpdate({
89 callback_query: {
90 id: 'issue-provider-failure-approval',
91 from: { id: 123 },
92 data: 'rerun:1001:55',
93 message: menuMessage
94 }
95 });
96 assert.equal(issued.approvalRequired, true);
97
98 const approvalEdit = mock.telegramCalls.filter(call => call.method === 'editMessageText').at(-1);
99 const approveButton = approvalEdit.body.reply_markup.inline_keyboard
100 .flat()
101 .find(button => String(button.callback_data || '').startsWith('approve:'));
102 assert.ok(approveButton);
103
104 const result = await sendUpdate({
105 callback_query: {
106 id: 'approve-provider-failure',
107 from: { id: 123 },
108 data: approveButton.callback_data,
109 message: {
110 ...menuMessage,
111 text: approvalEdit.body.text,
112 reply_markup: approvalEdit.body.reply_markup
113 }
114 }
115 });
116
117 assert.equal(result.ok, false);
118 const posts = mock.githubCalls.filter(call => call.method === 'POST');
119 assert.equal(posts.length, 1, 'approved provider write should be attempted exactly once');
120
121 const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
122 assert.equal(edits.length, 2, 'provider failure must visibly replace the approval message');
123 const feedback = edits.at(-1);
124 assert.match(feedback.body.text, /اجرا نشد|GitHub|403|دسترسی/);
125 assert.doesNotMatch(feedback.body.text, /عملیات با موفقیت|success/i);
126 assert.ok(
127 feedback.body.reply_markup.inline_keyboard.flat().some(button => button.callback_data === 'run:1001:55'),
128 'provider failure must provide a safe route back to the run'
129 );
130 } finally {
131 mock.restore();
132 }
133 });