From 79c7906fbaf8dc729b9b5ed7ebbc7a97d2fd45e6 Mon Sep 17 00:00:00 2001 From: sam detweiler Date: Sat, 9 Nov 2024 16:03:26 -0600 Subject: [PATCH] add support for compliments file refresh and testcases --- CHANGELOG.md | 1 + modules/default/compliments/compliments.js | 45 ++++++++++++++++++- .../modules/compliments/compliments_file.js | 17 +++++++ .../compliments/compliments_file_change.js | 19 ++++++++ tests/electron/modules/compliments_spec.js | 16 +++++++ tests/mocks/compliments_file.json | 5 +++ 6 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/configs/modules/compliments/compliments_file.js create mode 100644 tests/configs/modules/compliments/compliments_file_change.js create mode 100644 tests/mocks/compliments_file.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 579eff945d..7ae9045545 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/modules/default/compliments/compliments.js b/modules/default/compliments/compliments.js index 4c4bef471f..ea99eaca43 100644 --- a/modules/default/compliments/compliments.js +++ b/modules/default/compliments/compliments.js @@ -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. @@ -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: "", @@ -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 @@ -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(); }, @@ -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; }, diff --git a/tests/configs/modules/compliments/compliments_file.js b/tests/configs/modules/compliments/compliments_file.js new file mode 100644 index 0000000000..d73e1b5c10 --- /dev/null +++ b/tests/configs/modules/compliments/compliments_file.js @@ -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; } diff --git a/tests/configs/modules/compliments/compliments_file_change.js b/tests/configs/modules/compliments/compliments_file_change.js new file mode 100644 index 0000000000..51fd4f6408 --- /dev/null +++ b/tests/configs/modules/compliments/compliments_file_change.js @@ -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; } diff --git a/tests/electron/modules/compliments_spec.js b/tests/electron/modules/compliments_spec.js index 15c3c37c08..8626b503c8 100644 --- a/tests/electron/modules/compliments_spec.js +++ b/tests/electron/modules/compliments_spec.js @@ -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); + }); + }); }); }); diff --git a/tests/mocks/compliments_file.json b/tests/mocks/compliments_file.json new file mode 100644 index 0000000000..89171b16ed --- /dev/null +++ b/tests/mocks/compliments_file.json @@ -0,0 +1,5 @@ +{ + "morning": ["test in morning"], + "afternoon": ["test in afternoon"], + "evening": ["test in evening"] +}