mirror of
https://github.com/tiennm99/miti-telegram.git
synced 2026-04-17 15:20:57 +00:00
feat: base logic
This commit is contained in:
@@ -2,19 +2,39 @@ import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloud
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import worker from '../src';
|
||||
|
||||
describe('Hello World worker', () => {
|
||||
it('responds with Hello World! (unit style)', async () => {
|
||||
describe('Telegram Worker', () => {
|
||||
it('rejects GET requests', async () => {
|
||||
const request = new Request('http://example.com');
|
||||
// Create an empty context to pass to `worker.fetch()`.
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, env, ctx);
|
||||
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
|
||||
await waitOnExecutionContext(ctx);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
|
||||
expect(response.status).toBe(405);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Method not allowed"`);
|
||||
});
|
||||
|
||||
it('responds with Hello World! (integration style)', async () => {
|
||||
const response = await SELF.fetch('http://example.com');
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
|
||||
it('requires text parameter in JSON POST', async () => {
|
||||
const request = new Request('http://example.com', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({})
|
||||
});
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, env, ctx);
|
||||
await waitOnExecutionContext(ctx);
|
||||
expect(response.status).toBe(400);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Missing text parameter"`);
|
||||
});
|
||||
|
||||
it('requires environment variables', async () => {
|
||||
const request = new Request('http://example.com', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ text: 'Hello World!' })
|
||||
});
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, {}, ctx);
|
||||
await waitOnExecutionContext(ctx);
|
||||
expect(response.status).toBe(500);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables"`);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user