Personal Telegram developer console for GitHub

Add files via upload

davoodmehraban89 committed Oct 2, 2026 at 21:52 UTC 453c0ddfe514d67188fa6ca467bd48f820d32b07
3 files changed +291
test/bot.test.js new
+126
@@ -0,0 +1,126 @@
1 +import { describe, it } from 'node:test';
2 +import assert from 'node:assert';
3 +import { NexaDevBot } from '../src/bot.js';
4 +
5 +describe('Nexa Dev Bot - Webhook Dispatcher & In-Place Editing', () => {
6 + it('1. /start command sends a new message with Main Menu', async () => {
7 + const sentMessages = [];
8 + const bot = new NexaDevBot();
9 + bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
10 + sentMessages.push({ chatId, text, replyMarkup });
11 + return { ok: true, result: { message_id: 501 } };
12 + };
13 +
14 + const update = {
15 + update_id: 1,
16 + message: {
17 + message_id: 100,
18 + chat: { id: 123456 },
19 + text: '/start'
20 + }
21 + };
22 +
23 + const res = await bot.handleUpdate(update);
24 + assert.strictEqual(res.ok, true);
25 + assert.strictEqual(res.command, 'start');
26 + assert.strictEqual(sentMessages.length, 1);
27 + assert.strictEqual(sentMessages[0].chatId, 123456);
28 + assert.ok(sentMessages[0].text.includes('Nexa Dev Bot'));
29 + assert.ok(sentMessages[0].replyMarkup.inline_keyboard);
30 + });
31 +
32 + it('2. callback_query answers callback and EDITS existing message in-place', async () => {
33 + const answeredCallbacks = [];
34 + const editedMessages = [];
35 + const sentMessages = [];
36 +
37 + const bot = new NexaDevBot();
38 + bot.telegram.answerCallbackQuery = async (id, text) => {
39 + answeredCallbacks.push({ id, text });
40 + return { ok: true };
41 + };
42 + bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
43 + editedMessages.push({ chatId, messageId, text, replyMarkup });
44 + return { ok: true };
45 + };
46 + bot.telegram.sendMessage = async () => {
47 + sentMessages.push(true);
48 + return { ok: true };
49 + };
50 +
51 + // Simulate clicking "📁 مخزن‌ها"
52 + const update = {
53 + update_id: 2,
54 + callback_query: {
55 + id: 'cb_999',
56 + data: 'menu:repos',
57 + message: {
58 + message_id: 501,
59 + chat: { id: 123456 }
60 + }
61 + }
62 + };
63 +
64 + const res = await bot.handleUpdate(update);
65 + assert.strictEqual(res.ok, true);
66 + assert.strictEqual(res.edited, true);
67 +
68 + // 1. Answer callback was called to remove Telegram spinner
69 + assert.strictEqual(answeredCallbacks.length, 1);
70 + assert.strictEqual(answeredCallbacks[0].id, 'cb_999');
71 +
72 + // 2. Message was EDITED in-place (no new message sent)
73 + assert.strictEqual(editedMessages.length, 1);
74 + assert.strictEqual(sentMessages.length, 0, 'No new messages should be sent to prevent chat clutter');
75 + assert.strictEqual(editedMessages[0].chatId, 123456);
76 + assert.strictEqual(editedMessages[0].messageId, 501);
77 + assert.ok(editedMessages[0].text.includes('فهرست مخزن‌ها'));
78 + });
79 +
80 + it('3. Full multi-tier navigation loop with back-stack on single message', async () => {
81 + const edits = [];
82 + const bot = new NexaDevBot();
83 + bot.telegram.answerCallbackQuery = async () => ({ ok: true });
84 + bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
85 + edits.push({ text, replyMarkup });
86 + return { ok: true };
87 + };
88 +
89 + const msgContext = { message_id: 501, chat: { id: 123456 } };
90 +
91 + // Step A: User selects Factor-easy repository
92 + await bot.handleUpdate({
93 + callback_query: { id: '1', data: 'repo:Factor-easy', message: msgContext }
94 + });
95 + assert.ok(edits[0].text.includes('پروژه: Factor-easy'));
96 +
97 + // Step B: User opens Pull Requests
98 + await bot.handleUpdate({
99 + callback_query: { id: '2', data: 'pr:Factor-easy', message: msgContext }
100 + });
101 + assert.ok(edits[1].text.includes('Pull Requestهای پروژه Factor-easy'));
102 + assert.ok(edits[1].text.includes('#43'));
103 + assert.ok(edits[1].text.includes('Ops: strengthen long-running execution heartbeat rule'));
104 +
105 + // Step C: User clicks Back -> returns to Factor-easy detail
106 + await bot.handleUpdate({
107 + callback_query: { id: '3', data: 'repo:Factor-easy', message: msgContext }
108 + });
109 + assert.ok(edits[2].text.includes('پروژه: Factor-easy'));
110 +
111 + // Step D: User clicks Back -> returns to Repos list
112 + await bot.handleUpdate({
113 + callback_query: { id: '4', data: 'menu:repos', message: msgContext }
114 + });
115 + assert.ok(edits[3].text.includes('فهرست مخزن‌ها'));
116 +
117 + // Step E: User clicks Back -> returns to Main Menu
118 + await bot.handleUpdate({
119 + callback_query: { id: '5', data: 'menu:main', message: msgContext }
120 + });
121 + assert.ok(edits[4].text.includes('Nexa Dev Bot'));
122 +
123 + // Total steps: 5 edits on the SAME message, zero extra messages created!
124 + assert.strictEqual(edits.length, 5);
125 + });
126 +});
test/index.test.js new
+52
@@ -0,0 +1,52 @@
1 +import { describe, it } from 'node:test';
2 +import assert from 'node:assert';
3 +import worker from '../src/index.js';
4 +
5 +describe('Nexa Dev Bot - Cloudflare Worker Entrypoint', () => {
6 + it('1. GET / returns online status with version info', async () => {
7 + const req = new Request('https://nexa-dev-bot.davoodmehraban.workers.dev/', {
8 + method: 'GET'
9 + });
10 + const res = await worker.fetch(req);
11 + assert.strictEqual(res.status, 200);
12 +
13 + const body = await res.json();
14 + assert.strictEqual(body.status, 'online');
15 + assert.strictEqual(body.bot, 'Nexa Dev Bot');
16 + assert.strictEqual(body.version, '1.1.0');
17 + });
18 +
19 + it('2. GET /status returns service status payload', async () => {
20 + const req = new Request('https://nexa-dev-bot.davoodmehraban.workers.dev/status', {
21 + method: 'GET'
22 + });
23 + const res = await worker.fetch(req);
24 + assert.strictEqual(res.status, 200);
25 +
26 + const body = await res.json();
27 + assert.ok(body.text.includes('وضعیت سیستم و سرویس‌های فعال'));
28 + assert.ok(body.reply_markup);
29 + });
30 +
31 + it('3. POST / processes Telegram webhook updates seamlessly', async () => {
32 + const update = {
33 + update_id: 101,
34 + message: {
35 + message_id: 202,
36 + chat: { id: 88888 },
37 + text: '/start'
38 + }
39 + };
40 + const req = new Request('https://nexa-dev-bot.davoodmehraban.workers.dev/', {
41 + method: 'POST',
42 + headers: { 'Content-Type': 'application/json' },
43 + body: JSON.stringify(update)
44 + });
45 + const res = await worker.fetch(req);
46 + assert.strictEqual(res.status, 200);
47 +
48 + const body = await res.json();
49 + assert.strictEqual(body.ok, true);
50 + assert.strictEqual(body.command, 'start');
51 + });
52 +});
test/menu.test.js new
+113
@@ -0,0 +1,113 @@
1 +import { describe, it } from 'node:test';
2 +import assert from 'node:assert';
3 +import { GitHubService } from '../src/github.js';
4 +import { MenuManager } from '../src/menu.js';
5 +
6 +describe('Nexa Dev Bot - MenuManager & State Navigation', () => {
7 + const github = new GitHubService();
8 + const menu = new MenuManager(github);
9 +
10 + it('1. Main Menu has correct sections and buttons', () => {
11 + const view = menu.getMainMenuView();
12 + assert.ok(view.text.includes('Nexa Dev Bot'));
13 + assert.ok(view.reply_markup?.inline_keyboard);
14 +
15 + const buttons = view.reply_markup.inline_keyboard.flat();
16 + const callbacks = buttons.map(b => b.callback_data);
17 +
18 + assert.ok(callbacks.includes('menu:repos'));
19 + assert.ok(callbacks.includes('menu:status'));
20 + assert.ok(callbacks.includes('menu:tools'));
21 + assert.ok(callbacks.includes('menu:settings'));
22 + assert.ok(callbacks.includes('menu:security'));
23 + });
24 +
25 + it('2. Repositories list displays projects and has back button to main', async () => {
26 + const view = await menu.getReposListView();
27 + assert.ok(view.text.includes('فهرست مخزن‌ها'));
28 + assert.ok(view.text.includes('Nexa Dev > 📁 مخزن‌ها'));
29 +
30 + const buttons = view.reply_markup.inline_keyboard.flat();
31 + const btnTexts = buttons.map(b => b.text);
32 + const callbacks = buttons.map(b => b.callback_data);
33 +
34 + assert.ok(btnTexts.some(t => t.includes('Factor-easy')));
35 + assert.ok(btnTexts.some(t => t.includes('Finora-Invoice')));
36 + assert.ok(callbacks.includes('repo:Factor-easy'));
37 + assert.ok(callbacks.includes('menu:main'), 'Should have back button to main menu');
38 + });
39 +
40 + it('3. Repository detail menu has sub-operations and back button to repos list', async () => {
41 + const view = await menu.getRepoDetailView('Factor-easy');
42 + assert.ok(view.text.includes('پروژه: Factor-easy'));
43 + assert.ok(view.text.includes('Nexa Dev > 📁 مخزن‌ها > Factor-easy'));
44 +
45 + const buttons = view.reply_markup.inline_keyboard.flat();
46 + const callbacks = buttons.map(b => b.callback_data);
47 +
48 + assert.ok(callbacks.includes('files:Factor-easy'));
49 + assert.ok(callbacks.includes('pr:Factor-easy'));
50 + assert.ok(callbacks.includes('commits:Factor-easy'));
51 + assert.ok(callbacks.includes('status:Factor-easy'));
52 + assert.ok(callbacks.includes('menu:repos'), 'Back button must return to repos list');
53 + });
54 +
55 + it('4. Pull Requests view renders all target PRs with back to repo detail', async () => {
56 + const view = await menu.getPullRequestsView('Factor-easy');
57 + assert.ok(view.text.includes('Pull Requestهای پروژه Factor-easy'));
58 + assert.ok(view.text.includes('#43'));
59 + assert.ok(view.text.includes('Ops: strengthen long-running execution heartbeat rule'));
60 + assert.ok(view.text.includes('#40'));
61 + assert.ok(view.text.includes('#39'));
62 + assert.ok(view.text.includes('#28'));
63 + assert.ok(view.text.includes('#6'));
64 +
65 + const buttons = view.reply_markup.inline_keyboard.flat();
66 + const callbacks = buttons.map(b => b.callback_data);
67 + assert.ok(callbacks.includes('repo:Factor-easy'), 'Back button must return to repo detail');
68 + });
69 +
70 + it('5. Commits view displays recent commits and back button', async () => {
71 + const view = await menu.getCommitsView('Factor-easy');
72 + assert.ok(view.text.includes('آخرین Commitهای پروژه Factor-easy'));
73 + assert.ok(view.text.includes('87b25b1'));
74 +
75 + const callbacks = view.reply_markup.inline_keyboard.flat().map(b => b.callback_data);
76 + assert.ok(callbacks.includes('repo:Factor-easy'));
77 + });
78 +
79 + it('6. Files view displays directory structure and back button', async () => {
80 + const view = await menu.getFilesView('Factor-easy');
81 + assert.ok(view.text.includes('ساختار فایل‌های پروژه Factor-easy'));
82 + assert.ok(view.text.includes('src'));
83 +
84 + const callbacks = view.reply_markup.inline_keyboard.flat().map(b => b.callback_data);
85 + assert.ok(callbacks.includes('repo:Factor-easy'));
86 + });
87 +
88 + it('7. Service status view displays infrastructure SLA and back button', () => {
89 + const view = menu.getServiceStatusView();
90 + assert.ok(view.text.includes('وضعیت سیستم و سرویس‌های فعال'));
91 + assert.ok(view.text.includes('Cloudflare Worker'));
92 +
93 + const callbacks = view.reply_markup.inline_keyboard.flat().map(b => b.callback_data);
94 + assert.ok(callbacks.includes('menu:main'));
95 + });
96 +
97 + it('8. renderByCallback handles dynamic routing and navigation hierarchy', async () => {
98 + const mainView = await menu.renderByCallback('menu:main');
99 + assert.ok(mainView.text.includes('Nexa Dev Bot'));
100 +
101 + const reposView = await menu.renderByCallback('menu:repos');
102 + assert.ok(reposView.text.includes('فهرست مخزن‌ها'));
103 +
104 + const repoView = await menu.renderByCallback('repo:Factor-easy');
105 + assert.ok(repoView.text.includes('پروژه: Factor-easy'));
106 +
107 + const prView = await menu.renderByCallback('pr:Factor-easy');
108 + assert.ok(prView.text.includes('Pull Requestهای پروژه Factor-easy'));
109 +
110 + const pingView = await menu.renderByCallback('tool:ping');
111 + assert.ok(pingView.text.includes('Pong!'));
112 + });
113 +});