All files / scripts/mcp-client/methods calendar.ts

100% Statements 10/10
100% Branches 13/13
100% Functions 1/1
100% Lines 8/8

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                                                          9x 9x 9x   9x   9x 3x 3x         6x    
/**
 * @module mcp-client/methods/calendar
 * @description Calendar / kalender domain methods for the MCP client.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import type { MCPTransportClient } from '../transport/jsonrpc.js';
import { isDegradedKalenderSentinel } from '../../fetch-calendar/mcp/errors.js';
 
/**
 * Fetch parliamentary calendar events between two dates, optionally
 * filtered by organ (committee) and aktivitet (event type).
 *
 * Throws when the riksdag-regering server returns its degraded-kalender
 * sentinel (an empty `events` array alongside an `error`/`rawHtml` field,
 * emitted when `data.riksdagen.se/kalender/` serves an HTML error page).
 * Surfacing the failure lets callers fall back to the public-page scraper
 * via {@link module:scripts/fetch-calendar} instead of trusting a fake
 * zero-event window.
 */
export async function fetchCalendarEvents(
  transport: MCPTransportClient,
  from: string,
  tom: string,
  org: string | null = null,
  akt: string | null = null,
): Promise<unknown[]> {
  const params: Record<string, unknown> = { from, tom };
  if (org) params['org'] = org;
  if (akt) params['akt'] = akt;
 
  const response = await transport.request('get_calendar_events', params);
 
  if (isDegradedKalenderSentinel(response)) {
    const errorText = response['error'];
    throw new Error(
      `get_calendar_events degraded: ${typeof errorText === 'string' ? errorText : 'upstream HTML error from data.riksdagen.se/kalender/'}`,
    );
  }
 
  return (response['kalender'] ?? response['events'] ?? []) as unknown[];
}