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 | 1x 1x 1x 1x 4x 4x 2x 2x 3x 3x 3x 3x 3x 3x 2x 4x 2x 4x 3x 3x 1x 1x | #!/usr/bin/env tsx
/**
* @module scripts/calendar-fetch
* @description Emits a compact calendar-status JSON for forward indicators.
*
* Routes through `fetchCalendarWithFallback()` from `./fetch-calendar.js` so a
* transient MCP outage transparently falls back to the public
* `riksdagen.se/sv/kalendarium/` scrape. When `org` (or `akt`) is supplied we
* filter the resulting events to that scope so the downstream artifact still
* matches the caller's intent.
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { CalendarEvent } from './fetch-calendar.js';
import { fetchCalendarWithFallback } from './fetch-calendar.js';
import { MCPClient } from './mcp-client/client.js';
export interface CalendarStatus {
readonly schemaVersion: '1.0';
readonly fetchedAt: string;
readonly from: string;
readonly to: string;
readonly org: string | null;
readonly akt: string | null;
readonly eventCount: number;
readonly status: 'ok' | 'error';
readonly path: 'mcp-primary' | 'web-fallback' | 'none';
readonly events: readonly CalendarEvent[];
readonly notes?: string;
}
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = path.resolve(__dirname, '..');
export const DEFAULT_CALENDAR_STATUS_OUTPUT = path.join(REPO_ROOT, 'data', 'calendar-status.json');
type CalendarResilientFetcher = (
from: string,
to: string,
) => Promise<{ events: readonly CalendarEvent[]; path: 'mcp-primary' | 'web-fallback' | 'none'; primaryError?: string; fallbackError?: string }>;
const defaultResilientFetcher: CalendarResilientFetcher = async (from, to) => {
const result = await fetchCalendarWithFallback(from, to);
const ret: { events: readonly CalendarEvent[]; path: 'mcp-primary' | 'web-fallback' | 'none'; primaryError?: string; fallbackError?: string } = {
events: result.events,
path: result.manifest.path,
};
if (result.manifest.primaryError !== undefined) {
ret.primaryError = result.manifest.primaryError;
}
if (result.manifest.fallbackError !== undefined) {
ret.fallbackError = result.manifest.fallbackError;
}
return ret;
};
function matchesScope(event: CalendarEvent, org: string | null, akt: string | null): boolean {
// When a scope is requested, treat events with an empty/missing field as
// non-matches — otherwise unclassified events leak into a scoped artifact.
Eif (org) {
if (!event.org || event.org.toLowerCase() !== org.toLowerCase()) return false;
}
Iif (akt) {
if (!event.akt || event.akt.toLowerCase() !== akt.toLowerCase()) return false;
}
return true;
}
export async function fetchCalendarStatus(
from: string,
to: string,
org: string | null = null,
akt: string | null = null,
// Back-compat: tests pass a stub MCP client. When provided we route through it
// directly so tests can simulate MCP failure paths deterministically.
client?: Pick<MCPClient, 'fetchCalendarEvents'> | CalendarResilientFetcher,
): Promise<CalendarStatus> {
const fetchedAt = new Date().toISOString();
try {
// Resilient default: use the MCP→web fallback chain so a transient MCP
// outage does not block the artifact.
Iif (!client) {
const result = await defaultResilientFetcher(from, to);
const scoped = (org || akt)
? result.events.filter((event) => matchesScope(event, org, akt))
: result.events;
const okPath = result.path !== 'none';
const baseStatus: CalendarStatus = {
schemaVersion: '1.0',
fetchedAt,
from,
to,
org,
akt,
eventCount: scoped.length,
status: okPath ? 'ok' : 'error',
path: result.path,
events: scoped,
...(result.primaryError || result.fallbackError
? { notes: [
result.primaryError ? `primary: ${result.primaryError}` : '',
result.fallbackError ? `fallback: ${result.fallbackError}` : '',
].filter(Boolean).join(' | ') }
: {}),
};
return baseStatus;
}
// Legacy path: caller injected an MCP-style client. Keep the original
// behaviour so the test surface remains stable.
Eif (typeof (client as { fetchCalendarEvents?: unknown }).fetchCalendarEvents === 'function') {
const mcp = client as Pick<MCPClient, 'fetchCalendarEvents'>;
const raw = await mcp.fetchCalendarEvents(from, to, org, akt);
const { normalizeMcpCalendarEvent } = await import('./fetch-calendar.js');
const events = raw.map((event) => normalizeMcpCalendarEvent(event));
const scoped = (org || akt)
? events.filter((event) => matchesScope(event, org, akt))
: events;
return {
schemaVersion: '1.0',
fetchedAt,
from,
to,
org,
akt,
eventCount: scoped.length,
status: 'ok',
path: 'mcp-primary',
events: scoped,
};
}
const customFetcher = client as CalendarResilientFetcher;
const result = await customFetcher(from, to);
const scoped = (org || akt)
? result.events.filter((event) => matchesScope(event, org, akt))
: result.events;
return {
schemaVersion: '1.0',
fetchedAt,
from,
to,
org,
akt,
eventCount: scoped.length,
status: result.path !== 'none' ? 'ok' : 'error',
path: result.path,
events: scoped,
};
} catch (error) {
return {
schemaVersion: '1.0',
fetchedAt,
from,
to,
org,
akt,
eventCount: 0,
status: 'error',
path: 'none',
events: [],
notes: error instanceof Error ? error.message : String(error),
};
}
}
export function persistCalendarStatus(status: CalendarStatus, outputPath = DEFAULT_CALENDAR_STATUS_OUTPUT): string {
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, `${JSON.stringify(status, null, 2)}\n`, 'utf8');
return outputPath;
}
function parseArgs(argv: readonly string[]): { from: string; to: string; org: string | null; akt: string | null; output: string; persist: boolean } {
let from = new Date().toISOString().slice(0, 10);
let to = from;
let org: string | null = null;
let akt: string | null = null;
let output = DEFAULT_CALENDAR_STATUS_OUTPUT;
let persist = true;
for (let i = 2; i < argv.length; i++) {
const token = argv[i];
const next = argv[i + 1];
if (token === '--from' && next) {
from = next;
i++;
continue;
}
if (token === '--to' && next) {
to = next;
i++;
continue;
}
if (token === '--org' && next) {
org = next;
i++;
continue;
}
if (token === '--akt' && next) {
akt = next;
i++;
continue;
}
if (token === '--output' && next) {
output = next;
i++;
continue;
}
if (token === '--no-persist') {
persist = false;
}
}
return { from, to, org, akt, output, persist };
}
async function main(): Promise<void> {
const args = parseArgs(process.argv);
const status = await fetchCalendarStatus(args.from, args.to, args.org, args.akt);
if (args.persist) {
persistCalendarStatus(status, args.output);
}
process.stdout.write(`${JSON.stringify(status, null, 2)}\n`);
}
Iif (path.resolve(fileURLToPath(import.meta.url)) === path.resolve(process.argv[1] ?? '')) {
main().catch((error: unknown) => {
console.error(`calendar-fetch: ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
});
}
|