papertrail/tests/citationsClassify.test.ts
caelum0x 174222bcaa feat: Builder-Track named-user tools, bio + evidence-intelligence platform, Apache-2.0
Builds PaperTrail from the base skeleton into a full evidence-verification platform
for the "Built with Claude: Life Sciences" hackathon (Builder Track).

New named-user features (Build Beyond the Bench):
- Lab Notebook Companion — rough bench notes -> structured, searchable, grounded
  experiment records (lib/labNotebook, /console/lab-notebook, migration 0059)
- Clinical Trial Matcher — de-identified patient notes -> ranked eligible
  ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning
  (lib/trialMatcher, /console/trial-matcher, migrations 0060)

Platform:
- Deterministic verification moat (biostats recompute, exact-span grounding)
- 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar,
  PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier)
- Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise
  governance (RBAC, /api/v1 gateway, metering, audit chain, retention)
- 17 OSS projects assimilated into native TypeScript on our stack (backend/engines
  retained + credited in NOTICE)
- Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site
- Next.js 16 upgrade, benchmark harness (honest smoke-test caveat)

Licensing: Apache-2.0 (LICENSE + NOTICE).
2026-07-10 16:23:13 +03:00

83 lines
3.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
// Oracle test for smart-citation classification. We stub @/lib/claude so no network
// / API key is needed, and assert the two invariants that make heavy Claude use safe:
// (1) STANCE — the classified stance from Claude is surfaced to the caller.
// (2) GROUNDING — a context sentence that is a real substring of the citing text
// survives with correct offsets and the VERBATIM located text; a fabricated
// sentence is DROPPED (status "ungroundable"), never asserted.
const callClaudeForJson = vi.fn();
vi.mock("@/lib/claude", () => ({
callClaudeForJson: (...args: unknown[]) => callClaudeForJson(...args),
CLAUDE_MODEL: "test-model",
}));
import { classifyCitation, groundCitationClassification } from "@/lib/citations/classify";
const CITING =
"Amyloid-lowering therapies have shown clinical benefit. In contrast, our cohort found no significant mortality reduction and we were unable to replicate the reported 30% effect.";
beforeEach(() => {
callClaudeForJson.mockReset();
});
describe("classifyCitation", () => {
it("surfaces the stance and grounds a verbatim context sentence with correct offsets", async () => {
const quote =
"In contrast, our cohort found no significant mortality reduction and we were unable to replicate the reported 30% effect.";
callClaudeForJson.mockResolvedValueOnce({
stance: "contrasting",
context_sentence: quote,
reasoning: "The passage sets its own null result against the cited 30% effect.",
confidence: 0.86,
});
const outcome = await classifyCitation({
citing_text: CITING,
cited_claim: "The intervention reduced all-cause mortality by 30%.",
});
expect(outcome.status).toBe("classified");
if (outcome.status !== "classified") return;
expect(outcome.classification.stance).toBe("contrasting");
const { start, end } = outcome.classification.grounding;
// Offsets point at the real substring, and we return the verbatim located text.
expect(CITING.slice(start, end)).toBe(outcome.classification.context_sentence);
expect(outcome.classification.context_sentence).toContain("unable to replicate");
});
it("drops a fabricated context sentence as ungroundable rather than asserting it", async () => {
callClaudeForJson.mockResolvedValueOnce({
stance: "supporting",
context_sentence: "This passage strongly endorses the cited 30% mortality benefit.", // not in CITING
reasoning: "Fabricated — must not be grounded.",
confidence: 0.9,
});
const outcome = await classifyCitation({
citing_text: CITING,
cited_claim: "The intervention reduced all-cause mortality by 30%.",
});
expect(outcome.status).toBe("ungroundable");
});
});
describe("groundCitationClassification (pure trust layer)", () => {
it("recovers verbatim source text even when the model alters whitespace", () => {
const outcome = groundCitationClassification(CITING, {
stance: "contrasting",
// Whitespace-collapsed variant of a real sentence — grounding tier 2 recovers it.
context_sentence: "In contrast, our cohort found no significant mortality reduction",
reasoning: "r",
confidence: 0.5,
});
expect(outcome.status).toBe("classified");
if (outcome.status !== "classified") return;
const { start, end } = outcome.classification.grounding;
expect(CITING.slice(start, end)).toBe(outcome.classification.context_sentence);
});
});