Youtube listener done + somes tests
This commit is contained in:
		
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -1,7 +1,11 @@
 | 
			
		||||
// tested class
 | 
			
		||||
import { YoutubeListenerRss } from "./../src/";
 | 
			
		||||
import { YoutubeListenerRss } 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";
 | 
			
		||||
@@ -11,90 +15,122 @@ chai.use(sinonChai);
 | 
			
		||||
const expect = chai.expect;
 | 
			
		||||
 | 
			
		||||
// default value (more easy when it's aliases)
 | 
			
		||||
const default_channel_ID = "UCh2YBKhYIy-_LtfCIn2Jycg";
 | 
			
		||||
const default_timeloop = 15;
 | 
			
		||||
const defaultChannelID = "UCh2YBKhYIy-_LtfCIn2Jycg";
 | 
			
		||||
const defaultTimeloop = 15;
 | 
			
		||||
const defaultHistory = ["https://www.youtube.com/watch?v=OYoZgsyu7tw"];
 | 
			
		||||
 | 
			
		||||
// expected value during my test
 | 
			
		||||
const expected_default_address = "http://www.youtube.com/feeds/videos.xml?channel_id=".concat(
 | 
			
		||||
  default_channel_ID
 | 
			
		||||
);
 | 
			
		||||
const expected_custom_fields = {
 | 
			
		||||
const expectedChannelAddress = `https://www.youtube.com/feeds/videos.xml?channel_id=${defaultChannelID}`;
 | 
			
		||||
const expectedCustomFields = {
 | 
			
		||||
  description: ["media:group", "media:description"],
 | 
			
		||||
  icon: ["media:group", "media:thumbnail"],
 | 
			
		||||
};
 | 
			
		||||
const expected_resolve_fetch = {}; // TODO charger le fichier "youtube_feed.rss"
 | 
			
		||||
 | 
			
		||||
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 2 params", function () {
 | 
			
		||||
    it("construct with 3 params", function () {
 | 
			
		||||
      // given
 | 
			
		||||
      const listener = new YoutubeListenerRss(
 | 
			
		||||
        default_channel_ID,
 | 
			
		||||
        default_timeloop // 15 sec
 | 
			
		||||
        defaultChannelID,
 | 
			
		||||
        defaultTimeloop, // 15 sec
 | 
			
		||||
        defaultHistory
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      // assertions
 | 
			
		||||
      expect(listener.address).to.be.eql(expected_default_address);
 | 
			
		||||
      expect(listener.timeloop).to.be.eql(default_timeloop);
 | 
			
		||||
      expect(listener.customfields).to.be.eql(expected_custom_fields);
 | 
			
		||||
      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 1 params (without timeloop)", function () {
 | 
			
		||||
    it("construct with 2 params (without history)", function () {
 | 
			
		||||
      // given
 | 
			
		||||
      const listener = new YoutubeListenerRss(
 | 
			
		||||
        default_channel_ID,
 | 
			
		||||
        default_timeloop // 15 sec
 | 
			
		||||
        defaultChannelID,
 | 
			
		||||
        defaultTimeloop // 15 sec
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      // assertions
 | 
			
		||||
      expect(listener.address).to.be.eql(expected_default_address);
 | 
			
		||||
      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 YoutubeListenerRss(defaultChannelID);
 | 
			
		||||
 | 
			
		||||
      // assertions
 | 
			
		||||
      expect(listener.address).to.be.eql(expectedChannelAddress);
 | 
			
		||||
      expect(listener.timeloop).to.be.eql(5 * 60);
 | 
			
		||||
      expect(listener.customfields).to.be.eql(expected_custom_fields);
 | 
			
		||||
      expect(listener.customfields).to.be.eql(expectedCustomFields);
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  describe("test fetch", function () {
 | 
			
		||||
  describe("integration test", function () {
 | 
			
		||||
    beforeEach(function () {
 | 
			
		||||
      nock.disableNetConnect();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    afterEach(function () {
 | 
			
		||||
      nock.cleanAll();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it("fetches", async function () {
 | 
			
		||||
      // given
 | 
			
		||||
      nock.disableNetConnect();
 | 
			
		||||
 | 
			
		||||
      nock("http://www.youtube.com")
 | 
			
		||||
      nock("https://www.youtube.com")
 | 
			
		||||
        .get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
 | 
			
		||||
        .replyWithFile(200, __dirname + "RessourcesTest/youtube_feed.rss", {
 | 
			
		||||
          "content-type": "text/xml",
 | 
			
		||||
          charset: "UTF-8",
 | 
			
		||||
        });
 | 
			
		||||
        .replyWithFile(
 | 
			
		||||
          200,
 | 
			
		||||
          path.join(__dirname, "RessourcesTest/youtube_feed.rss"),
 | 
			
		||||
          {
 | 
			
		||||
            "content-type": "text/xml",
 | 
			
		||||
            charset: "UTF-8",
 | 
			
		||||
          }
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
      const listener = new YoutubeListenerRss(
 | 
			
		||||
        default_channel_ID,
 | 
			
		||||
        default_timeloop
 | 
			
		||||
        defaultChannelID,
 | 
			
		||||
        defaultTimeloop
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      //when
 | 
			
		||||
      const res = await listener.fetchRSS();
 | 
			
		||||
 | 
			
		||||
      // assertion
 | 
			
		||||
      expect(res).to.be.eql(expected_resolve_fetch);
 | 
			
		||||
      expect(res.items[0]).to.be.eql(expectedFirstElmt);
 | 
			
		||||
      expect(res.items[res.items.length - 1]).to.be.eql(expectedLastElmt);
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
  describe("start", function () {
 | 
			
		||||
    it("fetches with start (bad name, find a better)", async function () {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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.disableNetConnect();
 | 
			
		||||
 | 
			
		||||
      nock("http://www.youtube.com")
 | 
			
		||||
      nock("https://www.youtube.com")
 | 
			
		||||
        .get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
 | 
			
		||||
        .replyWithFile(200, __dirname + "RessourcesTest/youtube_feed.rss", {
 | 
			
		||||
          "content-type": "text/xml",
 | 
			
		||||
          charset: "UTF-8",
 | 
			
		||||
        });
 | 
			
		||||
        .replyWithFile(
 | 
			
		||||
          200,
 | 
			
		||||
          path.join(__dirname, "RessourcesTest/youtube_feed.rss"),
 | 
			
		||||
          {
 | 
			
		||||
            "content-type": "text/xml",
 | 
			
		||||
            charset: "UTF-8",
 | 
			
		||||
          }
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
      const listener = new YoutubeListenerRss(
 | 
			
		||||
        default_channel_ID,
 | 
			
		||||
        default_timeloop
 | 
			
		||||
        defaultChannelID,
 | 
			
		||||
        defaultTimeloop
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      // spy
 | 
			
		||||
@@ -103,29 +139,200 @@ describe("test ytbListener_RSS class", function () {
 | 
			
		||||
 | 
			
		||||
      // start timer
 | 
			
		||||
      listener.on("update", updateListenerSpy);
 | 
			
		||||
      listener.on("newnEntries", newEntriesListenerSpy);
 | 
			
		||||
      listener.on("newEntries", newEntriesListenerSpy);
 | 
			
		||||
 | 
			
		||||
      listener.start();
 | 
			
		||||
 | 
			
		||||
      // when
 | 
			
		||||
      await clock.tickAsync(1);
 | 
			
		||||
      await events.once(listener, "update");
 | 
			
		||||
 | 
			
		||||
      // assertion
 | 
			
		||||
      expect(updateListenerSpy).to.have.been.calledOnce;
 | 
			
		||||
      expect(updateListenerSpy).to.have.been.calledWith(expected_resolve_fetch);
 | 
			
		||||
      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://www.youtube.com")
 | 
			
		||||
        .get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
 | 
			
		||||
        .replyWithFile(
 | 
			
		||||
          200,
 | 
			
		||||
          path.join(__dirname, "RessourcesTest/youtube_feed_new_entries.rss"),
 | 
			
		||||
          {
 | 
			
		||||
            "content-type": "text/xml",
 | 
			
		||||
            charset: "UTF-8",
 | 
			
		||||
          }
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
      // when
 | 
			
		||||
      await clock.tickAsync(default_timeloop + 1);
 | 
			
		||||
      await clock.tickAsync(15000);
 | 
			
		||||
      await events.once(listener, "update");
 | 
			
		||||
 | 
			
		||||
      // assertion
 | 
			
		||||
      expect(updateListenerSpy).to.have.been.calledTwice;
 | 
			
		||||
      expect(updateListenerSpy).to.have.been.calledWith(expected_resolve_fetch);
 | 
			
		||||
      expect(newEntriesListenerSpy).to.have.been.calledOnce;
 | 
			
		||||
      expect(
 | 
			
		||||
        newEntriesListenerSpy
 | 
			
		||||
      ).to.have.been.calledWith(/* Put the added item */);
 | 
			
		||||
      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://www.youtube.com")
 | 
			
		||||
        .get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
 | 
			
		||||
        .replyWithFile(
 | 
			
		||||
          200,
 | 
			
		||||
          path.join(__dirname, "RessourcesTest/youtube_feed_new_entries.rss"),
 | 
			
		||||
          {
 | 
			
		||||
            "content-type": "text/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://www.youtube.com")
 | 
			
		||||
        .get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
 | 
			
		||||
        .replyWithFile(
 | 
			
		||||
          200,
 | 
			
		||||
          path.join(__dirname, "RessourcesTest/youtube_feed.rss"),
 | 
			
		||||
          {
 | 
			
		||||
            "content-type": "text/xml",
 | 
			
		||||
            charset: "UTF-8",
 | 
			
		||||
          }
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
      const listener = new YoutubeListenerRss(
 | 
			
		||||
        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://www.youtube.com")
 | 
			
		||||
        .get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
 | 
			
		||||
        .replyWithFile(
 | 
			
		||||
          200,
 | 
			
		||||
          path.join(__dirname, "RessourcesTest/youtube_feed_new_entries.rss"),
 | 
			
		||||
          {
 | 
			
		||||
            "content-type": "text/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://www.youtube.com")
 | 
			
		||||
        .get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
 | 
			
		||||
        .replyWithFile(
 | 
			
		||||
          200,
 | 
			
		||||
          path.join(__dirname, "RessourcesTest/youtube_feed_new_entries.rss"),
 | 
			
		||||
          {
 | 
			
		||||
            "content-type": "text/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();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user