Skip to content

Commit

Permalink
add support for compliments file refresh and testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
sdetweil committed Nov 9, 2024
1 parent b250cfa commit 79c7906
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _This release is scheduled to be released on 2025-01-01._
- [linter] re-added `eslint-plugin-import`now that it supports ESLint v9 (#3586)
- [docs] Added step for npm publishing in release process (#3595)
- [core] Add GitHub workflow to run spellcheck a few days before each release.
- [compliments] add support for refreshing remote compliments file, and testcases

### Removed

Expand Down
45 changes: 43 additions & 2 deletions modules/default/compliments/compliments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* global Cron */
/* global global */

console.log("window name='"+window.name+"'")
const compliments_test_mode=(window.name=='jsdom')?true:false

Module.register("compliments", {
// Module config defaults.
Expand All @@ -12,14 +16,17 @@ Module.register("compliments", {
},
updateInterval: 30000,
remoteFile: null,
remoteFileRefreshInterval: null,
fadeSpeed: 4000,
morningStartTime: 3,
morningEndTime: 12,
afternoonStartTime: 12,
afternoonEndTime: 17,
random: true,
specialDayUnique: false
specialDayUnique: false,
},
urlSuffix:"",
compliments_new:null,
lastIndexUsed: -1,
// Set currentweather from module
currentWeatherType: "",
Expand All @@ -41,6 +48,14 @@ Module.register("compliments", {
const response = await this.loadComplimentFile();
this.config.compliments = JSON.parse(response);
this.updateDom();
if (this.config.remoteFileRefreshInterval !== null) {
this.remoteFileRefreshFunc = setInterval(async () => {
const response = await this.loadComplimentFile();
this.compliments_new = JSON.parse(response);
},
this.config.remoteFileRefreshInterval
)
}
}
let minute_sync_delay = 1;
// loop thru all the configured when events
Expand Down Expand Up @@ -185,7 +200,14 @@ Module.register("compliments", {
async loadComplimentFile () {
const isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0,
url = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile);
const response = await fetch(url);
// because we may be fetching the same url,
// we need to force the server to not give us the cached result
// create an extra property (ignored by the server handler) just so the url string is different
// that will never be the same, using the ms value of date
if(this.config.remoteFileRefreshInterval!=null)
this.urlSuffix= "?dummy="+Date.now()
//
const response = await fetch(url+this.urlSuffix);
return await response.text();
},

Expand Down Expand Up @@ -236,6 +258,25 @@ Module.register("compliments", {
compliment.lastElementChild.remove();
wrapper.appendChild(compliment);
}
// if a new set of compliments was loaded from the refresh task
// we do this here to make sure no other function is using the compliments list
if(this.compliments_new){
// use them
if(JSON.stringify(this.config.compliments)!== JSON.stringify(this.compliments_new)){
// only reset if the contents changes
this.config.compliments = this.compliments_new
// reset the index
this.lastIndexUsed = -1;
}
}
// in test mode only
//if (compliments_test_mode) {
// check for (undocumented) remote file2 to change test new load
if(this.config.remoteFile2!== null && this.config.remoteFileRefreshInterval!==null){
console.log("running in test");
this.config.remoteFile=this.config.remoteFile2
}
//}
return wrapper;
},

Expand Down
17 changes: 17 additions & 0 deletions tests/configs/modules/compliments/compliments_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let config = {
address: "0.0.0.0",
ipWhitelist: [],
modules: [
{
module: "compliments",
position: "bottom_bar",
config: {
updateInterval: 3000,
remoteFile: "http://localhost:8080/tests/mocks/compliments_test.json"
}
}
]
};

/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") { module.exports = config; }
19 changes: 19 additions & 0 deletions tests/configs/modules/compliments/compliments_file_change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let config = {
address: "0.0.0.0",
ipWhitelist: [],
modules: [
{
module: "compliments",
position: "bottom_bar",
config: {
updateInterval: 3000,
remoteFileRefreshInterval: 1500,
remoteFile: "http://localhost:8080/tests/mocks/compliments_test.json",
remoteFile2: "http://localhost:8080/tests/mocks/compliments_file.json"
}
}
]
};

/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") { module.exports = config; }
16 changes: 16 additions & 0 deletions tests/electron/modules/compliments_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ describe("Compliments module", () => {
await expect(doTest(["just a test"])).resolves.toBe(true);
});
});
});

describe("Feature remote compliments file", () => {
describe("get list from remote file", () => {
it("shows 'Remote compliment file works!' as only anytime list set", async () => {
await helpers.startApplication("tests/configs/modules/compliments/compliments_file.js", "01 Jan 2022 10:00:00 GMT");
await expect(doTest(["Remote compliment file works!"])).resolves.toBe(true);
});
});
describe("get updated list from remote file", () => {
it("shows 'test in morning' as test time set to 10am", async () => {
await helpers.startApplication("tests/configs/modules/compliments/compliments_file_change.js", "01 Jan 2022 10:00:00 GMT");
await expect(doTest(["Remote compliment file works!"])).resolves.toBe(true);
await new Promise((r) => setTimeout(r, 10000));
await expect(doTest(["test in morning"])).resolves.toBe(true);
});
});
});
});
5 changes: 5 additions & 0 deletions tests/mocks/compliments_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"morning": ["test in morning"],
"afternoon": ["test in afternoon"],
"evening": ["test in evening"]
}

0 comments on commit 79c7906

Please sign in to comment.