Personal Telegram developer console for GitHub
test: align navigation smoke with persistent menu bootstrap
davoodmehraban89 committed
Oct 4, 2026 at 07:04 UTC
be2d28c17ac15b444ab9383e82dfb5fe3b9bbc37
1 file changed
+68
-187
test/navigation.test.js
+68
-187
@@ -3,30 +3,19 @@ import assert from 'node:assert/strict';
3
import worker from '../src/index.js';
4
5
const env = {
6
- TELEGRAM_WEBHOOK_SECRET: 'right',
7
- OWNER_TELEGRAM_ID: '123',
8
- TELEGRAM_BOT_TOKEN: 'telegram-test-token',
9
- GITHUB_TOKEN: 'github-test-token',
6
+ TELEGRAM_WEBHOOK_SECRET: 'right', OWNER_TELEGRAM_ID: '123',
7
+ TELEGRAM_BOT_TOKEN: 'telegram-test-token', GITHUB_TOKEN: 'github-test-token',
8
GITHUB_OWNER: 'davoodmehraban89'
9
};
10
11
function webhook(update) {
12
return new Request('https://example.test/telegram/webhook', {
13
method: 'POST',
16
- headers: {
17
- 'content-type': 'application/json',
18
- 'X-Telegram-Bot-Api-Secret-Token': 'right'
19
- },
14
+ headers: { 'content-type': 'application/json', 'X-Telegram-Bot-Api-Secret-Token': 'right' },
15
body: JSON.stringify(update)
16
});
17
}
23
-
24
-function json(data, status = 200) {
25
- return new Response(JSON.stringify(data), {
26
- status,
27
- headers: { 'content-type': 'application/json' }
28
- });
29
-}
18
+const json = (data, status = 200) => new Response(JSON.stringify(data), { status, headers: { 'content-type': 'application/json' } });
19
20
function installApiMock() {
21
const telegramCalls = [];
@@ -38,111 +27,49 @@ function installApiMock() {
27
28
globalThis.fetch = async (url, options = {}) => {
29
const value = String(url);
41
-
30
if (value.startsWith('https://api.telegram.org/')) {
31
const method = value.split('/').at(-1);
32
const body = options.body ? JSON.parse(options.body) : {};
33
telegramCalls.push({ method, body });
34
return json({ ok: true, result: { message_id: body.message_id || 700, ...body } });
35
}
48
-
49
- if (value.startsWith('https://api.github.com/')) {
50
- githubCalls.push(value);
51
-
52
- if (value.startsWith('https://api.github.com/user/repos?')) {
53
- return json([{
54
- id: 1001,
55
- name: 'demo',
56
- full_name: 'davoodmehraban89/demo',
57
- private: true,
58
- default_branch: 'main',
59
- stargazers_count: 0,
60
- forks_count: 0,
61
- description: 'Demo repository'
62
- }]);
63
- }
64
-
65
- if (value === 'https://api.github.com/repositories/1001' || value === 'https://api.github.com/repos/davoodmehraban89/demo') {
66
- return json({
67
- id: 1001,
68
- name: 'demo',
69
- full_name: 'davoodmehraban89/demo',
70
- private: true,
71
- default_branch: 'main',
72
- archived: false,
73
- description: 'Demo repository',
74
- html_url: 'https://github.com/davoodmehraban89/demo'
75
- });
76
- }
77
-
78
- if (value === 'https://api.github.com/repos/davoodmehraban89/demo/contents') {
79
- return json([
80
- { name: 'src', path: 'src', type: 'dir', size: 0, sha: folderSha },
81
- { name: 'README.md', path: 'README.md', type: 'file', size: 100, sha: fileSha }
82
- ]);
83
- }
84
-
85
- if (value === `https://api.github.com/repos/davoodmehraban89/demo/git/trees/${folderSha}`) {
86
- return json({ tree: [
87
- { path: 'index.js', type: 'blob', size: 42, sha: nestedFileSha }
88
- ] });
89
- }
90
-
91
- if (value === `https://api.github.com/repos/davoodmehraban89/demo/git/blobs/${nestedFileSha}`) {
92
- return json({
93
- encoding: 'base64',
94
- size: 21,
95
- content: btoa('console.log("hello")\n')
96
- });
97
- }
98
-
99
- if (value === 'https://api.github.com/repos/davoodmehraban89/demo/commits?per_page=5') {
100
- return json([{
101
- sha: '1234567890abcdef',
102
- commit: { message: 'fix mobile menu', author: { name: 'Davood', date: '2026-10-02T10:00:00Z' } },
103
- author: { login: 'davoodmehraban89' }
104
- }]);
105
- }
106
-
107
- if (value === 'https://api.github.com/repos/davoodmehraban89/demo/pulls?state=all&per_page=10') {
108
- return json([{
109
- number: 7,
110
- title: 'Improve bot UI',
111
- state: 'open',
112
- merged_at: null,
113
- created_at: '2026-10-02T10:00:00Z',
114
- html_url: 'https://github.com/davoodmehraban89/demo/pull/7',
115
- user: { login: 'davoodmehraban89' }
116
- }]);
117
- }
118
-
119
- if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=3') {
120
- return json({
121
- workflow_runs: [{
122
- name: 'CI',
123
- status: 'completed',
124
- conclusion: 'success',
125
- head_commit: { message: 'fix mobile menu' },
126
- html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
127
- }]
128
- });
129
- }
130
-
131
- return json({ message: 'Not Found' }, 404);
132
- }
133
-
134
- throw new Error(`Unexpected URL: ${value}`);
36
+ if (!value.startsWith('https://api.github.com/')) throw new Error(`Unexpected URL: ${value}`);
37
+ githubCalls.push(value);
38
+
39
+ if (value.startsWith('https://api.github.com/user/repos?')) return json([{
40
+ id: 1001, name: 'demo', full_name: 'davoodmehraban89/demo', private: true,
41
+ default_branch: 'main', stargazers_count: 0, forks_count: 0, description: 'Demo repository'
42
+ }]);
43
+ if (value === 'https://api.github.com/repositories/1001' || value === 'https://api.github.com/repos/davoodmehraban89/demo') return json({
44
+ id: 1001, name: 'demo', full_name: 'davoodmehraban89/demo', private: true,
45
+ default_branch: 'main', archived: false, description: 'Demo repository', html_url: 'https://github.com/davoodmehraban89/demo'
46
+ });
47
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/contents') return json([
48
+ { name: 'src', path: 'src', type: 'dir', size: 0, sha: folderSha },
49
+ { name: 'README.md', path: 'README.md', type: 'file', size: 100, sha: fileSha }
50
+ ]);
51
+ if (value === `https://api.github.com/repos/davoodmehraban89/demo/git/trees/${folderSha}`) return json({ tree: [
52
+ { path: 'index.js', type: 'blob', size: 42, sha: nestedFileSha }
53
+ ] });
54
+ if (value === `https://api.github.com/repos/davoodmehraban89/demo/git/blobs/${nestedFileSha}`) return json({
55
+ encoding: 'base64', size: 21, content: btoa('console.log("hello")\n')
56
+ });
57
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/commits?per_page=5') return json([{
58
+ sha: '1234567890abcdef', commit: { message: 'fix mobile menu', author: { name: 'Davood', date: '2026-10-02T10:00:00Z' } },
59
+ author: { login: 'davoodmehraban89' }
60
+ }]);
61
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/pulls?state=all&per_page=10') return json([{
62
+ number: 7, title: 'Improve bot UI', state: 'open', merged_at: null, created_at: '2026-10-02T10:00:00Z',
63
+ html_url: 'https://github.com/davoodmehraban89/demo/pull/7', user: { login: 'davoodmehraban89' }
64
+ }]);
65
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=3') return json({ workflow_runs: [{
66
+ name: 'CI', status: 'completed', conclusion: 'success', head_commit: { message: 'fix mobile menu' },
67
+ html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
68
+ }] });
69
+ return json({ message: 'Not Found' }, 404);
70
};
71
137
- return {
138
- telegramCalls,
139
- githubCalls,
140
- folderSha,
141
- nestedFileSha,
142
- restore() {
143
- globalThis.fetch = originalFetch;
144
- }
145
- };
72
+ return { telegramCalls, githubCalls, folderSha, nestedFileSha, restore: () => { globalThis.fetch = originalFetch; } };
73
}
74
75
async function sendUpdate(update) {
@@ -151,119 +78,73 @@ async function sendUpdate(update) {
78
return response.json();
79
}
80
154
-test('single-message navigation edits the same Telegram message across menu layers', async () => {
81
+test('persistent quick menu bootstrap keeps later inline navigation on one Telegram message', async () => {
82
const mock = installApiMock();
83
try {
157
- await sendUpdate({
158
- message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/start' }
159
- });
160
-
161
- const initial = mock.telegramCalls.find(call => call.method === 'sendMessage');
162
- assert.ok(initial);
163
- assert.match(initial.body.text, /Nexa Dev Bot/);
164
-
165
- const menuMessage = { message_id: 700, chat: { id: 123 } };
166
-
167
- await sendUpdate({
168
- callback_query: { id: 'repos', from: { id: 123 }, data: 'menu:repos', message: menuMessage }
169
- });
170
- await sendUpdate({
171
- callback_query: { id: 'repo', from: { id: 123 }, data: 'repo:1001', message: menuMessage }
172
- });
173
- await sendUpdate({
174
- callback_query: { id: 'back', from: { id: 123 }, data: 'menu:repos', message: menuMessage }
175
- });
176
- await sendUpdate({
177
- callback_query: { id: 'home', from: { id: 123 }, data: 'menu:main', message: menuMessage }
178
- });
179
-
84
+ await sendUpdate({ message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/start' } });
85
+ const startSends = mock.telegramCalls.filter(call => call.method === 'sendMessage');
86
+ assert.equal(startSends.length, 2);
87
+ assert.equal(startSends[0].body.reply_markup.is_persistent, true);
88
+ assert.ok(startSends[0].body.reply_markup.keyboard);
89
+ assert.match(startSends[1].body.text, /Nexa Dev Bot/);
90
+
91
+ const message = { message_id: 700, chat: { id: 123 } };
92
+ for (const data of ['menu:repos', 'repo:1001', 'menu:repos', 'menu:main']) {
93
+ await sendUpdate({ callback_query: { id: data, from: { id: 123 }, data, message } });
94
+ }
95
const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
96
assert.equal(edits.length, 4);
97
assert.ok(edits.every(call => call.body.message_id === 700));
98
assert.match(edits[0].body.text, /فهرست مخزنها/);
99
assert.match(edits[1].body.text, /پروژه: demo/);
100
assert.match(edits[3].body.text, /Nexa Dev Bot/);
186
-
187
- const postStartSends = mock.telegramCalls.filter(call => call.method === 'sendMessage');
188
- assert.equal(postStartSends.length, 1, 'callbacks must not create extra chat messages');
189
- } finally {
190
- mock.restore();
191
- }
101
+ assert.equal(mock.telegramCalls.filter(call => call.method === 'sendMessage').length, 2, 'callbacks must not create extra chat messages');
102
+ } finally { mock.restore(); }
103
});
104
105
test('repository activity views use live GitHub responses and edit in place', async () => {
106
const mock = installApiMock();
107
try {
197
- const menuMessage = { message_id: 701, chat: { id: 123 } };
108
+ const message = { message_id: 701, chat: { id: 123 } };
109
for (const data of ['files:1001', 'commits:1001', 'pr:1001', 'status:1001']) {
199
- await sendUpdate({
200
- callback_query: { id: data, from: { id: 123 }, data, message: menuMessage }
201
- });
110
+ await sendUpdate({ callback_query: { id: data, from: { id: 123 }, data, message } });
111
}
203
-
112
const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
113
assert.equal(edits.length, 4);
114
assert.match(edits[0].body.text, /README\.md/);
115
assert.match(edits[1].body.text, /fix mobile menu/);
116
assert.match(edits[2].body.text, /#7/);
209
- assert.match(edits[2].body.text, /Improve bot UI/);
117
assert.match(edits[3].body.text, /CI/);
211
- } finally {
212
- mock.restore();
213
- }
118
+ } finally { mock.restore(); }
119
});
120
121
test('/repos uses numeric repository ids in callbacks', async () => {
122
const mock = installApiMock();
123
try {
219
- await sendUpdate({
220
- message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/repos' }
221
- });
222
-
124
+ await sendUpdate({ message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/repos' } });
125
const sent = mock.telegramCalls.find(call => call.method === 'sendMessage');
224
- assert.ok(sent);
126
const buttons = sent.body.reply_markup.inline_keyboard.flat();
226
- assert.ok(buttons.some(button => button.text.includes('demo')));
127
assert.ok(buttons.some(button => button.callback_data === 'repo:1001'));
128
assert.ok(mock.githubCalls.some(url => url.startsWith('https://api.github.com/user/repos?')));
229
- } finally {
230
- mock.restore();
231
- }
129
+ } finally { mock.restore(); }
130
});
131
132
test('deep file browser opens directory and file previews in the same Telegram message', async () => {
133
const mock = installApiMock();
134
try {
237
- const menuMessage = { message_id: 702, chat: { id: 123 } };
238
-
239
- await sendUpdate({
240
- callback_query: { id: 'root', from: { id: 123 }, data: 'files:1001', message: menuMessage }
241
- });
242
-
135
+ const message = { message_id: 702, chat: { id: 123 } };
136
+ await sendUpdate({ callback_query: { id: 'root', from: { id: 123 }, data: 'files:1001', message } });
137
let edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
244
- const rootButtons = edits.at(-1).body.reply_markup.inline_keyboard.flat();
245
- assert.ok(rootButtons.some(button => button.callback_data === `tree:1001:${mock.folderSha}`));
246
-
247
- await sendUpdate({
248
- callback_query: { id: 'tree', from: { id: 123 }, data: `tree:1001:${mock.folderSha}`, message: menuMessage }
249
- });
138
+ assert.ok(edits.at(-1).body.reply_markup.inline_keyboard.flat().some(button => button.callback_data === `tree:1001:${mock.folderSha}`));
139
140
+ await sendUpdate({ callback_query: { id: 'tree', from: { id: 123 }, data: `tree:1001:${mock.folderSha}`, message } });
141
edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
252
- const treeView = edits.at(-1);
253
- assert.match(treeView.body.text, /index\.js/);
254
- const treeButtons = treeView.body.reply_markup.inline_keyboard.flat();
255
- assert.ok(treeButtons.some(button => button.callback_data === `blob:1001:${mock.nestedFileSha}`));
256
-
257
- await sendUpdate({
258
- callback_query: { id: 'blob', from: { id: 123 }, data: `blob:1001:${mock.nestedFileSha}`, message: menuMessage }
259
- });
142
+ assert.ok(edits.at(-1).body.reply_markup.inline_keyboard.flat().some(button => button.callback_data === `blob:1001:${mock.nestedFileSha}`));
143
144
+ await sendUpdate({ callback_query: { id: 'blob', from: { id: 123 }, data: `blob:1001:${mock.nestedFileSha}`, message } });
145
edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
262
- const blobView = edits.at(-1);
263
- assert.match(blobView.body.text, /console\.log/);
264
- assert.equal(blobView.body.message_id, 702);
265
- assert.ok(mock.telegramCalls.filter(call => call.method === 'sendMessage').length === 0);
266
- } finally {
267
- mock.restore();
268
- }
146
+ assert.match(edits.at(-1).body.text, /console\.log/);
147
+ assert.equal(edits.at(-1).body.message_id, 702);
148
+ assert.equal(mock.telegramCalls.filter(call => call.method === 'sendMessage').length, 0);
149
+ } finally { mock.restore(); }
150
});