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 221 lines 7.17 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { DurableWorkspaceStoreClient } from '../src/core/workspace-store.js';
4 import { WorkspaceStore } from '../src/durable/workspace-store.js';
5 import { createWorkspaceController } from '../src/plugins/github/workspace.js';
6
7 const OWNER = '42';
8 const EDITOR_URL = 'https://example.com/workspace/editor';
9 const ORIGINAL = 'node_modules/\n';
10 const SENTINEL = 'SOURCE_MUST_STAY_OUT_OF_TELEGRAM_WORKFLOW';
11
12 async function fixture(editorUrl = EDITOR_URL) {
13 const states = new Map();
14 const workspaces = new DurableWorkspaceStoreClient({
15 now: () => 1000,
16 namespace: {
17 idFromName: (name) => String(name),
18 get(id) {
19 if (!states.has(id)) {
20 const data = new Map();
21 const storage = {
22 async get(key) {
23 return data.get(key);
24 },
25 async put(key, value) {
26 data.set(key, structuredClone(value));
27 },
28 async delete(key) {
29 return data.delete(key);
30 },
31 async list() {
32 return new Map(data);
33 },
34 async transaction(fn) {
35 return fn(storage);
36 },
37 async setAlarm() {}
38 };
39 states.set(id, new WorkspaceStore({ storage }));
40 }
41 return { fetch: (request) => states.get(id).fetch(request) };
42 }
43 }
44 });
45 const workspace = await workspaces.createWorkspace({
46 ownerId: OWNER,
47 repoId: 123,
48 repoFullName: 'davood/demo',
49 baseBranch: 'main',
50 baseHeadSha: 'a'.repeat(40),
51 baseTreeSha: 'b'.repeat(40),
52 path: '.gitignore',
53 blobSha: 'c'.repeat(40),
54 originalText: ORIGINAL,
55 originalDigest: 'd'.repeat(64)
56 });
57 return {
58 workspace,
59 workspaces,
60 controller: createWorkspaceController({ github: {}, workspaces, editorUrl })
61 };
62 }
63
64 function buttons(view) {
65 return view.reply_markup.inline_keyboard.flat();
66 }
67
68 function assertEditor(view, workspaceId) {
69 const editor = buttons(view).find((button) => button.web_app);
70 assert.ok(editor, 'the rejected source-input route must offer the Mini App');
71 assert.equal(editor.web_app.url, `${EDITOR_URL}?workspaceId=${workspaceId}`);
72 assert.doesNotMatch(JSON.stringify(view), new RegExp(SENTINEL));
73 }
74
75 test('workspace offers only Mini App editing even when the editor is unavailable', async () => {
76 for (const editorUrl of [EDITOR_URL, '', 'http://example.com/editor']) {
77 const { controller, workspace, workspaces } = await fixture(editorUrl);
78 await workspaces.upsertWorkspaceFile({
79 ownerId: OWNER,
80 workspaceId: workspace.id,
81 file: {
82 path: 'README.md',
83 blobSha: 'e'.repeat(40),
84 originalText: '# Demo\n',
85 draftText: '# Demo\n',
86 originalDigest: 'f'.repeat(64),
87 draftDigest: 'f'.repeat(64)
88 }
89 });
90 const view = await controller
91 .resolveCallback(`ws:${workspace.id}`, { ownerId: OWNER })
92 .execute();
93 assert.ok(
94 view.reply_markup.inline_keyboard.every((row) => row.length > 0),
95 'the multi-file workspace must send usable keyboard rows'
96 );
97 assert.equal(
98 buttons(view).some((button) =>
99 /^w(?:text|file):/.test(button.callback_data || '')
100 ),
101 false,
102 `Telegram source input must remain disabled for ${editorUrl || 'missing editor'}`
103 );
104 if (editorUrl === EDITOR_URL)
105 assert.deepEqual(
106 buttons(view)
107 .filter((button) => button.web_app)
108 .map((button) => button.web_app.url),
109 [
110 `${EDITOR_URL}?workspaceId=${workspace.id}&path=.gitignore`,
111 `${EDITOR_URL}?workspaceId=${workspace.id}&path=README.md`
112 ]
113 );
114 else {
115 assert.equal(
116 buttons(view).some((button) => button.web_app),
117 false
118 );
119 assert.match(view.text, /Editor.*دسترس|Mini App.*دسترس/);
120 }
121 }
122 });
123
124 for (const [prefix, mode] of [
125 ['wtext', 'draft'],
126 ['wfile', 'draft_file']
127 ]) {
128 test(`stale ${prefix} callback clears its old pending source input and redirects to Mini App`, async () => {
129 const { controller, workspace, workspaces } = await fixture();
130 await workspaces.setPendingInput({
131 ownerId: OWNER,
132 workspaceId: workspace.id,
133 mode
134 });
135 const view = await controller
136 .resolveCallback(`${prefix}:${workspace.id}`, { ownerId: OWNER })
137 .execute();
138 assert.equal(await workspaces.getPendingInput({ ownerId: OWNER }), null);
139 assertEditor(view, workspace.id);
140 const saved = await workspaces.getWorkspace({
141 ownerId: OWNER,
142 workspaceId: workspace.id
143 });
144 assert.equal(saved.draftText, ORIGINAL);
145 });
146
147 test(`stale ${prefix} callback preserves an active commit-message input`, async () => {
148 const { controller, workspace, workspaces } = await fixture();
149 await workspaces.setPendingInput({
150 ownerId: OWNER,
151 workspaceId: workspace.id,
152 mode: 'commit_message'
153 });
154 const view = await controller
155 .resolveCallback(`${prefix}:${workspace.id}`, { ownerId: OWNER })
156 .execute();
157 assert.deepEqual(await workspaces.getPendingInput({ ownerId: OWNER }), {
158 workspaceId: workspace.id,
159 mode: 'commit_message'
160 });
161 assertEditor(view, workspace.id);
162 });
163
164 test(`pending ${mode} is consumed without accessing or saving Telegram source fields`, async () => {
165 const { controller, workspace, workspaces } = await fixture();
166 await workspaces.setPendingInput({
167 ownerId: OWNER,
168 workspaceId: workspace.id,
169 mode
170 });
171 const pending = await workspaces.getPendingInput({ ownerId: OWNER });
172 let sourceReads = 0;
173 const payload = {
174 pending,
175 get text() {
176 sourceReads += 1;
177 return SENTINEL;
178 },
179 get file() {
180 sourceReads += 1;
181 return { name: 'replacement.txt', text: SENTINEL, byteLength: 100 };
182 }
183 };
184 const view = await controller
185 .resolveMessage(payload, { ownerId: OWNER })
186 .execute();
187 assert.equal(
188 sourceReads,
189 0,
190 'disabled source input must not inspect Telegram body fields'
191 );
192 assert.equal(await workspaces.getPendingInput({ ownerId: OWNER }), null);
193 const saved = await workspaces.getWorkspace({
194 ownerId: OWNER,
195 workspaceId: workspace.id
196 });
197 assert.equal(saved.draftText, ORIGINAL);
198 assert.equal(saved.files[0].draftText, ORIGINAL);
199 assertEditor(view, workspace.id);
200 });
201
202 test(`pending ${mode} redirects with metadata alone and cannot be replayed`, async () => {
203 const { controller, workspace, workspaces } = await fixture();
204 await workspaces.setPendingInput({
205 ownerId: OWNER,
206 workspaceId: workspace.id,
207 mode
208 });
209 const pending = await workspaces.getPendingInput({ ownerId: OWNER });
210 const route = controller.resolveMessage({ pending }, { ownerId: OWNER });
211 const view = await route.execute();
212 assertEditor(view, workspace.id);
213 assert.equal(await workspaces.getPendingInput({ ownerId: OWNER }), null);
214 await assert.rejects(() => route.execute(), /consumed|expired/i);
215 const saved = await workspaces.getWorkspace({
216 ownerId: OWNER,
217 workspaceId: workspace.id
218 });
219 assert.equal(saved.draftText, ORIGINAL);
220 });
221 }