Skip to content

Commit

Permalink
move the fake implementation of telegram APIs from test folder to src…
Browse files Browse the repository at this point in the history
…/api/impl/fake
  • Loading branch information
TheodoreKrypton committed Jun 19, 2024
1 parent 44bb55b commit 5ce2497
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 23 deletions.
6 changes: 3 additions & 3 deletions test/utils/mock-gramjs-api.ts → src/api/impl/fake/gramjs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ITDLibClient } from 'src/api/interface';
import * as types from 'src/api/types';

import { MockMessages } from './mock-messages';
import { Messages } from './messages';

export class MockGramJSApi implements ITDLibClient {
constructor(private messages: MockMessages) {}
export class FakeGramJSApi implements ITDLibClient {
constructor(private messages: Messages) {}

public async getMessages(
req: types.GetMessagesReq,
Expand Down
2 changes: 2 additions & 0 deletions src/api/impl/fake/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as gramjs from './gramjs';
export * as telegraf from './telegraf';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Message = {
document?: types.Document;
};

export class MockMessages {
export class Messages {
constructor(
public readonly messages: { [key: number]: Message } = {},
public messageId: number = 1,
Expand Down
3 changes: 3 additions & 0 deletions src/api/impl/fake/telegraf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { IBot } from 'src/api/interface';

export class FakeTelegraf implements IBot {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ import bigInt from 'big-integer';

import { Logger } from 'src/utils/logger';

import { MockMessages } from './mock-messages';
import { Messages } from 'src/api/impl/fake/messages';

describe('test mock messages', () => {
beforeAll(() => {
Logger.info = jest.fn();
});

it('should send and get message', () => {
const mockMessages = new MockMessages();
const mockMessages = new Messages();
const messageId = mockMessages.sendMessage({ message: 'hello' });
const message = mockMessages.getMessage(messageId);
expect(message.text).toEqual('hello');
});

it('should increase message id after sending message', () => {
const mockMessages = new MockMessages();
const mockMessages = new Messages();
const messageId1 = mockMessages.sendMessage({ message: 'hello' });
const messageId2 = mockMessages.sendMessage({ message: 'world' });
expect(messageId2).toBeGreaterThan(messageId1);
});

it('should send and get message with file', () => {
const mockMessages = new MockMessages();
const mockMessages = new Messages();
const fileId = bigInt(123);
const messageId = mockMessages.sendMessage({
message: 'hello',
Expand All @@ -35,7 +35,7 @@ describe('test mock messages', () => {
});

it('should save and get file part', () => {
const mockMessages = new MockMessages();
const mockMessages = new Messages();
const fileId = bigInt(123);
const data = Buffer.from('hello');
mockMessages.saveFilePart(fileId, 0, data);
Expand All @@ -45,15 +45,15 @@ describe('test mock messages', () => {
});

it('should edit a message', () => {
const mockMessages = new MockMessages();
const mockMessages = new Messages();
const messageId = mockMessages.sendMessage({ message: 'hello' });
mockMessages.editMessage(messageId, { message: 'world' });
const message = mockMessages.getMessage(messageId);
expect(message.text).toEqual('world');
});

it('should edit a message with file', () => {
const mockMessages = new MockMessages();
const mockMessages = new Messages();
const fileId = bigInt(123);
const messageId = mockMessages.sendMessage({ file: fileId });
const fileId2 = bigInt(456);
Expand Down
2 changes: 1 addition & 1 deletion test/api/model/operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { saveToBuffer, saveToFile } from 'src/api/utils';
import { Logger } from 'src/utils/logger';
import { sleep } from 'src/utils/sleep';

import { createMockClient } from '../../utils/mock-tg-client';
import { createMockClient } from '../../mock/mock-tg-client';

describe('file and directory operations', () => {
beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/cmd/cmd.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { parser } from 'src/commands/parser';
import { TGFSDirectory } from 'src/model/directory';
import { Logger } from 'src/utils/logger';

import { createMockClient } from '../utils/mock-tg-client';
import { createMockClient } from '../mock/mock-tg-client';

const parse = () => {
const argv = parser(yargs(process.argv)).argv;
Expand Down
12 changes: 6 additions & 6 deletions test/utils/mock-tg-client.ts → test/mock/mock-tg-client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MockGramJSApi } from './mock-gramjs-api';
import { MockMessages } from './mock-messages';
import { MockTelegrafApi } from './mock-telegraf-api';
import { FakeGramJSApi } from 'src/api/impl/fake/gramjs';
import { Messages } from 'src/api/impl/fake/messages';
import { FakeTelegraf } from 'src/api/impl/fake/telegraf';

export const createMockClient = async () => {
jest.resetModules();

const mockMessages = new MockMessages();
const mockMessages = new Messages();

jest.mock('src/config', () => {
return {
Expand All @@ -25,14 +25,14 @@ export const createMockClient = async () => {
return {
GramJSApi: jest
.fn()
.mockImplementation(() => new MockGramJSApi(mockMessages)),
.mockImplementation(() => new FakeGramJSApi(mockMessages)),
loginAsAccount: jest.fn(),
loginAsBot: jest.fn(),
};
});
jest.mock('src/api/impl/telegraf', () => {
return {
TelegrafApi: jest.fn().mockImplementation(() => new MockTelegrafApi()),
TelegrafApi: jest.fn().mockImplementation(() => new FakeTelegraf()),
createBot: jest.fn().mockImplementation(() => null),
};
});
Expand Down
2 changes: 1 addition & 1 deletion test/server/webdav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { v2 as webdav } from 'webdav-server';
import { TGFSFileSystem } from 'src/server/webdav/tgfs-filesystem';
import { Logger } from 'src/utils/logger';

import { createMockClient } from '../utils/mock-tg-client';
import { createMockClient } from '../mock/mock-tg-client';

describe('TGFSFileSystem', () => {
const getServer = async () => {
Expand Down
3 changes: 0 additions & 3 deletions test/utils/mock-telegraf-api.ts

This file was deleted.

File renamed without changes.

0 comments on commit 5ce2497

Please sign in to comment.