mirror of
https://github.com/MichMich/MagicMirror.git
synced 2026-04-26 15:52:23 +00:00
This migrates the Weather module from client-side fetching to use the server-side centralized HTTPFetcher (introduced in #4016), following the same pattern as the Calendar and Newsfeed modules. ## Motivation This brings consistent error handling and better maintainability and completes the refactoring effort to centralize HTTP error handling across all default modules. Migrating to server-side providers with HTTPFetcher brings: - **Centralized error handling**: Inherits smart retry strategies (401/403, 429, 5xx backoff) and timeout handling (30s) - **Consistency**: Same architecture as Calendar and Newsfeed modules - **Security**: Possibility to hide API keys/secrets from client-side - **Performance**: Reduced API calls in multi-client setups - one server fetch instead of one per client - **Enabling possible future features**: e.g. server-side caching, rate limit monitoring, and data sharing with third-party modules ## Changes - All 10 weather providers now use HTTPFetcher for server-side fetching - Consistent error handling like Calendar and Newsfeed modules ## Breaking Changes None. Existing configurations continue to work. ## Testing To ensure proper functionality, I obtained API keys and credentials for all providers that require them. I configured all 10 providers in a carousel setup and tested each one individually. Screenshots for each provider are attached below demonstrating their working state. I even requested developer access from the Tempest/WeatherFlow team to properly test this provider. **Comprehensive test coverage**: A major advantage of the server-side architecture is the ability to thoroughly test providers with unit tests using real API response snapshots. Don't be alarmed by the many lines added in this PR - they are primarily test files and real-data mocks that ensure provider reliability. ## Review Notes I know this is an enormous change - I've been working on this for quite some time. Unfortunately, breaking it into smaller incremental PRs wasn't feasible due to the interdependencies between providers and the shared architecture. Given the scope, it's nearly impossible to manually review every change. To ensure quality, I've used both CodeRabbit and GitHub Copilot to review the code multiple times in my fork, and both provided extensive and valuable feedback. Most importantly, my test setup with all 10 providers working successfully is very encouraging. ## Related Part of the HTTPFetcher migration #4016. ## Screenshots <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-06-54" src="https://github.com/user-attachments/assets/2139f4d2-2a9b-4e49-8d0a-e4436983ed6e" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-02" src="https://github.com/user-attachments/assets/880f7ce2-4e44-42d5-bfe4-5ce475cca7c2" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-07" src="https://github.com/user-attachments/assets/abd89933-fe03-40ab-8a7c-41ae1ff99255" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-12" src="https://github.com/user-attachments/assets/22225852-f0a9-4d33-87ab-0733ba30fad3" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-17" src="https://github.com/user-attachments/assets/7a7192a5-f237-4060-85d7-6f50b9bef5af" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-22" src="https://github.com/user-attachments/assets/df84d9f1-e531-4995-8da8-d6f2601b6a08" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-27" src="https://github.com/user-attachments/assets/4cf391ac-db43-4b52-95f4-f5eadc5ea34d" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-32" src="https://github.com/user-attachments/assets/8dd8e688-d47f-4815-87f6-7f2630f15d58" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-37" src="https://github.com/user-attachments/assets/ee84a8bc-6b35-405a-b311-88658d9268dd" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-42" src="https://github.com/user-attachments/assets/f941f341-453f-4d4d-a8d9-6b9158eb2681" /> Provider "Weather API" added later: <img width="1910" height="1080" alt="Ekrankopio de 2026-02-15 19-39-06" src="https://github.com/user-attachments/assets/3f0c8ba3-105c-4f90-8b2e-3a1be543d3d2" />
186 lines
5.9 KiB
JavaScript
186 lines
5.9 KiB
JavaScript
const { cors, getUserAgent, replaceSecretPlaceholder } = require("#server_functions");
|
|
|
|
describe("server_functions tests", () => {
|
|
describe("The replaceSecretPlaceholder method", () => {
|
|
it("Calls string without secret placeholder", () => {
|
|
const teststring = "test string without secret placeholder";
|
|
const result = replaceSecretPlaceholder(teststring);
|
|
expect(result).toBe(teststring);
|
|
});
|
|
|
|
it("Calls string with 2 secret placeholders", () => {
|
|
const teststring = "test string with secret1=**SECRET_ONE** and secret2=**SECRET_TWO**";
|
|
process.env.SECRET_ONE = "secret1";
|
|
process.env.SECRET_TWO = "secret2";
|
|
const resultstring = `test string with secret1=${process.env.SECRET_ONE} and secret2=${process.env.SECRET_TWO}`;
|
|
const result = replaceSecretPlaceholder(teststring);
|
|
expect(result).toBe(resultstring);
|
|
});
|
|
});
|
|
|
|
describe("The cors method", () => {
|
|
let fetchResponse;
|
|
let fetchResponseHeadersGet;
|
|
let fetchResponseArrayBuffer;
|
|
let corsResponse;
|
|
let request;
|
|
let fetchMock;
|
|
|
|
beforeEach(() => {
|
|
fetchResponseHeadersGet = vi.fn(() => {});
|
|
fetchResponseArrayBuffer = vi.fn(() => {});
|
|
fetchResponse = {
|
|
headers: {
|
|
get: fetchResponseHeadersGet
|
|
},
|
|
arrayBuffer: fetchResponseArrayBuffer,
|
|
ok: true
|
|
};
|
|
|
|
fetch = vi.fn();
|
|
fetch.mockImplementation(() => fetchResponse);
|
|
|
|
fetchMock = fetch;
|
|
|
|
corsResponse = {
|
|
set: vi.fn(() => {}),
|
|
send: vi.fn(() => {}),
|
|
status: vi.fn(function (code) {
|
|
this.statusCode = code;
|
|
return this;
|
|
}),
|
|
json: vi.fn(() => {})
|
|
};
|
|
|
|
request = {
|
|
url: "/cors?url=www.test.com"
|
|
};
|
|
});
|
|
|
|
it("Calls correct URL once", async () => {
|
|
const urlToCall = "http://www.test.com/path?param1=value1";
|
|
request.url = `/cors?url=${urlToCall}`;
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchMock.mock.calls).toHaveLength(1);
|
|
expect(fetchMock.mock.calls[0][0]).toBe(urlToCall);
|
|
});
|
|
|
|
it("Forwards Content-Type if json", async () => {
|
|
fetchResponseHeadersGet.mockImplementation(() => "json");
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchResponseHeadersGet.mock.calls).toHaveLength(1);
|
|
expect(fetchResponseHeadersGet.mock.calls[0][0]).toBe("Content-Type");
|
|
|
|
expect(corsResponse.set.mock.calls).toHaveLength(1);
|
|
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
|
|
expect(corsResponse.set.mock.calls[0][1]).toBe("json");
|
|
});
|
|
|
|
it("Forwards Content-Type if xml", async () => {
|
|
fetchResponseHeadersGet.mockImplementation(() => "xml");
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchResponseHeadersGet.mock.calls).toHaveLength(1);
|
|
expect(fetchResponseHeadersGet.mock.calls[0][0]).toBe("Content-Type");
|
|
|
|
expect(corsResponse.set.mock.calls).toHaveLength(1);
|
|
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
|
|
expect(corsResponse.set.mock.calls[0][1]).toBe("xml");
|
|
});
|
|
|
|
it("Sends correct data from response", async () => {
|
|
const responseData = "some data";
|
|
const encoder = new TextEncoder();
|
|
const arrayBuffer = encoder.encode(responseData).buffer;
|
|
fetchResponseArrayBuffer.mockImplementation(() => arrayBuffer);
|
|
|
|
let sentData;
|
|
corsResponse.send = vi.fn((input) => {
|
|
sentData = input;
|
|
});
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchResponseArrayBuffer.mock.calls).toHaveLength(1);
|
|
expect(sentData).toEqual(Buffer.from(arrayBuffer));
|
|
});
|
|
|
|
it("Sends error data from response", async () => {
|
|
const error = new Error("error data");
|
|
fetchResponseArrayBuffer.mockImplementation(() => {
|
|
throw error;
|
|
});
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchResponseArrayBuffer.mock.calls).toHaveLength(1);
|
|
expect(corsResponse.status).toHaveBeenCalledWith(500);
|
|
expect(corsResponse.json).toHaveBeenCalledWith({ error: error.message });
|
|
});
|
|
|
|
it("Fetches with user agent by default", async () => {
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchMock.mock.calls).toHaveLength(1);
|
|
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
|
|
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("User-Agent");
|
|
});
|
|
|
|
it("Fetches with specified headers", async () => {
|
|
const headersParam = "sendheaders=header1:value1,header2:value2";
|
|
const urlParam = "http://www.test.com/path?param1=value1";
|
|
request.url = `/cors?${headersParam}&url=${urlParam}`;
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchMock.mock.calls).toHaveLength(1);
|
|
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
|
|
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("header1", "value1");
|
|
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("header2", "value2");
|
|
});
|
|
|
|
it("Sends specified headers", async () => {
|
|
fetchResponseHeadersGet.mockImplementation((input) => input.replace("header", "value"));
|
|
|
|
const expectedheaders = "expectedheaders=header1,header2";
|
|
const urlParam = "http://www.test.com/path?param1=value1";
|
|
request.url = `/cors?${expectedheaders}&url=${urlParam}`;
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchMock.mock.calls).toHaveLength(1);
|
|
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
|
|
expect(corsResponse.set.mock.calls).toHaveLength(3);
|
|
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
|
|
expect(corsResponse.set.mock.calls[1][0]).toBe("header1");
|
|
expect(corsResponse.set.mock.calls[1][1]).toBe("value1");
|
|
expect(corsResponse.set.mock.calls[2][0]).toBe("header2");
|
|
expect(corsResponse.set.mock.calls[2][1]).toBe("value2");
|
|
});
|
|
|
|
it("Gets User-Agent from configuration", async () => {
|
|
const previousConfig = global.config;
|
|
global.config = {};
|
|
let userAgent;
|
|
|
|
userAgent = getUserAgent();
|
|
expect(userAgent).toContain("Mozilla/5.0 (Node.js ");
|
|
|
|
global.config.userAgent = "Mozilla/5.0 (Foo)";
|
|
userAgent = getUserAgent();
|
|
expect(userAgent).toBe("Mozilla/5.0 (Foo)");
|
|
|
|
global.config.userAgent = () => "Mozilla/5.0 (Bar)";
|
|
userAgent = getUserAgent();
|
|
expect(userAgent).toBe("Mozilla/5.0 (Bar)");
|
|
|
|
global.config = previousConfig;
|
|
});
|
|
});
|
|
});
|