mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 16:23:12 +09:00
moved isFactorioCommand, more tests, researchSync stability
This commit is contained in:
1 parent
181cb1179c
commit
f131a83103
7 files changed
+76
-12
No files matched your search
+5
-5
@@ -41,7 +41,7 @@ describe('Master server endpoint testing', function() {
|
|||||||
request(app).get("/").end(function(err,res){
|
request(app).get("/").end(function(err,res){
|
||||||
assert.equal(res.statusCode, 200);
|
assert.equal(res.statusCode, 200);
|
||||||
validateHTML(res.text).then(result => {
|
validateHTML(res.text).then(result => {
|
||||||
assert(result.messages.length === 0, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -51,7 +51,7 @@ describe('Master server endpoint testing', function() {
|
|||||||
request(app).get("/nodes").end(function(err,res){
|
request(app).get("/nodes").end(function(err,res){
|
||||||
assert.equal(res.statusCode, 200);
|
assert.equal(res.statusCode, 200);
|
||||||
validateHTML(res.text).then(result => {
|
validateHTML(res.text).then(result => {
|
||||||
assert(result.messages.length === 0, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -61,7 +61,7 @@ describe('Master server endpoint testing', function() {
|
|||||||
request(app).get("/settings").end(function(err,res){
|
request(app).get("/settings").end(function(err,res){
|
||||||
assert.equal(res.statusCode, 200);
|
assert.equal(res.statusCode, 200);
|
||||||
validateHTML(res.text).then(result => {
|
validateHTML(res.text).then(result => {
|
||||||
assert(result.messages.length === 0, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -71,7 +71,7 @@ describe('Master server endpoint testing', function() {
|
|||||||
request(app).get("/nodeDetails").end(function(err,res){
|
request(app).get("/nodeDetails").end(function(err,res){
|
||||||
assert.equal(res.statusCode, 200);
|
assert.equal(res.statusCode, 200);
|
||||||
validateHTML(res.text).then(result => {
|
validateHTML(res.text).then(result => {
|
||||||
assert(result.messages.length === 0, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -82,7 +82,7 @@ describe('Master server endpoint testing', function() {
|
|||||||
assert.equal(res.statusCode, 200);
|
assert.equal(res.statusCode, 200);
|
||||||
validateHTML(res.text).then(result => {
|
validateHTML(res.text).then(result => {
|
||||||
// there should be 1 error regarding complaining about me using ES6 modules before they are fully supported
|
// there should be 1 error regarding complaining about me using ES6 modules before they are fully supported
|
||||||
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
assert(result.messages.length === 2, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
module.exports = require("./isFactorioCommand.js");
|
||||||
Generated
Vendored
File renamed without changes.
Generated
Vendored
File renamed without changes.
@@ -2,7 +2,7 @@ const assert = require("assert");
|
|||||||
const child_process = require("child_process");
|
const child_process = require("child_process");
|
||||||
const functions = require("./interfacingFunctions");
|
const functions = require("./interfacingFunctions");
|
||||||
const luamin = require("luamin");
|
const luamin = require("luamin");
|
||||||
const isFactorioCommand = require("./isFactorioCommand");
|
const isFactorioCommand = require("_app/isFactorioCommand");
|
||||||
const nock = require("nock");
|
const nock = require("nock");
|
||||||
|
|
||||||
// sinon does weird prototype manipulation and so doen't need to be saved to anything.
|
// sinon does weird prototype manipulation and so doen't need to be saved to anything.
|
||||||
|
|||||||
@@ -3,18 +3,17 @@ const needle = require("needle");
|
|||||||
|
|
||||||
|
|
||||||
class ResearchSync {
|
class ResearchSync {
|
||||||
constructor(slaveConfig, messageInterface){
|
constructor(slaveConfig, messageInterface, extras = {}){
|
||||||
this.functions = this.loadFunctions();
|
this.functions = this.loadFunctions();
|
||||||
this.config = slaveConfig;
|
this.config = slaveConfig;
|
||||||
this.messageInterface = messageInterface;
|
this.messageInterface = messageInterface;
|
||||||
|
|
||||||
this.research = {};
|
this.research = {};
|
||||||
|
|
||||||
messageInterface("ResearchSync enabled");
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
this.doSync();
|
this.doSync();
|
||||||
this.pollResearch();
|
this.pollResearch();
|
||||||
}, 30000);
|
}, extras.researchSyncPollInterval || 30000);
|
||||||
}
|
}
|
||||||
|
|
||||||
pollResearch() {
|
pollResearch() {
|
||||||
@@ -29,7 +28,7 @@ class ResearchSync {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (resp.statusCode != 200){
|
if (resp.statusCode != 200){
|
||||||
console.log("got error when calling getSlaveMeta", resp.statusCode, resp.body);
|
this.messageInterface("got error when calling getSlaveMeta", resp.statusCode, resp.body);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var allmeta = JSON.parse(resp.body);
|
var allmeta = JSON.parse(resp.body);
|
||||||
@@ -92,7 +91,15 @@ class ResearchSync {
|
|||||||
}
|
}
|
||||||
scriptOutput(data){
|
scriptOutput(data){
|
||||||
try {
|
try {
|
||||||
let research = JSON.parse(data.replace("=",":").replace("[","").replace("]",""));
|
// remove Lua table junk
|
||||||
|
let currentResearch = data.replace("=",":").replace("[","").replace("]","").replace('"', "").replace('"', "");
|
||||||
|
// add first " before key
|
||||||
|
currentResearch = '{"' + currentResearch.slice(1);
|
||||||
|
// add second " after the key to make it a string
|
||||||
|
currentResearch = currentResearch.split(" ")[0] + '"' + currentResearch.slice(currentResearch.split(" ")[0].length);
|
||||||
|
// turn our constructed JSON string into an object
|
||||||
|
currentResearch = JSON.parse(currentResearch);
|
||||||
|
// console.log(currentResearch)
|
||||||
this.research = Object.assign(this.research, currentResearch);
|
this.research = Object.assign(this.research, currentResearch);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
|
|
||||||
|
const isFactorioCommand = require("_app/isFactorioCommand");
|
||||||
|
|
||||||
const researchSync = require("./index.js");
|
const researchSync = require("./index.js");
|
||||||
|
|
||||||
describe("researchSync/index.js", ()=>{
|
describe("researchSync/index.js", ()=>{
|
||||||
@@ -8,7 +10,7 @@ describe("researchSync/index.js", ()=>{
|
|||||||
});
|
});
|
||||||
describe("class researchSync()", ()=>{
|
describe("class researchSync()", ()=>{
|
||||||
it(".diff(object1, object2) returns key:value pairs that exist in object2 but not object1", ()=>{
|
it(".diff(object1, object2) returns key:value pairs that exist in object2 but not object1", ()=>{
|
||||||
let reSync = new researchSync();
|
let reSync = new researchSync({}, function(){});
|
||||||
|
|
||||||
let obj1 = {
|
let obj1 = {
|
||||||
a:"str",
|
a:"str",
|
||||||
@@ -30,5 +32,59 @@ describe("researchSync/index.js", ()=>{
|
|||||||
assert(diffResult.d === undefined);
|
assert(diffResult.d === undefined);
|
||||||
assert(diffResult.e === "truthy");
|
assert(diffResult.e === "truthy");
|
||||||
});
|
});
|
||||||
|
it(".pollResearch() dumps a long Lua command", ()=>{
|
||||||
|
let reSync = new researchSync({}, io);
|
||||||
|
let ioRecieved;
|
||||||
|
function io(str){
|
||||||
|
ioRecieved = true;
|
||||||
|
assert(isFactorioCommand(str), "pollResearch should run a command");
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(!ioRecieved);
|
||||||
|
|
||||||
|
reSync.pollResearch();
|
||||||
|
|
||||||
|
assert(ioRecieved);
|
||||||
|
});
|
||||||
|
describe(".scriptOutput(data) handles file writes from factorio", ()=>{
|
||||||
|
it("wants a Lua table of a single research, parses and saves it", ()=>{
|
||||||
|
let reSync = new researchSync({hi:"hello"}, console.log);
|
||||||
|
let researches = [
|
||||||
|
'{automation = false}',
|
||||||
|
'{["automation-2"] = false}',
|
||||||
|
'{["automation-3"] = false}',
|
||||||
|
'{electronics = false}',
|
||||||
|
'{["advanced-electronics"] = false}',
|
||||||
|
'{["advanced-electronics-2"] = false}',
|
||||||
|
'{["circuit-network"] = false}',
|
||||||
|
'{explosives = false}',
|
||||||
|
'{logistics = false}',
|
||||||
|
'{["logistics-2"] = false}',
|
||||||
|
]
|
||||||
|
researches.forEach(research => {
|
||||||
|
reSync.scriptOutput(research);
|
||||||
|
assert(isFactorioCommand("/c x = "+research));
|
||||||
|
});
|
||||||
|
assert(Object.keys(reSync.research).length == researches.length);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("regularily polls and syncs research (configurable delay)", (done)=>{
|
||||||
|
let reSync = new researchSync({}, io, {
|
||||||
|
researchSyncPollInterval: 100,
|
||||||
|
});
|
||||||
|
let ioRecieved;
|
||||||
|
function io(str){
|
||||||
|
ioRecieved = true;
|
||||||
|
assert(isFactorioCommand(str), "pollResearch should run a command");
|
||||||
|
}
|
||||||
|
function waitForIo(){
|
||||||
|
if(!ioRecieved){
|
||||||
|
setTimeout(waitForIo,50);
|
||||||
|
} else {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
waitForIo();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user