fix(weather): respect NO_EXTERNAL_CONNECTION in location search and surface weather data states
This commit is contained in:
1 parent
aca3b5d861
commit
b2fcc94724
7 files changed
+205
-19
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { env } from "@homarr/common/env";
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
||||
import { withTimeoutAsync } from "@homarr/core/infrastructure/http/timeout";
|
||||
|
||||
@@ -37,6 +38,8 @@ export const locationRouter = createTRPCRouter({
|
||||
.input(locationSearchCityInput)
|
||||
.output(locationSearchCityOutput)
|
||||
.query(async ({ input }) => {
|
||||
if (env.NO_EXTERNAL_CONNECTION) return { results: [] };
|
||||
|
||||
const res = await withTimeoutAsync(async (signal) => {
|
||||
return await fetchWithTrustedCertificatesAsync(
|
||||
`https://geocoding-api.open-meteo.com/v1/search?name=${input.query}`,
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
fetchWithTrustedCertificatesAsync: vi.fn(),
|
||||
env: { NO_EXTERNAL_CONNECTION: false },
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/common/env", () => ({
|
||||
env: mocks.env,
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/core/infrastructure/http", () => ({
|
||||
fetchWithTrustedCertificatesAsync: mocks.fetchWithTrustedCertificatesAsync,
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/core/infrastructure/http/timeout", () => ({
|
||||
withTimeoutAsync: async (callback: (signal: AbortSignal) => Promise<Response>) => {
|
||||
return await callback(new AbortController().signal);
|
||||
},
|
||||
}));
|
||||
|
||||
import { locationRouter } from "../location";
|
||||
|
||||
const createJsonResponse = (body: unknown) =>
|
||||
new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } });
|
||||
|
||||
const createCaller = () => locationRouter.createCaller({ db: createDb(), deviceType: undefined, session: null });
|
||||
|
||||
afterEach(() => {
|
||||
mocks.env.NO_EXTERNAL_CONNECTION = false;
|
||||
mocks.fetchWithTrustedCertificatesAsync.mockReset();
|
||||
});
|
||||
|
||||
describe("locationRouter.searchCity", () => {
|
||||
test("should return empty results when external connections are disabled", async () => {
|
||||
// Arrange
|
||||
mocks.env.NO_EXTERNAL_CONNECTION = true;
|
||||
const caller = createCaller();
|
||||
|
||||
// Act
|
||||
const result = await caller.searchCity({ query: "Paris" });
|
||||
|
||||
// Assert
|
||||
expect(result.results).toEqual([]);
|
||||
expect(mocks.fetchWithTrustedCertificatesAsync).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should fetch the geocoding API when external connections are enabled", async () => {
|
||||
// Arrange
|
||||
mocks.env.NO_EXTERNAL_CONNECTION = false;
|
||||
mocks.fetchWithTrustedCertificatesAsync.mockResolvedValue(
|
||||
createJsonResponse({
|
||||
results: [
|
||||
{
|
||||
id: 1,
|
||||
name: "Paris",
|
||||
country: "France",
|
||||
country_code: "FR",
|
||||
latitude: 48.85341,
|
||||
longitude: 2.3488,
|
||||
population: 2138551,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
const caller = createCaller();
|
||||
|
||||
// Act
|
||||
const result = await caller.searchCity({ query: "Paris" });
|
||||
|
||||
// Assert
|
||||
expect(result.results[0]?.name).toBe("Paris");
|
||||
expect(mocks.fetchWithTrustedCertificatesAsync).toHaveBeenCalledWith(
|
||||
expect.stringContaining("geocoding-api.open-meteo.com"),
|
||||
expect.anything(),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getDataAsync: vi.fn(),
|
||||
handler: vi.fn(),
|
||||
env: { NO_EXTERNAL_CONNECTION: false },
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/common/env", () => ({
|
||||
env: mocks.env,
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/request-handler/weather", () => ({
|
||||
weatherRequestHandler: {
|
||||
handler: mocks.handler,
|
||||
},
|
||||
}));
|
||||
|
||||
import { weatherRouter } from "../../widgets/weather";
|
||||
|
||||
const weatherData = {
|
||||
current: { weathercode: 0, temperature: 20, windspeed: 10 },
|
||||
daily: [],
|
||||
};
|
||||
|
||||
const createCaller = () => weatherRouter.createCaller({ db: createDb(), deviceType: undefined, session: null });
|
||||
|
||||
afterEach(() => {
|
||||
mocks.env.NO_EXTERNAL_CONNECTION = false;
|
||||
mocks.handler.mockReset();
|
||||
mocks.getDataAsync.mockReset();
|
||||
});
|
||||
|
||||
describe("weatherRouter.atLocation", () => {
|
||||
test("should return null when external connections are disabled", async () => {
|
||||
// Arrange
|
||||
mocks.env.NO_EXTERNAL_CONNECTION = true;
|
||||
const caller = createCaller();
|
||||
|
||||
// Act
|
||||
const result = await caller.atLocation({ latitude: 48.85341, longitude: 2.3488 });
|
||||
|
||||
// Assert
|
||||
expect(result).toBeNull();
|
||||
expect(mocks.handler).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should return the weather data when external connections are enabled", async () => {
|
||||
// Arrange
|
||||
mocks.env.NO_EXTERNAL_CONNECTION = false;
|
||||
mocks.handler.mockReturnValue({ getDataAsync: mocks.getDataAsync });
|
||||
mocks.getDataAsync.mockResolvedValue({ data: weatherData, timestamp: new Date() });
|
||||
const caller = createCaller();
|
||||
|
||||
// Act
|
||||
const result = await caller.atLocation({ latitude: 48.85341, longitude: 2.3488 });
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual(weatherData);
|
||||
expect(mocks.handler).toHaveBeenCalledWith({ latitude: 48.85341, longitude: 2.3488 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user