#!/usr/bin/env python3 """Checks for the DOI route as a Persistence article (policy 2026-09-28): a paper submitted by its preprint DOI gets its own DOI under the Institute's prefix, linked to the preprint by a hasPreprint relation. Offline: no network, no mail; every write lands in one temporary directory that is removed at the end, pass or fail. Run from the repo root with the venv: .venv/bin/python intake/scripts/test_doi_route.py """ from __future__ import annotations import email as _email import email.policy as _policy import pathlib import shutil import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT)) failures: list[str] = [] def check(cond: bool, name: str) -> None: print((" ok " if cond else " FAIL ") + name) if not cond: failures.append(name) def eml_text(p: Path) -> str: msg = _email.message_from_bytes(p.read_bytes(), policy=_policy.default) return "".join(part.get_payload(decode=True).decode("utf-8", "replace") for part in msg.walk() if part.get_content_type() == "text/plain") tmp = Path(tempfile.mkdtemp(prefix="doiroute-")) real_home = pathlib.Path.home try: import crossref_deposit as cd import publications print("1. the preprint's DOI") pd = cd.preprint_doi check(pd({"source": "doi", "doi": "10.5281/zenodo.20211868"}) == "10.5281/zenodo.20211868", "Zenodo DOI kept") check(pd({"source": "doi", "doi": "2401.12345v2"}) == "10.48550/arXiv.2401.12345", "arXiv id -> arXiv DOI, version dropped") check(pd({"source": "doi", "doi": "10.48550/arXiv.2401.12345"}) == "10.48550/arXiv.2401.12345", "arXiv DOI kept") check(pd({"source": "doi", "doi": "https://doi.org/10.5281/zenodo.1"}) == "10.5281/zenodo.1", "resolver prefix stripped") check(pd({"source": "upload", "doi": ""}) is None, "upload route has no preprint") check(pd({"source": "doi", "doi": "not a doi"}) is None, "garbage is no preprint") print("2. the deposit") base = {"sub_id": "ICSAC-SUB-00099", "title": "A test paper", "abstract": "An abstract.", "license": "cc-by-4.0", "creators": [{"name": "Test, Author", "orcid": "0000-0002-1825-0097"}]} doi_sub = dict(base, source="doi", doi="10.5281/zenodo.123456") xml = cd.build_deposit_xml(doi_sub, doi="10.67697/icsac.2026.099", landing_url="https://icsacinstitute.org/publications/a-test-paper", pdf_url="https://icsacinstitute.org/papers/ICSAC-SUB-00099.pdf", content_type="journal-article") try: cd.validate_xml(xml) check(True, "DOI-route deposit validates against the Crossref 5.4.0 schema") except Exception as exc: check(False, f"DOI-route deposit validates against the Crossref 5.4.0 schema ({exc})") x = xml.decode() check('relationship-type="hasPreprint"' in x and ">10.5281/zenodo.123456<" in x, "the deposit links the preprint (hasPreprint)") art = x[x.find("