Skip to content

Commit

Permalink
Refactor so calls to 7daystodie-api-wrapper can be tested (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
halkeye authored May 3, 2020
1 parent 869f4d2 commit b07651e
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 25 deletions.
7 changes: 2 additions & 5 deletions api/hooks/sdtdLogs/logProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = async function(job) {

// If latest log line is not found, get it from the server
if (!lastLogLine) {
const webUIUpdate = await module.exports.getWebUIUpdates(job.data.server);
const webUIUpdate = await sails.helpers.sdtdApi.getWebUIUpdates(job.data.server);
lastLogLine = parseInt(webUIUpdate.newlogs) + 1;
}

Expand All @@ -29,7 +29,7 @@ module.exports = async function(job) {
: 50;

// Get new logs from the server
const newLogs = await module.exports.getLog(job.data.server, lastLogLine, count);
const newLogs = await sails.helpers.sdtdApi.getLog(job.data.server, lastLogLine, count);

// Adjust latest log line based on new logs we got
lastLogLine = lastLogLine + newLogs.entries.length;
Expand Down Expand Up @@ -69,6 +69,3 @@ module.exports = async function(job) {
logs: resultLogs
});
};
module.exports.getWebUIUpdates = SdtdApi.getWebUIUpdates;
module.exports.getLog = SdtdApi.getLog;

6 changes: 6 additions & 0 deletions config/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const SdtdApi = require("7daystodie-api-wrapper");
/**
* Bootstrap
* (sails.config.bootstrap)
Expand All @@ -10,6 +11,11 @@
*/

module.exports.bootstrap = async function (done) {
sails.helpers.sdtdApi = {};
for (const func of Object.keys(SdtdApi)) {
sails.helpers.sdtdApi[func] = SdtdApi[func];
}

if (process.env.IS_TEST) {
sails.cache = new Object();
return done();
Expand Down
121 changes: 120 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"nodemon": "^2.0.3",
"nyc": "^15.0.1",
"sails-disk": "^1.1.2",
"sinon": "^9.0.2",
"supertest": "^3.3.0"
},
"mocha": {
Expand Down
25 changes: 6 additions & 19 deletions test/integration/hooks/sdtdLogs/logProcessor.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
const expect = require("chai").expect;
const sinon = require('sinon');
const logProcessor = require("../../../../api/hooks/sdtdLogs/logProcessor");

const origGetWebUIUpdates = logProcessor.getWebUIUpdates;
const origGetLog = logProcessor.getLog;

describe('logProcessor', function () {
beforeEach(() => {
sails.cache = {};
if (!logProcessor.getWebUIUpdates) {
throw new Error('Fix test, function is undefined');
}
logProcessor.getWebUIUpdates = origGetWebUIUpdates;
if (!logProcessor.getLog) {
throw new Error('Fix test, function is undefined');
}
logProcessor.getLog = origGetLog;
});
it('should return an array', async function () {
it('Confirm able to fetch log messages', async function () {
sails.cache[`sdtdserver:${sails.testServer.id}:sdtdLogs:lastLogLine`] = 1100;
logProcessor.getWebUIUpdates = async function() {
sinon.stub(sails.helpers.sdtdApi, "getWebUIUpdates").callsFake(async function() {
return {
newlogs: 1100
}
};
logProcessor.getLog = async function() {
});
sinon.stub(sails.helpers.sdtdApi, "getLog").callsFake(async function() {
return {
firstLine: 1100,
lastLine: 1150,
Expand All @@ -46,7 +33,7 @@ describe('logProcessor', function () {
}
],
}
};
});
const ret = await logProcessor({
data: {
server: {
Expand Down
10 changes: 10 additions & 0 deletions test/lifecycle.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const sails = require('sails');
const faker = require('faker');
const MockDate = require('mockdate');
const sinon = require('sinon')

process.env.IS_TEST = true;
process.env.NODE_ENV = 'test';
Expand All @@ -10,6 +11,15 @@ delete process.env.REDISSTRING;
beforeEach(function() {
MockDate.set('2020-05-01T01:20:05+0000');
});
before(() => {
global.sandbox = sinon.createSandbox()
})
beforeEach(() => {
global.sandbox.restore()
})
beforeEach(() => {
sails.cache = {};
});
// Before running any tests...
before(function (done) {

Expand Down

0 comments on commit b07651e

Please sign in to comment.