papertrail/tests/paperExtract.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

82 lines
3.2 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { groundEffects } from "../lib/extraction/paperExtract";
import type { ExtractedEffect } from "../lib/extraction/schemas";
// Oracle test for the structured-extraction TRUST LAYER: no DB, no LLM. It proves
// the invariant that makes heavy full-paper Claude use safe — an effect whose quote
// is a real substring of the source survives (with correct offsets) and gets its
// number deterministically reconciled, while an effect whose quote is fabricated is
// DROPPED. No unsourced number ever reaches the caller.
const RAW =
"The primary endpoint occurred in 9.4% of the treatment group and 12.5% of the " +
"placebo group (hazard ratio 0.74; 95% CI, 0.63 to 0.87; P<0.001), corresponding " +
"to a 26% relative risk reduction. All-cause mortality did not differ (HR 0.92; 95% CI, 0.79 to 1.08).";
function effect(over: Partial<ExtractedEffect>): ExtractedEffect {
return {
endpoint: "primary composite",
measure: "HR",
point: 0.74,
ci_low: 0.63,
ci_high: 0.87,
is_percent: false,
quote: "hazard ratio 0.74; 95% CI, 0.63 to 0.87",
...over,
};
}
describe("groundEffects — grounding invariant", () => {
it("drops an effect whose quote is not a substring of the source", () => {
const fabricated = effect({
point: 0.5,
quote: "hazard ratio 0.50 for the fabricated endpoint",
});
const { effects, droppedCount } = groundEffects([fabricated], RAW);
expect(effects).toHaveLength(0);
expect(droppedCount).toBe(1);
});
it("keeps a verbatim effect with offsets that slice back to the located quote", () => {
const { effects, droppedCount } = groundEffects([effect({})], RAW);
expect(droppedCount).toBe(0);
expect(effects).toHaveLength(1);
const kept = effects[0];
// The stored quote is the VERBATIM located substring, and the offsets prove it.
expect(RAW.slice(kept.grounding.start, kept.grounding.end)).toBe(kept.quote);
expect(kept.quote).toContain("hazard ratio 0.74");
});
});
describe("groundEffects — deterministic number reconciliation", () => {
it("CONFIRMS a number that the deterministic parser re-reads from the grounded quote", () => {
const { effects } = groundEffects([effect({})], RAW);
const kept = effects[0];
expect(kept.reconciliation).toBe("confirmed");
expect(kept.parsed_point).toBe(0.74);
});
it("flags a MISMATCH when Claude's number disagrees with the grounded quote", () => {
// The quote is real (grounds fine), but Claude's reported point (0.55) does not
// match the number the deterministic parser reads from that same quote (0.74).
const lying = effect({ point: 0.55 });
const { effects } = groundEffects([lying], RAW);
expect(effects).toHaveLength(1);
expect(effects[0].reconciliation).toBe("mismatch");
expect(effects[0].parsed_point).toBe(0.74);
});
it("marks UNVERIFIED when the grounded quote has no re-parseable number", () => {
const prose = effect({
measure: "unknown",
point: null,
ci_low: null,
ci_high: null,
quote: "of the treatment group and 12.5% of the",
});
const { effects } = groundEffects([prose], RAW);
expect(effects).toHaveLength(1);
expect(effects[0].reconciliation).toBe("unverified");
});
});