Public Beta
Not set

Experience

Changes how pages explain things, never what you can do.

More

Experience

Changes how pages explain things, never what you can do.

Sign inGet started

Fact Check v0.1.2 · generic

Archive SHA-256 7d384ad99b1d1deaf8ccdfdc086eac1b0dfb7516746fbc60278cfac1fec823c7 · 28.7 KB · built Nov 27, 2026

tests/fact-check.test.ts

import { describe, it, expect, vi } from "vitest";
// @ts-ignore -- plain-JS agent module without type declarations
import agentModule, { handleMcpMessage, makeContext } from "../src/index.js";

const agent: any = agentModule;
const now = () => new Date("2026-03-21T10:00:00Z");

const A = {
  url: "https://energy.example.org/heat-pumps-2025",
  title: "Heat pumps in 2025",
  text: "Heat pump sales in Europe grew 30% in 2025, according to the industry association. Germany and France accounted for most new installations last year. The association said 3.2 million units were installed in 2025.",
};
const B = {
  url: "https://news.example.com/eu-heat-pumps",
  excerpt: "<p>The European Commission did not approve the new subsidy scheme in June.</p><p>Heat pump sales in Europe grew 30% in 2025.</p>",
};
const C = { url: "https://launch.example.com/news", text: "The satellite launched on March 10, 2026 from Kourou." };
const sources = [A, B, C];

async function check(claims: any[], extra: any = {}) {
  const out = await agent.call("check_claims", { claims, sources, ...extra }, { now });
  return out.report;
}

