Personal Telegram developer console for GitHub
test: require webhook to fail closed without Telegram token
davoodmehraban89 committed
Oct 2, 2026 at 22:50 UTC
89949953ada45fc30cb801c270c2eab3d0e1bf4d
1 file changed
+49
-20
test/index.test.js
+49
-20
@@ -5,10 +5,31 @@ import worker from '../src/index.js';
5
const runtime = {
6
TELEGRAM_WEBHOOK_SECRET: 'unit-test-secret',
7
OWNER_TELEGRAM_ID: '123',
8
- TELEGRAM_BOT_TOKEN: '',
8
+ TELEGRAM_BOT_TOKEN: 'unit-test-token',
9
GITHUB_TOKEN: 'unit-test-github'
10
};
11
12
+const startUpdate = {
13
+ update_id: 101,
14
+ message: {
15
+ message_id: 202,
16
+ from: { id: 123 },
17
+ chat: { id: 123 },
18
+ text: '/start'
19
+ }
20
+};
21
+
22
+function webhookRequest(update = startUpdate) {
23
+ return new Request('https://example.test/telegram/webhook', {
24
+ method: 'POST',
25
+ headers: {
26
+ 'Content-Type': 'application/json',
27
+ 'X-Telegram-Bot-Api-Secret-Token': 'unit-test-secret'
28
+ },
29
+ body: JSON.stringify(update)
30
+ });
31
+}
32
+
33
describe('Nexa Dev Bot - Cloudflare Worker Entrypoint', () => {
34
it('GET / returns online status without fabricated uptime', async () => {
35
const res = await worker.fetch(new Request('https://example.test/', { method: 'GET' }), runtime);
@@ -30,28 +51,36 @@ describe('Nexa Dev Bot - Cloudflare Worker Entrypoint', () => {
51
});
52
53
it('authorized Telegram webhook processes /start', async () => {
33
- const update = {
34
- update_id: 101,
35
- message: {
36
- message_id: 202,
37
- from: { id: 123 },
38
- chat: { id: 123 },
39
- text: '/start'
54
+ const originalFetch = globalThis.fetch;
55
+ globalThis.fetch = async (url) => {
56
+ if (String(url).startsWith('https://api.telegram.org/')) {
57
+ return new Response(JSON.stringify({ ok: true, result: { message_id: 303 } }), {
58
+ status: 200,
59
+ headers: { 'content-type': 'application/json' }
60
+ });
61
}
62
+ return originalFetch(url);
63
};
42
- const req = new Request('https://example.test/telegram/webhook', {
43
- method: 'POST',
44
- headers: {
45
- 'Content-Type': 'application/json',
46
- 'X-Telegram-Bot-Api-Secret-Token': 'unit-test-secret'
47
- },
48
- body: JSON.stringify(update)
64
+
65
+ try {
66
+ const res = await worker.fetch(webhookRequest(), runtime);
67
+ assert.equal(res.status, 200);
68
+ const body = await res.json();
69
+ assert.equal(body.ok, true);
70
+ assert.equal(body.command, 'start');
71
+ } finally {
72
+ globalThis.fetch = originalFetch;
73
+ }
74
+ });
75
+
76
+ it('webhook fails closed when TELEGRAM_BOT_TOKEN is missing', async () => {
77
+ const res = await worker.fetch(webhookRequest(), {
78
+ ...runtime,
79
+ TELEGRAM_BOT_TOKEN: ''
80
});
50
- const res = await worker.fetch(req, runtime);
51
- assert.equal(res.status, 200);
52
- const body = await res.json();
53
- assert.equal(body.ok, true);
54
- assert.equal(body.command, 'start');
81
+
82
+ assert.equal(res.status, 503);
83
+ assert.match(await res.text(), /Telegram bot token not configured/i);
84
});
85
86
it('arbitrary POST paths are not treated as webhooks', async () => {