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 264 lines 7.93 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { createWorkspaceController } from '../src/plugins/github/workspace.js';
4 import { createGitHubPlugin } from '../src/plugins/github/plugin.js';
5 import { MenuManager } from '../src/menu.js';
6 import { handleWorkspaceEditorRequest } from '../src/workspace-editor.js';
7 import { signedWebAppData } from './helpers/webapp-auth.js';
8
9 const source = 'SOURCE_BOUNDARY_SENTINEL';
10 const changed = 'DRAFT_BOUNDARY_SENTINEL';
11 const sha = 'a'.repeat(40);
12 const repo = {
13 id: '123',
14 name: 'demo',
15 fullName: 'owner/demo',
16 defaultBranch: 'main'
17 };
18 const workspace = {
19 id: 'W000000001',
20 repoId: 123,
21 path: 'README.md',
22 originalText: source,
23 draftText: changed,
24 state: 'DRAFT',
25 baseHeadSha: sha
26 };
27 function stores() {
28 return {
29 getWorkspace: async ({ ownerId }) => (ownerId === '42' ? workspace : null),
30 resolveNavigationRef: async ({ ownerId }) =>
31 ownerId === '42' ? { repoId: 123, path: 'README.md', type: 'file' } : null
32 };
33 }
34 function github() {
35 return {
36 getRepository: async () => repo,
37 getBranchSnapshot: async () => ({ headSha: sha, treeSha: 'b'.repeat(40) }),
38 getFileAtRef: async () => ({
39 path: 'README.md',
40 content: source,
41 size: source.length,
42 sha
43 }),
44 getBlob: async () => ({
45 content: btoa(source),
46 encoding: 'base64',
47 size: source.length
48 }),
49 listPullRequestFiles: async () => [
50 {
51 filename: 'README.md',
52 status: 'modified',
53 additions: 1,
54 deletions: 1,
55 patch: `-${source}\n+${changed}`
56 }
57 ],
58 getWorkflowJob: async () => ({
59 id: 4,
60 name: 'test',
61 status: 'completed',
62 conclusion: 'failure',
63 htmlUrl: 'https://github.com/owner/demo/actions/runs/1/job/4'
64 }),
65 getWorkflowJobLog: async () => ({ text: source, truncated: false })
66 };
67 }
68 const assertNoSource = (view) =>
69 assert.doesNotMatch(
70 JSON.stringify(view),
71 /SOURCE_BOUNDARY_SENTINEL|DRAFT_BOUNDARY_SENTINEL/
72 );
73
74 test('file Telegram view contains metadata and authenticated Mini App link, never source', async () => {
75 const controller = createWorkspaceController({
76 github: github(),
77 workspaces: stores(),
78 editorUrl: 'https://app.test/workspace/editor'
79 });
80 const view = await controller
81 .resolveCallback('wnav:R000000001', { ownerId: '42' })
82 .execute();
83 assertNoSource(view);
84 const link = view.reply_markup.inline_keyboard
85 .flat()
86 .find((button) => button.web_app);
87 assert.equal(
88 new URL(link.web_app.url).searchParams.get('refId'),
89 'R000000001'
90 );
91 });
92
93 test('diff Telegram view contains counts and Mini App diff link, never source lines', async () => {
94 const controller = createWorkspaceController({
95 github: github(),
96 workspaces: stores(),
97 editorUrl: 'https://app.test/workspace/editor'
98 });
99 const view = await controller
100 .resolveCallback('wdiff:W000000001', { ownerId: '42' })
101 .execute();
102 assertNoSource(view);
103 assert.match(view.text, /\+1.*-1/);
104 const link = view.reply_markup.inline_keyboard
105 .flat()
106 .find((button) => button.web_app);
107 assert.equal(new URL(link.web_app.url).searchParams.get('view'), 'diff');
108 });
109
110 test('legacy blob, PR patch and job-log callbacks cannot forward source into Telegram', async () => {
111 assertNoSource(await new MenuManager(github()).getBlobView(123, sha));
112 const plugin = createGitHubPlugin({ github: github(), menu: {} });
113 for (const callback of ['prfiles:123:7:1', 'joblog:123:4']) {
114 assertNoSource(
115 await plugin.resolveCallback(callback, { ownerId: '42' }).execute()
116 );
117 }
118 });
119
120 test('only authenticated owner can read pinned file ref in Mini App without creating a workspace', async () => {
121 const botToken = 'test-only-token';
122 const headers = {
123 'x-telegram-init-data': await signedWebAppData({ botToken })
124 };
125 const options = {
126 workspaces: stores(),
127 github: github(),
128 botToken,
129 ownerId: '42'
130 };
131 const response = await handleWorkspaceEditorRequest(
132 new Request('https://app.test/workspace/api/editor?refId=R000000001', {
133 headers
134 }),
135 options
136 );
137 assert.equal(response.status, 200);
138 const body = await response.json();
139 assert.equal(body.workspace.draftText, source);
140 assert.equal(body.workspace.readOnly, true);
141 const bad = {
142 'x-telegram-init-data': await signedWebAppData({ botToken, userId: 99 })
143 };
144 assert.equal(
145 (
146 await handleWorkspaceEditorRequest(
147 new Request('https://app.test/workspace/api/editor?refId=R000000001', {
148 headers: bad
149 }),
150 options
151 )
152 ).status,
153 403
154 );
155 });
156
157 test('authenticated Mini App diff supplies exact counts and text with no provider write', async () => {
158 const botToken = 'test-only-token';
159 const headers = {
160 'x-telegram-init-data': await signedWebAppData({ botToken })
161 };
162 const response = await handleWorkspaceEditorRequest(
163 new Request(
164 'https://app.test/workspace/api/editor?workspaceId=W000000001&view=diff',
165 { headers }
166 ),
167 { workspaces: stores(), botToken, ownerId: '42' }
168 );
169 assert.equal(response.status, 200);
170 const body = await response.json();
171 assert.equal(body.workspace.readOnly, true);
172 assert.equal(body.diff.additions, 1);
173 assert.equal(body.diff.deletions, 1);
174 assert.match(
175 body.diff.text,
176 /\+DRAFT_BOUNDARY_SENTINEL\n-SOURCE_BOUNDARY_SENTINEL/
177 );
178 assert.equal(response.headers.get('cache-control'), 'no-store');
179 });
180
181 test('Mini App preview rejects expired references and sensitive files', async () => {
182 const botToken = 'test-only-token';
183 const headers = {
184 'x-telegram-init-data': await signedWebAppData({ botToken })
185 };
186 const request = () =>
187 new Request('https://app.test/workspace/api/editor?refId=R000000001', {
188 headers
189 });
190 const absent = { ...stores(), resolveNavigationRef: async () => null };
191 assert.equal(
192 (
193 await handleWorkspaceEditorRequest(request(), {
194 workspaces: absent,
195 github: github(),
196 botToken,
197 ownerId: '42'
198 })
199 ).status,
200 404
201 );
202 const secretFile = {
203 ...github(),
204 getFileAtRef: async () => ({
205 path: '.env.production',
206 content: source,
207 size: source.length
208 })
209 };
210 const denied = await handleWorkspaceEditorRequest(request(), {
211 workspaces: stores(),
212 github: secretFile,
213 botToken,
214 ownerId: '42'
215 });
216 assert.equal(denied.status, 422);
217 assert.doesNotMatch(await denied.text(), /SOURCE_BOUNDARY_SENTINEL/);
218 });
219
220 test('preview provider error text cannot become a client validation error', async () => {
221 const botToken = 'test-only-token';
222 const headers = {
223 'x-telegram-init-data': await signedWebAppData({ botToken })
224 };
225 const provider = {
226 ...github(),
227 getFileAtRef: async () => {
228 throw new Error('user path github_pat_SECRET_SENTINEL');
229 }
230 };
231 const response = await handleWorkspaceEditorRequest(
232 new Request('https://app.test/workspace/api/editor?refId=R000000001', {
233 headers
234 }),
235 { workspaces: stores(), github: provider, botToken, ownerId: '42' }
236 );
237 assert.equal(response.status, 500);
238 assert.doesNotMatch(await response.text(), /SECRET_SENTINEL|user path/);
239 });
240
241 test('Mini App reports complex diffs explicitly without approximate content or counts', async () => {
242 const botToken = 'test-only-token';
243 const headers = {
244 'x-telegram-init-data': await signedWebAppData({ botToken })
245 };
246 const large = {
247 ...workspace,
248 originalText: 'a\n'.repeat(600),
249 draftText: 'b\n'.repeat(600)
250 };
251 const workspaces = { getWorkspace: async () => large };
252 const response = await handleWorkspaceEditorRequest(
253 new Request(
254 'https://app.test/workspace/api/editor?workspaceId=W000000001&view=diff',
255 { headers }
256 ),
257 { workspaces, botToken, ownerId: '42' }
258 );
259 assert.equal(response.status, 422);
260 const body = await response.json();
261 assert.equal(body.code, 'DIFF_COMPLEXITY_LIMIT');
262 assert.match(body.error, /کوچک‌تر/);
263 assert.equal(body.diff, undefined);
264 });