SPB Git

Repository Public Research preview

live.test.mjs 11.8 KB 191 lines javascript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : LLM API toolkits8 * File             : scholarly/mcp/test/live.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// Live tests: the real Crossref, OpenAlex, arXiv and doi.org, through the MCP protocol over stdio.43// Run with `npm run test:live`. They need the network and no account. OpenAlex is called sparingly:44// without a key its daily budget is small and shared per IP address, so those tests skip (not fail)45// when the budget is exhausted.46import assert from "node:assert/strict";47import { mkdtempSync, symlinkSync, writeFileSync } from "node:fs";48import { tmpdir } from "node:os";49import { dirname, join } from "node:path";50import { after, before, test } from "node:test";51import { fileURLToPath } from "node:url";52import { Client } from "@modelcontextprotocol/sdk/client/index.js";53import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";5455const root = dirname(dirname(fileURLToPath(import.meta.url)));56const work = mkdtempSync(join(tmpdir(), "scholarly-live-"));57const 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";58const TITLE = "Generalized autoregressive conditional heteroskedasticity";59let client;6061async function connect(entry = join(root, "server.mjs")) {62  const c = new Client({ name: "live-test", version: "1.0.0" });63  const env = { PATH: process.env.PATH, SCHOLARLY_ROOT: work };64  if (process.env.OPENALEX_API_KEY) env.OPENALEX_API_KEY = process.env.OPENALEX_API_KEY;65  await c.connect(new StdioClientTransport({ command: process.execPath, args: [entry], env, stderr: "pipe" }));66  return c;67}68async function call(name, args = {}, c = client) {69  const r = await c.callTool({ name, arguments: args }, undefined, { timeout: 180_000 });70  return { data: r.structuredContent, isError: r.isError === true };71}72/** OpenAlex tests skip when the shared key-less budget is used up: that is a property of the network, not of the code. */73async function openalex(t, name, args) {74  const r = await call(name, args);75  if (r.isError && r.data.error.code === "rate_limited") { t.skip("OpenAlex key-less budget exhausted for this IP address"); return null; }76  assert.equal(r.isError, false, JSON.stringify(r.data));77  return r.data;78}7980before(async () => { client = await connect(); });81after(async () => { await client?.close(); });8283test("a correct citation is verified", async () => {84  const { data } = await call("verify_doi", { doi: `https://doi.org/${B}`, title: TITLE, authors: ["Bollerslev, Tim"], year: 1986, venue: "J. Econometrics", volume: "31", pages: "307-327" });85  assert.deepEqual([data.status, data.doi_resolves, data.verdict], ["verified", true, "match"]);86  assert.equal(data.work.venue, "Journal of Econometrics");87  assert.match(data._notice, /untrusted/);88});8990test("a real DOI with a wrong year is a mismatch, and a real DOI on another paper too", async () => {91  const y = (await call("verify_doi", { doi: B, title: TITLE, first_author: "Bollerslev", year: 1996 })).data;92  assert.deepEqual([y.status, y.mismatches, y.comparison.year.actual], ["metadata_mismatch", ["year"], 1986]);93  const o = (await call("verify_doi", { doi: B, title: "Attention is all you need", first_author: "Vaswani", year: 2017 })).data;94  assert.equal(o.status, "metadata_mismatch");95  assert.ok(o.mismatches.includes("title") && o.mismatches.includes("first_author"));96});9798test("a fabricated DOI is not found, on every registry", async () => {99  const { data, isError } = await call("verify_doi", { doi: FAKE, title: "A study that does not exist", year: 2021 });100  assert.equal(isError, false);101  assert.deepEqual([data.status, data.doi_resolves], ["not_found", false]);102  const g = await call("get_work", { id: FAKE });103  assert.deepEqual([g.isError, g.data.error.code], [true, "not_found"]);104  const b = await call("bibtex_from_doi", { doi: FAKE });105  assert.equal(b.data.error.code, "not_found");106});107108test("a retracted article is flagged, with the notice", async () => {109  const v = (await call("verify_doi", { doi: W, title: "Ileal-lymphoid-nodular hyperplasia, non-specific colitis, and pervasive developmental disorder in children", first_author: "Wakefield", year: 1998 })).data;110  assert.deepEqual([v.status, v.retraction.status], ["retracted", "retracted"]);111  assert.ok(v.retraction.notices.some((n) => n.type === "retraction" && n.doi === "10.1016/s0140-6736(10)60175-4"));112  const r = (await call("check_retractions", { dois: [W, B, FAKE], use_openalex: false })).data;113  assert.deepEqual(r.results.map((x) => x.status), ["retracted", "no_notice", "not_found"]);114  assert.deepEqual(r.flagged, [W]);115});116117test("references without a DOI: found when they exist, not_found when invented", async () => {118  const found = (await call("resolve_reference", { reference: "Engle, R. F. (1982). Autoregressive conditional heteroscedasticity with estimates of the variance of United Kingdom inflation. Econometrica, 50(4), 987-1007." })).data;119  assert.deepEqual([found.status, found.confident, found.match.doi], ["found", true, "10.2307/1912773"]);120  const none = (await call("resolve_reference", { reference: "Smith, J. and Doe, A. (2021). Quantum hedonic regression of lunar real estate under stochastic cheese constraints. Journal of Imaginary Results, 12(3), 45-67." })).data;121  assert.deepEqual([none.status, none.confident], ["not_found", false]);122  assert.equal(none.match, undefined); // nothing is offered as "the" reference123});124125test("arXiv: lookup by id, by DOI, and search", async () => {126  const g = (await call("get_work", { id: "arXiv:1706.03762" })).data.work;127  assert.deepEqual([g.title, g.year, g.authors.length, g.type, g.source], ["Attention Is All You Need", 2017, 8, "preprint", "arxiv"]);128  const d = (await call("verify_doi", { doi: "10.48550/arXiv.1706.03762", title: "Attention is all you need", first_author: "Vaswani", year: 2017 })).data;129  assert.equal(d.status, "verified");130  const s = (await call("search_arxiv", { query: 'ti:"attention is all you need" AND au:vaswani', limit: 3 })).data;131  assert.ok(s.works.some((w) => w.arxiv_id === "1706.03762"), JSON.stringify(s.works.map((w) => w.title)));132  assert.ok(s.works[0].pdf_url.startsWith("https://arxiv.org/pdf/"));133});134135test("search, reference list, formatting and BibTeX from the registries", async () => {136  const s = (await call("search_works", { title: TITLE, author: "Bollerslev", from_year: 1986, to_year: 1986, limit: 5 })).data;137  assert.ok(s.works.some((w) => w.doi === B), JSON.stringify(s.works.map((w) => w.doi)));138  const refs = (await call("get_references", { id: B, limit: 30 })).data;139  assert.equal(refs.source, "crossref");140  assert.ok(refs.total >= 15 && refs.references.some((r) => r.first_author === "Engle"));141  const f = (await call("format_citation", { id: B, styles: ["apa", "bibtex"] })).data;142  assert.equal(f.citations.apa, `Bollerslev, T. (1986). ${TITLE}. Journal of Econometrics, 31(3), 307–327. https://doi.org/${B}`);143  assert.match(f.citations.bibtex, /^@article\{bollerslev1986generalized,/);144  const b = (await call("bibtex_from_doi", { doi: B })).data;145  assert.equal(b.source, "registry");146  assert.match(b.bibtex, /^@article\{[\s\S]*Bollerslev, Tim/);147});148149test("a small .bib file is audited entry by entry", async () => {150  writeFileSync(join(work, "refs.bib"), `151@article{bollerslev1986, author = {Bollerslev, Tim}, title = {Generalized Autoregressive Conditional Heteroskedasticity}, journal = {Journal of Econometrics}, year = {1986}, volume = {31}, pages = {307--327}, doi = {${B}}}152@article{bollerslev_wrong, author = {Bollerslev, Tim}, title = {Generalized Autoregressive Conditional Heteroskedasticity}, journal = {Journal of Econometrics}, year = {1996}, doi = {${B}}}153@article{engle1982, author = {Engle, Robert F.}, title = {Autoregressive Conditional Heteroscedasticity with Estimates of the Variance of {U}nited {K}ingdom Inflation}, journal = {Econometrica}, year = {1982}}154@article{ghost2021, author = {Smith, John and Doe, Alice}, title = {Quantum hedonic regression of lunar real estate under stochastic cheese constraints}, journal = {Journal of Imaginary Results}, year = {2021}, doi = {${FAKE}}}155@article{wakefield1998, author = {Wakefield, A. J.}, title = {Ileal-lymphoid-nodular hyperplasia, non-specific colitis, and pervasive developmental disorder in children}, journal = {The Lancet}, year = {1998}, doi = {${W}}}156@misc{vaswani2017, author = {Vaswani, Ashish and Shazeer, Noam}, title = {Attention Is All You Need}, year = {2017}, eprint = {1706.03762}, archivePrefix = {arXiv}}157`);158  const p = (await call("parse_bibtex", { path: "refs.bib" })).data;159  assert.deepEqual([p.entries_total, p.without_doi], [6, ["engle1982", "vaswani2017"]]);160  const { data } = await call("verify_bibliography", { path: "refs.bib" });161  const st = Object.fromEntries(data.results.map((r) => [r.key, r.status]));162  assert.deepEqual(st, { bollerslev1986: "verified", bollerslev_wrong: "metadata_mismatch", engle1982: "doi_missing_found", ghost2021: "doi_not_found", wakefield1998: "retracted", vaswani2017: "verified" });163  const by = Object.fromEntries(data.results.map((r) => [r.key, r]));164  assert.equal(by.engle1982.suggested_doi, "10.2307/1912773");165  assert.equal(by.ghost2021.suggested_doi, undefined);166  assert.deepEqual(data.needs_attention, ["bollerslev_wrong", "ghost2021", "wakefield1998"]);167  assert.deepEqual(data.duplicates, [{ doi: B, keys: ["bollerslev1986", "bollerslev_wrong"] }]);168});169170test("OpenAlex: enrichment and citing works (two small budget units)", async (t) => {171  const g = await openalex(t, "get_work", { id: B, enrich: true });172  if (!g) return;173  if (g.work.enrichment_error) { t.skip(`OpenAlex enrichment unavailable: ${g.work.enrichment_error.code}`); return; }174  assert.equal(g.work.ids.openalex, "W1999996900");175  assert.ok(g.work.citation_count > 10000);176  const c = await openalex(t, "get_citations", { id: B, limit: 3 });177  if (!c) return;178  assert.ok(c.total_citing > 10000);179  assert.equal(c.citing_works.length, 3);180  const s = (await call("scholarly_status")).data;181  assert.equal(typeof s.openalex_budget.remaining_usd, "number");182  assert.equal(s.config.mailto_sent, false);183});184185test("the server starts through a symlinked path", async () => {186  const link = join(work, "linked-server.mjs");187  symlinkSync(join(root, "server.mjs"), link);188  const c = await connect(link);189  try { assert.equal((await c.listTools()).tools.length, 16); assert.equal((await call("verify_doi", { doi: B, title: TITLE }, c)).data.status, "verified"); } finally { await c.close(); }190});191
Files main
Menu

Theme