[compliments] refactor: optimize loadComplimentFile method and add unit tests (#3969)

## Changes

- Replace `indexOf()` with `startsWith()` for cleaner protocol detection
- Use `URL` API for robust cache-busting parameter handling
- Add HTTP response validation and improved error logging
- Add JSDoc type annotations for better documentation
- Remove unused `urlSuffix` instance variable
- Add unit tests
- Fix `.gitignore` pattern

## Motivation

After merging #3967, I noticed some potential for improving reliability
and user experience related to the method `loadComplimentFile`. With
these changes the method now validates URLs upfront to catch
configuration errors early, checks HTTP status codes to detect server
issues (404/500), and provides specific error messages that help users
troubleshoot problems.

The complexity of the code does not really increase with the changes. On
the contrary, the method should now be more intuitive to understand.

## Testing

Added unit tests for `loadComplimentFile()` to validate the
improvements:
- HTTP error handling
- Cache-busting

Since E2E tests already cover the happy path, these unit tests focus on
error cases and edge cases.

## Additional Fix

While adding the test file, I discovered that the `.gitignore` pattern
`modules` was incorrectly matching `tests/unit/modules/`, preventing
test files from being tracked. Changed to `/modules` to only match the
root directory.
This commit is contained in:
Kristjan ESPERANTO
2025-11-23 17:13:13 +01:00
committed by GitHub
parent 74b682fdf1
commit 7934e7aef8
4 changed files with 180 additions and 16 deletions

View File

@@ -21,7 +21,6 @@ Module.register("compliments", {
random: true,
specialDayUnique: false
},
urlSuffix: "",
compliments_new: null,
refreshMinimumDelay: 15 * 60 * 1000, // 15 minutes
lastIndexUsed: -1,
@@ -205,23 +204,35 @@ Module.register("compliments", {
/**
* Retrieve a file from the local filesystem
* @returns {Promise} Resolved when the file is loaded
* @returns {Promise<string|null>} Resolved with file content or null on error
*/
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);
// 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 (isRemote && this.config.remoteFileRefreshInterval !== 0) {
this.urlSuffix = this.config.remoteFile.includes("?") ? `&dummy=${Date.now()}` : `?dummy=${Date.now()}`;
}
const { remoteFile, remoteFileRefreshInterval } = this.config;
const isRemote = remoteFile.startsWith("http://") || remoteFile.startsWith("https://");
let url = isRemote ? remoteFile : this.file(remoteFile);
try {
const response = await fetch(url + this.urlSuffix);
// Validate URL
const urlObj = new URL(url);
// Add cache-busting parameter to remote URLs to prevent cached responses
if (isRemote && remoteFileRefreshInterval !== 0) {
urlObj.searchParams.set("dummy", Date.now());
}
url = urlObj.toString();
} catch {
Log.warn(`[compliments] Invalid URL: ${url}`);
}
try {
const response = await fetch(url);
if (!response.ok) {
Log.error(`[compliments] HTTP error: ${response.status} ${response.statusText}`);
return null;
}
return await response.text();
} catch (error) {
Log.info(`[compliments] ${this.name} fetch failed error=`, error);
Log.info("[compliments] fetch failed:", error.message);
return null;
}
},