/**
 * Poll-http source evaluator.
 *
 * GETs the URL; the observation is `<status>\n<body>` so a status flip OR a body
 * change both register as a diff. Uses the built-in fetch (Node 22+).
 */
const DEFAULT_TIMEOUT_MS = 30_000;
const MAX_BODY = 1024 * 1024;
/** GET the source URL and return status + body as the observation. */
export async function evaluate(source) {
    const url = source.url;
    if (!url)
        return null;
    const controller = new AbortController();
    const timer = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
    try {
        const res = await fetch(url, { signal: controller.signal, redirect: 'follow' });
        let body = await res.text();
        if (body.length > MAX_BODY)
            body = body.slice(0, MAX_BODY);
        return {
            raw: `${res.status}\n${body}`,
            meta: { status: res.status, ok: res.ok },
        };
    }
    catch (err) {
        // A network failure is itself a real observation (the endpoint went down);
        // surface it so an on-change monitor can fire on reachability flips.
        return { raw: `error: ${err.message}`, meta: { status: 0, ok: false } };
    }
    finally {
        clearTimeout(timer);
    }
}
