Personal Telegram developer console for GitHub

feat/ux-platform-8-12
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 207 lines 10.2 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 { clientModule = await import('../src/core/workspace-store.js'); } catch {}
9 try { durableModule = await import('../src/durable/workspace-store.js'); } catch {}
10 try { indexModule = await import('../src/index.js'); } catch {}
11
12 const requireExport = (mod, name) => {
13 assert.equal(typeof mod[name], 'function', `${name} must be implemented`);
14 return mod[name];
15 };
16
17 function makeState() {
18 const data = new Map();
19 let alarm = null;
20 const api = {
21 async get(key) { return data.get(key); },
22 async put(key, value) { data.set(key, structuredClone(value)); },
23 async delete(key) { return data.delete(key); },
24 async list() { return new Map(data); }
25 };
26 return {
27 data,
28 get alarm() { return alarm; },
29 storage: {
30 ...api,
31 async transaction(fn) { return fn(api); },
32 async setAlarm(value) { alarm = value; },
33 async deleteAll() { data.clear(); alarm = null; }
34 }
35 };
36 }
37
38 function makeNamespace(StoreClass, states = new Map()) {
39 return {
40 states,
41 idFromName(name) { return String(name); },
42 get(id) {
43 if (!states.has(id)) states.set(id, makeState());
44 const state = states.get(id);
45 const instance = new StoreClass(state);
46 return { fetch: request => instance.fetch(request) };
47 }
48 };
49 }
50
51 function baseWorkspace(now = 1_000) {
52 return {
53 repoId: 123,
54 repoFullName: 'davoodmehraban89/nexa-dev-bot',
55 baseBranch: 'main',
56 baseHeadSha: 'a'.repeat(40),
57 baseTreeSha: 'b'.repeat(40),
58 path: 'README.md',
59 blobSha: 'c'.repeat(40),
60 originalText: 'before\n',
61 originalDigest: 'd'.repeat(64),
62 expiresAt: now + 30 * 60_000
63 };
64 }
65
66 function baseIntent(workspace, overrides = {}) {
67 return {
68 repoId: workspace.repoId,
69 workspaceId: workspace.id,
70 baseBranch: workspace.baseBranch,
71 baseHeadSha: workspace.baseHeadSha,
72 baseTreeSha: workspace.baseTreeSha,
73 path: workspace.path,
74 sourceBlobSha: workspace.blobSha,
75 contentDigest: '1'.repeat(64),
76 commitMessageDigest: '2'.repeat(64),
77 targetBranch: `nexa/edit-${workspace.id}`,
78 commitTimestamp: '2026-10-03T08:00:00.000Z',
79 intentDigest: '3'.repeat(64),
80 ...overrides
81 };
82 }
83
84 test('DurableWorkspaceStoreClient fails closed without WORKSPACE_STORE', async () => {
85 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
86 const client = new Client();
87 assert.equal(client.isConfigured(), false);
88 await assert.rejects(() => client.getWorkspace({ ownerId: 'owner', workspaceId: 'Ab3_-xYz09' }), /not configured/i);
89 });
90
91 test('workspace store isolates refs and workspaces by owner', async () => {
92 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
93 const Store = requireExport(durableModule, 'WorkspaceStore');
94 const namespace = makeNamespace(Store);
95 const client = new Client({ namespace, now: () => 1_000 });
96
97 const [ref] = await client.createNavigationRefs({ ownerId: 'A', refs: [{ repoId: 123, path: 'src/app.js', type: 'file', sha: 'a'.repeat(40) }] });
98 assert.match(ref.refId, /^[A-Za-z0-9_-]{10}$/);
99 assert.equal((await client.resolveNavigationRef({ ownerId: 'A', refId: ref.refId })).path, 'src/app.js');
100 assert.equal(await client.resolveNavigationRef({ ownerId: 'B', refId: ref.refId }), null);
101
102 const workspace = await client.createWorkspace({ ownerId: 'A', ...baseWorkspace() });
103 assert.match(workspace.id, /^[A-Za-z0-9_-]{10}$/);
104 assert.equal((await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id })).path, 'README.md');
105 assert.equal(await client.getWorkspace({ ownerId: 'B', workspaceId: workspace.id }), null);
106 });
107
108 test('expired refs and workspaces read as missing and schedule cleanup', async () => {
109 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
110 const Store = requireExport(durableModule, 'WorkspaceStore');
111 let now = 1_000;
112 const namespace = makeNamespace(Store);
113 const client = new Client({ namespace, now: () => now });
114
115 const [ref] = await client.createNavigationRefs({ ownerId: 'A', refs: [{ repoId: 1, path: 'a.txt', type: 'file', sha: 'a'.repeat(40) }], ttlMs: 50 });
116 const workspace = await client.createWorkspace({ ownerId: 'A', ...baseWorkspace(now), expiresAt: now + 50 });
117 now = 1_051;
118
119 assert.equal(await client.resolveNavigationRef({ ownerId: 'A', refId: ref.refId }), null);
120 assert.equal(await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id }), null);
121 assert.ok(namespace.states.get('A').alarm !== null);
122 });
123
124 test('pending workspace input is owner scoped and consumed exactly once', async () => {
125 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
126 const Store = requireExport(durableModule, 'WorkspaceStore');
127 const client = new Client({ namespace: makeNamespace(Store), now: () => 1_000 });
128 const workspace = await client.createWorkspace({ ownerId: 'A', ...baseWorkspace() });
129
130 await client.setPendingInput({ ownerId: 'A', workspaceId: workspace.id, mode: 'draft' });
131 assert.deepEqual(await client.getPendingInput({ ownerId: 'A' }), { workspaceId: workspace.id, mode: 'draft' });
132 assert.equal(await client.getPendingInput({ ownerId: 'B' }), null);
133 assert.equal(await client.consumePendingInput({ ownerId: 'A', workspaceId: workspace.id, mode: 'draft' }), true);
134 assert.equal(await client.consumePendingInput({ ownerId: 'A', workspaceId: workspace.id, mode: 'draft' }), false);
135 });
136
137 test('material draft or message changes invalidate READY intent while identical values reuse it', async () => {
138 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
139 const Store = requireExport(durableModule, 'WorkspaceStore');
140 const client = new Client({ namespace: makeNamespace(Store), now: () => 1_000 });
141 let workspace = await client.createWorkspace({ ownerId: 'A', ...baseWorkspace() });
142 workspace = await client.updateDraft({ ownerId: 'A', workspaceId: workspace.id, text: 'after\n', digest: '1'.repeat(64) });
143 workspace = await client.setCommitMessage({ ownerId: 'A', workspaceId: workspace.id, message: 'test: edit', digest: '2'.repeat(64) });
144
145 const first = await client.sealIntent({ ownerId: 'A', workspaceId: workspace.id, intent: baseIntent(workspace) });
146 assert.match(first.id, /^[A-Za-z0-9_-]{8}$/);
147 const same = await client.sealIntent({ ownerId: 'A', workspaceId: workspace.id, intent: baseIntent(workspace) });
148 assert.equal(same.id, first.id);
149
150 await client.updateDraft({ ownerId: 'A', workspaceId: workspace.id, text: 'after\n', digest: '1'.repeat(64) });
151 assert.equal((await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id })).currentIntentId, first.id);
152
153 await client.updateDraft({ ownerId: 'A', workspaceId: workspace.id, text: 'changed\n', digest: '9'.repeat(64) });
154 const changed = await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id });
155 assert.equal(changed.currentIntentId, null);
156 assert.equal(changed.state, 'DRAFT');
157 assert.equal((await client.getIntent({ ownerId: 'A', intentId: first.id })).intentDigest, first.intentDigest);
158 });
159
160 test('sealed intent is immutable and beginCommit is atomic through COMMITTED result', async () => {
161 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
162 const Store = requireExport(durableModule, 'WorkspaceStore');
163 const client = new Client({ namespace: makeNamespace(Store), now: () => 1_000 });
164 let workspace = await client.createWorkspace({ ownerId: 'A', ...baseWorkspace() });
165 workspace = await client.updateDraft({ ownerId: 'A', workspaceId: workspace.id, text: 'after\n', digest: '1'.repeat(64) });
166 workspace = await client.setCommitMessage({ ownerId: 'A', workspaceId: workspace.id, message: 'test: edit', digest: '2'.repeat(64) });
167 const intent = await client.sealIntent({ ownerId: 'A', workspaceId: workspace.id, intent: baseIntent(workspace) });
168
169 const start = await client.beginCommit({ ownerId: 'A', intentId: intent.id });
170 assert.equal(start.disposition, 'start');
171 const resume = await client.beginCommit({ ownerId: 'A', intentId: intent.id });
172 assert.equal(resume.disposition, 'resume');
173
174 await client.setCandidateCommit({ ownerId: 'A', intentId: intent.id, commitSha: 'e'.repeat(40) });
175 const completed = await client.completeCommit({ ownerId: 'A', intentId: intent.id, commitSha: 'e'.repeat(40), branch: intent.targetBranch });
176 assert.equal(completed.state, 'COMMITTED');
177 assert.equal(completed.commitSha, 'e'.repeat(40));
178 assert.equal(completed.branch, intent.targetBranch);
179
180 const again = await client.beginCommit({ ownerId: 'A', intentId: intent.id });
181 assert.equal(again.disposition, 'committed');
182 assert.equal(again.intent.commitSha, 'e'.repeat(40));
183
184 const stored = await client.getIntent({ ownerId: 'A', intentId: intent.id });
185 assert.equal(stored.intentDigest, intent.intentDigest);
186 assert.equal(stored.contentDigest, intent.contentDigest);
187 });
188
189 test('cancelWorkspace removes draft and matching pending input', async () => {
190 const Client = requireExport(clientModule, 'DurableWorkspaceStoreClient');
191 const Store = requireExport(durableModule, 'WorkspaceStore');
192 const client = new Client({ namespace: makeNamespace(Store), now: () => 1_000 });
193 const workspace = await client.createWorkspace({ ownerId: 'A', ...baseWorkspace() });
194 await client.setPendingInput({ ownerId: 'A', workspaceId: workspace.id, mode: 'commit_message' });
195 await client.cancelWorkspace({ ownerId: 'A', workspaceId: workspace.id });
196 assert.equal(await client.getWorkspace({ ownerId: 'A', workspaceId: workspace.id }), null);
197 assert.equal(await client.getPendingInput({ ownerId: 'A' }), null);
198 });
199
200 test('Worker exports WorkspaceStore and Wrangler declares SQLite durable binding without legacy migrations', async () => {
201 const Store = requireExport(durableModule, 'WorkspaceStore');
202 assert.equal(indexModule.WorkspaceStore, Store);
203 const config = JSON.parse(await readFile(new URL('../wrangler.jsonc', import.meta.url), 'utf8'));
204 assert.ok(config.durable_objects?.bindings?.some(binding => binding.name === 'WORKSPACE_STORE' && binding.class_name === 'WorkspaceStore'));
205 assert.deepEqual(config.exports?.WorkspaceStore, { type: 'durable-object', storage: 'sqlite' });
206 assert.equal('migrations' in config, false);
207 });