355 lines
11 KiB
TypeScript
355 lines
11 KiB
TypeScript
|
// tested class
|
||
|
import { PeertubeListenerRss } from "../";
|
||
|
|
||
|
// Unit test
|
||
|
import path from "path";
|
||
|
|
||
|
import events from "events";
|
||
|
|
||
|
import nock from "nock";
|
||
|
import * as chai from "chai";
|
||
|
import sinon from "ts-sinon";
|
||
|
import sinonChai from "sinon-chai";
|
||
|
|
||
|
chai.use(sinonChai);
|
||
|
const expect = chai.expect;
|
||
|
|
||
|
// default value (more easy when it's aliases)
|
||
|
const defaultChannelID = "3";
|
||
|
const defaultInstanceAdress = "video.monsieurbidouille.fr";
|
||
|
const defaultTimeloop = 15;
|
||
|
const defaultHistory = [
|
||
|
"https://video.monsieurbidouille.fr/videos/watch/5ffe5a3a-8377-472d-9c04-d4ed130d6775",
|
||
|
];
|
||
|
|
||
|
// expected value during my test
|
||
|
const expectedChannelAddress = `https://${defaultInstanceAdress}/feeds/videos.xml?videoChannelId=${defaultChannelID}`;
|
||
|
const expectedCustomFields = {
|
||
|
"media:group": ["media:group"],
|
||
|
};
|
||
|
|
||
|
const expectedElmts = require("./RessourcesTest/expectedElmts.json");
|
||
|
const expectedFirstElmt = expectedElmts[0];
|
||
|
const expectedLastElmt = {}; //expectedElmts[1];
|
||
|
|
||
|
// let's test
|
||
|
describe("test ytbListener_RSS class", function () {
|
||
|
describe("test constructor", function () {
|
||
|
it("construct with 3 params", function () {
|
||
|
// given
|
||
|
const listener = new PeertubeListenerRss(
|
||
|
defaultInstanceAdress,
|
||
|
defaultChannelID,
|
||
|
defaultTimeloop, // 15 sec
|
||
|
defaultHistory
|
||
|
);
|
||
|
|
||
|
// assertions
|
||
|
expect(listener.address).to.be.eql(expectedChannelAddress);
|
||
|
expect(listener.timeloop).to.be.eql(defaultTimeloop);
|
||
|
expect(listener.customfields).to.be.eql(expectedCustomFields);
|
||
|
expect(listener.lastEntriesLinks).to.be.eql(defaultHistory);
|
||
|
});
|
||
|
|
||
|
it("construct with 2 params (without history)", function () {
|
||
|
// given
|
||
|
const listener = new PeertubeListenerRss(
|
||
|
defaultInstanceAdress,
|
||
|
defaultChannelID,
|
||
|
defaultTimeloop // 15 sec
|
||
|
);
|
||
|
|
||
|
// assertions
|
||
|
expect(listener.address).to.be.eql(expectedChannelAddress);
|
||
|
expect(listener.timeloop).to.be.eql(defaultTimeloop);
|
||
|
expect(listener.customfields).to.be.eql(expectedCustomFields);
|
||
|
});
|
||
|
|
||
|
it("construct with 1 params (without history and timeloop)", function () {
|
||
|
// given
|
||
|
const listener = new PeertubeListenerRss(
|
||
|
defaultInstanceAdress,
|
||
|
defaultChannelID
|
||
|
);
|
||
|
|
||
|
// assertions
|
||
|
expect(listener.address).to.be.eql(expectedChannelAddress);
|
||
|
expect(listener.timeloop).to.be.eql(5 * 60);
|
||
|
expect(listener.customfields).to.be.eql(expectedCustomFields);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe.only("integration test", function () {
|
||
|
beforeEach(function () {
|
||
|
nock.disableNetConnect();
|
||
|
});
|
||
|
|
||
|
afterEach(function () {
|
||
|
nock.cleanAll();
|
||
|
});
|
||
|
|
||
|
it.only("fetches", async function () {
|
||
|
// given
|
||
|
nock(`https://${defaultInstanceAdress}`)
|
||
|
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
|
||
|
.replyWithFile(
|
||
|
200,
|
||
|
path.join(__dirname, "RessourcesTest/peertube_feed.rss"),
|
||
|
{
|
||
|
"content-type": "application/rss+xml",
|
||
|
charset: "UTF-8",
|
||
|
}
|
||
|
);
|
||
|
|
||
|
const listener = new PeertubeListenerRss(
|
||
|
defaultInstanceAdress,
|
||
|
defaultChannelID,
|
||
|
defaultTimeloop
|
||
|
);
|
||
|
|
||
|
//when
|
||
|
const res = await listener.fetchRSS();
|
||
|
|
||
|
// assertion
|
||
|
|
||
|
// console.log(JSON.stringify(res.items[0]));
|
||
|
|
||
|
expect(res.items[0]).to.be.eql(expectedFirstElmt);
|
||
|
//expect(res.items[res.items.length - 1]).to.be.eql(expectedLastElmt);
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* This test will test the usage of the librairie with, in the order
|
||
|
* - 1 fetch of the original document (here trigger update and new_entries events)
|
||
|
* - 1 fetch with the original document and a new entry (here trigger update and new_entries events)
|
||
|
* - 1 fetch of the previous document (here trigger update events)
|
||
|
*/
|
||
|
it("fetches with start loop in 3 times", async function () {
|
||
|
// given
|
||
|
const clock = sinon.useFakeTimers();
|
||
|
|
||
|
nock(`https://${defaultInstanceAdress}`)
|
||
|
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
|
||
|
.replyWithFile(
|
||
|
200,
|
||
|
path.join(__dirname, "RessourcesTest/peertube_feed.rss"),
|
||
|
{
|
||
|
"content-type": "application/rss+xml",
|
||
|
charset: "UTF-8",
|
||
|
}
|
||
|
);
|
||
|
|
||
|
const listener = new PeertubeListenerRss(
|
||
|
defaultInstanceAdress,
|
||
|
defaultChannelID,
|
||
|
defaultTimeloop
|
||
|
);
|
||
|
|
||
|
// spy
|
||
|
const updateListenerSpy = sinon.spy();
|
||
|
const newEntriesListenerSpy = sinon.spy();
|
||
|
|
||
|
// start timer
|
||
|
listener.on("update", updateListenerSpy);
|
||
|
listener.on("newEntries", newEntriesListenerSpy);
|
||
|
|
||
|
listener.start();
|
||
|
|
||
|
// when
|
||
|
await events.once(listener, "update");
|
||
|
|
||
|
// assertion
|
||
|
expect(updateListenerSpy).to.have.been.calledOnce;
|
||
|
expect(updateListenerSpy.firstCall.args[0])
|
||
|
.to.have.property("items")
|
||
|
.that.deep.include.members([expectedFirstElmt, expectedLastElmt]);
|
||
|
expect(newEntriesListenerSpy).to.have.been.called;
|
||
|
expect(
|
||
|
newEntriesListenerSpy.firstCall.args[0]
|
||
|
).that.deep.include.members([expectedFirstElmt, expectedLastElmt]);
|
||
|
expect(updateListenerSpy.firstCall.args[0].items.length).to.be.eql(
|
||
|
newEntriesListenerSpy.firstCall.args[0].length
|
||
|
);
|
||
|
|
||
|
// todo update the rss feed
|
||
|
// Fake RSS entry to simulate an update
|
||
|
const newEntry = expectedElmts[2];
|
||
|
|
||
|
nock(`https://${defaultInstanceAdress}`)
|
||
|
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
|
||
|
.replyWithFile(
|
||
|
200,
|
||
|
path.join(__dirname, "RessourcesTest/peertube_feed_new_entries.rss"),
|
||
|
{
|
||
|
"content-type": "application/rss+xml",
|
||
|
charset: "UTF-8",
|
||
|
}
|
||
|
);
|
||
|
|
||
|
// when
|
||
|
await clock.tickAsync(15000);
|
||
|
await events.once(listener, "update");
|
||
|
|
||
|
// assertion
|
||
|
expect(updateListenerSpy).to.have.been.calledTwice;
|
||
|
expect(updateListenerSpy.secondCall.args[0])
|
||
|
.to.have.property("items")
|
||
|
.that.deep.include.members([
|
||
|
expectedFirstElmt,
|
||
|
expectedLastElmt,
|
||
|
newEntry,
|
||
|
]);
|
||
|
expect(newEntriesListenerSpy).to.have.been.calledTwice;
|
||
|
expect(updateListenerSpy.secondCall.args[0])
|
||
|
.to.have.property("items")
|
||
|
.that.deep.include.members([newEntry]);
|
||
|
|
||
|
// when
|
||
|
nock(`https://${defaultInstanceAdress}`)
|
||
|
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
|
||
|
.replyWithFile(
|
||
|
200,
|
||
|
path.join(__dirname, "RessourcesTest/peertube_feed_new_entries.rss"),
|
||
|
{
|
||
|
"content-type": "application/rss+xml",
|
||
|
charset: "UTF-8",
|
||
|
}
|
||
|
);
|
||
|
|
||
|
await clock.tickAsync(15000);
|
||
|
await events.once(listener, "update");
|
||
|
|
||
|
// assertion
|
||
|
expect(updateListenerSpy).to.have.been.calledThrice;
|
||
|
expect(updateListenerSpy.secondCall.args[0])
|
||
|
.to.have.property("items")
|
||
|
.that.deep.include.members([
|
||
|
expectedFirstElmt,
|
||
|
expectedLastElmt,
|
||
|
newEntry,
|
||
|
]);
|
||
|
expect(newEntriesListenerSpy).to.not.have.been.calledThrice;
|
||
|
|
||
|
// then
|
||
|
listener.stop();
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* This test will test the usage of the librairie with, in the order
|
||
|
* - 1 fetch of the original document (here trigger update and new_entries events)
|
||
|
* - 1 fetch with the original document and a new entry (here trigger update and new_entries events)
|
||
|
* - 1 fetch of the previous document (here trigger update events)
|
||
|
*/
|
||
|
it("fetches with start loop in 3 times and history is initialize", async function () {
|
||
|
// given
|
||
|
const clock = sinon.useFakeTimers();
|
||
|
|
||
|
nock(`https://${defaultInstanceAdress}`)
|
||
|
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
|
||
|
.replyWithFile(
|
||
|
200,
|
||
|
path.join(__dirname, "RessourcesTest/peertube_feed.rss"),
|
||
|
{
|
||
|
"content-type": "application/rss+xml",
|
||
|
charset: "UTF-8",
|
||
|
}
|
||
|
);
|
||
|
|
||
|
const listener = new PeertubeListenerRss(
|
||
|
defaultInstanceAdress,
|
||
|
defaultChannelID,
|
||
|
defaultTimeloop,
|
||
|
defaultHistory
|
||
|
);
|
||
|
|
||
|
// spy
|
||
|
const updateListenerSpy = sinon.spy();
|
||
|
const newEntriesListenerSpy = sinon.spy();
|
||
|
|
||
|
// start timer
|
||
|
listener.on("update", updateListenerSpy);
|
||
|
listener.on("newEntries", newEntriesListenerSpy);
|
||
|
|
||
|
listener.start();
|
||
|
|
||
|
// when
|
||
|
await events.once(listener, "update");
|
||
|
|
||
|
// assertion
|
||
|
expect(updateListenerSpy).to.have.been.calledOnce;
|
||
|
expect(updateListenerSpy.firstCall.args[0])
|
||
|
.to.have.property("items")
|
||
|
.that.deep.include.members([expectedFirstElmt, expectedLastElmt]);
|
||
|
expect(newEntriesListenerSpy).to.have.been.called;
|
||
|
expect(newEntriesListenerSpy.firstCall.args[0])
|
||
|
.that.deep.include.members([expectedLastElmt])
|
||
|
.and.that.not.deep.include.members([expectedFirstElmt]);
|
||
|
expect(updateListenerSpy.firstCall.args[0].items.length).to.be.eql(
|
||
|
newEntriesListenerSpy.firstCall.args[0].length + 1
|
||
|
);
|
||
|
|
||
|
// todo update the rss feed
|
||
|
// Fake RSS entry to simulate an update
|
||
|
const newEntry = expectedElmts[2];
|
||
|
|
||
|
nock(`https://${defaultInstanceAdress}`)
|
||
|
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
|
||
|
.replyWithFile(
|
||
|
200,
|
||
|
path.join(__dirname, "RessourcesTest/peertube_feed_new_entries.rss"),
|
||
|
{
|
||
|
"content-type": "application/rss+xml",
|
||
|
charset: "UTF-8",
|
||
|
}
|
||
|
);
|
||
|
|
||
|
// when
|
||
|
await clock.tickAsync(15000);
|
||
|
await events.once(listener, "update");
|
||
|
|
||
|
// assertion
|
||
|
expect(updateListenerSpy).to.have.been.calledTwice;
|
||
|
expect(updateListenerSpy.secondCall.args[0])
|
||
|
.to.have.property("items")
|
||
|
.that.deep.include.members([
|
||
|
expectedFirstElmt,
|
||
|
expectedLastElmt,
|
||
|
newEntry,
|
||
|
]);
|
||
|
expect(newEntriesListenerSpy).to.have.been.calledTwice;
|
||
|
expect(updateListenerSpy.secondCall.args[0])
|
||
|
.to.have.property("items")
|
||
|
.that.deep.include.members([newEntry]);
|
||
|
|
||
|
// when
|
||
|
nock(`https://${defaultInstanceAdress}`)
|
||
|
.get(`/feeds/videos.xml?videoChannelId=${defaultChannelID}`)
|
||
|
.replyWithFile(
|
||
|
200,
|
||
|
path.join(__dirname, "RessourcesTest/peertube_feed_new_entries.rss"),
|
||
|
{
|
||
|
"content-type": "application/rss+xml",
|
||
|
charset: "UTF-8",
|
||
|
}
|
||
|
);
|
||
|
|
||
|
await clock.tickAsync(15000);
|
||
|
await events.once(listener, "update");
|
||
|
|
||
|
// assertion
|
||
|
expect(updateListenerSpy).to.have.been.calledThrice;
|
||
|
expect(updateListenerSpy.secondCall.args[0])
|
||
|
.to.have.property("items")
|
||
|
.that.deep.include.members([
|
||
|
expectedFirstElmt,
|
||
|
expectedLastElmt,
|
||
|
newEntry,
|
||
|
]);
|
||
|
expect(newEntriesListenerSpy).to.not.have.been.calledThrice;
|
||
|
|
||
|
// then
|
||
|
listener.stop();
|
||
|
});
|
||
|
});
|
||
|
});
|