Skip to content

Commit

Permalink
Enhance compliments remote file with refresh support (MagicMirrorOrg#…
Browse files Browse the repository at this point in the history
…3630)

add support to refresh the compliments remotefile
add testcases for both without refresh (testcase missing) and with
refresh

doc to be updated
  • Loading branch information
sdetweil authored Nov 13, 2024
1 parent f1522da commit bd620e0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _This release is scheduled to be released on 2025-01-01._
- [docs] Added step for npm publishing in release process (#3595)
- [core] Add GitHub workflow to run spellcheck a few days before each release.
- [core] Add intest flag to index.html to pass to module js for test mode detection (needed by #3630)
- [compliments] add support for refreshing remote compliments file, and testcases

### Removed

Expand Down
44 changes: 43 additions & 1 deletion modules/default/compliments/compliments.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Module.register("compliments", {
},
updateInterval: 30000,
remoteFile: null,
remoteFileRefreshInterval: 0,
fadeSpeed: 4000,
morningStartTime: 3,
morningEndTime: 12,
Expand All @@ -20,6 +21,9 @@ Module.register("compliments", {
random: true,
specialDayUnique: false
},
urlSuffix: "",
compliments_new: null,
refreshMinimumDelay: 15 * 60 * 60 * 1000, // 15 minutes
lastIndexUsed: -1,
// Set currentweather from module
currentWeatherType: "",
Expand All @@ -41,6 +45,17 @@ Module.register("compliments", {
const response = await this.loadComplimentFile();
this.config.compliments = JSON.parse(response);
this.updateDom();
if (this.config.remoteFileRefreshInterval !== 0) {
if ((this.config.remoteFileRefreshInterval >= this.refreshMinimumDelay) || window.mmTestMode === "true") {
setInterval(async () => {
const response = await this.loadComplimentFile();
this.compliments_new = JSON.parse(response);
},
this.config.remoteFileRefreshInterval);
} else {
Log.error(`${this.name} remoteFileRefreshInterval less than minimum`);
}
}
}
let minute_sync_delay = 1;
// loop thru all the configured when events
Expand Down Expand Up @@ -185,7 +200,13 @@ 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 !== 0) this.urlSuffix = `?dummy=${Date.now()}`;
//
const response = await fetch(url + this.urlSuffix);
return await response.text();
},

Expand Down Expand Up @@ -236,6 +257,27 @@ 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;
}
// clear new file list so we don't waste cycles comparing between refreshes
this.compliments_new = null;
}
// only in test mode
if (window.mmTestMode === "true") {
// check for (undocumented) remoteFile2 to test new file load
if (this.config.remoteFile2 !== null && this.config.remoteFileRefreshInterval !== 0) {
// switch the file so that next time it will be loaded from a changed file
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 bd620e0

Please sign in to comment.