Personal Telegram developer console for GitHub

test: define durable workspace store behavior

davoodmehraban89 committed Oct 3, 2026 at 12:29 UTC 1449273aeeaf17b7084de47e9028cd7f9a56bb67
1 file changed +207
test/workspace-store.test.js new
+207
@@ -0,0 +1,207 @@
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 +});