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 429 lines 11.8 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { readFile } from 'node:fs/promises';
4
5 let clientModule = {};
6 let durableModule = {};
7 let indexModule = {};
8 try {
9 clientModule = await import('../src/core/workspace-store.js');
10 } catch {}
11 try {
12 durableModule = await import('../src/durable/workspace-store.js');
13 } catch {}
14 try {
15 indexModule = await import('../src/index.js');
16 } catch {}
17
18 const requireExport = (mod, name) => {
19 assert.equal(typeof mod[name], 'function', `${name} must be implemented`);
20 return mod[name];
21 };
22
23 function makeState() {
24 const data = new Map();
25 let alarm = null;
26 const api = {
27 async get(key) {
28 return data.get(key);
29 },
30 async put(key, value) {
31 data.set(key, structuredClone(value));
32 },
33 async delete(key) {
34 return data.delete(key);
35 },
36 async list() {
37 return new Map(data);
38 }
39 };
40 return {
41 data,
42 get alarm() {
43 return alarm;
44 },
45 storage: {
46 ...api,
47 async transaction(fn) {
48 return fn(api);
49 },
50 async setAlarm(value) {
51 alarm = value;
52 },
53 async deleteAll() {
54 data.clear();
55 alarm = null;
56 }
57 }
58 };
59 }
60
61 function makeNamespace(StoreClass, states = new Map()) {
62 return {
63 states,
64 idFromName(name) {
65 return String(name);
66 },
67 get(id) {
68 if (!states.has(id)) states.set(id, makeState());
69 const state = states.get(id);
70 const instance = new StoreClass(state);
71 return { fetch: (request) => instance.fetch(request) };
72 }
73 };
74 }
75
76 function baseWorkspace(now = 1_000) {
77 return {
78 repoId: 123,
79 repoFullName: 'davoodmehraban89/nexa-dev-bot',
80 baseBranch: 'main',
81 baseHeadSha: 'a'.repeat(40),
82 baseTreeSha: 'b'.repeat(40),
83 path: 'README.md',
84 blobSha: 'c'.repeat(40),
85 originalText: 'before\n',
86 originalDigest: 'd'.repeat(64),
87 expiresAt: now + 30 * 60_000
88 };
89 }
90
91 function baseIntent(workspace, overrides = {}) {
92 return {
93 repoId: workspace.repoId,
94 workspaceId: workspace.id,
95 baseBranch: workspace.baseBranch,
96 baseHeadSha: workspace.baseHeadSha,
97 baseTreeSha: workspace.baseTreeSha,
98 path: workspace.path,
99 sourceBlobSha: workspace.blobSha,
100 contentDigest: '1'.repeat(64),
101 commitMessageDigest: '2'.repeat(64),
102 targetBranch: `nexa/edit-${workspace.id}`,
103 commitTimestamp: '2026-10-03T08:00:00.000Z',
104 intentDigest: '3'.repeat(64),
105 ...overrides
106 };
107 }
108
109 test('DurableWorkspaceStoreClient fails closed without WORKSPACE_STORE', async () => {
110 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
111 const client = new Client();
112 assert.equal(client.isConfigured(), false);
113 await assert.rejects(
114 () => client.getWorkspace({ ownerId: 'owner', workspaceId: 'Ab3_-xYz09' }),
115 /not configured/i
116 );
117 });
118
119 test('workspace store isolates refs and workspaces by owner', async () => {
120 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
121 const Store = requireExport(durableModule, 'WorkspaceStore');
122 const namespace = makeNamespace(Store);
123 const client = new Client({ namespace, now: () => 1_000 });
124
125 const [ref] = await client.createNavigationRefs({
126 ownerId: 'A',
127 refs: [
128 { repoId: 123, path: 'src/app.js', type: 'file', sha: 'a'.repeat(40) }
129 ]
130 });
131 assert.match(ref.refId, /^[A-Za-z0-9_-]{10}$/);
132 assert.equal(
133 (await client.resolveNavigationRef({ ownerId: 'A', refId: ref.refId }))
134 .path,
135 'src/app.js'
136 );
137 assert.equal(
138 await client.resolveNavigationRef({ ownerId: 'B', refId: ref.refId }),
139 null
140 );
141
142 const workspace = await client.createWorkspace({
143 ownerId: 'A',
144 ...baseWorkspace()
145 });
146 assert.match(workspace.id, /^[A-Za-z0-9_-]{10}$/);
147 assert.equal(
148 (await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id }))
149 .path,
150 'README.md'
151 );
152 assert.equal(
153 await client.getWorkspace({ ownerId: 'B', workspaceId: workspace.id }),
154 null
155 );
156 });
157
158 test('workspace navigation refs preserve bounded workspace metadata', async () => {
159 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
160 const Store = requireExport(durableModule, 'WorkspaceStore');
161 const namespace = makeNamespace(Store);
162 const client = new Client({ namespace, now: () => 1_000 });
163
164 const [ref] = await client.createNavigationRefs({
165 ownerId: 'A',
166 refs: [
167 {
168 repoId: 1,
169 workspaceId: 'W000000001',
170 path: 'src/a.js',
171 type: 'workspace-add',
172 sha: 'a'.repeat(40)
173 }
174 ]
175 });
176 assert.deepEqual(
177 await client.resolveNavigationRef({ ownerId: 'A', refId: ref.refId }),
178 {
179 repoId: 1,
180 workspaceId: 'W000000001',
181 path: 'src/a.js',
182 type: 'workspace-add',
183 sha: 'a'.repeat(40),
184 expiresAt: 901000
185 }
186 );
187 });
188
189 test('expired refs and workspaces read as missing and schedule cleanup', async () => {
190 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
191 const Store = requireExport(durableModule, 'WorkspaceStore');
192 let now = 1_000;
193 const namespace = makeNamespace(Store);
194 const client = new Client({ namespace, now: () => now });
195
196 const [ref] = await client.createNavigationRefs({
197 ownerId: 'A',
198 refs: [{ repoId: 1, path: 'a.txt', type: 'file', sha: 'a'.repeat(40) }],
199 ttlMs: 50
200 });
201 const workspace = await client.createWorkspace({
202 ownerId: 'A',
203 ...baseWorkspace(now),
204 expiresAt: now + 50
205 });
206 now = 1_051;
207
208 assert.equal(
209 await client.resolveNavigationRef({ ownerId: 'A', refId: ref.refId }),
210 null
211 );
212 assert.equal(
213 await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id }),
214 null
215 );
216 assert.ok(namespace.states.get('A').alarm !== null);
217 });
218
219 test('pending workspace input is owner scoped and consumed exactly once', async () => {
220 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
221 const Store = requireExport(durableModule, 'WorkspaceStore');
222 const client = new Client({
223 namespace: makeNamespace(Store),
224 now: () => 1_000
225 });
226 const workspace = await client.createWorkspace({
227 ownerId: 'A',
228 ...baseWorkspace()
229 });
230
231 await client.setPendingInput({
232 ownerId: 'A',
233 workspaceId: workspace.id,
234 mode: 'draft'
235 });
236 assert.deepEqual(await client.getPendingInput({ ownerId: 'A' }), {
237 workspaceId: workspace.id,
238 mode: 'draft'
239 });
240 assert.equal(await client.getPendingInput({ ownerId: 'B' }), null);
241 assert.equal(
242 await client.consumePendingInput({
243 ownerId: 'A',
244 workspaceId: workspace.id,
245 mode: 'draft'
246 }),
247 true
248 );
249 assert.equal(
250 await client.consumePendingInput({
251 ownerId: 'A',
252 workspaceId: workspace.id,
253 mode: 'draft'
254 }),
255 false
256 );
257 });
258
259 test('material draft or message changes invalidate READY intent while identical values reuse it', async () => {
260 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
261 const Store = requireExport(durableModule, 'WorkspaceStore');
262 const client = new Client({
263 namespace: makeNamespace(Store),
264 now: () => 1_000
265 });
266 let workspace = await client.createWorkspace({
267 ownerId: 'A',
268 ...baseWorkspace()
269 });
270 workspace = await client.updateDraft({
271 ownerId: 'A',
272 workspaceId: workspace.id,
273 text: 'after\n',
274 digest: '1'.repeat(64)
275 });
276 workspace = await client.setCommitMessage({
277 ownerId: 'A',
278 workspaceId: workspace.id,
279 message: 'test: edit',
280 digest: '2'.repeat(64)
281 });
282
283 const first = await client.sealIntent({
284 ownerId: 'A',
285 workspaceId: workspace.id,
286 intent: baseIntent(workspace)
287 });
288 assert.match(first.id, /^[A-Za-z0-9_-]{8}$/);
289 const same = await client.sealIntent({
290 ownerId: 'A',
291 workspaceId: workspace.id,
292 intent: baseIntent(workspace)
293 });
294 assert.equal(same.id, first.id);
295
296 await client.updateDraft({
297 ownerId: 'A',
298 workspaceId: workspace.id,
299 text: 'after\n',
300 digest: '1'.repeat(64)
301 });
302 assert.equal(
303 (await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id }))
304 .currentIntentId,
305 first.id
306 );
307
308 await client.updateDraft({
309 ownerId: 'A',
310 workspaceId: workspace.id,
311 text: 'changed\n',
312 digest: '9'.repeat(64)
313 });
314 const changed = await client.getWorkspace({
315 ownerId: 'A',
316 workspaceId: workspace.id
317 });
318 assert.equal(changed.currentIntentId, null);
319 assert.equal(changed.state, 'DRAFT');
320 assert.equal(
321 (await client.getIntent({ ownerId: 'A', intentId: first.id })).intentDigest,
322 first.intentDigest
323 );
324 });
325
326 test('sealed intent is immutable and beginCommit is atomic through COMMITTED result', async () => {
327 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
328 const Store = requireExport(durableModule, 'WorkspaceStore');
329 const client = new Client({
330 namespace: makeNamespace(Store),
331 now: () => 1_000
332 });
333 let workspace = await client.createWorkspace({
334 ownerId: 'A',
335 ...baseWorkspace()
336 });
337 workspace = await client.updateDraft({
338 ownerId: 'A',
339 workspaceId: workspace.id,
340 text: 'after\n',
341 digest: '1'.repeat(64)
342 });
343 workspace = await client.setCommitMessage({
344 ownerId: 'A',
345 workspaceId: workspace.id,
346 message: 'test: edit',
347 digest: '2'.repeat(64)
348 });
349 const intent = await client.sealIntent({
350 ownerId: 'A',
351 workspaceId: workspace.id,
352 intent: baseIntent(workspace)
353 });
354
355 const start = await client.beginCommit({ ownerId: 'A', intentId: intent.id });
356 assert.equal(start.disposition, 'start');
357 const resume = await client.beginCommit({
358 ownerId: 'A',
359 intentId: intent.id
360 });
361 assert.equal(resume.disposition, 'resume');
362
363 await client.setCandidateCommit({
364 ownerId: 'A',
365 intentId: intent.id,
366 commitSha: 'e'.repeat(40)
367 });
368 const completed = await client.completeCommit({
369 ownerId: 'A',
370 intentId: intent.id,
371 commitSha: 'e'.repeat(40),
372 branch: intent.targetBranch
373 });
374 assert.equal(completed.state, 'COMMITTED');
375 assert.equal(completed.commitSha, 'e'.repeat(40));
376 assert.equal(completed.branch, intent.targetBranch);
377
378 const again = await client.beginCommit({ ownerId: 'A', intentId: intent.id });
379 assert.equal(again.disposition, 'committed');
380 assert.equal(again.intent.commitSha, 'e'.repeat(40));
381
382 const stored = await client.getIntent({ ownerId: 'A', intentId: intent.id });
383 assert.equal(stored.intentDigest, intent.intentDigest);
384 assert.equal(stored.contentDigest, intent.contentDigest);
385 });
386
387 test('cancelWorkspace removes draft and matching pending input', async () => {
388 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
389 const Store = requireExport(durableModule, 'WorkspaceStore');
390 const client = new Client({
391 namespace: makeNamespace(Store),
392 now: () => 1_000
393 });
394 const workspace = await client.createWorkspace({
395 ownerId: 'A',
396 ...baseWorkspace()
397 });
398 await client.setPendingInput({
399 ownerId: 'A',
400 workspaceId: workspace.id,
401 mode: 'commit_message'
402 });
403 await client.cancelWorkspace({ ownerId: 'A', workspaceId: workspace.id });
404 assert.equal(
405 await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id }),
406 null
407 );
408 assert.equal(await client.getPendingInput({ ownerId: 'A' }), null);
409 });
410
411 test('Worker exports WorkspaceStore and Wrangler declares SQLite durable binding without legacy migrations', async () => {
412 const Store = requireExport(durableModule, 'WorkspaceStore');
413 assert.equal(indexModule.WorkspaceStore, Store);
414 const config = JSON.parse(
415 await readFile(new URL('../wrangler.jsonc', import.meta.url), 'utf8')
416 );
417 assert.ok(
418 config.durable_objects?.bindings?.some(
419 (binding) =>
420 binding.name === 'WORKSPACE_STORE' &&
421 binding.class_name === 'WorkspaceStore'
422 )
423 );
424 assert.deepEqual(config.exports?.WorkspaceStore, {
425 type: 'durable-object',
426 storage: 'sqlite'
427 });
428 assert.equal('migrations' in config, false);
429 });