describe("fact-check: check_claims", () => {
  it("supports a claim only with matching excerpts (happy path)", async () => {
    const r = await check(["Heat pump sales in Europe grew 30% in 2025."]);
    const c = r.claims[0];
    expect(c.verdict).toBe("supported");
    expect(c.evidence).toEqual([A.url, B.url]);
    expect(c.excerpts.filter((e: any) => e.relation === "supports").map((e: any) => e.text)).toEqual([
      "Heat pump sales in Europe grew 30% in 2025, according to the industry association.",
      "Heat pump sales in Europe grew 30% in 2025.",
    ]);
    expect(c.number_mismatches).toEqual([]);
    expect(r.summary).toEqual({ total: 1, supported: 1, unsupported: 0, needs_review: 0 });
    expect(r.generated_at).toBe("2026-03-21T10:00:00.000Z");
  });

  it("flags mismatched numbers and dates", async () => {
    const r = await check(["Heat pump sales in Europe grew 45% in 2025.", "The satellite launched on March 12, 2026 from Kourou."]);
    const [pct, date] = r.claims;
    expect(pct.verdict).toBe("unsupported");
    expect(pct.evidence).toEqual([]);
    expect(pct.number_mismatches).toEqual([
      { claim_value: "45%", source_values: ["30%"], url: A.url },
      { claim_value: "45%", source_values: ["30%"], url: B.url },
    ]);
    expect(date.verdict).toBe("unsupported");
    expect(date.number_mismatches).toEqual([{ claim_value: "March 12, 2026", source_values: ["March 10, 2026"], url: C.url }]);
  });

  it("returns unsupported when no source supports the claim", async () => {
    const r = await check(["Wind turbines generated half of Spain's electricity last winter."]);
    expect(r.claims[0]).toMatchObject({ verdict: "unsupported", evidence: [], excerpts: [] });
    expect(r.claims[0].reasons[0]).toMatch(/No provided source contains a sentence that matches/);
  });

  it("never marks supported without a source excerpt (sources with no text)", async () => {
    const out = await agent.call("check_claims", { claims: ["Heat pump sales in Europe grew 30% in 2025."], sources: [{ url: "https://empty.example.com/" }] }, { now });
    expect(out.report.claims[0].verdict).toBe("unsupported");
    const none = await agent.call("check_claims", { claims: ["Heat pump sales in Europe grew 30% in 2025."], sources: [] }, { now });
    expect(none.report.claims[0].verdict).toBe("unsupported");
    expect(none.report.notes.join(" ")).toMatch(/No sources were provided/);
  });

  it("detects contradictions (negation) and partial matches", async () => {
    const r = await check(["The European Commission approved the new subsidy scheme in June.", "3.2 million heat pump units were installed in 2025."]);
    const [neg, partial] = r.claims;
    expect(neg.verdict).toBe("unsupported");
    expect(neg.reasons.join(" ")).toMatch(/Polarity differs/);
    expect(neg.excerpts[0]).toMatchObject({ url: B.url, relation: "conflicts" });
    expect(partial.verdict).toBe("needs_review");
    expect(partial.evidence).toContain(A.url);
    expect(partial.excerpts.every((e: any) => e.relation === "partial")).toBe(true);
  });

  it("checks a script's claims and their citations", async () => {
    const script = {
      title: "t",
      claims: [
        { text: "Heat pump sales in Europe grew 30% in 2025, according to the industry association.", source_url: A.url, sources: [A.url] },
        { text: "Heat pump sales in Europe grew 30% in 2025.", source_url: C.url },
      ],
    };
    const out = await agent.call("check_claims", { script, sources }, { now });
    const [ok, wrongCitation] = out.report.claims;
    expect(ok).toMatchObject({ verdict: "supported", cited_source_matched: true });
    expect(wrongCitation).toMatchObject({ verdict: "needs_review", cited_source_matched: false });
    expect(wrongCitation.reasons.join(" ")).toMatch(/different source than the one cited/);
  });

  it("treats rose/grew as the same direction and rose/fell as a contradiction", async () => {
    const r = await check(["Heat pump sales in Europe rose 30% in 2025.", "Heat pump sales in Europe fell 30% in 2025."]);
    expect(r.claims[0].verdict).toBe("supported");
    expect(r.claims[0].evidence).toEqual([A.url, B.url]);
    expect(r.claims[1].verdict).toBe("unsupported");
    expect(r.claims[1].evidence).toEqual([]);
    expect(r.claims[1].reasons.join(" ")).toMatch(/Direction differs: the claim says it fell, but https:\/\/energy\.example\.org\/heat-pumps-2025 says it rose/);
  });

  it("does not support a claimed rise from a sentence that states no direction", async () => {
    const out = await agent.call(
      "check_claims",
      { claims: ["Heat pump installations rose to 3.2 million units in 2025."], sources: [{ url: "https://x.example.com/a", text: "Heat pump installations reached 3.2 million units in 2025." }] },
      { now },
    );
    const c = out.report.claims[0];
    expect(c.verdict).toBe("needs_review");
    expect(c.reasons.join(" ")).toMatch(/do not say whether it rose or fell/);
  });

  it("ignores an extra comparison figure in the source and names conflicting figures", async () => {
    const src = [{ url: "https://x.example.com/survey", text: "The average e-bike commute in the survey was 9.4 km each way, compared with 5.1 km for a conventional bicycle." }];
    const out = await agent.call(
      "check_claims",
      { claims: ["The average e-bike commute in the survey was 9.4 km each way.", "The average e-bike commute in the survey was 9.4 km each way, compared with 3.0 km for a conventional bicycle."], sources: src },
      { now },
    );
    const [ok, mixed] = out.report.claims;
    expect(ok.verdict).toBe("supported");
    expect(ok.number_mismatches).toEqual([]);
    expect(mixed.verdict).not.toBe("supported");
    expect(mixed.number_mismatches).toEqual([{ claim_value: "3.0", source_values: ["5.1"], url: src[0].url }]);
  });

  it("checks a multi-sentence claim (or top-level text) one sentence at a time", async () => {
    const src = [{ url: "https://solar.example.org/2025", text: "Community solar subscriptions grew 20 percent in 2025, according to the provincial energy board." }];
    const paragraph = "Community solar subscriptions grew 20 percent in 2025. Every renter in Ontario now has rooftop solar.";
    const asClaim = await agent.call("check_claims", { claims: [paragraph], sources: src }, { now });
    expect(asClaim.report.claims.map((c: any) => [c.text, c.verdict, c.split_from])).toEqual([
      ["Community solar subscriptions grew 20 percent in 2025.", "supported", "claims[0]"],
      ["Every renter in Ontario now has rooftop solar.", "unsupported", "claims[0]"],
    ]);
    expect(asClaim.report.summary).toEqual({ total: 2, supported: 1, unsupported: 1, needs_review: 0 });
    expect(asClaim.report.notes.join(" ")).toMatch(/1 input\(s\) contained several sentences/);
    // The same paragraph as top-level text, with a question that is skipped rather than judged.
    const asText = await agent.call("check_claims", { text: `${paragraph} Is that good news?`, sources: src }, { now });
    expect(asText.report.claims.map((c: any) => c.verdict)).toEqual(["supported", "unsupported"]);
    expect(asText.report.claims.every((c: any) => c.split_from === "text")).toBe(true);
    expect(asText.report.notes.join(" ")).toMatch(/Skipped 1 question\(s\) or fragment\(s\)/);
    // A citation on a multi-sentence object claim applies to each of its sentences.
    const cited = await agent.call("check_claims", { claims: [{ text: paragraph, source_url: src[0].url }], sources: src }, { now });
    expect(cited.report.claims[0]).toMatchObject({ verdict: "supported", cited_source_matched: true });
    // Single-sentence claims are unchanged (no split_from).
    const single = await agent.call("check_claims", { claims: ["Community solar subscriptions grew 20 percent in 2025."], sources: src }, { now });
    expect(single.report.claims[0].split_from).toBeUndefined();
    await expect(agent.call("check_claims", { text: "Is this true?", sources: src }, { now })).rejects.toMatchObject({ code: "invalid_input" });
  });

  it("does not treat a figure for a different year as a contradiction", async () => {
    const out = await agent.call(
      "check_claims",
      { claims: ["Heat pump sales in Europe grew 20% in 2024."], sources: [{ url: "https://y.example.com/", text: "Heat pump sales in Europe grew 30% in 2025." }] },
      { now },
    );
    expect(out.report.claims[0].verdict).toBe("needs_review");
    expect(out.report.claims[0].reasons.join(" ")).toMatch(/different year or date/);
  });

  it("holds the invariant: every supported claim has a supporting excerpt and evidence", async () => {
    const claims = [
      "Heat pump sales in Europe grew 30% in 2025.",
      "Germany and France accounted for most new installations last year.",
      "The satellite launched on March 10, 2026 from Kourou.",
      "Heat pump sales in Europe grew 45% in 2025.",
      "Kourou is in French Guiana.",
    ];
    const r = await check(claims);
    expect(r.summary.total).toBe(5);
    for (const c of r.claims) {
      if (c.verdict === "supported") {
        expect(c.evidence.length).toBeGreaterThan(0);
        expect(c.excerpts.some((e: any) => e.relation === "supports" && c.evidence.includes(e.url))).toBe(true);
      }
    }
    expect(r.claims.map((c: any) => c.verdict)).toEqual(["supported", "supported", "supported", "unsupported", "unsupported"]);
  });

  it("rejects invalid input", async () => {
    await expect(agent.call("check_claims", { sources })).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("check_claims", { claims: ["x"] })).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("check_claims", { claims: [42], sources })).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("check_claims", { claims: ["  "], sources })).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("check_claims", { claims: ["x y z"], sources, support_threshold: 0.6, review_threshold: 0.9 })).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("check_claims", { claims: ["x y z"], sources: [{ url: "ftp://a.example.com/" }] })).rejects.toMatchObject({ code: "invalid_input" });
  });
});

describe("fact-check: permissions and MCP", () => {
  it("has no network, filesystem or credential permissions", async () => {
    expect(agent.manifest.permissions).toEqual({ filesystem: "none", network: [], credentials: [] });
    const fetch = vi.fn();
    await expect(makeContext(agent.manifest, { fetch }).fetch("https://energy.example.org/")).rejects.toMatchObject({ code: "network_denied" });
    expect(fetch).not.toHaveBeenCalled();
  });

  it("serves tools/list and tools/call over MCP", async () => {
    const list = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 1, method: "tools/list" });
    expect(list.result.tools.map((t: any) => t.name)).toEqual(["check_claims"]);
    const call = await handleMcpMessage(agent, {
      jsonrpc: "2.0",
      id: 2,
      method: "tools/call",
      params: { name: "check_claims", arguments: { claims: ["Heat pump sales in Europe grew 30% in 2025."], sources } },
    });
    expect(call.result.isError).toBe(false);
    expect(call.result.structuredContent.report.claims[0].verdict).toBe("supported");
    const bad = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "check_claims", arguments: { claims: [] } } });
    expect(bad.result.isError).toBe(true);
  });
});

Find an agent

Do not include passwords, keys, customer data or other confidential details.
Rule-based matching is free during the public beta (fair use).