Personal Telegram developer console for GitHub

test: define workspace file upload flow

davoodmehraban89 committed Oct 3, 2026 at 14:31 UTC 8251b3272338076550207118a16533b51d75938e
1 file changed +148
test/github-workspace-file-upload.test.js new
+148
@@ -0,0 +1,148 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { NexaDevBot } from '../src/bot.js';
4 +import { createGitHubPlugin } from '../src/plugins/github/plugin.js';
5 +
6 +const OWNER = '42';
7 +const WORKSPACE_ID = 'W000000001';
8 +
9 +function makeMenu() {
10 + return {
11 + async getReposListView() { return { text: 'repos', reply_markup: { inline_keyboard: [] } }; },
12 + async renderByCallback(data) { return { text: `legacy:${data}`, reply_markup: { inline_keyboard: [] } }; }
13 + };
14 +}
15 +
16 +function makeWorkspaces() {
17 + let pending = null;
18 + const workspace = {
19 + id: WORKSPACE_ID,
20 + repoId: 123,
21 + repoFullName: 'davood/demo',
22 + baseBranch: 'main',
23 + baseHeadSha: 'a'.repeat(40),
24 + baseTreeSha: 'b'.repeat(40),
25 + path: '.gitignore',
26 + blobSha: 'c'.repeat(40),
27 + originalText: 'node_modules/\n',
28 + draftText: 'node_modules/\n',
29 + commitMessage: '',
30 + state: 'DRAFT'
31 + };
32 + return {
33 + workspace,
34 + async getWorkspace({ ownerId, workspaceId }) {
35 + return ownerId === OWNER && workspaceId === WORKSPACE_ID ? structuredClone(workspace) : null;
36 + },
37 + async setPendingInput({ ownerId, workspaceId, mode }) {
38 + assert.equal(ownerId, OWNER);
39 + pending = { workspaceId, mode };
40 + return structuredClone(pending);
41 + },
42 + async getPendingInput({ ownerId }) {
43 + return ownerId === OWNER && pending ? structuredClone(pending) : null;
44 + },
45 + async consumePendingInput({ ownerId, workspaceId, mode }) {
46 + if (ownerId !== OWNER || !pending || pending.workspaceId !== workspaceId || pending.mode !== mode) return false;
47 + pending = null;
48 + return true;
49 + },
50 + async updateDraft({ ownerId, workspaceId, text, digest }) {
51 + assert.equal(ownerId, OWNER);
52 + assert.equal(workspaceId, WORKSPACE_ID);
53 + workspace.draftText = text;
54 + workspace.draftDigest = digest;
55 + workspace.state = 'DRAFT';
56 + return structuredClone(workspace);
57 + }
58 + };
59 +}
60 +
61 +function makePlugin() {
62 + const workspaces = makeWorkspaces();
63 + const github = {
64 + async getRepository(repoId) {
65 + return { id: String(repoId), name: 'demo', fullName: 'davood/demo', defaultBranch: 'main', archived: false };
66 + }
67 + };
68 + return { workspaces, plugin: createGitHubPlugin({ github, menu: makeMenu(), workspaces }) };
69 +}
70 +
71 +test('workspace exposes a primary file-upload input route with Telegram-safe callback data', async () => {
72 + const { workspaces, plugin } = makePlugin();
73 + const workspaceView = await plugin.resolveCallback(`ws:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
74 + const buttons = workspaceView.reply_markup.inline_keyboard.flat();
75 + const upload = buttons.find(button => button.callback_data === `wfile:${WORKSPACE_ID}`);
76 + assert.ok(upload);
77 + assert.match(upload.text, /file|فایل|upload|بارگذاری/i);
78 + assert.ok(new TextEncoder().encode(upload.callback_data).byteLength <= 64);
79 +
80 + const route = plugin.resolveCallback(upload.callback_data, { ownerId: OWNER });
81 + assert.equal(route.actionId, 'github.workspace.input.prepare');
82 + const prompt = await route.execute();
83 + assert.match(prompt.text, /12,?000|12000|UTF-?8|فایل/i);
84 + assert.deepEqual(await workspaces.getPendingInput({ ownerId: OWNER }), {
85 + workspaceId: WORKSPACE_ID,
86 + mode: 'draft_file'
87 + });
88 +});
89 +
90 +test('uploaded text file replaces only the local draft and preserves the repository target path', async () => {
91 + const { workspaces, plugin } = makePlugin();
92 + await plugin.resolveCallback(`wfile:${WORKSPACE_ID}`, { ownerId: OWNER }).execute();
93 + const pending = await workspaces.getPendingInput({ ownerId: OWNER });
94 + const route = plugin.resolveMessage({
95 + pending,
96 + file: { name: 'replacement.txt', text: 'node_modules/\n# smoke\n', byteLength: 22 }
97 + }, { ownerId: OWNER });
98 + assert.equal(route.actionId, 'github.workspace.input.apply');
99 + const view = await route.execute();
100 + assert.match(view.text, /Draft|ذخیره|فایل/i);
101 + assert.equal(workspaces.workspace.path, '.gitignore');
102 + assert.equal(workspaces.workspace.draftText, 'node_modules/\n# smoke\n');
103 + assert.equal(await workspaces.getPendingInput({ ownerId: OWNER }), null);
104 +});
105 +
106 +test('bot downloads a pending workspace document and dispatches its decoded file payload', async () => {
107 + const bot = new NexaDevBot({});
108 + const calls = [];
109 + bot.workspaceStore = {
110 + isConfigured: () => true,
111 + async getPendingInput({ ownerId }) {
112 + calls.push(['getPendingInput', ownerId]);
113 + return { workspaceId: WORKSPACE_ID, mode: 'draft_file' };
114 + }
115 + };
116 + bot.telegram = {
117 + async downloadTextFile(fileId, options) {
118 + calls.push(['downloadTextFile', fileId, options]);
119 + return { text: 'node_modules/\n# smoke\n', byteLength: 22, filePath: 'documents/replacement.txt' };
120 + },
121 + async sendMessage(chatId, text, replyMarkup) {
122 + calls.push(['sendMessage', chatId, text, replyMarkup]);
123 + return { ok: true };
124 + }
125 + };
126 + bot.router = {
127 + async dispatchMessage(payload, context) {
128 + calls.push(['dispatchMessage', structuredClone(payload), structuredClone(context)]);
129 + return { text: 'file accepted', reply_markup: { inline_keyboard: [] } };
130 + }
131 + };
132 +
133 + const result = await bot.handleMessage({
134 + chat: { id: 7 },
135 + from: { id: 42 },
136 + document: { file_id: 'FILE123', file_name: 'replacement.txt', file_size: 22 }
137 + });
138 +
139 + assert.equal(result.workspaceInput, true);
140 + assert.deepEqual(calls.find(call => call[0] === 'downloadTextFile'), ['downloadTextFile', 'FILE123', { maxBytes: 12000 }]);
141 + const dispatch = calls.find(call => call[0] === 'dispatchMessage');
142 + assert.deepEqual(dispatch[1].file, {
143 + name: 'replacement.txt',
144 + text: 'node_modules/\n# smoke\n',
145 + byteLength: 22
146 + });
147 + assert.deepEqual(dispatch[2], { isOwner: true, ownerId: OWNER });
148 +});
\ No newline at end of file