diff --git a/test/index-test.js b/test/index-test.js index 9b6a98e..6935022 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -4,6 +4,7 @@ const api = require("./suites/api-test.js").suite; const credentials = require("./suites/credentials-test.js").suite; const network = require("./suites/network-helper-test.js").suite; +const platform = require("./suites/platform-data-test.js").suite; const scraper = require("./suites/scraper-test.js").suite; const searcher = require("./suites/searcher-test.js").suite; const uScraper = require("./suites/user-scraper-test.js").suite; @@ -24,6 +25,7 @@ describe("Test F95 modules", function testF95Modules() { this.timeout(15000); // All tests in this suite get 15 seconds before timeout //#endregion Set-up + describe("Test platform data fetch", platform.bind(this)); describe("Test scraper methods", scraper.bind(this)); describe("Test searcher methods", searcher.bind(this)); describe("Test user scraper methods", uScraper.bind(this)); diff --git a/test/suites/platform-data-test.js b/test/suites/platform-data-test.js new file mode 100644 index 0000000..e00272a --- /dev/null +++ b/test/suites/platform-data-test.js @@ -0,0 +1,65 @@ +"use strict"; + +// Public module from npm +const expect = require("chai").expect; +const dotenv = require("dotenv"); +const { isEqual } = require("lodash"); + +// Core modules +const fs = require("fs"); + +// Modules from file +const shared = require("../../app/scripts/shared.js"); +const platform = require("../../app/scripts/platform-data.js"); +const Credentials = require("../../app/scripts/classes/credentials.js"); +const { authenticate } = require("../../app/scripts/network-helper.js"); + +// Configure the .env reader +dotenv.config(); + +// Global variables +const USERNAME = process.env.F95_USERNAME; +const PASSWORD = process.env.F95_PASSWORD; + +module.exports.suite = function suite() { + //#region Setup + before(async function beforeAll() { + // Authenticate + const creds = new Credentials(USERNAME, PASSWORD); + await creds.fetchToken(); + await authenticate(creds); + }); + //#endregion Setup + + it("Fetch new platform data", async function fetchNewPlatformData() { + // Delete the current platform data (if exists) + if(fs.existsSync(shared.cachePath)) fs.unlinkSync(shared.cachePath); + + // Fetch data + await platform.fetchPlatformData(); + + // Check data + const enginesEquality = isEqual({}, shared.engines); + const statusEquality = isEqual({}, shared.statuses); + const tagsEquality = isEqual({}, shared.tags); + expect(enginesEquality, "Should not be empty").to.be.false; + expect(statusEquality, "Should not be empty").to.be.false; + expect(tagsEquality, "Should not be empty").to.be.false; + + // Check if the file exists + expect(fs.existsSync(shared.cachePath)).to.be.true; + }); + + it("Fetch cached platform data", async function fetchCachedPlatformData() { + // Fetch data + await platform.fetchPlatformData(); + + // Check data + const enginesEquality = isEqual({}, shared.engines); + const statusEquality = isEqual({}, shared.statuses); + const tagsEquality = isEqual({}, shared.tags); + expect(enginesEquality, "Should not be empty").to.be.false; + expect(statusEquality, "Should not be empty").to.be.false; + expect(tagsEquality, "Should not be empty").to.be.false; + }); +};