diff --git a/integrations/transit-tfl/provider.ts b/integrations/transit-tfl/provider.ts index e06437186..ef87425fc 100644 --- a/integrations/transit-tfl/provider.ts +++ b/integrations/transit-tfl/provider.ts @@ -241,28 +241,78 @@ export async function searchByName( .map(normalizeStop); } -function mapTflSeverity(severityNumber: number): AlertSeverity { - if (severityNumber >= 9) return "info"; - if (severityNumber >= 5) return "warning"; - if (severityNumber >= 1) return "severe"; +/** + * TfL line-status codes (GET /Line/Meta/Severity). Above 10 the scale is not + * ordered by badness: 11–17 and 19 are disruptions or notices, 18 means no + * issues and 20 is the routine end of service. Null means no alert. + */ +function mapTflSeverity(code: number): AlertSeverity | null { + switch (code) { + case 10: // Good Service + case 18: // No Issues + case 20: // Service Closed (overnight); "No Service" for national-rail + return null; + case 16: // Not Running + return "severe"; + case 11: // Part Closed + case 12: // Exit Only + case 13: // No Step Free Access + case 15: // Diverted + case 17: // Issues Reported + return "warning"; + case 14: // Change of frequency + case 19: // Information + return "info"; + } + if (code > 10) return "info"; // a code TfL may add later: show, do not hide + if (code >= 9) return "info"; + if (code >= 5) return "warning"; + if (code >= 1) return "severe"; return "critical"; } -function lineToAlert(line: TflLineStatus): ServiceAlert | null { - const status = line.lineStatuses?.[0]; - if (!status) return null; - if ((status.statusSeverity ?? 10) >= 10) return null; // Good Service - return { - id: `tfl:status:${line.id}`, - providers: ["tfl"], - severity: mapTflSeverity(status.statusSeverity ?? 0), - effect: status.statusSeverityDescription ?? undefined, - title: `${line.name}: ${status.statusSeverityDescription ?? ""}`, - description: status.reason ? status.reason.replace(/<[^>]+>/g, "") : undefined, - affectedRouteIds: [`tfl:${line.id}`], - affectedStopIds: [], - activePeriods: [], - }; +function stripTags(text: string): string { + // Repeat until stable, so that removing one tag cannot leave another behind. + let current = text; + let previous: string; + do { + previous = current; + current = current.replace(/<[^>]+>/g, ""); + } while (current !== previous); + return current; +} + +/** One alert per distinct alerting status code on the line. */ +function lineToAlerts(line: TflLineStatus): ServiceAlert[] { + const byCode = new Map(); + for (const status of line.lineStatuses ?? []) { + const code = status.statusSeverity; + if (code === null || code === undefined) continue; + const severity = mapTflSeverity(code); + if (!severity) continue; + const reason = status.reason ? stripTags(status.reason) : undefined; + const existing = byCode.get(code); + if (existing) { + // Same status on another section: keep one card, list each reason once. + if (reason && !existing.description?.split("\n\n").includes(reason)) + existing.description = existing.description + ? `${existing.description}\n\n${reason}` + : reason; + continue; + } + byCode.set(code, { + id: `tfl:status:${line.id}:${code}`, + providers: ["tfl"], + severity, + effect: status.statusSeverityDescription ?? undefined, + title: `${line.name}: ${status.statusSeverityDescription ?? ""}`, + description: reason, + affectedRouteIds: [`tfl:${line.id}`], + affectedStopIds: [], + activePeriods: [], + }); + } + return [...byCode.values()]; } async function lineAlerts( @@ -274,8 +324,7 @@ async function lineAlerts( const lines = parseProviderPayload(z.array(z.unknown()), data, `TfL ${what}`); const alerts: ServiceAlert[] = []; for (const line of parseProviderRecords(lineStatusSchema, lines, `TfL ${what}`)) { - const alert = lineToAlert(line); - if (alert) alerts.push(alert); + alerts.push(...lineToAlerts(line)); } return alerts; } diff --git a/integrations/transit-tfl/tfl.test.ts b/integrations/transit-tfl/tfl.test.ts index fee24598a..60f2fc52b 100644 --- a/integrations/transit-tfl/tfl.test.ts +++ b/integrations/transit-tfl/tfl.test.ts @@ -248,7 +248,7 @@ describe("TfL transit provider", () => { expect(alerts).toEqual([ { - id: "tfl:status:central", + id: "tfl:status:central:5", providers: ["tfl"], severity: "warning", effect: "Minor Delays", @@ -261,6 +261,164 @@ describe("TfL transit provider", () => { ]); }); + it("alerts for disruption codes above Good Service", async () => { + mockFetch.mockResolvedValueOnce( + mockOk([ + { + id: "northern", + name: "Northern", + lineStatuses: [ + { + statusSeverity: 16, + statusSeverityDescription: "Not Running", + reason: "No service between A and B", + }, + ], + }, + { + id: "district", + name: "District", + lineStatuses: [{ statusSeverity: 11, statusSeverityDescription: "Part Closed" }], + }, + ]), + ); + + const alerts = await getAlerts(); + + expect(alerts).toHaveLength(2); + expect(alerts[0]).toMatchObject({ + id: "tfl:status:northern:16", + severity: "severe", + title: "Northern: Not Running", + description: "No service between A and B", + }); + expect(alerts[1]).toMatchObject({ + id: "tfl:status:district:11", + severity: "warning", + title: "District: Part Closed", + }); + }); + + it("maps every documented notice code", async () => { + const notices: Array<[number, string]> = [ + [12, "Exit Only"], + [13, "No Step Free Access"], + [14, "Change of frequency"], + [15, "Diverted"], + [17, "Issues Reported"], + [19, "Information"], + ]; + mockFetch.mockResolvedValueOnce( + mockOk( + notices.map(([code, description]) => ({ + id: `line-${code}`, + name: `Line ${code}`, + lineStatuses: [{ statusSeverity: code, statusSeverityDescription: description }], + })), + ), + ); + + const alerts = await getAlerts(); + + expect(alerts.map((alert) => alert.severity)).toEqual([ + "warning", + "warning", + "info", + "warning", + "warning", + "info", + ]); + }); + + it("raises no alert for good service, no issues or the overnight closure", async () => { + mockFetch.mockResolvedValueOnce( + mockOk([ + { + id: "central", + name: "Central", + lineStatuses: [{ statusSeverity: 10, statusSeverityDescription: "Good Service" }], + }, + { + id: "victoria", + name: "Victoria", + lineStatuses: [{ statusSeverity: 18, statusSeverityDescription: "No Issues" }], + }, + { + id: "jubilee", + name: "Jubilee", + lineStatuses: [{ statusSeverity: 20, statusSeverityDescription: "Service Closed" }], + }, + { id: "bakerloo", name: "Bakerloo", lineStatuses: [{ statusSeverity: null }] }, + ]), + ); + + expect(await getAlerts()).toEqual([]); + }); + + it("represents every simultaneous status on a line", async () => { + mockFetch.mockResolvedValueOnce( + mockOk([ + { + id: "piccadilly", + name: "Piccadilly", + lineStatuses: [ + { + statusSeverity: 9, + statusSeverityDescription: "Minor Delays", + reason: "Earlier signal failure", + }, + { + statusSeverity: 5, + statusSeverityDescription: "Part Closure", + reason: "No service west of Acton Town", + }, + { + statusSeverity: 5, + statusSeverityDescription: "Part Closure", + reason: "No service Hyde Park Corner to Holborn", + }, + { statusSeverity: 10, statusSeverityDescription: "Good Service" }, + ], + }, + ]), + ); + + const alerts = await getAlerts(); + + expect(alerts).toHaveLength(2); + expect(alerts[0]).toMatchObject({ + id: "tfl:status:piccadilly:9", + severity: "info", + description: "Earlier signal failure", + }); + expect(alerts[1]).toMatchObject({ + id: "tfl:status:piccadilly:5", + severity: "warning", + description: "No service west of Acton Town\n\nNo service Hyde Park Corner to Holborn", + }); + }); + + it("keeps an existing low code mapping", async () => { + mockFetch.mockResolvedValueOnce( + mockOk([ + { + id: "metropolitan", + name: "Metropolitan", + lineStatuses: [{ statusSeverity: 6, statusSeverityDescription: "Severe Delays" }], + }, + { + id: "circle", + name: "Circle", + lineStatuses: [{ statusSeverity: 2, statusSeverityDescription: "Suspended" }], + }, + ]), + ); + + const alerts = await getAlerts(); + + expect(alerts.map((alert) => alert.severity)).toEqual(["warning", "severe"]); + }); + it("strips the tfl: prefix when requesting route stop sequences", async () => { mockFetch.mockResolvedValueOnce( mockOk({