Personal Telegram developer console for GitHub

fix/editor-safe-file-options
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 139 lines 6.4 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: [], textContent: '', className: '', disabled: true,
12 get value() {
13 if (tagName === 'select') return this.children.find(option => option.value === value)?.value || '';
14 return value ?? (tagName === 'option' ? this.textContent : '');
15 },
16 set value(next) { value = String(next); },
17 replaceChildren(...children) { this.children = children; },
18 appendChild(child) { this.children.push(child); },
19 addEventListener(type, listener) { listeners.set(type, listener); },
20 async dispatch(type) {
21 assert.ok(listeners.has(type), `missing ${type} listener`);
22 await listeners.get(type)();
23 }
24 };
25 }
26
27 async function bootEditor(workspace, search = '?workspaceId=W000000001') {
28 const elements = Object.fromEntries(['editor', 'status', 'path', 'files', 'counter', 'save', 'close']
29 .map(id => [id, element(id === 'files' ? 'select' : id)]));
30 const requests = [];
31 const location = { search };
32 // Both load success and failure display status. Await that observable completion,
33 // rather than sleeping or guessing how many promise microtasks need draining.
34 let displayStatus;
35 const loaded = new Promise(resolve => { displayStatus = resolve; });
36 let statusClassName = '';
37 Object.defineProperty(elements.status, 'className', {
38 get: () => statusClassName,
39 set(value) { statusClassName = value; displayStatus(); }
40 });
41 const script = renderWorkspaceEditorPage().match(/<script>\s*([\s\S]*?)<\/script>/)[1];
42 vm.runInNewContext(script, {
43 document: { getElementById: id => elements[id], createElement: element },
44 window: { Telegram: { WebApp: { initData: 'signed telegram data', ready() {}, expand() {} } } },
45 location, URLSearchParams, TextEncoder,
46 async fetch(url, options) {
47 requests.push({ url, ...JSON.parse(JSON.stringify(options)) });
48 return {
49 ok: true, status: 200,
50 async json() {
51 return options.method === 'PUT'
52 ? { ok: true, workspace: { id: 'W000000001', path: workspace.path, state: 'DRAFT' } }
53 : { workspace, limits: { maxChars: 20000, maxBytes: 65536 } };
54 }
55 };
56 }
57 });
58 await loaded;
59 assert.equal(elements.editor.disabled, false, elements.status.textContent);
60 assert.equal(elements.save.disabled, false, elements.status.textContent);
61 return { elements, requests, location };
62 }
63
64 const quoted = 'src/owner\'s "draft" & notes.md';
65 const entity = 'docs/&quot;&amp;&#39;&#x3C;.md';
66 const markup = 'docs/<img src=x onerror=alert(1)>.md';
67 const unicode = 'docs/راهنما-日本語-🧪.md';
68 const filenames = [quoted, entity, markup, unicode];
69 const workspace = {
70 id: 'W000000001', path: entity, files: filenames.map(path => ({ path })),
71 state: 'DRAFT', baseHeadSha: 'abcdef1234', draftText: '# existing draft\n'
72 };
73
74 test('editor preserves exact option labels and values for repository-controlled filenames', async () => {
75 const { elements, requests } = await bootEditor(workspace,
76 '?workspaceId=W000000001&path=docs%2F%26quot%3B%26amp%3B%26%2339%3B%26%23x3C%3B.md');
77 assert.equal(elements.files.children.length, 4);
78 assert.deepEqual(elements.files.children.map(option => option.textContent), filenames);
79 assert.deepEqual(elements.files.children.map(option => option.value), filenames);
80 assert.equal(elements.files.value, entity);
81 assert.equal(elements.path.textContent, entity);
82 assert.equal(elements.editor.value, '# existing draft\n');
83 assert.deepEqual(requests, [{
84 url: '/workspace/api/editor?workspaceId=W000000001&path=docs%2F%26quot%3B%26amp%3B%26%2339%3B%26%23x3C%3B.md',
85 headers: { 'x-telegram-init-data': 'signed telegram data' }
86 }]);
87 });
88
89 test('saving sends the selected literal filename and draft with Telegram authentication', async () => {
90 const { elements, requests } = await bootEditor(workspace);
91 elements.editor.value = '# edited & <literal> 🧪\n';
92 await elements.save.dispatch('click');
93 assert.equal(requests.length, 2);
94 const saved = requests[1];
95 assert.equal(saved.url, '/workspace/api/editor');
96 assert.equal(saved.method, 'PUT');
97 assert.deepEqual(saved.headers, {
98 'content-type': 'application/json', 'x-telegram-init-data': 'signed telegram data'
99 });
100 assert.deepEqual(JSON.parse(saved.body), {
101 workspaceId: 'W000000001', path: 'docs/&quot;&amp;&#39;&#x3C;.md', text: '# edited & <literal> 🧪\n'
102 });
103 assert.equal(elements.status.className, 'status ok');
104 assert.equal(elements.save.disabled, false);
105 });
106
107 test('legacy workspace without a files array exposes its one literal filename', async () => {
108 const { elements } = await bootEditor({
109 id: 'W000000001', path: 'legacy/&quot; & "draft".md', state: 'DRAFT',
110 baseHeadSha: 'abcdef1234', draftText: 'legacy draft'
111 });
112 assert.equal(elements.files.children.length, 1);
113 assert.equal(elements.files.children[0].textContent, 'legacy/&quot; & "draft".md');
114 assert.equal(elements.files.children[0].value, 'legacy/&quot; & "draft".md');
115 assert.equal(elements.files.value, 'legacy/&quot; & "draft".md');
116 assert.equal(elements.editor.value, 'legacy draft');
117 });
118
119 test('changing files preserves special filenames through navigation and the next load', async () => {
120 const first = await bootEditor(workspace, '?workspaceId=W000000001&source=telegram');
121 for (const filename of filenames) {
122 first.elements.files.value = filename;
123 await first.elements.files.dispatch('change');
124 const params = new URLSearchParams(first.location.search);
125 assert.equal(params.get('path'), filename);
126 assert.equal(params.get('workspaceId'), 'W000000001');
127 assert.equal(params.get('source'), 'telegram');
128 const next = await bootEditor({ ...workspace, path: filename }, first.location.search);
129 assert.equal(new URL(next.requests[0].url, 'https://example.test').searchParams.get('path'), filename);
130 assert.equal(next.elements.files.value, filename);
131 assert.equal(next.elements.path.textContent, filename);
132 }
133 });
134
135 test('editor option construction avoids parser-dependent HTML interpolation', () => {
136 const page = renderWorkspaceEditorPage();
137 assert.doesNotMatch(page, /files\.innerHTML\s*=/);
138 assert.doesNotMatch(page, /<option value=['\"][^\n]*file\.path/);
139 });