Personal Telegram developer console for GitHub

test: isolate bot navigation from external GitHub network

davoodmehraban89 committed Oct 2, 2026 at 22:16 UTC b5fecf4b17a2fc0cf0d4dbb7239389aef64b98fc
1 file changed +84 -82
test/bot.test.js
+84 -82
@@ -1,42 +1,78 @@
1 import { describe, it } from 'node:test';
2 -import assert from 'node:assert';
2 +import assert from 'node:assert/strict';
3 import { NexaDevBot } from '../src/bot.js';
4
5 +const fakeGitHub = {
6 + owner: 'davoodmehraban89',
7 + async listRepositories() {
8 + return [{ id: 'Factor-easy', name: 'Factor-easy', private: true }];
9 + },
10 + async getRepository(repo) {
11 + return {
12 + id: repo,
13 + name: repo,
14 + fullName: `davoodmehraban89/${repo}`,
15 + private: true,
16 + description: 'Factor-easy project',
17 + defaultBranch: 'main',
18 + archived: false
19 + };
20 + },
21 + async listPullRequests() {
22 + return [{
23 + number: 43,
24 + title: 'Ops: strengthen long-running execution heartbeat rule',
25 + author: 'davoodmehraban89',
26 + state: 'open',
27 + merged: false
28 + }];
29 + },
30 + async listCommits() {
31 + return [];
32 + },
33 + async listFiles() {
34 + return [];
35 + },
36 + async getRepoStatus() {
37 + return { defaultBranch: 'main', lastRuns: [] };
38 + }
39 +};
40 +
41 +function makeBot() {
42 + const bot = new NexaDevBot();
43 + bot.menu.github = fakeGitHub;
44 + return bot;
45 +}
46 +
47 describe('Nexa Dev Bot - Webhook Dispatcher & In-Place Editing', () => {
6 - it('1. /start command sends a new message with Main Menu', async () => {
48 + it('/start sends one new message with Main Menu', async () => {
49 const sentMessages = [];
8 - const bot = new NexaDevBot();
50 + const bot = makeBot();
51 bot.telegram.sendMessage = async (chatId, text, replyMarkup) => {
52 sentMessages.push({ chatId, text, replyMarkup });
53 return { ok: true, result: { message_id: 501 } };
54 };
55
14 - const update = {
15 - update_id: 1,
16 - message: {
17 - message_id: 100,
18 - chat: { id: 123456 },
19 - text: '/start'
20 - }
21 - };
56 + const res = await bot.handleUpdate({
57 + message: { message_id: 100, chat: { id: 123456 }, text: '/start' }
58 + });
59
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'));
60 + assert.equal(res.ok, true);
61 + assert.equal(res.command, 'start');
62 + assert.equal(sentMessages.length, 1);
63 + assert.equal(sentMessages[0].chatId, 123456);
64 + assert.match(sentMessages[0].text, /Nexa Dev Bot/);
65 assert.ok(sentMessages[0].replyMarkup.inline_keyboard);
66 });
67
32 - it('2. callback_query answers callback and EDITS existing message in-place', async () => {
68 + it('callback answers Telegram and edits the existing message in place', async () => {
69 const answeredCallbacks = [];
70 const editedMessages = [];
71 const sentMessages = [];
72 + const bot = makeBot();
73
37 - const bot = new NexaDevBot();
38 - bot.telegram.answerCallbackQuery = async (id, text) => {
39 - answeredCallbacks.push({ id, text });
74 + bot.telegram.answerCallbackQuery = async (id) => {
75 + answeredCallbacks.push(id);
76 return { ok: true };
77 };
78 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
@@ -48,79 +84,45 @@ describe('Nexa Dev Bot - Webhook Dispatcher & In-Place Editing', () => {
84 return { ok: true };
85 };
86
51 - // Simulate clicking "📁 مخزن‌ها"
52 - const update = {
53 - update_id: 2,
87 + const res = await bot.handleUpdate({
88 callback_query: {
89 id: 'cb_999',
90 data: 'menu:repos',
57 - message: {
58 - message_id: 501,
59 - chat: { id: 123456 }
60 - }
91 + message: { message_id: 501, chat: { id: 123456 } }
92 }
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');
93 + });
94
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('فهرست مخزن‌ها'));
95 + assert.equal(res.ok, true);
96 + assert.equal(res.edited, true);
97 + assert.deepEqual(answeredCallbacks, ['cb_999']);
98 + assert.equal(editedMessages.length, 1);
99 + assert.equal(sentMessages.length, 0);
100 + assert.equal(editedMessages[0].messageId, 501);
101 + assert.match(editedMessages[0].text, /فهرست مخزن‌ها/);
102 });
103
80 - it('3. Full multi-tier navigation loop with back-stack on single message', async () => {
104 + it('multi-tier back navigation stays on the same Telegram message', async () => {
105 const edits = [];
82 - const bot = new NexaDevBot();
106 + const bot = makeBot();
107 bot.telegram.answerCallbackQuery = async () => ({ ok: true });
108 bot.telegram.editMessageText = async (chatId, messageId, text, replyMarkup) => {
85 - edits.push({ text, replyMarkup });
109 + edits.push({ chatId, messageId, text, replyMarkup });
110 return { ok: true };
111 };
112
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);
113 + const message = { message_id: 501, chat: { id: 123456 } };
114 + for (const data of ['repo:Factor-easy', 'pr:Factor-easy', 'repo:Factor-easy', 'menu:repos', 'menu:main']) {
115 + const result = await bot.handleUpdate({
116 + callback_query: { id: data, data, message }
117 + });
118 + assert.equal(result.ok, true);
119 + }
120 +
121 + assert.equal(edits.length, 5);
122 + assert.ok(edits.every(edit => edit.messageId === 501));
123 + assert.match(edits[0].text, /پروژه: Factor-easy/);
124 + assert.match(edits[1].text, /#43/);
125 + assert.match(edits[3].text, /فهرست مخزن‌ها/);
126 + assert.match(edits[4].text, /Nexa Dev Bot/);
127 });
128 });