Personal Telegram developer console for GitHub
test: define workspace draft diff ready contract
davoodmehraban89 committed
Oct 3, 2026 at 13:06 UTC
bacc5fe93b8d9d0477ad6376486355fa1d6fb520
1 file changed
+223
test/github-workspace-integration.test.js
new
+223
@@ -0,0 +1,223 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import { PluginRouter } from '../src/core/router.js';
4
+import { PluginRegistry } from '../src/core/plugin-registry.js';
5
+import { ActionPolicy } from '../src/core/action-policy.js';
6
+import { AuditLogger } from '../src/core/audit.js';
7
+import { createGitHubPlugin } from '../src/plugins/github/plugin.js';
8
+import { digestText } from '../src/core/workspace-policy.js';
9
+
10
+const OWNER = '42';
11
+const WORKSPACE_ID = 'W000000001';
12
+
13
+function flatten(view) {
14
+ return view?.reply_markup?.inline_keyboard?.flat() || [];
15
+}
16
+
17
+function makeMenu() {
18
+ return {
19
+ async getReposListView() { return { text: 'repos', reply_markup: { inline_keyboard: [] } }; },
20
+ async renderByCallback(data) { return { text: `legacy:${data}`, reply_markup: { inline_keyboard: [] } }; }
21
+ };
22
+}
23
+
24
+function makeGitHub() {
25
+ const calls = [];
26
+ return {
27
+ calls,
28
+ async getRepository(repoId) {
29
+ calls.push(['getRepository', Number(repoId)]);
30
+ return { id: String(repoId), name: 'demo', fullName: 'davood/demo', defaultBranch: 'main', archived: false };
31
+ }
32
+ };
33
+}
34
+
35
+function makeWorkspaces() {
36
+ const calls = [];
37
+ let pending = null;
38
+ let intentCounter = 0;
39
+ const intents = new Map();
40
+ const workspace = {
41
+ id: WORKSPACE_ID,
42
+ repoId: 123,
43
+ repoFullName: 'davood/demo',
44
+ baseBranch: 'main',
45
+ baseHeadSha: 'a'.repeat(40),
46
+ baseTreeSha: 'b'.repeat(40),
47
+ path: 'src/app.js',
48
+ blobSha: 'c'.repeat(40),
49
+ originalText: 'before\n',
50
+ originalDigest: '0'.repeat(64),
51
+ draftText: 'before\n',
52
+ draftDigest: '0'.repeat(64),
53
+ commitMessage: '',
54
+ commitMessageDigest: '',
55
+ currentIntentId: null,
56
+ state: 'DRAFT'
57
+ };
58
+
59
+ return {
60
+ calls,
61
+ workspace,
62
+ intents,
63
+ async getWorkspace({ ownerId, workspaceId }) {
64
+ calls.push(['getWorkspace', ownerId, workspaceId]);
65
+ return ownerId === OWNER && workspaceId === WORKSPACE_ID ? structuredClone(workspace) : null;
66
+ },
67
+ async setPendingInput({ ownerId, workspaceId, mode }) {
68
+ calls.push(['setPendingInput', ownerId, workspaceId, mode]);
69
+ pending = { workspaceId, mode };
70
+ return structuredClone(pending);
71
+ },
72
+ async getPendingInput({ ownerId }) {
73
+ calls.push(['getPendingInput', ownerId]);
74
+ return ownerId === OWNER && pending ? structuredClone(pending) : null;
75
+ },
76
+ async consumePendingInput({ ownerId, workspaceId, mode }) {
77
+ calls.push(['consumePendingInput', ownerId, workspaceId, mode]);
78
+ if (ownerId !== OWNER || !pending || pending.workspaceId !== workspaceId || pending.mode !== mode) return false;
79
+ pending = null;
80
+ return true;
81
+ },
82
+ async updateDraft({ ownerId, workspaceId, text, digest }) {
83
+ calls.push(['updateDraft', ownerId, workspaceId, text, digest]);
84
+ workspace.draftText = text;
85
+ workspace.draftDigest = digest;
86
+ workspace.currentIntentId = null;
87
+ workspace.state = 'DRAFT';
88
+ return structuredClone(workspace);
89
+ },
90
+ async setCommitMessage({ ownerId, workspaceId, message, digest }) {
91
+ calls.push(['setCommitMessage', ownerId, workspaceId, message, digest]);
92
+ workspace.commitMessage = message;
93
+ workspace.commitMessageDigest = digest;
94
+ workspace.currentIntentId = null;
95
+ workspace.state = 'DRAFT';
96
+ return structuredClone(workspace);
97
+ },
98
+ async sealIntent({ ownerId, workspaceId, intent }) {
99
+ calls.push(['sealIntent', ownerId, workspaceId, structuredClone(intent)]);
100
+ if (workspace.currentIntentId) {
101
+ const existing = intents.get(workspace.currentIntentId);
102
+ if (existing?.intentDigest === intent.intentDigest) return structuredClone(existing);
103
+ }
104
+ intentCounter += 1;
105
+ const id = `I${String(intentCounter).padStart(7, '0')}`;
106
+ const sealed = { ...structuredClone(intent), id, workspaceId, state: 'READY' };
107
+ intents.set(id, sealed);
108
+ workspace.currentIntentId = id;
109
+ workspace.state = 'READY';
110
+ return structuredClone(sealed);
111
+ }
112
+ };
113
+}
114
+
115
+function makePlugin() {
116
+ const github = makeGitHub();
117
+ const workspaces = makeWorkspaces();
118
+ const plugin = createGitHubPlugin({ github, menu: makeMenu(), workspaces });
119
+ return { github, workspaces, plugin };
120
+}
121
+
122
+test('PluginRouter dispatchMessage applies the same policy boundary without auditing source text', async () => {
123
+ const auditEvents = [];
124
+ const plugin = {
125
+ manifest: { id: 'demo', name: 'Demo', version: 1, commands: [], actions: { 'demo.message': { risk: 'READ' } } },
126
+ resolveMessage(payload) {
127
+ if (!payload?.pending) return null;
128
+ return { actionId: 'demo.message', execute: async () => ({ text: 'accepted' }) };
129
+ }
130
+ };
131
+ const router = new PluginRouter({
132
+ registry: new PluginRegistry([plugin]),
133
+ policy: new ActionPolicy(),
134
+ audit: new AuditLogger({ sink: event => auditEvents.push(event) })
135
+ });
136
+ const result = await router.dispatchMessage({ pending: { mode: 'draft' }, text: 'TOP SECRET SOURCE' }, { isOwner: true, ownerId: OWNER });
137
+ assert.equal(result.text, 'accepted');
138
+ assert.equal(JSON.stringify(auditEvents).includes('TOP SECRET SOURCE'), false);
139
+});
140
+
141
+test('draft input is prepared without GitHub calls and the next owner text is consumed exactly once', async () => {
142
+ const { github, workspaces, plugin } = makePlugin();
143
+ const prepare = plugin.resolveCallback(`wtext:${WORKSPACE_ID}`, { ownerId: OWNER });
144
+ assert.equal(prepare.actionId, 'github.workspace.input.prepare');
145
+ assert.deepEqual(github.calls, []);
146
+ const prompt = await prepare.execute();
147
+ assert.match(prompt.text, /3,000|3000/);
148
+ assert.deepEqual(github.calls, []);
149
+
150
+ const pending = await workspaces.getPendingInput({ ownerId: OWNER });
151
+ assert.deepEqual(pending, { workspaceId: WORKSPACE_ID, mode: 'draft' });
152
+ const route = plugin.resolveMessage({ pending, text: 'after <tag>\n' }, { ownerId: OWNER });
153
+ assert.equal(route.actionId, 'github.workspace.input.apply');
154
+ const view = await route.execute();
155
+ assert.match(view.text, /ذخیره|Draft|پیش.?نویس/i);
156
+ assert.equal(workspaces.workspace.draftText, 'after <tag>\n');
157
+ assert.equal(await workspaces.getPendingInput({ ownerId: OWNER }), null);
158
+
159
+ const late = plugin.resolveMessage({ pending: await workspaces.getPendingInput({ ownerId: OWNER }), text: 'late mutation' }, { ownerId: OWNER });
160
+ assert.equal(late, null);
161
+ assert.equal(workspaces.workspace.draftText, 'after <tag>\n');
162
+ assert.deepEqual(github.calls, []);
163
+});
164
+
165
+test('oversized multibyte draft input is visibly rejected without mutating the old draft', async () => {
166
+ const { workspaces, plugin } = makePlugin();
167
+ await plugin.resolveCallback(`wtext:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
168
+ const before = workspaces.workspace.draftText;
169
+ const pending = await workspaces.getPendingInput({ ownerId: OWNER });
170
+ const route = plugin.resolveMessage({ pending, text: '😀'.repeat(3000) }, { ownerId: OWNER });
171
+ const view = await route.execute();
172
+ assert.match(view.text, /12,000|12000|3,000|3000|مجاز|حد/i);
173
+ assert.equal(workspaces.workspace.draftText, before);
174
+ assert.equal(await workspaces.getPendingInput({ ownerId: OWNER }), null);
175
+});
176
+
177
+test('commit message input normalizes text and diff renders escaped bounded exact counts without GitHub mutation', async () => {
178
+ const { github, workspaces, plugin } = makePlugin();
179
+ await plugin.resolveCallback(`wmsg:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
180
+ const pending = await workspaces.getPendingInput({ ownerId: OWNER });
181
+ await plugin.resolveMessage({ pending, text: ' chore: update workspace ' }, { ownerId: OWNER }).execute();
182
+ assert.equal(workspaces.workspace.commitMessage, 'chore: update workspace');
183
+
184
+ workspaces.workspace.draftText = 'after <script>\n';
185
+ workspaces.workspace.draftDigest = await digestText(workspaces.workspace.draftText);
186
+ const diffRoute = plugin.resolveCallback(`wdiff:${WORKSPACE_ID}`, { ownerId: OWNER });
187
+ assert.equal(diffRoute.actionId, 'github.workspace.diff');
188
+ const diffView = await diffRoute.execute();
189
+ assert.match(diffView.text, /\+1/);
190
+ assert.match(diffView.text, /-1/);
191
+ assert.doesNotMatch(diffView.text, /<script>/);
192
+ assert.match(diffView.text, /<script>/);
193
+ assert.deepEqual(github.calls, []);
194
+});
195
+
196
+test('READY rejects no-change, reuses identical material and mints a new intent after material change', async () => {
197
+ const { github, workspaces, plugin } = makePlugin();
198
+ workspaces.workspace.commitMessage = 'edit app';
199
+ workspaces.workspace.commitMessageDigest = await digestText('edit app');
200
+
201
+ const noChange = plugin.resolveCallback(`wready:${WORKSPACE_ID}`, { ownerId: OWNER });
202
+ await assert.rejects(() => noChange.execute(), /change|تغییر|بدون/i);
203
+
204
+ workspaces.workspace.draftText = 'after\n';
205
+ workspaces.workspace.draftDigest = await digestText('after\n');
206
+ const ready1 = await plugin.resolveCallback(`wready:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
207
+ const commit1 = flatten(ready1).find(button => button.callback_data?.startsWith('wscommit:'))?.callback_data;
208
+ assert.match(commit1, /^wscommit:123:[A-Za-z0-9_-]{8}$/);
209
+ assert.ok(new TextEncoder().encode(commit1).byteLength <= 64);
210
+
211
+ const ready2 = await plugin.resolveCallback(`wready:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
212
+ const commit2 = flatten(ready2).find(button => button.callback_data?.startsWith('wscommit:'))?.callback_data;
213
+ assert.equal(commit2, commit1);
214
+
215
+ workspaces.workspace.draftText = 'after again\n';
216
+ workspaces.workspace.draftDigest = await digestText('after again\n');
217
+ workspaces.workspace.currentIntentId = null;
218
+ workspaces.workspace.state = 'DRAFT';
219
+ const ready3 = await plugin.resolveCallback(`wready:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
220
+ const commit3 = flatten(ready3).find(button => button.callback_data?.startsWith('wscommit:'))?.callback_data;
221
+ assert.notEqual(commit3, commit1);
222
+ assert.deepEqual(github.calls, []);
223
+});