Using pino as a log writer + Append the Generics to make Routing more scalable

This commit is contained in:
Amaury
2021-06-25 16:19:43 +02:00
parent d1f1165136
commit 01ca99c4e7
9 changed files with 4341 additions and 464 deletions

View File

@ -1,77 +0,0 @@
import chai, { expect } from 'chai';
import sinon from 'ts-sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
import { DiscordParser } from '../src/discordParser';
//data
const config: DiscordParser.Config = {
channelsId: {
idChannelPeerTube: 'peertubeID',
idChannelYtb: 'ytbChannel',
},
keyWord: 'myDiscordKeyword',
name: 'DiscordTestedInterface',
token: 'mySecretDiscordToken',
};
//mockeded imports
import { Channel, ChannelManager, Client } from 'discord.js';
describe.skip('test DiscordParser', function () {
let clientMockOn: sinon.SinonStub,
clientMockLogin: sinon.SinonStub,
channelStub: sinon.SinonStubbedInstance<Channel>,
channelManagerMockResolve: sinon.SinonStub;
before(() => {
clientMockOn = sinon.stub(Client.prototype, 'on');
clientMockOn.withArgs('message', sinon.match.func).onFirstCall();
clientMockOn.throws('Error, bad parameter or too much call');
clientMockLogin = sinon.stub(Client.prototype, 'login');
clientMockLogin
.withArgs(config.token)
.onFirstCall()
.resolves('ok')
.returnsThis();
clientMockLogin.throws('Error, bad parameter or too much call');
channelStub = sinon.createStubInstance(Channel);
channelManagerMockResolve = sinon.stub(
ChannelManager.prototype,
'resolve'
);
channelManagerMockResolve
.withArgs(config.channelsId.idChannelPeerTube)
.onFirstCall()
.returns(channelStub);
channelManagerMockResolve
.withArgs(config.channelsId.idChannelYtb)
.onFirstCall()
.returns(channelStub);
channelManagerMockResolve.throws("Error Bad id's or too much call");
});
after(() => {
clientMockOn.restore();
clientMockLogin.restore();
channelManagerMockResolve.restore();
});
it('it will test the DiscordParser constructor', async function () {
//when
const discordParser = new DiscordParser(config);
// expect
expect(discordParser.token).to.be.eql(config.token);
await discordParser.channels.then((channels) => {
expect(channels.ChannelYtb.id).to.be.eql(
config.channelsId.idChannelYtb
);
});
});
});

View File

@ -5,42 +5,49 @@ import sinonChai from 'sinon-chai';
chai.use(sinonChai);
const expect = chai.expect;
import { withFile } from 'tmp-promise';
import { writeFileSync } from 'fs';
import { ImplementableApi, Router } from '../src/index';
import { Router } from '../src/index';
import { DiscordParser } from '../src/discordParser';
import { LogWriter } from '../src/logWriter';
import { PeerTubeRequester } from '../src/peertubeRequester';
// const well_build_routing_file: Router.GlobalConfig = require('./rsrc/wellBuildedRoutingFile.json');
const well_build_routing_file: Router.GlobalConfig = require('./rsrc/wellBuildedRoutingFile.json');
const events: Router.EventsType[] = [
{
name: 'newEntries',
type: 'emit',
},
{
name: 'addListener',
type: 'received',
},
];
type AllowedEvents = 'newEntries' | 'addListener'; // need to match with the EventsType's names
describe('testing the routing part', function () {
describe('testing the building part', function () {
it('it will test a normal building', async function () {
await withFile(
async (file) => {
const edit_config = {
...well_build_routing_file,
logWriter: {
...well_build_routing_file.logWriter,
...{ path: file.path },
},
};
const r = new Router(edit_config);
class FakeApi extends ImplementableApi<AllowedEvents> {
constructor(conf: ImplementableApi.Config) {
super(conf);
}
// expect(r.api_array['Discord']).to.be.instanceOf(
// DiscordParser
// );
expect(r.api_array['logWriter']).to.be.instanceOf(
LogWriter
);
expect(r.api_array['peertubeRequester']).to.be.instanceOf(
PeerTubeRequester
);
},
{ postfix: '.log' }
);
receivedMessage(msg: ImplementableApi.Message<AllowedEvents>) {
// don't do anything
}
}
describe.only('testing the routing part', function () {
describe('testing the injection', function () {
it('will test the dependency injection', function () {
// given
const myRouter = new Router<AllowedEvents>(events);
const myObj = new FakeApi({ name: 'myFakeApi' });
// first expect
expect(myRouter.api_array).to.be.empty;
expect(myRouter.events).to.be.empty;
// when
myRouter.injectDependency(myObj);
// expect
expect(myRouter.api_array).to.have.keys(['myFakeApi']);
expect(myRouter.api_array['myFakeApi']).to.be.instanceOf(FakeApi);
expect(myRouter.events).to.be.empty;
});
});
describe('testing the data transmission', function () {
@ -48,6 +55,31 @@ describe('testing the routing part', function () {
it('it will emit a new listener request', function () {});
});
describe('testing the data reception', function () {
it('it will received a new entries notification', function () {});
it.only('it will received a new entries notification', function () {
//given
const myMessageWhoWouldBeSend: ImplementableApi.Message<AllowedEvents> =
{
rawContent: 'My content',
type: 'logging',
idListener: 5,
};
const myStub = sinon.stub(FakeApi.prototype, 'receivedMessage');
const myRouter = new Router([]);
const myObj = new FakeApi({ name: 'myFakeApi' });
myRouter.injectDependency(myObj);
// when
myRouter.receivedMessage(myMessageWhoWouldBeSend);
// expect
expect(myStub.firstCall).to.have.been.calledWith(
myMessageWhoWouldBeSend
);
expect(myStub.secondCall).to.have.been.calledWith({
rawContent:
'The dependency `myFakeApi` was well injected into the router',
type: 'logging',
});
});
});
});

View File

@ -1,43 +0,0 @@
import chai, { expect } from 'chai';
import sinon from 'ts-sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
import { withDir } from 'tmp-promise';
import { existsSync, readFileSync } from 'fs';
import { LogWriter } from '../src/logWriter';
const config: LogWriter.Config = {
name: 'logWirterTested',
path: 'it will be set by tmp-promise',
};
describe('test logWriter', function () {
describe('constructor', function () {
it('will test the constructor with a new file', async function () {
await withDir(
async (dir) => {
const log_writer = new LogWriter({
...config,
...{ path: dir.path + '/toto.log' },
});
expect(existsSync(dir.path + '/toto.log')).to.be.true;
expect(
readFileSync(dir.path + '/toto.log', {
encoding: 'utf-8',
})
).to.match(/LogWriter is running\n+$/g);
},
{ unsafeCleanup: true }
);
});
});
describe('received message', function () {
it('will test the print function', function () {});
});
});
//presenter le projet . listener -> import sous forme de plugin