Skip to content

Commit

Permalink
Merge pull request #23 from adidoesnt/main
Browse files Browse the repository at this point in the history
redeploy database
  • Loading branch information
adidoesnt authored Feb 8, 2024
2 parents bb950bd + 58371c5 commit 62883ec
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 5 deletions.
25 changes: 22 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,29 @@ jobs:
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

deploy-telegram-bot:
test-telegram-bot:
needs: deploy-backend
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest

- name: Install dependencies
run: cd telegram-bot && bun install

- name: Run tests
run: cd telegram-bot && bun test

deploy-telegram-bot:
needs: test-telegram-bot
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down Expand Up @@ -175,8 +194,8 @@ jobs:
- name: Set environment variables
run: |
cd health && heroku config:set \
SERVER_LIST=${{ secrets.TELEGRAM_BOT_TOKEN }} \
PING_INTERVAL=${{ secrets.TELEGRAM_BOT_WEBHOOK_URL }} \
SERVER_LIST=${{ secrets.SERVER_LIST }} \
PING_INTERVAL=${{ secrets.PING_INTERVAL }} \
--app echofinder-health
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
logs
Binary file modified telegram-bot/bun.lockb
Binary file not shown.
5 changes: 5 additions & 0 deletions telegram-bot/bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[test]
root = 'src/__tests__'
coverage = true
coverageThreshold = { line = 0.9, function = 0.9, statement = 0.9, branch = 0.9 }
coverageSkipTestFiles = true
5 changes: 4 additions & 1 deletion telegram-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
"@types/bun": "latest",
"@types/express": "^4.17.21",
"@types/node-telegram-bot-api": "^0.64.2",
"prettier": "^3.2.5"
"@types/supertest": "^6.0.2",
"prettier": "^3.2.5",
"supertest": "^6.3.4"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"axios": "^1.6.7",
"axios-mock-adapter": "^1.22.0",
"body-parser": "^1.20.2",
"express": "^4.18.2",
"log4js": "^6.9.1",
Expand Down
50 changes: 50 additions & 0 deletions telegram-bot/src/__tests__/components/apiClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, it, expect, afterEach, beforeAll, spyOn } from 'bun:test';
import MockAdapter from 'axios-mock-adapter';
import { ApiClient } from 'components/apiClient';

describe('ApiClient', () => {
let apiClient: ApiClient;
let mock: MockAdapter;

beforeAll(() => {
apiClient = ApiClient.getInstance();
// @ts-ignore
mock = new MockAdapter(apiClient.client);
});

afterEach(() => {
mock.reset();
});

it('should make a GET request', async () => {
const responseData = { foo: 'bar' };
mock.onGet('/test').reply(200, responseData);

const data = await apiClient.get('/test');
expect(data).toEqual(responseData);
});

it('should make a POST request', async () => {
const responseData = { foo: 'bar' };
mock.onPost('/test').reply(200, responseData);

const data = await apiClient.post('/test', { baz: 'qux' });
expect(data).toEqual(responseData);
});

it('should make a PUT request', async () => {
const responseData = { foo: 'bar' };
mock.onPut('/test').reply(200, responseData);

const data = await apiClient.put('/test', { baz: 'qux' });
expect(data).toEqual(responseData);
});

it('should make a DELETE request', async () => {
const responseData = { foo: 'bar' };
mock.onDelete('/test').reply(200, responseData);

const data = await apiClient.delete('/test');
expect(data).toEqual(responseData);
});
});
58 changes: 58 additions & 0 deletions telegram-bot/src/__tests__/components/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, expect, it, spyOn } from 'bun:test';
import log4js from 'log4js';
import { Logger } from 'components/logger';

describe('Logger', () => {
const loggerConfigSpy = spyOn(log4js, 'configure');
const loggerGetSpy = spyOn(log4js, 'getLogger');

it('should construct a logger', () => {
try {
// @ts-ignore
new Logger();
} catch (error) {
expect(error).not.toBeDefined();
}
});

it('should call log4js.configure with default log level', () => {
Logger.getLogger();
expect(loggerConfigSpy).toHaveBeenCalledWith({
appenders: {
console: { type: 'console' },
file: { type: 'file', filename: 'logs/combined.log' },
error: { type: 'file', filename: 'logs/error.log' },
},
categories: {
default: { appenders: ['console', 'file'], level: 'debug' },
error: { appenders: ['error', 'file'], level: 'error' },
},
});
});

it('should call log4js.configure with custom log level', () => {
Logger.configure('info');
Logger.getLogger();
expect(loggerConfigSpy).toHaveBeenCalledWith({
appenders: {
console: { type: 'console' },
file: { type: 'file', filename: 'logs/combined.log' },
error: { type: 'file', filename: 'logs/error.log' },
},
categories: {
default: { appenders: ['console', 'file'], level: 'debug' },
error: { appenders: ['error', 'file'], level: 'error' },
},
});
});

it('should call log4js.getLogger with default category', () => {
Logger.getLogger();
expect(loggerGetSpy).toHaveBeenCalledWith('default');
});

it('should call log4js.getLogger with custom category', () => {
Logger.getLogger('custom');
expect(loggerGetSpy).toHaveBeenCalledWith('custom');
});
});
36 changes: 36 additions & 0 deletions telegram-bot/src/__tests__/components/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect, spyOn, beforeAll } from 'bun:test';
import { Bot } from 'components/bot';
import { Server } from 'components/server';
import request from 'supertest';

describe('Server', () => {
let server: Server;
let bot: Bot;

spyOn(Bot.prototype, 'processUpdate').mockResolvedValue(
undefined as unknown as never,
);

beforeAll(() => {
// @ts-ignore
bot = new Bot();
server = Server.getInstance();
server.setupBot(bot);
});

it('should return OK for GET /health', async () => {
// @ts-ignore
const response = await request(server.app).get('/health');
expect(response.status).toBe(200);
expect(response.text).toBe('OK');
});

it('should call bot.processUpdate for POST /:token', async () => {
// @ts-ignore
const response = await request(server.app)
.post(`/${process.env.TELEGRAM_BOT_TOKEN}`)
.send({});
expect(response.status).toBe(200);
expect(bot.processUpdate).toHaveBeenCalled();
});
});

0 comments on commit 62883ec

Please sign in to comment.