All files / src/mcp ep-open-data-client.ts

93.75% Statements 105/112
81.55% Branches 84/103
94.73% Functions 18/19
97.08% Lines 100/103

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609                                                                                                                  1x     1x             1x             1x             1x               1x                                                                                                                                                                 5x 4x 4x                             13x 13x                     5x 1x 5x   1x 1x                                                     25x     25x 25x   25x     25x     25x 25x 3x   25x   25x             25x                 3x                 4x                   11x 11x 11x   3x 3x                       6x         8x                                               13x 13x 2x 2x   11x 11x             13x 7x 13x                 13x           4x 4x 4x                                 17x 17x 17x 15x 15x   1x                                     6x                                                 11x 11x 11x 11x 11x         9x 1x   8x   11x                           11x 8x 8x   1x 1x               1x                                                                               8x       8x 1x   7x 1x       6x 1x               5x         5x 5x   1x           4x 4x             4x 2x               2x           4x             1x                         5x 4x 4x 4x 3x   1x 1x     4x         7x 3x 3x      
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module MCP/EPOpenDataClient
 * @description Direct HTTP fallback client for the European Parliament Open Data
 * Portal (`https://data.europarl.europa.eu/api/v2/`) — specifically the
 * `/decision` endpoint that exposes roll-call voting records.
 *
 * ## Why this exists (Defect D-02)
 *
 * The EP MCP server tool `get_voting_records` relies on an EP feed that
 * publishes roll-call data with a 4–6 week delay (documented limitation,
 * see `.github/prompts/07-mcp-reference.md` §11 item #6). That lag can
 * leave the most recent voting window empty in MCP even when the EP Open
 * Data Portal `/api/v2/decision` endpoint already exposes the underlying
 * roll-call records directly. This client therefore queries that endpoint
 * whenever the MCP layer returns an empty votes array, improving freshness
 * without limiting the fallback to older date ranges.
 *
 * ## Three-state fallback logic
 *
 * Call {@link getVotingRecordsWithFallback} to execute the decision tree:
 *
 * ```
 * (a) MCP returns non-empty votes  → use MCP result (source: "mcp")
 * (b) MCP empty, Portal has data   → use Portal result (source: "ep-open-data-portal")
 * (c) Both empty                   → emit 🔴 unavailable marker (source: "unavailable")
 * ```
 *
 * ## Transport
 *
 * - Uses the native Node 25 `fetch()` — no extra runtime dependency.
 * - Every call has an independent `AbortController` with a configurable
 *   timeout (`EP_OPEN_DATA_TIMEOUT_MS`, default 30 s).
 * - Errors (HTTP 4xx/5xx, network faults, JSON parse failures, abort) are
 *   caught and converted to an empty-votes envelope matching the MCP fallback
 *   shape, so consumers that already expect `response.content[0]?.text` to
 *   hold a JSON blob keep working without change.
 *
 * ## Attribution
 *
 * All data returned by this client originates from the EP Open Data Portal
 * which is published under the CC BY 4.0 licence. Every fallback response
 * includes a `_attribution` field with the canonical citation string.
 *
 * Environment variables:
 * - `EP_OPEN_DATA_BASE_URL` — override base URL (default
 *   `https://data.europarl.europa.eu/api/v2`).
 * - `EP_OPEN_DATA_TIMEOUT_MS` — per-request timeout (default `30000`).
 */
 
import type { MCPToolResult, MCPClientOptions } from '../types/index.js';
 
// ─── Defaults ────────────────────────────────────────────────────────────────
 
/** Default base URL for the EP Open Data Portal API v2. */
const DEFAULT_EP_OPEN_DATA_BASE_URL = 'https://data.europarl.europa.eu/api/v2';
 
/** Default per-request timeout (milliseconds). */
const DEFAULT_EP_OPEN_DATA_TIMEOUT_MS = 30_000;
 
/**
 * Attribution string required by the EP Open Data Portal licence (CC BY 4.0).
 * Appended to every fallback response.
 */
const EP_OPEN_DATA_ATTRIBUTION =
  'European Parliament Open Data Portal — https://data.europarl.europa.eu — CC BY 4.0';
 
/**
 * Generic empty-votes fallback payload returned when the portal call cannot
 * produce usable voting data, including invalid input, missing dates, empty
 * portal responses, and HTTP/network/parse/timeout failures.
 */
const EMPTY_VOTES_FALLBACK: MCPToolResult = {
  content: [{ type: 'text', text: '{"votes":[]}' }],
};
 
// ─── Virtual tool list ────────────────────────────────────────────────────────
 
/** Canonical tool name for EP voting records (kept DRY for tools list and warnings). */
const EP_GET_VOTING_RECORDS_TOOL = 'ep-get-voting-records';
 
/**
 * Virtual tool names exposed by this client. The list is used as a drift
 * guard in `test/unit/ep-open-data-client.test.js` and in the Stage-C
 * editorial fingerprint for `voting-patterns.md` (the article must cite
 * at least one of these when the fallback is active).
 */
export const EP_OPEN_DATA_TOOLS: readonly string[] = [EP_GET_VOTING_RECORDS_TOOL];
 
// ─── Response types (EP Open Data Portal JSON-LD narrow subset) ──────────────
 
interface EPDecisionRecord {
  identifier?: string;
  date?: string;
  activityType?: string;
  prefLabel?: string | Record<string, string>;
  favorable?: number;
  against?: number;
  abstention?: number;
  /** Raw @id URI (used when `identifier` is absent). */
  '@id'?: string;
}
 
interface EPDecisionResponse {
  data?: EPDecisionRecord[];
}
 
// ─── Public types ─────────────────────────────────────────────────────────────
 
/**
 * Source tag for a voting-records result.
 * - `"mcp"` — the EP MCP server returned non-empty data.
 * - `"ep-open-data-portal"` — MCP was empty; the fallback portal query succeeded.
 * - `"unavailable"` — both sources returned empty; a `🔴` marker was emitted.
 */
export type VotingDataSource = 'mcp' | 'ep-open-data-portal' | 'unavailable';
 
/**
 * Result envelope from {@link getVotingRecordsWithFallback}.
 * Exposes the raw MCP-shaped result plus provenance metadata consumed
 * by `voting-patterns.md` §"Voting Data Freshness".
 */
export interface VotingRecordsFallbackResult {
  /** MCP-shaped voting records (or 🔴 marker when unavailable). */
  result: MCPToolResult;
  /** Which source delivered the data. */
  source: VotingDataSource;
  /**
   * Human-readable freshness string for the `voting-data-freshness` audit row.
   * E.g. `"🟢 MCP (2026-04-01 → 2026-04-26)"`,
   *      `"🟡 EP Open Data Portal fallback (2026-03-01 → 2026-04-01)"`, or
   *      `"🔴 voting data unavailable for window 2026-04-01 → 2026-04-26"`.
   */
  freshnessLabel: string;
}
 
/**
 * Options for {@link getVotingRecordsWithFallback} and
 * {@link EPOpenDataClient.getVotingRecords}.
 */
export interface VotingRecordsFallbackOptions extends MCPClientOptions {
  /** Inclusive start date (YYYY-MM-DD). */
  dateFrom: string;
  /** Inclusive end date (YYYY-MM-DD). */
  dateTo: string;
  /** Maximum records to return (default 50). */
  limit?: number;
  /** Pagination offset (default 0). */
  offset?: number;
  /** Optional `fetch` implementation injection for testing. */
  fetchImpl?: typeof fetch;
  /** Override the EP Open Data base URL. */
  apiBaseUrl?: string;
  /** Per-request timeout in milliseconds. */
  timeoutMs?: number;
}
 
// ─── Utilities ────────────────────────────────────────────────────────────────
 
/**
 * Unwrap a multilingual JSON-LD label to a plain string.
 * Prefers the English value; falls back to the first available string value.
 *
 * @param raw - Raw label (string, locale object, or undefined).
 * @returns Plain string (empty when no label is available).
 * @internal
 */
function unwrapLabel(raw: string | Record<string, string> | undefined): string {
  if (!raw) return '';
  Iif (typeof raw === 'string') return raw;
  Eif (typeof raw['en'] === 'string') return raw['en'];
  for (const v of Object.values(raw)) {
    if (typeof v === 'string') return v;
  }
  return '';
}
 
/**
 * Wrap a value in the canonical MCP tool-result shape.
 *
 * @param payload - Serialisable payload.
 * @returns MCP tool-result envelope.
 * @internal
 */
function wrapAsMCPResult(payload: unknown): MCPToolResult {
  const text = typeof payload === 'string' ? payload : JSON.stringify(payload ?? null);
  return { content: [{ type: 'text', text }] };
}
 
/**
 * Extract an identifier from an EP decision record.
 *
 * @param record - Raw EP Open Data Portal decision record.
 * @returns Best-effort identifier string.
 * @internal
 */
function extractIdentifier(record: EPDecisionRecord): string {
  if (record.identifier) return record.identifier;
  const rawId = record['@id'] ?? '';
  Iif (!rawId) return '';
  // Strip the URI prefix — last path segment is the local id.
  const lastSlash = rawId.lastIndexOf('/');
  return lastSlash >= 0 ? rawId.slice(lastSlash + 1) : rawId;
}
 
// ─── Client ──────────────────────────────────────────────────────────────────
 
/**
 * Direct HTTP client for EP Open Data Portal roll-call vote data.
 *
 * Queries `GET /api/v2/decision` with `date-of-vote-start` and
 * `date-of-vote-end` parameters. The response is JSON-LD; we extract
 * the `data` array and normalise each entry to the same `votes[]` shape
 * that the MCP `get_voting_records` tool returns, augmented with
 * `_source` and `_attribution` metadata.
 *
 * @example
 * ```ts
 * const client = new EPOpenDataClient();
 * const result = await client.getVotingRecords({
 *   dateFrom: '2026-03-01',
 *   dateTo:   '2026-04-01',
 * });
 * ```
 */
export class EPOpenDataClient {
  private readonly _apiBaseUrl: string;
  private readonly _timeoutMs: number;
  private readonly _fetchImpl: typeof fetch;
  private _connected = false;
 
  constructor(options: VotingRecordsFallbackOptions = { dateFrom: '', dateTo: '' }) {
    const envBase = process.env['EP_OPEN_DATA_BASE_URL'];
    const envTimeout = process.env['EP_OPEN_DATA_TIMEOUT_MS'];
    const parsedEnvTimeout =
      envTimeout !== undefined && envTimeout !== '' ? Number.parseInt(envTimeout, 10) : Number.NaN;
 
    const base =
      options.apiBaseUrl ?? (envBase && envBase !== '' ? envBase : DEFAULT_EP_OPEN_DATA_BASE_URL);
 
    // Strip trailing slashes without a regex (avoids polynomial-ReDoS flags).
    let end = base.length;
    while (end > 0 && base.charCodeAt(end - 1) === 47 /* '/' */) {
      end -= 1;
    }
    this._apiBaseUrl = end === base.length ? base : base.slice(0, end);
 
    this._timeoutMs =
      options.timeoutMs !== undefined && Number.isFinite(options.timeoutMs) && options.timeoutMs > 0
        ? options.timeoutMs
        : Number.isFinite(parsedEnvTimeout) && parsedEnvTimeout > 0
          ? parsedEnvTimeout
          : DEFAULT_EP_OPEN_DATA_TIMEOUT_MS;
 
    this._fetchImpl = options.fetchImpl ?? globalThis.fetch.bind(globalThis);
  }
 
  /**
   * Base URL currently in use (set at construction time).
   *
   * @returns The fully-qualified EP Open Data Portal base URL (no trailing slash).
   */
  getApiBaseUrl(): string {
    return this._apiBaseUrl;
  }
 
  /**
   * Per-request timeout in milliseconds.
   *
   * @returns The timeout applied to every `fetch()` call.
   */
  getTimeoutMs(): number {
    return this._timeoutMs;
  }
 
  /**
   * Mark the client as ready (HTTP is stateless; validates URL shape only).
   *
   * @returns A resolved promise; never throws for valid URLs.
   * @throws When the base URL is malformed (e.g. missing protocol).
   */
  async connect(): Promise<void> {
    try {
      new URL(this._apiBaseUrl);
      this._connected = true;
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      throw new Error(`Invalid EP_OPEN_DATA_BASE_URL "${this._apiBaseUrl}": ${message}`, {
        cause: error,
      });
    }
  }
 
  /**
   * Whether {@link connect} has been called successfully.
   *
   * @returns `true` after a successful {@link connect}.
   */
  isConnected(): boolean {
    return this._connected;
  }
 
  /** Reset the connected flag. No real socket to close. */
  disconnect(): void {
    this._connected = false;
  }
 
  /**
   * Fetch roll-call voting records from the EP Open Data Portal.
   *
   * Virtual tool: `ep-get-voting-records` (see {@link EP_GET_VOTING_RECORDS_TOOL}).
   *
   * Queries `/decision?date-of-vote-start=<dateFrom>&date-of-vote-end=<dateTo>`
   * and normalises the JSON-LD response to a `{ votes: VoteEntry[] }`
   * envelope whose shape mirrors `get_voting_records` from the EP MCP server,
   * augmented with `_source` and `_attribution` metadata.
   *
   * Returns the EMPTY_VOTES_FALLBACK envelope on any error so callers can
   * safely chain the result through {@link EPOpenDataClient.isVotingDataEmpty}
   * without defensive try/catch.
   *
   * @param options - Query options. `dateFrom` and `dateTo` are required and
   *   must be non-empty YYYY-MM-DD strings.
   * @returns MCP-shaped result with `votes[]` array (possibly empty on error).
   */
  async getVotingRecords(
    options: Pick<VotingRecordsFallbackOptions, 'dateFrom' | 'dateTo' | 'limit' | 'offset'>
  ): Promise<MCPToolResult> {
    const { dateFrom, dateTo } = options;
    if (!dateFrom || !dateTo) {
      console.warn(`${EP_GET_VOTING_RECORDS_TOOL}: dateFrom and dateTo are required`);
      return EMPTY_VOTES_FALLBACK;
    }
    try {
      const qs = new URLSearchParams({
        'date-of-vote-start': dateFrom,
        'date-of-vote-end': dateTo,
        limit: String(options.limit ?? 50),
        offset: String(options.offset ?? 0),
        format: 'application/ld+json',
      });
      const json = await this._getJSON<EPDecisionResponse>(`/decision?${qs.toString()}`);
      const records = json?.data ?? [];
      const votes = records.map((r) => ({
        identifier: extractIdentifier(r),
        date: r.date ?? '',
        title: unwrapLabel(r.prefLabel),
        activityType: r.activityType ?? '',
        for: r.favorable ?? 0,
        against: r.against ?? 0,
        abstain: r.abstention ?? 0,
      }));
      return wrapAsMCPResult({
        votes,
        _source: 'ep-open-data-portal',
        _attribution: EP_OPEN_DATA_ATTRIBUTION,
      });
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      console.warn(`${EP_GET_VOTING_RECORDS_TOOL} not available:`, message);
      return EMPTY_VOTES_FALLBACK;
    }
  }
 
  // ─── Static helpers ──────────────────────────────────────────────────────
 
  /**
   * Return `true` when a `get_voting_records` (or fallback) result carries
   * zero votes. Used to decide whether to proceed to the next fallback tier.
   *
   * Handles both the `{"votes":[]}` MCP empty shape and the plain
   * `{"votes":null}` edge case.
   *
   * @param result - MCP tool result to inspect.
   * @returns `true` when the votes array is absent or empty.
   */
  static isVotingDataEmpty(result: MCPToolResult): boolean {
    try {
      const text = result.content?.[0]?.text ?? '';
      if (!text) return true;
      const parsed = JSON.parse(text) as { votes?: unknown[] | null };
      return !Array.isArray(parsed.votes) || parsed.votes.length === 0;
    } catch {
      return true;
    }
  }
 
  /**
   * Build the canonical `🔴 voting data unavailable` marker emitted when
   * both MCP and the EP Open Data Portal fallback return empty.
   *
   * The returned payload remains a normal MCP JSON envelope containing an
   * empty `votes` array plus human-readable marker metadata (`_marker`,
   * `_reason`, `_unavailable`). This preserves compatibility with consumers
   * that parse `response.content[0]?.text` as JSON, while still exposing a
   * clear alert message for downstream display or diagnostics.
   *
   * @param dateFrom - Analysis period start (YYYY-MM-DD).
   * @param dateTo   - Analysis period end (YYYY-MM-DD).
   * @returns MCP-shaped result containing the 🔴 unavailability marker.
   */
  static buildVotingUnavailableMarker(dateFrom: string, dateTo: string): MCPToolResult {
    return wrapAsMCPResult({
      votes: [],
      _unavailable: true,
      _marker: `🔴 voting data unavailable for window ${dateFrom} → ${dateTo}`,
      _reason:
        'EP MCP server returned no roll-call votes and EP Open Data Portal returned no decisions ' +
        'for the requested window. This is expected when the window lies within the 4–6 week EP ' +
        'publication delay AND no older data has been published. Do not substitute with ' +
        'structural-proxy cohesion scores without an explicit LOW-confidence flag per ' +
        'osint-tradecraft-standards.md §3.1.',
    });
  }
 
  // ─── Private transport helpers ────────────────────────────────────────────
 
  /**
   * Build a full URL and GET it as text, enforcing the client-wide timeout.
   *
   * @param path - Path (already URL-encoded) to append to the base URL.
   * @returns Response body as a string.
   * @throws When the HTTP status is not 2xx, the request times out, or
   *   the network layer raises.
   * @internal
   */
  private async _getText(path: string): Promise<string> {
    const url = `${this._apiBaseUrl}${path.startsWith('/') ? path : `/${path}`}`;
    const controller = new AbortController();
    const timer = setTimeout(() => controller.abort(), this._timeoutMs);
    try {
      const response = await this._fetchImpl(url, {
        method: 'GET',
        headers: { Accept: 'application/ld+json, application/json;q=0.9' },
        signal: controller.signal,
      });
      if (!response.ok) {
        throw new Error(`HTTP ${response.status} ${response.statusText} for ${url}`);
      }
      return await response.text();
    } finally {
      clearTimeout(timer);
    }
  }
 
  /**
   * GET a URL and parse the response body as JSON.
   *
   * @template T - Narrow response type declared by the caller.
   * @param path - Path to append to the base URL.
   * @returns Parsed JSON value.
   * @throws When the response is not JSON, not 2xx, or the request fails.
   * @internal
   */
  private async _getJSON<T>(path: string): Promise<T> {
    const raw = await this._getText(path);
    try {
      return JSON.parse(raw) as T;
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      throw new Error(`Failed to parse EP Open Data response as JSON: ${message}`, {
        cause: error,
      });
    }
  }
}
 
/** Forward-looking alias (preferred for new code). */
export const EPOpenDataPortalClient = EPOpenDataClient;
 
// ─── Top-level three-state fallback helper ────────────────────────────────────
 
/**
 * Execute the three-state voting-data fallback decision tree.
 *
 * Given an already-retrieved MCP result and fallback options (dateFrom /
 * dateTo / fetchImpl / …), returns a {@link VotingRecordsFallbackResult}
 * that records both the data and the provenance label consumed by the
 * `voting-patterns.md` §"Voting Data Freshness" section.
 *
 * Decision tree:
 * ```
 * (a) mcpResult has votes  → source: "mcp",                freshness: 🟢
 * (b) MCP empty, portal ok → source: "ep-open-data-portal", freshness: 🟡
 * (c) both empty           → source: "unavailable",          freshness: 🔴
 * ```
 *
 * @param mcpResult - Result of `get_voting_records` from the EP MCP server.
 * @param options   - Fallback options; `dateFrom` and `dateTo` are required
 *   non-empty YYYY-MM-DD strings.
 * @returns Fallback result with source tag and human-readable freshness label.
 * @throws When `dateFrom` or `dateTo` is missing/blank, or when the EP Open
 *   Data Portal base URL is malformed (configuration error — distinct from
 *   the 🔴 `unavailable` data-empty path).
 *
 * @example
 * ```ts
 * const mcp = await epClient.getVotingRecords({ dateFrom, dateTo, limit: 50 });
 * const { result, source, freshnessLabel } = await getVotingRecordsWithFallback(mcp, {
 *   dateFrom, dateTo,
 * });
 * // Write freshnessLabel into voting-patterns.md §"Voting Data Freshness"
 * ```
 */
export async function getVotingRecordsWithFallback(
  mcpResult: MCPToolResult,
  options: VotingRecordsFallbackOptions
): Promise<VotingRecordsFallbackResult> {
  const { dateFrom, dateTo } = options;
 
  // Fail fast on missing/blank dates so we don't emit misleading freshness
  // labels like "🟢 MCP ( → )" or empty-window 🔴 markers downstream.
  if (typeof dateFrom !== 'string' || dateFrom.trim() === '') {
    throw new Error('getVotingRecordsWithFallback: dateFrom is required (non-empty YYYY-MM-DD)');
  }
  if (typeof dateTo !== 'string' || dateTo.trim() === '') {
    throw new Error('getVotingRecordsWithFallback: dateTo is required (non-empty YYYY-MM-DD)');
  }
 
  // (a) MCP returned real data — use it.
  if (!EPOpenDataClient.isVotingDataEmpty(mcpResult)) {
    return {
      result: mcpResult,
      source: 'mcp',
      freshnessLabel: `🟢 MCP (${dateFrom} → ${dateTo})`,
    };
  }
 
  // (b) MCP was empty — try the EP Open Data Portal fallback.
  const portalClient = new EPOpenDataClient(options);
 
  // Validate base URL up front so misconfiguration surfaces as a hard error
  // rather than being swallowed into the 🔴 "unavailable" path (which is
  // reserved for genuine "both sources empty" outcomes).
  try {
    await portalClient.connect();
  } catch (error) {
    throw new Error(
      'Invalid EP Open Data Portal configuration: unable to validate EP_OPEN_DATA_BASE_URL',
      { cause: error }
    );
  }
 
  try {
    const portalResult = await portalClient.getVotingRecords({
      dateFrom,
      dateTo,
      ...(options.limit !== undefined ? { limit: options.limit } : {}),
      ...(options.offset !== undefined ? { offset: options.offset } : {}),
    });
 
    if (!EPOpenDataClient.isVotingDataEmpty(portalResult)) {
      return {
        result: portalResult,
        source: 'ep-open-data-portal',
        freshnessLabel: `🟡 EP Open Data Portal fallback (${dateFrom} → ${dateTo}) — attribution: ${EP_OPEN_DATA_ATTRIBUTION}`,
      };
    }
 
    // (c) Both empty — emit the 🔴 unavailability marker.
    return {
      result: EPOpenDataClient.buildVotingUnavailableMarker(dateFrom, dateTo),
      source: 'unavailable',
      freshnessLabel: `🔴 voting data unavailable for window ${dateFrom} → ${dateTo}`,
    };
  } finally {
    portalClient.disconnect();
  }
}
 
// ─── Singleton lifecycle ──────────────────────────────────────────────────────
 
/** Singleton instance, created lazily by {@link getEPOpenDataClient}. */
let epOpenDataClientInstance: EPOpenDataClient | null = null;
 
/**
 * Get or create the singleton EP Open Data client, validating the base URL
 * on first use. Subsequent calls return the cached instance.
 *
 * @param options - Client options (override env vars and defaults).
 * @returns Connected singleton client.
 * @throws When the base URL is malformed.
 */
export async function getEPOpenDataClient(
  options: VotingRecordsFallbackOptions = { dateFrom: '', dateTo: '' }
): Promise<EPOpenDataClient> {
  if (!epOpenDataClientInstance) {
    const client = new EPOpenDataClient(options);
    try {
      await client.connect();
      epOpenDataClientInstance = client;
    } catch (error) {
      epOpenDataClientInstance = null;
      throw error;
    }
  }
  return epOpenDataClientInstance;
}
 
/** Close and clear the singleton instance (idempotent). */
export async function closeEPOpenDataClient(): Promise<void> {
  if (epOpenDataClientInstance) {
    epOpenDataClientInstance.disconnect();
    epOpenDataClientInstance = null;
  }
}