File size: 3,197 Bytes
a753e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Tests for reloading a persisted paper module."""

import json

from researchlink.schemas.paper import PaperExtraction, PaperMetadata
from researchlink.schemas.provenance import SourceRecord
from researchlink.services import module_io, paper_module
from researchlink.services.metadata_resolver import build_provenance


def _write_module(tmp_path):
    mod = tmp_path / "2017-attention"
    mod.mkdir()
    meta = PaperMetadata(title="Attention Is All You Need", year=2017, doi="10.1/x",
                         arxiv_id="1706.03762", venue="NeurIPS",
                         authors_provisional=["Vaswani", "Shazeer"], slug="2017-attention")
    ext = PaperExtraction(abstract="We propose the Transformer.",
                          section_headings=["Introduction", "Method"],
                          full_text="We propose the Transformer. It relies on attention.")
    (mod / "paper.md").write_text(paper_module.paper_markdown(meta, ext), encoding="utf-8")

    prov = build_provenance([SourceRecord(name="user", fields={
        "title": meta.title, "year": 2017, "doi": "10.1/x", "arxiv_id": "1706.03762",
        "venue": "NeurIPS", "authors": meta.authors_provisional, "url": "https://arxiv.org/abs/1706.03762",
    })])
    payload, sources = paper_module.build_metadata_json(prov, meta)
    (mod / "metadata.json").write_text(json.dumps(payload), encoding="utf-8")
    (mod / "sources.json").write_text(json.dumps(sources), encoding="utf-8")
    (mod / "references.md").write_text(
        "# References\n1. Bahdanau et al. Neural MT. 2015. ⚠️ `needs-verification`\n"
        "2. Sutskever et al. Seq2Seq. 2014.\n", encoding="utf-8")
    return mod


def test_load_metadata_roundtrip(tmp_path):
    mod = _write_module(tmp_path)
    meta = module_io.load_metadata(mod)
    assert meta.title == "Attention Is All You Need"
    assert meta.year == 2017
    assert meta.doi == "10.1/x"
    assert meta.arxiv_id == "1706.03762"
    assert meta.venue == "NeurIPS"
    assert meta.authors_provisional == ["Vaswani", "Shazeer"]
    assert meta.slug == "2017-attention"


def test_load_extraction_roundtrip(tmp_path):
    mod = _write_module(tmp_path)
    ext = module_io.load_extraction(mod)
    assert ext.abstract == "We propose the Transformer."
    assert ext.section_headings == ["Introduction", "Method"]
    assert "relies on attention" in (ext.full_text or "")
    assert len(ext.references_raw) == 2


def test_load_references_strips_markers(tmp_path):
    mod = _write_module(tmp_path)
    refs = module_io.load_references(mod)
    assert refs[0] == "Bahdanau et al. Neural MT. 2015."   # status marker stripped
    assert "Sutskever" in refs[1]


def test_load_missing_files_tolerant(tmp_path):
    empty = tmp_path / "empty"
    empty.mkdir()
    assert module_io.load_metadata(empty).title == "Unknown Title"
    assert module_io.load_extraction(empty).references_raw == []


def test_write_module_file_maps_spec_name(tmp_path):
    mod = tmp_path / "m"
    mod.mkdir()
    dest = module_io.write_module_file(mod, "teaching-path.md", "# Study\n")
    assert dest.name == "study_notes.md"      # internal key mapped to spec name
    assert dest.read_text() == "# Study\n"