mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 15:34:03 +00:00
feat(WHE-21): scaffold grammy webhook bot server
This commit is contained in:
62
apps/bot/src/server.test.ts
Normal file
62
apps/bot/src/server.test.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
|
||||
import { createBotWebhookServer } from './server'
|
||||
|
||||
describe('createBotWebhookServer', () => {
|
||||
const server = createBotWebhookServer({
|
||||
webhookPath: '/webhook/telegram',
|
||||
webhookSecret: 'secret-token',
|
||||
webhookHandler: async () => new Response('ok', { status: 200 })
|
||||
})
|
||||
|
||||
test('returns health payload', async () => {
|
||||
const response = await server.fetch(new Request('http://localhost/healthz'))
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(await response.json()).toEqual({ ok: true })
|
||||
})
|
||||
|
||||
test('rejects unknown path', async () => {
|
||||
const response = await server.fetch(new Request('http://localhost/unknown'))
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
})
|
||||
|
||||
test('rejects webhook request with missing secret', async () => {
|
||||
const response = await server.fetch(
|
||||
new Request('http://localhost/webhook/telegram', {
|
||||
method: 'POST'
|
||||
})
|
||||
)
|
||||
|
||||
expect(response.status).toBe(401)
|
||||
})
|
||||
|
||||
test('rejects non-post method for webhook endpoint', async () => {
|
||||
const response = await server.fetch(
|
||||
new Request('http://localhost/webhook/telegram', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-telegram-bot-api-secret-token': 'secret-token'
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
expect(response.status).toBe(405)
|
||||
})
|
||||
|
||||
test('accepts authorized webhook request', async () => {
|
||||
const response = await server.fetch(
|
||||
new Request('http://localhost/webhook/telegram', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-telegram-bot-api-secret-token': 'secret-token'
|
||||
},
|
||||
body: JSON.stringify({ update_id: 1 })
|
||||
})
|
||||
)
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(await response.text()).toBe('ok')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user