Personal Telegram developer console for GitHub
test: replace stale navigation coverage with single-message flow
davoodmehraban89 committed
Oct 2, 2026 at 22:13 UTC
7f6c46cae662f43cbed4520087f8afe4452035ab
1 file changed
+181
-165
test/navigation.test.js
+181
-165
@@ -1,196 +1,212 @@
1
-import test from "node:test";
2
-import assert from "node:assert/strict";
3
-import worker from "../src/index.js";
1
+import test from 'node:test';
2
+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: "token",
9
- GITHUB_TOKEN: "gh-token",
6
+ TELEGRAM_WEBHOOK_SECRET: 'right',
7
+ OWNER_TELEGRAM_ID: '123',
8
+ TELEGRAM_BOT_TOKEN: 'telegram-test-token',
9
+ GITHUB_TOKEN: 'github-test-token',
10
+ GITHUB_OWNER: 'davoodmehraban89'
11
};
12
13
function webhook(update) {
13
- return new Request("https://example.com/telegram/webhook", {
14
- method: "POST",
14
+ return new Request('https://example.test/telegram/webhook', {
15
+ method: 'POST',
16
headers: {
16
- "content-type": "application/json",
17
- "X-Telegram-Bot-Api-Secret-Token": "right",
17
+ 'content-type': 'application/json',
18
+ 'X-Telegram-Bot-Api-Secret-Token': 'right'
19
},
19
- body: JSON.stringify(update),
20
+ body: JSON.stringify(update)
21
});
22
}
23
23
-function telegramOk() {
24
- return new Response(JSON.stringify({ ok: true, result: {} }), {
25
- status: 200,
26
- headers: { "content-type": "application/json" },
24
+function json(data, status = 200) {
25
+ return new Response(JSON.stringify(data), {
26
+ status,
27
+ headers: { 'content-type': 'application/json' }
28
});
29
}
30
30
-test("/repos renders repositories as tappable inline buttons", async () => {
31
- const calls = [];
32
- globalThis.fetch = async (url, options = {}) => {
33
- const u = String(url);
34
- if (u.includes("api.github.com/user/repos")) {
35
- return new Response(JSON.stringify([
36
- { id: 101, full_name: "davood/demo", name: "demo", private: true, default_branch: "main" },
37
- ]), { status: 200, headers: { "content-type": "application/json" } });
38
- }
39
- calls.push({ url: u, body: options.body ? JSON.parse(options.body) : null });
40
- return telegramOk();
41
- };
42
-
43
- const res = await worker.fetch(webhook({
44
- message: { text: "/repos", from: { id: 123 }, chat: { id: 123 } },
45
- }), env);
46
-
47
- assert.equal(res.status, 200);
48
- const sent = calls.find((c) => c.url.includes("sendMessage"));
49
- assert.ok(sent);
50
- assert.equal(sent.body.reply_markup.inline_keyboard[0][0].text, "🔒 demo");
51
- assert.equal(sent.body.reply_markup.inline_keyboard[0][0].callback_data, "repo:101");
52
-});
53
-
54
-test("repository callback opens a mobile action menu", async () => {
31
+function installApiMock() {
32
const telegramCalls = [];
56
- globalThis.fetch = async (url, options = {}) => {
57
- const u = String(url);
58
- if (u === "https://api.github.com/repositories/101") {
59
- return new Response(JSON.stringify({
60
- id: 101,
61
- full_name: "davood/demo",
62
- private: true,
63
- default_branch: "main",
64
- description: "Demo repo",
65
- }), { status: 200, headers: { "content-type": "application/json" } });
66
- }
67
- telegramCalls.push({ url: u, body: options.body ? JSON.parse(options.body) : null });
68
- return telegramOk();
69
- };
33
+ const githubCalls = [];
34
+ const originalFetch = globalThis.fetch;
35
71
- const res = await worker.fetch(webhook({
72
- callback_query: {
73
- id: "cb1",
74
- from: { id: 123 },
75
- data: "repo:101",
76
- message: { chat: { id: 123 }, message_id: 50 },
77
- },
78
- }), env);
79
-
80
- assert.equal(res.status, 200);
81
- assert.ok(telegramCalls.some((c) => c.url.includes("answerCallbackQuery")));
82
- const sent = telegramCalls.find((c) => c.url.includes("sendMessage"));
83
- assert.match(sent.body.text, /davood\/demo/);
84
- const buttons = sent.body.reply_markup.inline_keyboard.flat().map((b) => b.callback_data);
85
- assert.deepEqual(buttons, ["files:101", "commits:101", "prs:101", "repos"]);
86
-});
87
-
88
-test("files callback lists folders and files using compact SHA callbacks", async () => {
89
- const telegramCalls = [];
36
globalThis.fetch = async (url, options = {}) => {
91
- const u = String(url);
92
- if (u === "https://api.github.com/repositories/101") {
93
- return new Response(JSON.stringify({ id: 101, full_name: "davood/demo", default_branch: "main" }), { status: 200, headers: { "content-type": "application/json" } });
37
+ const value = String(url);
38
+
39
+ if (value.startsWith('https://api.telegram.org/')) {
40
+ const method = value.split('/').at(-1);
41
+ const body = options.body ? JSON.parse(options.body) : {};
42
+ telegramCalls.push({ method, body });
43
+ return json({ ok: true, result: { message_id: body.message_id || 700, ...body } });
44
}
95
- if (u.startsWith("https://api.github.com/repos/davood/demo/contents")) {
96
- return new Response(JSON.stringify([
97
- { name: "src", type: "dir", sha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" },
98
- { name: "README.md", type: "file", sha: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" },
99
- ]), { status: 200, headers: { "content-type": "application/json" } });
45
+
46
+ if (value.startsWith('https://api.github.com/')) {
47
+ githubCalls.push(value);
48
+
49
+ if (value.startsWith('https://api.github.com/user/repos?')) {
50
+ return json([{
51
+ name: 'demo',
52
+ full_name: 'davoodmehraban89/demo',
53
+ private: true,
54
+ default_branch: 'main',
55
+ stargazers_count: 0,
56
+ forks_count: 0,
57
+ description: 'Demo repository'
58
+ }]);
59
+ }
60
+
61
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo') {
62
+ return json({
63
+ name: 'demo',
64
+ full_name: 'davoodmehraban89/demo',
65
+ private: true,
66
+ default_branch: 'main',
67
+ archived: false,
68
+ description: 'Demo repository',
69
+ html_url: 'https://github.com/davoodmehraban89/demo'
70
+ });
71
+ }
72
+
73
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/contents') {
74
+ return json([
75
+ { name: 'src', path: 'src', type: 'dir', size: 0, sha: 'a'.repeat(40) },
76
+ { name: 'README.md', path: 'README.md', type: 'file', size: 100, sha: 'b'.repeat(40) }
77
+ ]);
78
+ }
79
+
80
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/commits?per_page=5') {
81
+ return json([{
82
+ sha: '1234567890abcdef',
83
+ commit: { message: 'fix mobile menu', author: { name: 'Davood', date: '2026-10-02T10:00:00Z' } },
84
+ author: { login: 'davoodmehraban89' }
85
+ }]);
86
+ }
87
+
88
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/pulls?state=all&per_page=10') {
89
+ return json([{
90
+ number: 7,
91
+ title: 'Improve bot UI',
92
+ state: 'open',
93
+ merged_at: null,
94
+ created_at: '2026-10-02T10:00:00Z',
95
+ html_url: 'https://github.com/davoodmehraban89/demo/pull/7',
96
+ user: { login: 'davoodmehraban89' }
97
+ }]);
98
+ }
99
+
100
+ if (value === 'https://api.github.com/repos/davoodmehraban89/demo/actions/runs?per_page=3') {
101
+ return json({
102
+ workflow_runs: [{
103
+ name: 'CI',
104
+ status: 'completed',
105
+ conclusion: 'success',
106
+ head_commit: { message: 'fix mobile menu' },
107
+ html_url: 'https://github.com/davoodmehraban89/demo/actions/runs/1'
108
+ }]
109
+ });
110
+ }
111
+
112
+ return json({ message: 'Not Found' }, 404);
113
}
101
- telegramCalls.push({ url: u, body: options.body ? JSON.parse(options.body) : null });
102
- return telegramOk();
103
- };
114
105
- await worker.fetch(webhook({
106
- callback_query: {
107
- id: "cb2",
108
- from: { id: 123 },
109
- data: "files:101",
110
- message: { chat: { id: 123 }, message_id: 51 },
111
- },
112
- }), env);
113
-
114
- const sent = telegramCalls.find((c) => c.url.includes("sendMessage"));
115
- const buttons = sent.body.reply_markup.inline_keyboard.flat();
116
- assert.equal(buttons[0].text, "📁 src");
117
- assert.equal(buttons[0].callback_data, "tree:101:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
118
- assert.equal(buttons[1].text, "📄 README.md");
119
- assert.equal(buttons[1].callback_data, "blob:101:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
120
-});
115
+ throw new Error(`Unexpected URL: ${value}`);
116
+ };
117
122
-test("blob callback shows escaped text content and a back button", async () => {
123
- const telegramCalls = [];
124
- globalThis.fetch = async (url, options = {}) => {
125
- const u = String(url);
126
- if (u === "https://api.github.com/repositories/101") {
127
- return new Response(JSON.stringify({ id: 101, full_name: "davood/demo", default_branch: "main" }), { status: 200, headers: { "content-type": "application/json" } });
118
+ return {
119
+ telegramCalls,
120
+ githubCalls,
121
+ restore() {
122
+ globalThis.fetch = originalFetch;
123
}
129
- if (u === "https://api.github.com/repos/davood/demo/git/blobs/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") {
130
- const content = Buffer.from("<hello> & goodbye").toString("base64");
131
- return new Response(JSON.stringify({ encoding: "base64", content, size: 17 }), { status: 200, headers: { "content-type": "application/json" } });
132
- }
133
- telegramCalls.push({ url: u, body: options.body ? JSON.parse(options.body) : null });
134
- return telegramOk();
124
};
125
+}
126
137
- await worker.fetch(webhook({
138
- callback_query: {
139
- id: "cb3",
140
- from: { id: 123 },
141
- data: "blob:101:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
142
- message: { chat: { id: 123 }, message_id: 52 },
143
- },
144
- }), env);
127
+async function sendUpdate(update) {
128
+ const response = await worker.fetch(webhook(update), env);
129
+ assert.equal(response.status, 200);
130
+ return response.json();
131
+}
132
146
- const sent = telegramCalls.find((c) => c.url.includes("sendMessage"));
147
- assert.match(sent.body.text, /<hello> & goodbye/);
148
- assert.equal(sent.body.reply_markup.inline_keyboard.at(-1)[0].callback_data, "files:101");
133
+test('single-message navigation edits the same Telegram message across menu layers', async () => {
134
+ const mock = installApiMock();
135
+ try {
136
+ await sendUpdate({
137
+ message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/start' }
138
+ });
139
+
140
+ const initial = mock.telegramCalls.find(call => call.method === 'sendMessage');
141
+ assert.ok(initial);
142
+ assert.match(initial.body.text, /Nexa Dev Bot/);
143
+
144
+ const menuMessage = { message_id: 700, chat: { id: 123 } };
145
+
146
+ await sendUpdate({
147
+ callback_query: { id: 'repos', from: { id: 123 }, data: 'menu:repos', message: menuMessage }
148
+ });
149
+ await sendUpdate({
150
+ callback_query: { id: 'repo', from: { id: 123 }, data: 'repo:demo', message: menuMessage }
151
+ });
152
+ await sendUpdate({
153
+ callback_query: { id: 'back', from: { id: 123 }, data: 'menu:repos', message: menuMessage }
154
+ });
155
+ await sendUpdate({
156
+ callback_query: { id: 'home', from: { id: 123 }, data: 'menu:main', message: menuMessage }
157
+ });
158
+
159
+ const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
160
+ assert.equal(edits.length, 4);
161
+ assert.ok(edits.every(call => call.body.message_id === 700));
162
+ assert.match(edits[0].body.text, /فهرست مخزنها/);
163
+ assert.match(edits[1].body.text, /پروژه: demo/);
164
+ assert.match(edits[3].body.text, /Nexa Dev Bot/);
165
+
166
+ const postStartSends = mock.telegramCalls.filter(call => call.method === 'sendMessage');
167
+ assert.equal(postStartSends.length, 1, 'callbacks must not create extra chat messages');
168
+ } finally {
169
+ mock.restore();
170
+ }
171
});
172
151
-test("commits and pull requests callbacks render concise repository activity", async () => {
152
- const sentTexts = [];
153
- globalThis.fetch = async (url, options = {}) => {
154
- const u = String(url);
155
- if (u === "https://api.github.com/repositories/101") {
156
- return new Response(JSON.stringify({ id: 101, full_name: "davood/demo", default_branch: "main" }), { status: 200, headers: { "content-type": "application/json" } });
157
- }
158
- if (u.includes("/commits?")) {
159
- return new Response(JSON.stringify([{ sha: "1234567890", commit: { message: "fix mobile menu" }, author: { login: "davood" } }]), { status: 200, headers: { "content-type": "application/json" } });
173
+test('repository activity views use live GitHub responses and edit in place', async () => {
174
+ const mock = installApiMock();
175
+ try {
176
+ const menuMessage = { message_id: 701, chat: { id: 123 } };
177
+ for (const data of ['files:demo', 'commits:demo', 'pr:demo', 'status:demo']) {
178
+ await sendUpdate({
179
+ callback_query: { id: data, from: { id: 123 }, data, message: menuMessage }
180
+ });
181
}
161
- if (u.includes("/pulls?")) {
162
- return new Response(JSON.stringify([{ number: 7, title: "Improve bot UI", user: { login: "davood" } }]), { status: 200, headers: { "content-type": "application/json" } });
163
- }
164
- if (u.includes("sendMessage")) sentTexts.push(JSON.parse(options.body).text);
165
- return telegramOk();
166
- };
182
168
- for (const data of ["commits:101", "prs:101"]) {
169
- await worker.fetch(webhook({
170
- callback_query: { id: data, from: { id: 123 }, data, message: { chat: { id: 123 }, message_id: 53 } },
171
- }), env);
183
+ const edits = mock.telegramCalls.filter(call => call.method === 'editMessageText');
184
+ assert.equal(edits.length, 4);
185
+ assert.match(edits[0].body.text, /README\.md/);
186
+ assert.match(edits[1].body.text, /fix mobile menu/);
187
+ assert.match(edits[2].body.text, /#7/);
188
+ assert.match(edits[2].body.text, /Improve bot UI/);
189
+ assert.match(edits[3].body.text, /CI/);
190
+ assert.ok(mock.githubCalls.some(url => url.startsWith('https://api.github.com/user/repos?')) === false);
191
+ } finally {
192
+ mock.restore();
193
}
173
-
174
- assert.ok(sentTexts.some((t) => t.includes("fix mobile menu")));
175
- assert.ok(sentTexts.some((t) => t.includes("#7") && t.includes("Improve bot UI")));
194
});
195
178
-test("callback queries from non-owner users are ignored", async () => {
179
- let calls = 0;
180
- globalThis.fetch = async () => {
181
- calls += 1;
182
- return telegramOk();
183
- };
184
-
185
- const res = await worker.fetch(webhook({
186
- callback_query: {
187
- id: "bad",
188
- from: { id: 999 },
189
- data: "repo:101",
190
- message: { chat: { id: 999 }, message_id: 1 },
191
- },
192
- }), env);
193
-
194
- assert.equal(res.status, 200);
195
- assert.equal(calls, 0);
196
-});
\ No newline at end of file
196
+test('/repos uses authenticated repository discovery and renders the API result', async () => {
197
+ const mock = installApiMock();
198
+ try {
199
+ await sendUpdate({
200
+ message: { message_id: 1, from: { id: 123 }, chat: { id: 123 }, text: '/repos' }
201
+ });
202
+
203
+ const sent = mock.telegramCalls.find(call => call.method === 'sendMessage');
204
+ assert.ok(sent);
205
+ const buttons = sent.body.reply_markup.inline_keyboard.flat();
206
+ assert.ok(buttons.some(button => button.text.includes('demo')));
207
+ assert.ok(buttons.some(button => button.callback_data === 'repo:demo'));
208
+ assert.ok(mock.githubCalls.some(url => url.startsWith('https://api.github.com/user/repos?')));
209
+ } finally {
210
+ mock.restore();
211
+ }
212
+});