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 205 lines 6.91 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { spawnSync } from 'node:child_process';
4 import { buildUnifiedDiff } from '../src/core/workspace-policy.js';
5
6 // Independent legacy oracle: small fixtures only, never editor-sized matrices.
7 function legacyDiff(before, after, maxChars = 10000) {
8 const a = before.split('\n');
9 const b = after.split('\n');
10 const table = Array.from({ length: a.length + 1 }, () =>
11 Array(b.length + 1).fill(0)
12 );
13 for (let i = a.length - 1; i >= 0; i -= 1) {
14 for (let j = b.length - 1; j >= 0; j -= 1) {
15 table[i][j] =
16 a[i] === b[j]
17 ? table[i + 1][j + 1] + 1
18 : Math.max(table[i + 1][j], table[i][j + 1]);
19 }
20 }
21 const output = ['--- a/file', '+++ b/file', '@@'];
22 let additions = 0;
23 let deletions = 0;
24 let i = 0;
25 let j = 0;
26 while (i < a.length || j < b.length) {
27 if (i < a.length && j < b.length && a[i] === b[j]) {
28 output.push(` ${a[i++]}`);
29 j += 1;
30 } else if (
31 j < b.length &&
32 (i === a.length || table[i][j + 1] >= table[i + 1][j])
33 ) {
34 output.push(`+${b[j++]}`);
35 additions += 1;
36 } else {
37 output.push(`-${a[i++]}`);
38 deletions += 1;
39 }
40 }
41 const raw = output.join('\n');
42 const truncated = raw.length > maxChars;
43 return {
44 text: truncated ? `${raw.slice(0, maxChars - 2)}\n…` : raw,
45 additions,
46 deletions,
47 truncated
48 };
49 }
50
51 function runIsolated(body) {
52 const moduleUrl = new URL('../src/core/workspace-policy.js', import.meta.url);
53 const script = `
54 import assert from 'node:assert/strict';
55 import { buildUnifiedDiff, validateEditableFile } from ${JSON.stringify(moduleUrl.href)};
56 // Reject dangerous allocations before allocating, so the RED regression
57 // cannot reserve the old ~800 MiB matrix in either process.
58 let allocatedBytes = 0;
59 for (const name of ['Uint8Array', 'Uint16Array', 'Uint32Array', 'Int32Array']) {
60 const Original = globalThis[name];
61 globalThis[name] = new Proxy(Original, {
62 construct(target, args) {
63 const bytes = Number(args[0]) * Original.BYTES_PER_ELEMENT;
64 allocatedBytes += bytes;
65 assert.ok(bytes <= 2 * 1024 * 1024, 'oversized diff allocation: ' + bytes);
66 assert.ok(allocatedBytes <= 4 * 1024 * 1024, 'excessive diff allocations');
67 return Reflect.construct(target, args);
68 }
69 });
70 }
71 ${body}
72 `;
73 const child = spawnSync(
74 process.execPath,
75 ['--input-type=module', '--eval', script],
76 {
77 timeout: 5000,
78 maxBuffer: 16384,
79 encoding: 'utf8'
80 }
81 );
82 assert.ifError(child.error);
83 assert.equal(child.status, 0, child.stderr);
84 }
85
86 test('editor-sized identical newline and separated sparse changes stay within a small memory budget', () => {
87 runIsolated(`
88 const identical = '\\n'.repeat(20000);
89 assert.equal(validateEditableFile({ path: 'file', text: identical }).editable, true);
90 const same = buildUnifiedDiff({ before: identical, after: identical, maxChars: 120 });
91 assert.equal(same.additions, 0);
92 assert.equal(same.deletions, 0);
93 assert.equal(same.truncated, true);
94 const lines = Array(19999).fill('');
95 const before = lines.join('\\n');
96 lines[5000] = 'x';
97 lines[15000] = 'y';
98 const after = lines.join('\\n');
99 assert.equal(after.length, 20000);
100 assert.equal(validateEditableFile({ path: 'file', text: after }).editable, true);
101 const sparse = buildUnifiedDiff({ before, after, maxChars: 120 });
102 assert.equal(sparse.additions, 2);
103 assert.equal(sparse.deletions, 2);
104 assert.equal(sparse.truncated, true);
105 assert.ok(sparse.text.length <= 120);
106 `);
107 });
108
109 test('large disjoint and repetitive edits fail explicitly before exhausting memory or work', () => {
110 runIsolated(`
111 const cases = [
112 ['a\\n'.repeat(10000), 'b\\n'.repeat(10000)],
113 ['a\\n'.repeat(5000) + 'b\\n'.repeat(5000), 'b\\n'.repeat(5000) + 'a\\n'.repeat(5000)]
114 ];
115 for (const [before, after] of cases) {
116 assert.throws(() => buildUnifiedDiff({ before, after }), (error) => {
117 assert.equal(error.code, 'DIFF_COMPLEXITY_LIMIT');
118 assert.match(error.message, /smaller/i);
119 return true;
120 });
121 }
122 `);
123 });
124
125 test('large sparse edits exceeding the reconstruction budget fail without approximate counts', () => {
126 runIsolated(`
127 const lines = Array(19988).fill('');
128 const before = lines.join('\\n');
129 for (let i = 0; i < 13; i += 1) lines[1 + 1500 * i] = 'x';
130 const after = lines.join('\\n');
131 assert.equal(after.length, 20000);
132 assert.throws(() => buildUnifiedDiff({ before, after }), {
133 code: 'DIFF_COMPLEXITY_LIMIT'
134 });
135 `);
136 });
137
138 test('ordinary diffs preserve legacy insertion-first ties, exact counts and truncation', () => {
139 let seed = 0x6e657861;
140 const random = (limit) => {
141 seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
142 return seed % limit;
143 };
144 const alphabet = ['', 'a', 'b', 'c', '😀', '\r'];
145 for (let fixture = 0; fixture < 500; fixture += 1) {
146 const makeText = () =>
147 Array.from(
148 { length: random(24) + 1 },
149 () => alphabet[random(alphabet.length)]
150 ).join('\n');
151 const before = makeText();
152 const after = makeText();
153 const maxChars = fixture % 2 ? 10000 : 32;
154 assert.deepEqual(
155 buildUnifiedDiff({ before, after, maxChars }),
156 legacyDiff(before, after, maxChars),
157 `fixture ${fixture}`
158 );
159 }
160 });
161
162 test('large sparse diffs retain canonical ties even in repeated common suffixes', () => {
163 const cases = [
164 [`a\n${Array(500).fill('b').join('\n')}`, Array(499).fill('b').join('\n')],
165 [Array(500).fill('b').join('\n'), `a\n${Array(499).fill('b').join('\n')}`],
166 [
167 Array.from({ length: 500 }, (_, i) => `${i % 7}`).join('\n'),
168 Array.from({ length: 500 }, (_, i) =>
169 i === 100 || i === 400 ? 'x' : `${i % 7}`
170 ).join('\n')
171 ]
172 ];
173 for (const [before, after] of cases) {
174 assert.deepEqual(
175 buildUnifiedDiff({ before, after, maxChars: 10000 }),
176 legacyDiff(before, after)
177 );
178 }
179 });
180
181 test('randomized large sparse diffs match the legacy oracle through band reconstruction', () => {
182 let seed = 0xabcdef;
183 const random = (limit) => {
184 seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
185 return seed % limit;
186 };
187 for (let fixture = 0; fixture < 40; fixture += 1) {
188 const beforeLines = Array.from({ length: 300 }, () => `${random(7)}`);
189 const afterLines = [...beforeLines];
190 for (let edit = 0; edit < 8; edit += 1) {
191 const position = random(afterLines.length);
192 const kind = random(3);
193 if (kind === 0) afterLines.splice(position, 1);
194 else if (kind === 1) afterLines.splice(position, 0, 'x');
195 else afterLines[position] = 'x';
196 }
197 const before = beforeLines.join('\n');
198 const after = afterLines.join('\n');
199 assert.deepEqual(
200 buildUnifiedDiff({ before, after, maxChars: 10000 }),
201 legacyDiff(before, after),
202 `band fixture ${fixture}`
203 );
204 }
205 });