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 246 lines 8.1 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import vm from 'node:vm';
4 import { renderWorkspaceEditorPage } from '../src/workspace-editor.js';
5
6 // These doubles exercise the rendered script's DOM/API boundaries, not HTML parsing.
7 function element(tagName) {
8 const listeners = new Map();
9 let value;
10 return {
11 children: [],
12 textContent: '',
13 className: '',
14 disabled: true,
15 get value() {
16 if (tagName === 'select')
17 return (
18 this.children.find((option) => option.value === value)?.value || ''
19 );
20 return value ?? (tagName === 'option' ? this.textContent : '');
21 },
22 set value(next) {
23 value = String(next);
24 },
25 replaceChildren(...children) {
26 this.children = children;
27 },
28 appendChild(child) {
29 this.children.push(child);
30 },
31 addEventListener(type, listener) {
32 listeners.set(type, listener);
33 },
34 async dispatch(type) {
35 assert.ok(listeners.has(type), `missing ${type} listener`);
36 await listeners.get(type)();
37 }
38 };
39 }
40
41 async function bootEditor(workspace, search = '?workspaceId=W000000001', diff) {
42 const elements = Object.fromEntries(
43 ['editor', 'status', 'path', 'files', 'counter', 'save', 'close'].map(
44 (id) => [id, element(id === 'files' ? 'select' : id)]
45 )
46 );
47 const requests = [];
48 const location = { search };
49 // Both load success and failure display status. Await that observable completion,
50 // rather than sleeping or guessing how many promise microtasks need draining.
51 let displayStatus;
52 const loaded = new Promise((resolve) => {
53 displayStatus = resolve;
54 });
55 let statusClassName = '';
56 Object.defineProperty(elements.status, 'className', {
57 get: () => statusClassName,
58 set(value) {
59 statusClassName = value;
60 displayStatus();
61 }
62 });
63 const script = renderWorkspaceEditorPage().match(
64 /<script>\s*([\s\S]*?)<\/script>/
65 )[1];
66 vm.runInNewContext(script, {
67 document: { getElementById: (id) => elements[id], createElement: element },
68 window: {
69 Telegram: {
70 WebApp: { initData: 'signed telegram data', ready() {}, expand() {} }
71 }
72 },
73 location,
74 URLSearchParams,
75 TextEncoder,
76 async fetch(url, options) {
77 requests.push({ url, ...JSON.parse(JSON.stringify(options)) });
78 return {
79 ok: true,
80 status: 200,
81 async json() {
82 return options.method === 'PUT'
83 ? {
84 ok: true,
85 workspace: {
86 id: 'W000000001',
87 path: workspace.path,
88 state: 'DRAFT'
89 }
90 }
91 : { workspace, diff, limits: { maxChars: 20000, maxBytes: 65536 } };
92 }
93 };
94 }
95 });
96 await loaded;
97 assert.equal(elements.editor.disabled, false, elements.status.textContent);
98 assert.equal(
99 elements.save.disabled,
100 workspace.readOnly === true,
101 elements.status.textContent
102 );
103 return { elements, requests, location };
104 }
105
106 const quoted = 'src/owner\'s "draft" & notes.md';
107 const entity = 'docs/&quot;&amp;&#39;&#x3C;.md';
108 const markup = 'docs/<img src=x onerror=alert(1)>.md';
109 const unicode = 'docs/راهنما-日本語-🧪.md';
110 const filenames = [quoted, entity, markup, unicode];
111 const workspace = {
112 id: 'W000000001',
113 path: entity,
114 files: filenames.map((path) => ({ path })),
115 state: 'DRAFT',
116 baseHeadSha: 'abcdef1234',
117 draftText: '# existing draft\n'
118 };
119
120 test('editor preserves exact option labels and values for repository-controlled filenames', async () => {
121 const { elements, requests } = await bootEditor(
122 workspace,
123 '?workspaceId=W000000001&path=docs%2F%26quot%3B%26amp%3B%26%2339%3B%26%23x3C%3B.md'
124 );
125 assert.equal(elements.files.children.length, 4);
126 assert.deepEqual(
127 elements.files.children.map((option) => option.textContent),
128 filenames
129 );
130 assert.deepEqual(
131 elements.files.children.map((option) => option.value),
132 filenames
133 );
134 assert.equal(elements.files.value, entity);
135 assert.equal(elements.path.textContent, entity);
136 assert.equal(elements.editor.value, '# existing draft\n');
137 assert.deepEqual(requests, [
138 {
139 url: '/workspace/api/editor?workspaceId=W000000001&path=docs%2F%26quot%3B%26amp%3B%26%2339%3B%26%23x3C%3B.md',
140 headers: { 'x-telegram-init-data': 'signed telegram data' }
141 }
142 ]);
143 });
144
145 test('saving sends the selected literal filename and draft with Telegram authentication', async () => {
146 const { elements, requests } = await bootEditor(workspace);
147 elements.editor.value = '# edited & <literal> 🧪\n';
148 await elements.save.dispatch('click');
149 assert.equal(requests.length, 2);
150 const saved = requests[1];
151 assert.equal(saved.url, '/workspace/api/editor');
152 assert.equal(saved.method, 'PUT');
153 assert.deepEqual(saved.headers, {
154 'content-type': 'application/json',
155 'x-telegram-init-data': 'signed telegram data'
156 });
157 assert.deepEqual(JSON.parse(saved.body), {
158 workspaceId: 'W000000001',
159 path: 'docs/&quot;&amp;&#39;&#x3C;.md',
160 text: '# edited & <literal> 🧪\n'
161 });
162 assert.equal(elements.status.className, 'status ok');
163 assert.equal(elements.save.disabled, false);
164 });
165
166 test('legacy workspace without a files array exposes its one literal filename', async () => {
167 const { elements } = await bootEditor({
168 id: 'W000000001',
169 path: 'legacy/&quot; & "draft".md',
170 state: 'DRAFT',
171 baseHeadSha: 'abcdef1234',
172 draftText: 'legacy draft'
173 });
174 assert.equal(elements.files.children.length, 1);
175 assert.equal(
176 elements.files.children[0].textContent,
177 'legacy/&quot; & "draft".md'
178 );
179 assert.equal(elements.files.children[0].value, 'legacy/&quot; & "draft".md');
180 assert.equal(elements.files.value, 'legacy/&quot; & "draft".md');
181 assert.equal(elements.editor.value, 'legacy draft');
182 });
183
184 test('changing files preserves special filenames through navigation and the next load', async () => {
185 const first = await bootEditor(
186 workspace,
187 '?workspaceId=W000000001&source=telegram'
188 );
189 for (const filename of filenames) {
190 first.elements.files.value = filename;
191 await first.elements.files.dispatch('change');
192 const params = new URLSearchParams(first.location.search);
193 assert.equal(params.get('path'), filename);
194 assert.equal(params.get('workspaceId'), 'W000000001');
195 assert.equal(params.get('source'), 'telegram');
196 const next = await bootEditor(
197 { ...workspace, path: filename },
198 first.location.search
199 );
200 assert.equal(
201 new URL(next.requests[0].url, 'https://example.test').searchParams.get(
202 'path'
203 ),
204 filename
205 );
206 assert.equal(next.elements.files.value, filename);
207 assert.equal(next.elements.path.textContent, filename);
208 }
209 });
210
211 test('editor option construction avoids parser-dependent HTML interpolation', () => {
212 const page = renderWorkspaceEditorPage();
213 assert.doesNotMatch(page, /files\.innerHTML\s*=/);
214 assert.doesNotMatch(page, /<option value=['\"][^\n]*file\.path/);
215 });
216
217 test('read-only file preview uses the owner-bound reference and cannot save a draft', async () => {
218 const { elements, requests } = await bootEditor(
219 { ...workspace, readOnly: true },
220 '?refId=R000000001'
221 );
222 assert.equal(requests[0].url, '/workspace/api/editor?refId=R000000001');
223 assert.equal(elements.editor.readOnly, true);
224 assert.equal(elements.editor.value, '# existing draft\n');
225 assert.equal(elements.save.hidden, true);
226 await elements.save.dispatch('click');
227 assert.equal(requests.length, 1);
228 });
229
230 test('read-only diff displays API text and counts without exposing a save action', async () => {
231 const { elements, requests } = await bootEditor(
232 { ...workspace, readOnly: true },
233 '?workspaceId=W000000001&view=diff',
234 { text: '+literal diff', additions: 1, deletions: 0, truncated: false }
235 );
236 assert.equal(
237 requests[0].url,
238 '/workspace/api/editor?workspaceId=W000000001&view=diff'
239 );
240 assert.equal(elements.editor.value, '+literal diff');
241 assert.match(elements.status.textContent, /Diff.*\+1.*-0/);
242 assert.equal(elements.editor.readOnly, true);
243 assert.equal(elements.save.hidden, true);
244 await elements.save.dispatch('click');
245 assert.equal(requests.length, 1);
246 });