Add test logWriter + more things inside my other tests
This commit is contained in:
@ -1,11 +1,8 @@
|
||||
import chai from 'chai';
|
||||
import chai, { expect } from 'chai';
|
||||
import sinon from 'ts-sinon';
|
||||
import sinonChai from 'sinon-chai';
|
||||
|
||||
chai.use(sinonChai);
|
||||
const expect = chai.expect;
|
||||
|
||||
import nock, { disableNetConnect, RequestBodyMatcher } from 'nock';
|
||||
|
||||
import { DiscordParser } from '../src/discordParser';
|
||||
|
||||
@ -20,36 +17,61 @@ const config: DiscordParser.Config = {
|
||||
token: 'mySecretDiscordToken',
|
||||
};
|
||||
|
||||
//stubed imports
|
||||
//mockeded imports
|
||||
import { Channel, ChannelManager, Client } from 'discord.js';
|
||||
import { utils } from 'mocha';
|
||||
|
||||
describe.only('test DiscordParser', function () {
|
||||
let clientStub: sinon.SinonStubbedInstance<Client>,
|
||||
channelManagerStub: sinon.SinonStubbedInstance<ChannelManager>;
|
||||
describe.skip('test DiscordParser', function () {
|
||||
let clientMockOn: sinon.SinonStub,
|
||||
clientMockLogin: sinon.SinonStub,
|
||||
channelStub: sinon.SinonStubbedInstance<Channel>,
|
||||
channelManagerMockResolve: sinon.SinonStub;
|
||||
|
||||
before(() => {
|
||||
clientStub = sinon.createStubInstance(Client);
|
||||
clientStub.login.withArgs(config.token).onFirstCall().resolves('ok');
|
||||
clientStub.login.throws('Error, bad parameter or too much call');
|
||||
clientMockOn = sinon.stub(Client.prototype, 'on');
|
||||
clientMockOn.withArgs('message', sinon.match.func).onFirstCall();
|
||||
clientMockOn.throws('Error, bad parameter or too much call');
|
||||
|
||||
channelManagerStub = sinon.createStubInstance(ChannelManager);
|
||||
channelManagerStub.resolve
|
||||
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(new Channel(new Client()))
|
||||
.returns(channelStub);
|
||||
channelManagerMockResolve
|
||||
.withArgs(config.channelsId.idChannelYtb)
|
||||
.onFirstCall()
|
||||
.returns(new Channel(new Client()));
|
||||
channelManagerStub.resolve.throws("Error Bad id's or too much call");
|
||||
.returns(channelStub);
|
||||
channelManagerMockResolve.throws("Error Bad id's or too much call");
|
||||
});
|
||||
|
||||
after(() => {
|
||||
clientStub.login.restore();
|
||||
channelManagerStub.resolve.restore();
|
||||
clientMockOn.restore();
|
||||
clientMockLogin.restore();
|
||||
channelManagerMockResolve.restore();
|
||||
});
|
||||
it('it will test the DiscordParser constructor', function () {
|
||||
|
||||
it('it will test the DiscordParser constructor', async function () {
|
||||
//when
|
||||
const discordParser = new DiscordParser(config);
|
||||
|
||||
console.log("c'est bon signe");
|
||||
// expect
|
||||
expect(discordParser.token).to.be.eql(config.token);
|
||||
await discordParser.channels.then((channels) => {
|
||||
expect(channels.ChannelYtb.id).to.be.eql(
|
||||
config.channelsId.idChannelYtb
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -13,7 +13,6 @@ import { DiscordParser } from '../src/discordParser';
|
||||
import { LogWriter } from '../src/logWriter';
|
||||
import { PeerTubeRequester } from '../src/peertubeRequester';
|
||||
|
||||
// const path = require("path");
|
||||
const well_build_routing_file: Router.GlobalConfig = require('./rsrc/wellBuildedRoutingFile.json');
|
||||
|
||||
describe('testing the routing part', function () {
|
||||
@ -30,9 +29,9 @@ describe('testing the routing part', function () {
|
||||
};
|
||||
const r = new Router(edit_config);
|
||||
|
||||
expect(r.api_array['Discord']).to.be.instanceOf(
|
||||
DiscordParser
|
||||
);
|
||||
// expect(r.api_array['Discord']).to.be.instanceOf(
|
||||
// DiscordParser
|
||||
// );
|
||||
expect(r.api_array['logWriter']).to.be.instanceOf(
|
||||
LogWriter
|
||||
);
|
||||
|
43
tests/logWriter-spec.ts
Normal file
43
tests/logWriter-spec.ts
Normal file
@ -0,0 +1,43 @@
|
||||
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
|
@ -9,7 +9,6 @@ import nock, { disableNetConnect, RequestBodyMatcher } from 'nock';
|
||||
|
||||
import { ImplementableApi } from '../src';
|
||||
import { PeerTubeRequester } from '../src/peertubeRequester';
|
||||
import { Request } from 'node-fetch';
|
||||
|
||||
const paramsPeerTube: PeerTubeRequester.Config = {
|
||||
name: 'testedRequester',
|
||||
@ -42,7 +41,7 @@ const newEntriesMessage: ImplementableApi.Message = {
|
||||
};
|
||||
|
||||
const UploadInstruction = {
|
||||
channelID: 'undefined', //todo uncompleted test but incompleted function too
|
||||
channelId: 'undefined', //todo uncompleted test but incompleted function too
|
||||
targetUrl: 'myTargerUrl',
|
||||
};
|
||||
|
||||
@ -64,6 +63,7 @@ const bodyTokenRequest: RequestBodyMatcher = {
|
||||
username: paramsPeerTube.username,
|
||||
password: paramsPeerTube.password,
|
||||
};
|
||||
|
||||
describe('PeerTube Requester Test', function () {
|
||||
before(function () {
|
||||
disableNetConnect();
|
||||
@ -81,36 +81,34 @@ describe('PeerTube Requester Test', function () {
|
||||
.reply(200, expectedReplyOauthClient)
|
||||
.post(`/users/token`, bodyTokenRequest)
|
||||
.times(3)
|
||||
.reply(200, expectedReplyTokenAddress)
|
||||
.post(`/videos/imports`, {
|
||||
channelID: 'undefined',
|
||||
targeUrl: newEntriesMessage.rawContent.items[0].link,
|
||||
})
|
||||
.reply(200, expectedReplyTokenAddress);
|
||||
const import_scope = nock(
|
||||
`https://${paramsPeerTube.domain_name}/api/v1`
|
||||
)
|
||||
.matchHeader(
|
||||
'Authorization',
|
||||
'authorization',
|
||||
`Bearer ${expectedReplyTokenAddress.access_token}`
|
||||
)
|
||||
.reply(200)
|
||||
.post(`/videos/imports`, {
|
||||
channelID: 'undefined',
|
||||
targeUrl: newEntriesMessage.rawContent.items[1].link,
|
||||
})
|
||||
.matchHeader(
|
||||
'Authorization',
|
||||
`Bearer ${expectedReplyTokenAddress.access_token}`
|
||||
)
|
||||
.reply(200)
|
||||
.post(`/videos/imports`, {
|
||||
channelID: 'undefined',
|
||||
targeUrl: newEntriesMessage.rawContent.items[2].link,
|
||||
})
|
||||
.matchHeader(
|
||||
'Authorization',
|
||||
`Bearer ${expectedReplyTokenAddress.access_token}`
|
||||
)
|
||||
.reply(200);
|
||||
.post(`/videos/imports`, (reqBody) => {
|
||||
let links: string[] = newEntriesMessage.rawContent.items.map(
|
||||
(item: any) => item.link
|
||||
);
|
||||
|
||||
if (scope.isDone()) console.log(scope.activeMocks());
|
||||
const body = new URLSearchParams(reqBody);
|
||||
if (body.get('channelId') === 'undefined') {
|
||||
const targUrl = body.get('targetUrl');
|
||||
if (targUrl && links.includes(targUrl)) {
|
||||
const index = links.findIndex(
|
||||
(elmt) => elmt === targUrl
|
||||
);
|
||||
links.splice(index, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.times(3)
|
||||
.reply(200);
|
||||
|
||||
const requester = new PeerTubeRequester(paramsPeerTube);
|
||||
|
||||
@ -118,6 +116,8 @@ describe('PeerTube Requester Test', function () {
|
||||
await requester.receivedMessage(newEntriesMessage);
|
||||
|
||||
//expected
|
||||
if (scope.isDone()) console.log(scope.activeMocks());
|
||||
// all the scope need to be completed
|
||||
expect(scope.isDone()).to.be.true;
|
||||
expect(import_scope.isDone()).to.be.true;
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user