SPB Git

Repository Public Research preview

offline.test.mjs 36.1 KB 493 lines javascript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : LLM API toolkits8 * File             : scholarly/mcp/test/offline.test.mjs9 * Module           : scholarly10 * Repository       : https://git.spboucher.ai/llm-api-toolkits11 * Created          : 2026-11-1012 * Last Modified    : 2026-11-1013 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: dev@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Offline tests: no network. Registry answers come from recorded fixtures through an injected fetch.43import assert from "node:assert/strict";44import { mkdtempSync, readFileSync, symlinkSync, writeFileSync } from "node:fs";45import { tmpdir } from "node:os";46import { dirname, join } from "node:path";47import { test } from "node:test";48import { fileURLToPath } from "node:url";49import { Client } from "@modelcontextprotocol/sdk/client/index.js";50import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";51import { latexToText, parseBibtex, splitAuthors } from "../lib/bibtex.mjs";52import { bibtexKey, formatWork } from "../lib/format.mjs";53import { ScholarlyError, USER_AGENT, createHttp, mapLimit } from "../lib/http.mjs";54import { createEngine, parseId } from "../lib/index.mjs";55import { compareWork, judgeCandidate, parseReference, sameVenue, titleContainment } from "../lib/match.mjs";56import { extractArxivId, extractDoi, familyName, normalizeArxivId, normalizeDoi, sameFamily, titleSimilarity } from "../lib/normalize.mjs";57import { abstractFromInvertedIndex, workFromCrossref, workFromCsl, workFromOpenAlex, worksFromArxivFeed } from "../lib/sources.mjs";5859const root = dirname(dirname(fileURLToPath(import.meta.url)));60const fx = (f) => readFileSync(join(root, "test/fixtures", f), "utf8");61const B = "10.1016/0304-4076(86)90063-1", W = "10.1016/s0140-6736(97)11096-0", FAKE = "10.1234/this-doi-does-not-exist-zz9";62const TITLE = "Generalized autoregressive conditional heteroskedasticity";63const tmp = mkdtempSync(join(tmpdir(), "scholarly-off-"));64const code = async (fn) => { try { await fn(); return null; } catch (e) { return e.code ?? e.message; } };6566/** A stand-in for the four services. Records every request so tests can assert on what went out. */67function fakeNet({ failHosts = [] } = {}) {68  const calls = [];69  const res = (body, status = 200, headers = {}) => new Response(body, { status, headers });70  const fetchImpl = async (url, opts = {}) => {71    const u = new URL(url);72    calls.push({ url, host: u.host, headers: opts.headers ?? {} });73    if (failHosts.includes(u.host)) return res("down", 503);74    const p = decodeURIComponent(u.pathname);75    if (u.host === "api.crossref.org") {76      if (p === `/works/${B}`) return res(fx("crossref-work-bollerslev.json"));77      if (p === `/works/${W}`) return res(fx("crossref-work-retracted.json"));78      if (p === "/works") { const q = u.searchParams.get("query.bibliographic") ?? ""; return res(/heterosk/i.test(q) ? fx("crossref-search.json") : JSON.stringify({ message: { "total-results": 0, items: [] } })); }79      return res("Resource not found.", 404);80    }81    if (u.host === "api.openalex.org") {82      const h = { "x-ratelimit-remaining-usd": "0.0987", "x-ratelimit-limit-usd": "0.1" };83      if (p === `/works/doi:${B}`) return res(fx("openalex-work-bollerslev.json"), 200, h);84      if (p === "/works") return res(fx("openalex-search.json"), 200, h);85      return res("{}", 404, h);86    }87    if (u.host === "export.arxiv.org") return res(u.searchParams.get("id_list") === "1706.03762" ? fx("arxiv-attention.xml") : '<feed xmlns:opensearch="x"><opensearch:totalResults>0</opensearch:totalResults></feed>');88    if (u.host === "doi.org") {89      if (p === "/10.5555/datacite-only") return res(fx("csl-arxiv-attention.json"));90      return res("not found", 404);91    }92    return res("", 404);93  };94  return { calls, fetchImpl };95}96const engineWith = (net, env = {}) => createEngine({ env, fetchImpl: net.fetchImpl, httpOptions: { wait: async () => {} } });9798test("DOI normalisation: prefixes, case, trailing punctuation, brackets that belong to the DOI", () => {99  assert.equal(normalizeDoi("https://doi.org/10.1016/0304-4076(86)90063-1"), B);100  assert.equal(normalizeDoi("doi:10.1016/S0140-6736(97)11096-0."), W);101  assert.equal(normalizeDoi("http://dx.doi.org/10.1000/ABC.123;"), "10.1000/abc.123");102  assert.equal(normalizeDoi("10.1000/abc)"), "10.1000/abc"); // closing bracket of the sentence, not of the DOI103  assert.equal(normalizeDoi("10.1002/(SICI)1097-4571(199205)43:4<284::AID-ASI3>3.0.CO;2-0"), "10.1002/(sici)1097-4571(199205)43:4<284::aid-asi3>3.0.co;2-0");104  for (const bad of ["", "not a doi", "11.1000/x", "10.12/x", "10.1000/", null, 42]) assert.equal(normalizeDoi(bad), null);105  assert.equal(extractDoi(`See Bollerslev (1986, https://doi.org/${B}).`), B);106  assert.equal(extractDoi("no identifier here"), null);107});108109test("arXiv identifiers and the generic id parser", () => {110  assert.equal(normalizeArxivId("arXiv:1706.03762v7"), "1706.03762");111  assert.equal(normalizeArxivId("https://arxiv.org/pdf/1706.03762.pdf"), "1706.03762");112  assert.equal(normalizeArxivId("hep-th/9901001"), "hep-th/9901001");113  assert.equal(normalizeArxivId("1706"), null);114  assert.equal(extractArxivId("Vaswani et al., arXiv:1706.03762, 2017"), "1706.03762");115  assert.deepEqual(parseId("W1999996900"), { kind: "openalex", value: "W1999996900" });116  assert.deepEqual(parseId("pmid: 9500320"), { kind: "pmid", value: "9500320" });117  assert.deepEqual(parseId(`doi:${B}`), { kind: "doi", value: B });118  assert.equal(parseId("10.48550/arXiv.1706.03762").kind, "arxiv");119  assert.equal(parseId("1706.03762").value, "1706.03762");120  assert.throws(() => parseId("hello world"), (e) => e.code === "invalid_input");121});122123test("title similarity and family-name comparison", () => {124  assert.equal(titleSimilarity(TITLE, "Generalized Autoregressive Conditional Heteroskedasticity."), 1);125  assert.equal(titleSimilarity("RETRACTED: Ileal-lymphoid-nodular hyperplasia", "Ileal lymphoid nodular hyperplasia"), 1);126  assert.ok(titleSimilarity("Attention is all you need", "Attention Is All You Need: A Retrospective Study") >= 0.9); // title plus subtitle127  assert.ok(titleSimilarity(TITLE, "Fractionally integrated generalized autoregressive conditional heteroskedasticity") < 0.9);128  assert.equal(titleSimilarity(TITLE, "Expected stock returns and volatility"), 0);129  assert.equal(titleSimilarity("", TITLE), 0);130  assert.equal(familyName("Tim Bollerslev"), "bollerslev");131  assert.equal(familyName("van der Waals, Johannes"), "van der waals");132  assert.equal(familyName("Johannes van der Waals"), "van der waals");133  assert.equal(familyName({ family: "Müller", given: "Jörg" }), "muller");134  assert.equal(sameFamily("Bollerslev, T.", "Tim Bollerslev B"), true); // noisy index data135  assert.equal(sameFamily("Müller", { family: "Muller" }), true);136  assert.equal(sameFamily("Engle", "Bollerslev"), false);137  assert.equal(sameFamily("", ""), false);138});139140test("BibTeX parser: macros, concatenation, nested braces, accents, authors, recovery after a broken entry", () => {141  const src = [142    '@string{je = "Journal of Econometrics"}',143    "% a comment line",144    "@comment{ignored = {x}}",145    "@article{bollerslev1986, author = {Bollerslev, Tim}, title = {Generalized {A}utoregressive Conditional Heteroskedasticity}, journal = je, year = 1986, volume = {31}, number = 3, pages = {307--327}, doi = {10.1016/0304-4076(86)90063-1}}",146    '@inproceedings{vaswani2017, author = "Ashish Vaswani and Noam Shazeer and {OpenAI Team} and others", title = "Attention Is " # "All You Need", booktitle = {NeurIPS}, year = {2017}, eprint = {1706.03762}, archivePrefix = {arXiv}}',147    "@book{gomez, author = {G{\\'o}mez, Aidan N. and M{\\\"u}ller, J\\\"org and van der Waals, J. D.}, title = {Caf\\'e \\& M\\\"usli: the \\textit{long} story}, year = {2001}, url = {https://doi.org/10.1000/XYZ}}",148    "@misc{broken, title = {No closing brace",149    "",150    "@article{after, title = {Still parsed}, year = {1999}}",151  ].join("\n");152  const { entries, errors } = parseBibtex(src);153  assert.deepEqual(entries.map((e) => e.key), ["bollerslev1986", "vaswani2017", "gomez"]);154  const [b, v, g] = entries;155  assert.equal(b.title, "Generalized Autoregressive Conditional Heteroskedasticity");156  assert.equal(b.venue, "Journal of Econometrics");157  assert.deepEqual([b.year, b.volume, b.issue, b.pages, b.doi, b.line], [1986, "31", "3", "307–327", B, 4]);158  assert.equal(v.title, "Attention Is All You Need");159  assert.deepEqual(v.authors.map((a) => a.family), ["Vaswani", "Shazeer", "OpenAI Team"]); // "others" dropped, corporate author kept whole160  assert.equal(v.arxiv, "1706.03762");161  assert.deepEqual(g.authors.map((a) => `${a.given} ${a.family}`), ["Aidan N. Gómez", "Jörg Müller", "J. D. van der Waals"]);162  assert.equal(g.title, "Café & Müsli: the long story");163  assert.equal(g.doi, "10.1000/xyz"); // taken from a doi.org URL164  assert.equal(errors.length, 1);165  assert.equal(errors[0].line, 7);166  // An unterminated entry swallows what follows it: the parser reports the line instead of inventing entries.167  assert.equal(parseBibtex("").entries.length, 0);168  assert.equal(latexToText("{\\o}rsted \\ss{} na\\\"{\\i}ve"), "ørsted ß naïve");169  assert.deepEqual(splitAuthors("Sandy Band and Andy Rand").map((a) => a.family), ["Band", "Rand"]); // "and" inside names170});171172test("registry answers map to one work shape", () => {173  const c = workFromCrossref(JSON.parse(fx("crossref-work-bollerslev.json")).message);174  assert.deepEqual([c.doi, c.title, c.year, c.venue, c.volume, c.issue, c.pages, c.type], [B, TITLE, 1986, "Journal of Econometrics", "31", "3", "307-327", "journal-article"]);175  assert.deepEqual([c.authors[0].family, c.authors[0].given, c.is_retracted], ["Bollerslev", "Tim", false]);176177  const r = workFromCrossref(JSON.parse(fx("crossref-work-retracted.json")).message);178  assert.equal(r.is_retracted, true);179  assert.equal(r.retraction.type, "retraction");180  assert.equal(r.retraction.doi, "10.1016/s0140-6736(10)60175-4");181  assert.ok(r.updates.some((u) => u.type === "correction"));182183  const o = workFromOpenAlex(JSON.parse(fx("openalex-work-bollerslev.json")));184  assert.deepEqual([o.doi, o.year, o.ids.openalex, o.is_retracted, o.pages], [B, 1986, "W1999996900", false, "307-327"]);185  assert.ok(o.citation_count > 10000);186  assert.equal(abstractFromInvertedIndex({ world: [1], hello: [0, 2] }), "hello world hello");187  assert.equal(abstractFromInvertedIndex(null), null);188189  const d = workFromCsl(JSON.parse(fx("csl-arxiv-attention.json")));190  assert.deepEqual([d.doi, d.title, d.year, d.authors.length, d.venue], ["10.48550/arxiv.1706.03762", "Attention Is All You Need", 2017, 8, "arXiv"]);191192  const a = worksFromArxivFeed(fx("arxiv-attention.xml")).works[0];193  assert.deepEqual([a.ids.arxiv, a.title, a.year, a.authors[0].name, a.authors.length, a.type], ["1706.03762", "Attention Is All You Need", 2017, "Ashish Vaswani", 8, "preprint"]);194  assert.equal(a.open_access.pdf_url, "https://arxiv.org/pdf/1706.03762");195  assert.ok(a.categories.includes("cs.CL"));196  assert.equal(worksFromArxivFeed("<feed></feed>").works.length, 0);197});198199test("compareWork: what counts as a match, a minor difference and a mismatch", () => {200  const w = workFromCrossref(JSON.parse(fx("crossref-work-bollerslev.json")).message);201  const ok = compareWork({ title: TITLE.toUpperCase(), authors: ["Bollerslev, T."], year: 1986, venue: "J. Econometrics", volume: "31", pages: "307--327" }, w);202  assert.equal(ok.verdict, "match");203  assert.equal(ok.fields.title.similarity, 1);204  const y1 = compareWork({ title: TITLE, year: 1987 }, w);205  assert.deepEqual([y1.verdict, y1.fields.year.status], ["minor_differences", "off_by_one"]);206  const y10 = compareWork({ title: TITLE, first_author: "Bollerslev", year: 1996 }, w);207  assert.deepEqual([y10.verdict, y10.mismatches], ["mismatch", ["year"]]);208  assert.deepEqual(compareWork({ title: TITLE, first_author: "Engle, R.", year: 1986 }, w).mismatches, ["first_author"]);209  assert.deepEqual(compareWork({ title: "Autoregressive conditional heteroscedasticity with estimates of the variance of United Kingdom inflation", year: 1986 }, w).mismatches, ["title"]);210  const extra = compareWork({ title: TITLE, authors: ["Bollerslev, T.", "Engle, R."], year: 1986 }, w);211  assert.deepEqual([extra.verdict, extra.fields.authors.not_in_record], ["mismatch", ["Engle, R."]]);212  assert.equal(compareWork({ title: TITLE, year: 1986, venue: "Econometrica" }, w).fields.venue.status, "differs");213  assert.equal(compareWork({ title: TITLE, year: 1986, pages: "300-320" }, w).warnings.includes("pages"), true);214  const two = { title: "T one two three", authors: [{ family: "Alpha", given: "A" }, { family: "Beta", given: "B" }], year: 2000 };215  assert.equal(compareWork({ title: "T one two three", first_author: "Beta", year: 2000 }, two).fields.first_author.status, "order_differs");216  assert.equal(sameVenue("J. Financ. Econ.", "Journal of Financial Economics"), true);217  assert.equal(sameVenue("JFE", "Journal of Financial Economics"), true);218  assert.equal(sameVenue("Journal of Finance", "Journal of Financial Economics"), false);219  assert.equal(sameVenue("", "x"), null);220});221222test("free-text references are split into fields, in the common styles", () => {223  const apa = parseReference("Engle, R. F. (1982). Autoregressive conditional heteroscedasticity with estimates of the variance of United Kingdom inflation. Econometrica, 50(4), 987-1007.");224  assert.deepEqual([apa.first_author, apa.year, apa.title], ["Engle", 1982, "Autoregressive conditional heteroscedasticity with estimates of the variance of United Kingdom inflation"]);225  const van = parseReference("[12] Vaswani A, Shazeer N, Parmar N, et al. Attention is all you need. Advances in Neural Information Processing Systems. 2017.");226  assert.deepEqual([van.first_author_family, van.year, van.title], ["vaswani", 2017, "Attention is all you need"]);227  const chi = parseReference(`Bollerslev, Tim. 1986. “Generalized Autoregressive Conditional Heteroskedasticity.” Journal of Econometrics 31 (3): 307–27. https://doi.org/${B}.`);228  assert.deepEqual([chi.title, chi.doi, chi.first_author], ["Generalized Autoregressive Conditional Heteroskedasticity", B, "Bollerslev"]);229  assert.equal(parseReference("See arXiv:1706.03762 for details").arxiv, "1706.03762");230  assert.equal(parseReference("").title, null);231  assert.equal(titleContainment("Bollerslev T. Generalized autoregressive conditional heteroskedasticity. J Econom 1986", TITLE), 1);232});233234test("a search hit is accepted only with agreeing title and author or year; a reprint is not the original", () => {235  const items = JSON.parse(fx("crossref-search.json")).message.items.map(workFromCrossref);236  const claim = { title: TITLE, first_author: "Bollerslev", year: 1986 };237  const j = items.map((w) => judgeCandidate(claim, w));238  assert.deepEqual(j.map((x) => x.confident), [true, false, false]);239  assert.equal(j[1].year_agrees, false); // "Reprint of: …" (2023)240  assert.equal(judgeCandidate({ title: TITLE }, items[0]).confident, false); // title alone is not enough241  assert.equal(judgeCandidate({ title: TITLE, first_author: "Engle", year: 1986 }, items[0]).confident, false); // author contradicts242  assert.equal(judgeCandidate({ raw: "Bollerslev T. Generalized autoregressive conditional heteroskedasticity. J Econom. 1986;31:307-327." }, items[0]).confident, true);243  assert.equal(judgeCandidate({ title: "Introduction", year: 1986 }, { title: "Introduction", authors: [{ family: "Someone" }], year: 1986 }).confident, false); // generic title needs author and year244});245246test("formatters produce exact, stable output", () => {247  const w = workFromCrossref(JSON.parse(fx("crossref-work-bollerslev.json")).message);248  assert.equal(formatWork(w, "apa"), `Bollerslev, T. (1986). ${TITLE}. Journal of Econometrics, 31(3), 307–327. https://doi.org/${B}`);249  assert.equal(formatWork(w, "chicago"), `Bollerslev, Tim. 1986. “${TITLE}.” Journal of Econometrics 31 (3): 307–327. https://doi.org/${B}.`);250  assert.equal(formatWork(w, "mla"), `Bollerslev, Tim. “${TITLE}.” Journal of Econometrics, vol. 31, no. 3, 1986, pp. 307–327. https://doi.org/${B}.`);251  assert.equal(bibtexKey(w), "bollerslev1986generalized");252  assert.equal(formatWork(w, "bibtex"), `@article{bollerslev1986generalized,\n  author = {Bollerslev, Tim},\n  title = {${TITLE}},\n  journal = {Journal of Econometrics},\n  year = {1986},\n  volume = {31},\n  number = {3},\n  pages = {307--327},\n  doi = {${B}}\n}`);253  assert.deepEqual(parseBibtex(formatWork(w, "bibtex")).entries[0].doi, B); // our BibTeX reads back254  assert.match(formatWork(w, "ris"), /^TY {2}- JOUR\nAU {2}- Bollerslev, Tim\n[\s\S]*SP {2}- 307\nEP {2}- 327\n[\s\S]*ER {2}- $/);255  assert.deepEqual(formatWork(w, "csl-json").author, [{ family: "Bollerslev", given: "Tim" }]);256  const many = { title: "A & B: 50% of_cases", authors: [{ name: "Ashish Vaswani" }, { name: "Jean-Pierre de la Cruz" }, { name: "Plato" }], year: 2017, type: "preprint", ids: { arxiv: "1706.03762" } };257  assert.equal(formatWork(many, "apa"), "Vaswani, A., de la Cruz, J.-P., & Plato. (2017). A & B: 50% of_cases.");258  assert.match(formatWork(many, "bibtex"), /@misc\{vaswani2017b,\n {2}author = \{Vaswani, Ashish and de la Cruz, Jean-Pierre and \{Plato\}\},\n {2}title = \{A \\& B: 50\\% of\\_cases\}/);259  assert.equal(formatWork({ title: "Untitled?", authors: [] }, "apa"), "(n.d.). Untitled?");260  assert.throws(() => formatWork(w, "harvard"), /Unknown style/);261});262263test("HTTP layer: generic client name, retries, no retry on a long Retry-After, cache, errors", async () => {264  let n = 0;265  const seen = [];266  const http = createHttp({ wait: async () => {}, fetchImpl: async (url, o) => { seen.push(o.headers); n++; return n < 3 ? new Response("busy", { status: 503 }) : new Response('{"ok":true}', { status: 200 }); } });267  assert.deepEqual(await http.get("https://api.crossref.org/works/x"), { ok: true });268  assert.equal(n, 3);269  assert.equal(http.stats.retries, 2);270  assert.equal(seen[0]["User-Agent"], USER_AGENT);271  assert.match(USER_AGENT, /^scholarly-mcp\/\d+\.\d+$/); // no name, no address, no host name272  await http.get("https://api.crossref.org/works/x");273  assert.deepEqual([n, http.stats.cache_hits], [3, 1]);274275  let m = 0;276  const limited = createHttp({ wait: async () => {}, fetchImpl: async () => { m++; return new Response("slow down", { status: 429, headers: { "retry-after": "3600" } }); } });277  assert.equal(await code(() => limited.get("https://api.openalex.org/works?x=1")), "rate_limited");278  assert.equal(m, 1);279  const always = createHttp({ wait: async () => {}, retries: 2, fetchImpl: async () => new Response("x", { status: 500 }) });280  assert.equal(await code(() => always.get("https://doi.org/10.1/x")), "upstream_error");281  const nf = createHttp({ fetchImpl: async () => new Response("x", { status: 404 }) });282  await assert.rejects(() => nf.get("https://doi.org/10.1/x", { notFoundMessage: "nope" }), (e) => e instanceof ScholarlyError && e.code === "not_found" && e.message === "nope");283  const bad = createHttp({ fetchImpl: async () => new Response("<html>", { status: 200 }) });284  assert.equal(await code(() => bad.get("https://api.crossref.org/works/y")), "upstream_error");285  const hang = createHttp({ timeoutMs: 20, retries: 0, fetchImpl: (u, o) => new Promise((_, rej) => o.signal.addEventListener("abort", () => rej(Object.assign(new Error("aborted"), { name: "AbortError" })))) });286  assert.equal(await code(() => hang.get("https://api.crossref.org/works/z")), "timeout");287  const down = createHttp({ wait: async () => {}, retries: 1, fetchImpl: async () => { throw new TypeError("fetch failed"); } });288  assert.equal(await code(() => down.get("https://api.crossref.org/works/q")), "upstream_error");289  assert.deepEqual(await mapLimit([3, 1, 2], 2, async (x) => x * 2), [6, 2, 4]);290});291292test("nothing personal is sent unless the operator opts in", async () => {293  const net = fakeNet(), e = engineWith(net, { USER: "someone", HOME: "/Users/someone", EMAIL: "someone@example.org" });294  await e.verifyDoi({ doi: B, title: TITLE });295  await e.getWork(B, { enrich: true });296  await e.getWork("1706.03762");297  await code(() => e.verifyDoi({ doi: FAKE, title: "x y z" }));298  assert.ok(net.calls.length >= 4);299  for (const c of net.calls) {300    assert.equal(c.headers["User-Agent"], USER_AGENT);301    assert.doesNotMatch(c.url, /mailto|api_key|someone|example\.org/);302    assert.deepEqual(Object.keys(c.headers).sort(), ["Accept", "User-Agent"]);303  }304  assert.equal(e.status().config.mailto_sent, false);305306  const net2 = fakeNet(), e2 = engineWith(net2, { SCHOLARLY_MAILTO: "ops@example.org", OPENALEX_API_KEY: "k-123" });307  await e2.getWork(B, { enrich: true });308  await e2.getWork("1706.03762");309  await code(() => e2.verifyDoi({ doi: FAKE }));310  const by = (h) => net2.calls.filter((c) => c.host === h).map((c) => c.url).join(" ");311  assert.match(by("api.crossref.org"), /mailto=ops%40example\.org/);312  assert.match(by("api.openalex.org"), /mailto=ops%40example\.org.*api_key=k-123/);313  assert.doesNotMatch(by("export.arxiv.org") + by("doi.org"), /mailto|api_key/); // only the two services that document these parameters314  assert.doesNotMatch(JSON.stringify(e2.status()), /k-123|ops@example\.org/); // status reports that they are set, not their values315  const e3 = engineWith(fakeNet(), { SCHOLARLY_MAILTO: "not an address" });316  assert.equal(e3.status().config.mailto_sent, false);317});318319test("verify_doi: verified, mismatch, retracted, not found, invalid, and unchecked", async () => {320  const net = fakeNet(), e = engineWith(net);321  const v = await e.verifyDoi({ doi: `https://doi.org/${B}`, title: TITLE, authors: ["Bollerslev, Tim"], year: 1986, venue: "Journal of Econometrics" });322  assert.deepEqual([v.status, v.doi_resolves, v.verdict, v.doi], ["verified", true, "match", B]);323  const m = await e.verifyDoi({ doi: B, title: TITLE, first_author: "Bollerslev", year: 1996 });324  assert.deepEqual([m.status, m.mismatches, m.comparison.year.actual], ["metadata_mismatch", ["year"], 1986]);325  const wrong = await e.verifyDoi({ doi: B, title: "Attention is all you need", first_author: "Vaswani", year: 2017 }); // a real DOI pasted on another paper326  assert.deepEqual([wrong.status, wrong.mismatches], ["metadata_mismatch", ["title", "first_author", "year"]]);327  const r = await e.verifyDoi({ doi: W, title: "Ileal-lymphoid-nodular hyperplasia, non-specific colitis, and pervasive developmental disorder in children", year: 1998 });328  assert.deepEqual([r.status, r.retraction.status, r.verdict], ["retracted", "retracted", "match"]);329  const nf = await e.verifyDoi({ doi: FAKE, title: "Anything at all" });330  assert.deepEqual([nf.status, nf.doi_resolves], ["not_found", false]);331  assert.equal((await e.verifyDoi({ doi: "ten.1000/x" })).status, "invalid_doi");332  assert.equal((await e.verifyDoi({ doi: B })).status, "doi_resolves_unchecked"); // existence alone verifies nothing333  assert.equal((await e.verifyDoi({ doi: B, year: 1986 })).status, "doi_resolves_unchecked");334  const dc = await e.verifyDoi({ doi: "10.5555/datacite-only", title: "Attention is all you need", first_author: "Vaswani", year: 2017 }); // not in Crossref → doi.org335  assert.deepEqual([dc.status, dc.work.source], ["verified", "doi.org"]);336  const off = engineWith(fakeNet({ failHosts: ["api.crossref.org"] }));337  assert.equal(await code(() => off.verifyDoi({ doi: B, title: TITLE })), "upstream_error"); // an outage is an error, never "not found"338});339340test("resolve_reference: confident match, DOI repair suggestion, and honest not_found", async () => {341  const e = engineWith(fakeNet());342  const r = await e.resolveReference({ reference: "Bollerslev, T. (1986). Generalized autoregressive conditional heteroskedasticity. Journal of Econometrics, 31(3), 307-327." });343  assert.deepEqual([r.status, r.confident, r.match.doi, r.alternatives.length], ["found", true, B, 0]);344  const viaDoi = await e.resolveReference({ reference: `Bollerslev T. Generalized autoregressive conditional heteroskedasticity. J Econom. 1986. doi:${B}` });345  assert.deepEqual([viaDoi.status, viaDoi.via], ["found", "doi"]);346  const badDoi = await e.resolveReference({ title: TITLE, first_author: "Bollerslev", year: 1986, doi: FAKE });347  assert.deepEqual([badDoi.status, badDoi.match.doi, badDoi.claimed_doi.status], ["found", B, "not_found"]);348  const none = await e.resolveReference({ reference: "Smith, J. (2021). Quantum hedonic regression of lunar real estate under stochastic cheese constraints. Journal of Imaginary Results, 12(3), 45-67." });349  assert.deepEqual([none.status, none.confident, none.candidates.length], ["not_found", false, 0]);350  assert.match(none.notes.join(" "), /not proof of fabrication/);351  const amb = await e.resolveReference({ title: "Generalized autoregressive conditional heteroskedasticity models", first_author: "Zzyzx", year: 1950 });352  assert.deepEqual([amb.status, amb.confident], ["ambiguous", false]); // similar titles exist, nothing agrees: listed, not chosen353  const ax = await e.resolveReference({ reference: "Vaswani et al. (2017). Attention is all you need. arXiv:1706.03762" });354  assert.deepEqual([ax.status, ax.via, ax.match.arxiv_id], ["found", "arxiv", "1706.03762"]);355  assert.equal(await code(() => e.resolveReference({})), "invalid_input");356});357358const BIB = `359@article{ok, author = {Bollerslev, Tim}, title = {Generalized autoregressive conditional heteroskedasticity}, journal = {Journal of Econometrics}, year = {1986}, doi = {${B}}}360@article{wrongyear, author = {Bollerslev, Tim}, title = {Generalized autoregressive conditional heteroskedasticity}, year = {1996}, doi = {${B}}}361@article{nodoi, author = {Bollerslev, Tim}, title = {Generalized autoregressive conditional heteroskedasticity}, year = {1986}}362@article{fakedoi, author = {Bollerslev, Tim}, title = {Generalized autoregressive conditional heteroskedasticity}, year = {1986}, doi = {${FAKE}}}363@article{invented, author = {Smith, John}, title = {Quantum hedonic regression of lunar real estate}, year = {2021}, doi = {10.1234/also-invented}}364@article{retracted, author = {Wakefield, A. J.}, title = {Ileal-lymphoid-nodular hyperplasia, non-specific colitis, and pervasive developmental disorder in children}, year = {1998}, doi = {${W}}}365@misc{empty, note = {no title}}366@misc{preprint, author = {Vaswani, Ashish}, title = {Attention Is All You Need}, year = {2017}, eprint = {1706.03762}, archivePrefix = {arXiv}}367@book{ghost, author = {Nobody, N.}, title = {A monograph that no registry lists}, year = {1975}}368@article{broken, title = {never closed369`;370371test("verify_bibliography gives every entry one status and never upgrades a failure", async () => {372  const e = engineWith(fakeNet());373  const r = await e.verifyBibliography({ bibtex: BIB });374  const st = Object.fromEntries(r.results.map((x) => [x.key, x.status]));375  assert.deepEqual(st, { ok: "verified", wrongyear: "metadata_mismatch", nodoi: "doi_missing_found", fakedoi: "doi_not_found", invented: "doi_not_found", retracted: "retracted", empty: "unverifiable", preprint: "verified", ghost: "not_found" });376  const by = Object.fromEntries(r.results.map((x) => [x.key, x]));377  assert.equal(by.nodoi.suggested_doi, B);378  assert.equal(by.fakedoi.suggested_doi, B);379  assert.equal(by.invented.suggested_doi, undefined);380  assert.deepEqual(by.wrongyear.mismatches, ["year"]);381  assert.equal(by.wrongyear.comparison.year.actual, 1986);382  assert.equal(r.entries_total, 9);383  assert.equal(r.parse_errors.length, 1);384  assert.deepEqual(r.summary, { verified: 2, metadata_mismatch: 1, doi_missing_found: 1, doi_not_found: 2, retracted: 1, unverifiable: 1, not_found: 1 });385  assert.deepEqual(r.needs_attention, ["wrongyear", "fakedoi", "invented", "retracted", "empty", "ghost"]);386  assert.deepEqual(r.duplicates, [{ doi: B, keys: ["ok", "wrongyear", "nodoi", "fakedoi"] }]);387388  const some = await e.verifyBibliography({ bibtex: BIB, keys: ["ok", "ghost"], only_problems: true });389  assert.deepEqual([some.entries_checked, some.results.map((x) => x.key)], [2, ["ghost"]]);390  const paged = await e.verifyBibliography({ bibtex: BIB, max_entries: 2 });391  assert.deepEqual([paged.truncated, paged.next_offset, paged.results.length], [true, 2, 2]);392  const list = await e.verifyBibliography({ references: ["Bollerslev, T. (1986). Generalized autoregressive conditional heteroskedasticity. Journal of Econometrics, 31(3), 307-327.", { key: "x", doi: B, title: TITLE, year: 1986 }] });393  assert.deepEqual(list.results.map((x) => [x.key, x.status]), [["ref1", "doi_missing_found"], ["x", "verified"]]);394395  const down = engineWith(fakeNet({ failHosts: ["api.crossref.org"] }));396  const d = await down.verifyBibliography({ bibtex: BIB, keys: ["ok", "nodoi"] });397  assert.deepEqual(d.results.map((x) => x.status), ["error", "error"]); // registry down: "error", not "not_found"398  assert.equal(d.results[0].error.code, "upstream_error");399  assert.equal(await code(() => e.verifyBibliography({})), "invalid_input");400  assert.equal(await code(() => e.verifyBibliography({ bibtex: BIB, references: ["x"] })), "invalid_input");401});402403test("retractions, citation graph and open access through the fake registries", async () => {404  const net = fakeNet(), e = engineWith(net);405  const r = await e.checkRetractions({ dois: [W, B, FAKE, "nonsense"], use_openalex: false });406  assert.deepEqual(r.results.map((x) => x.status), ["retracted", "no_notice", "not_found", "invalid_doi"]);407  assert.deepEqual(r.flagged, [W]);408  assert.equal(net.calls.filter((c) => c.host === "api.openalex.org").length, 0);409  const c = await e.getCitations({ id: B, limit: 3 });410  assert.equal(c.work.openalex_id, "W1999996900");411  assert.equal(c.citing_works.length, 3);412  assert.doesNotMatch(JSON.stringify(c), /upstream notice/); // registry "meta" text never reaches the caller413  assert.deepEqual(e.status().openalex_budget, { remaining_usd: 0.0987, limit_usd: 0.1 });414  const refs = await e.getReferences({ id: B, limit: 5 });415  assert.deepEqual([refs.source, refs.returned, refs.total > 5], ["crossref", 5, true]);416  const oa = await e.getOpenAccess({ id: "arXiv:1706.03762" });417  assert.deepEqual([oa.is_open_access, oa.best.pdf_url], [true, "https://arxiv.org/pdf/1706.03762"]);418  assert.equal(await code(() => e.getCitations({ id: FAKE })), "not_found");419  assert.equal(await code(() => e.checkRetractions({ dois: [] })), "invalid_input");420  assert.equal(await code(() => e.authorWorks({})), "invalid_input");421  assert.equal(await code(() => e.authorWorks({ orcid: "1234" })), "invalid_input");422  assert.equal(await code(() => e.searchWorks({})), "invalid_input");423  assert.equal(await code(() => e.formatCitation({ id: B, style: "harvard" })), "invalid_input");424  const f = await e.formatCitation({ work: { title: "Own notes", authors: ["Ada Lovelace"], year: 1843 }, style: "apa" });425  assert.deepEqual([f.citation, f.metadata_source], ["Lovelace, A. (1843). Own notes.", "caller"]);426  assert.match((await e.bibtexFromDoi({ doi: B, source: "generated" })).bibtex, /^@article\{bollerslev1986generalized,/);427  assert.equal(await code(() => e.bibtexFromDoi({ doi: FAKE })), "not_found");428});429430test("files are read only inside the allowed directory, and only bibliography formats", async () => {431  writeFileSync(join(tmp, "refs.bib"), BIB);432  writeFileSync(join(tmp, "refs.txt"), "Bollerslev, T. (1986). Generalized autoregressive conditional heteroskedasticity. Journal of Econometrics, 31(3), 307-327.\n\n# heading\n\nshort\n");433  writeFileSync(join(tmp, "secret.env"), "TOKEN=1");434  symlinkSync("/etc/hosts", join(tmp, "out.txt"));435  const outside = mkdtempSync(join(tmpdir(), "scholarly-out-"));436  writeFileSync(join(outside, "other.bib"), "@misc{a, title={x}}");437  symlinkSync(join(outside, "other.bib"), join(tmp, "link.bib"));438  const e = engineWith(fakeNet(), { SCHOLARLY_ROOT: tmp });439  assert.equal(e.parseBibtexInput({ path: "refs.bib" }).entries_total, 9);440  assert.equal(e.parseBibtexInput({ path: join(tmp, "refs.bib") }).without_doi.length, 4);441  assert.deepEqual((await e.verifyBibliography({ path: "refs.txt" })).results.map((x) => x.status), ["doi_missing_found"]);442  assert.equal(await code(() => e.parseBibtexInput({ path: "../etc/passwd" })), "not_found");443  assert.equal(await code(() => e.parseBibtexInput({ path: "/etc/hosts" })), "path_not_allowed");444  assert.equal(await code(() => e.parseBibtexInput({ path: join(outside, "other.bib") })), "path_not_allowed");445  assert.equal(await code(() => e.parseBibtexInput({ path: "link.bib" })), "path_not_allowed"); // symlink leaving the root446  assert.equal(await code(() => e.parseBibtexInput({ path: "out.txt" })), "path_not_allowed");447  assert.equal(await code(() => e.parseBibtexInput({ path: "secret.env" })), "path_not_allowed");448  assert.equal(await code(() => e.parseBibtexInput({ path: "missing.bib" })), "not_found");449  assert.equal(await code(() => e.parseBibtexInput({})), "invalid_input");450});451452test("MCP protocol over stdio: tools, annotations, manifest agreement, offline tools, symlinked start", async () => {453  const connect = async (entry) => { const c = new Client({ name: "offline-test", version: "1.0.0" }); await c.connect(new StdioClientTransport({ command: process.execPath, args: [entry], env: { PATH: process.env.PATH, SCHOLARLY_ROOT: tmp }, stderr: "pipe" })); return c; };454  const c = await connect(join(root, "server.mjs"));455  try {456    const { tools } = await c.listTools();457    const manifest = JSON.parse(readFileSync(join(root, "agent-registry.json"), "utf8"));458    assert.deepEqual(tools.map((t) => t.name).sort(), manifest.mcp.tools.map((t) => t.name).sort());459    assert.equal(tools.length, 16);460    for (const t of tools) {461      assert.ok(t.description.length > 40, t.name);462      assert.equal(t.annotations.readOnlyHint, true, t.name); // every tool only reads463      assert.equal(t.annotations.destructiveHint, false, t.name);464      assert.equal(typeof t.annotations.openWorldHint, "boolean", t.name);465      assert.ok(t.title && t.inputSchema && t.outputSchema, t.name);466    }467    assert.match(c.getInstructions(), /untrusted data/);468    assert.equal(c.getServerVersion().version, manifest.version);469    assert.equal(JSON.parse(readFileSync(join(root, "package.json"), "utf8")).version, manifest.version);470471    const s = await c.callTool({ name: "scholarly_status", arguments: {} });472    assert.equal(s.isError, undefined);473    assert.equal(s.structuredContent.config.mailto_sent, false);474    assert.equal(s.structuredContent.http.requests, 0);475    assert.deepEqual(JSON.parse(s.content[0].text), s.structuredContent);476    const p = await c.callTool({ name: "parse_bibtex", arguments: { path: "refs.bib" } });477    assert.equal(p.structuredContent.entries_total, 9);478    assert.match(p.structuredContent._notice, /untrusted/);479    const bad = await c.callTool({ name: "parse_bibtex", arguments: { path: "/etc/hosts" } });480    assert.equal(bad.isError, true);481    assert.equal(bad.structuredContent.error.code, "path_not_allowed");482    const inv = await c.callTool({ name: "get_work", arguments: { id: "not an identifier" } });483    assert.equal(inv.structuredContent.error.code, "invalid_input");484    const schema = await c.callTool({ name: "verify_doi", arguments: { doi: 42 } }).catch((e) => ({ thrown: String(e) }));485    assert.ok(schema.thrown || schema.isError); // wrong argument types are rejected by the schema486  } finally { await c.close(); }487488  const link = join(tmp, "linked-server.mjs");489  symlinkSync(join(root, "server.mjs"), link);490  const c2 = await connect(link);491  try { assert.equal((await c2.listTools()).tools.length, 16); } finally { await c2.close(); }492});493
Files main
Menu

Theme