Skip to content

Commit 34bb3e4

Browse files
committed
fix(transit): harden local MOTIS endpoint resolution
1 parent e65a232 commit 34bb3e4

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, it } from "vitest";
2+
import { resolveLocalMotisUrl } from "../local.js";
3+
4+
describe("resolveLocalMotisUrl", () => {
5+
it("prefers the service registry and trims its URL", () => {
6+
expect(
7+
resolveLocalMotisUrl(
8+
" http://motis:8080 ",
9+
"https://configured.example",
10+
"https://environment.example",
11+
),
12+
).toBe("http://motis:8080");
13+
});
14+
15+
it("skips blank candidates instead of configuring an empty base URL", () => {
16+
expect(resolveLocalMotisUrl(undefined, " ", " https://environment.example ")).toBe(
17+
"https://environment.example",
18+
);
19+
});
20+
21+
it("uses the localhost fallback when no non-empty candidate exists", () => {
22+
expect(resolveLocalMotisUrl(null, undefined, "")).toBe("http://localhost:8081");
23+
});
24+
});

integrations/transit-motis/local.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,21 @@ function resolveDateTime(departureTime?: string): { date: string; time: string }
250250
};
251251
}
252252

253+
export function resolveLocalMotisUrl(
254+
serviceUrl: unknown,
255+
configuredEndpoint: unknown,
256+
environmentUrl: unknown,
257+
): string {
258+
return (
259+
[serviceUrl, configuredEndpoint, environmentUrl]
260+
.find(
261+
(candidate): candidate is string =>
262+
typeof candidate === "string" && candidate.trim().length > 0,
263+
)
264+
?.trim() ?? "http://localhost:8081"
265+
);
266+
}
267+
253268
async function planWithInstance(
254269
instance: MotisInstance,
255270
params: TripPlanRequest,
@@ -311,12 +326,9 @@ export function setupLocal(ctx: IntegrationContext): void {
311326
// and live-transit-motis honour, so a single deployment-wide
312327
// `MOTIS_URL=…` reaches every consumer of the local instance.
313328
const resolved = ctx.getRequiredService("motis");
314-
const motisUrl =
315-
resolved?.url ??
316-
(ctx.config.endpoint as string | undefined) ??
317-
process.env.MOTIS_URL ??
318-
"http://localhost:8081";
329+
const motisUrl = resolveLocalMotisUrl(resolved?.url, ctx.config.endpoint, process.env.MOTIS_URL);
319330
setMotisLocalUrl(motisUrl);
331+
ctx.log.info(`[transit-motis] configured local MOTIS endpoint: ${motisUrl}`);
320332
cachedLocalReachable = false;
321333
cachedLocalReachableAt = 0;
322334
// Warm the live rental-capability probe so the access options reflect reality

0 commit comments

Comments
 (0)