Personal Telegram developer console for GitHub
test: align worker tests with secured webhook route
davoodmehraban89 committed
Oct 2, 2026 at 22:12 UTC
b809c246db20b03aac261d50f0fa849e39a99d3e
1 file changed
+41
-28
test/index.test.js
+41
-28
@@ -1,52 +1,65 @@
1
import { describe, it } from 'node:test';
2
-import assert from 'node:assert';
2
+import assert from 'node:assert/strict';
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);
5
+const runtime = {
6
+ TELEGRAM_WEBHOOK_SECRET: 'unit-test-secret',
7
+ OWNER_TELEGRAM_ID: '123',
8
+ TELEGRAM_BOT_TOKEN: '',
9
+ GITHUB_TOKEN: 'unit-test-github'
10
+};
11
12
+describe('Nexa Dev Bot - Cloudflare Worker Entrypoint', () => {
13
+ it('GET / returns online status without fabricated uptime', async () => {
14
+ const res = await worker.fetch(new Request('https://example.test/', { method: 'GET' }), runtime);
15
+ assert.equal(res.status, 200);
16
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
+ assert.equal(body.status, 'online');
18
+ assert.equal(body.bot, 'Nexa Dev Bot');
19
+ assert.equal(body.version, '1.1.1');
20
+ assert.equal('uptime' in body, false);
21
});
22
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
-
23
+ it('GET /status returns service status payload', async () => {
24
+ const res = await worker.fetch(new Request('https://example.test/status', { method: 'GET' }), runtime);
25
+ assert.equal(res.status, 200);
26
const body = await res.json();
27
- assert.ok(body.text.includes('وضعیت سیستم و سرویسهای فعال'));
27
+ assert.ok(body.text.includes('وضعیت سیستم و سرویس'));
28
assert.ok(body.reply_markup);
29
+ assert.doesNotMatch(body.text, /100% SLA/i);
30
});
31
31
- it('3. POST / processes Telegram webhook updates seamlessly', async () => {
32
+ it('authorized Telegram webhook processes /start', async () => {
33
const update = {
34
update_id: 101,
35
message: {
36
message_id: 202,
36
- chat: { id: 88888 },
37
+ from: { id: 123 },
38
+ chat: { id: 123 },
39
text: '/start'
40
}
41
};
40
- const req = new Request('https://nexa-dev-bot.davoodmehraban.workers.dev/', {
42
+ const req = new Request('https://example.test/telegram/webhook', {
43
method: 'POST',
42
- headers: { 'Content-Type': 'application/json' },
44
+ headers: {
45
+ 'Content-Type': 'application/json',
46
+ 'X-Telegram-Bot-Api-Secret-Token': 'unit-test-secret'
47
+ },
48
body: JSON.stringify(update)
49
});
45
- const res = await worker.fetch(req);
46
- assert.strictEqual(res.status, 200);
47
-
50
+ const res = await worker.fetch(req, runtime);
51
+ assert.equal(res.status, 200);
52
const body = await res.json();
49
- assert.strictEqual(body.ok, true);
50
- assert.strictEqual(body.command, 'start');
53
+ assert.equal(body.ok, true);
54
+ assert.equal(body.command, 'start');
55
+ });
56
+
57
+ it('arbitrary POST paths are not treated as webhooks', async () => {
58
+ const res = await worker.fetch(new Request('https://example.test/', {
59
+ method: 'POST',
60
+ headers: { 'Content-Type': 'application/json' },
61
+ body: '{}'
62
+ }), runtime);
63
+ assert.equal(res.status, 404);
64
});
65
});