Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

7.1 KB
Raw Blame
"""Evernote exports work with the declared application dependencies."""

import pytest
from defusedxml.common import DTDForbidden

from local_deep_research.document_loaders import load_from_bytes


def test_upload_extracts_each_note_and_preserves_metadata():
    export = """<?xml version="1.0" encoding="UTF-8"?>
<en-export>
  <note>
    <title>First note</title>
    <content><![CDATA[<en-note><p>Hello <strong>世界</strong></p></en-note>]]></content>
    <created>20240305T134500Z</created>
  </note>
  <note>
    <title>Second note</title>
    <content><![CDATA[<en-note><p>Another note</p></en-note>]]></content>
    <created>20240306T134500Z</created>
  </note>
</en-export>"""
    documents = load_from_bytes(export.encode(), ".enex", "notes.enex")
    assert len(documents) == 2
    assert [doc.metadata["title"] for doc in documents] == [
        "First note",
        "Second note",
    ]
    assert "世界" in documents[0].page_content
    assert "<strong>" not in documents[0].page_content
    assert "Another note" in documents[1].page_content
    assert all(doc.metadata["created"] for doc in documents)
    assert all(
        doc.metadata["original_filename"] == "notes.enex" for doc in documents
    )


@pytest.mark.parametrize(
    "export",
    [
        pytest.param(
            b"""<?xml version="1.0"?>
<!DOCTYPE en-export [<!ATTLIST note added CDATA "x">]>
<en-export><note><content>t</content></note></en-export>""",
            id="attribute-default",
        ),
        pytest.param(
            b"""<?xml version="1.0"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd"
  [<!ATTLIST note added CDATA "x">]>
<en-export><note><content>t</content></note></en-export>""",
            id="standard-identifier-with-subset",
        ),
        pytest.param(
            b"""<?xml version="1.0"?>
<!DOCTYPE en-export []>
<en-export><note><content>t</content></note></en-export>""",
            id="empty-subset",
        ),
        pytest.param(
            b"""<?xml version="1.0"?>
<!DOCTYPE en-export [<!ENTITY secret SYSTEM "file:///etc/passwd">]>
<en-export><note><content>&secret;</content></note></en-export>""",
            id="external-entity",
        ),
        pytest.param(
            b"""<?xml version="1.0"?>
<!DOCTYPE en-export [<!ENTITY repeated "untrusted expansion">]>
<en-export><note><content>&repeated;</content></note></en-export>""",
            id="internal-entity",
        ),
    ],
)
def test_evernote_refuses_any_internal_dtd_subset(export):
    # An internal subset can declare entities, or attribute defaults that
    # expat would add to every matching element, so none is accepted.
    with pytest.raises(DTDForbidden):
        load_from_bytes(export, ".enex", "untrusted.enex")


def test_evernote_loads_a_standard_export_with_exact_note_text(monkeypatch):
    def unexpected_network(*args, **kwargs):
        raise AssertionError("ENEX parsing must not fetch its doctype")

    monkeypatch.setattr("socket.create_connection", unexpected_network)
    monkeypatch.setattr("urllib.request.urlopen", unexpected_network)
    # The header lines follow Evernote's published sample export, and each
    # note's ENML carries its own declarations inside the CDATA section.
    export = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
<en-export export-date="20130730T205637Z" application="Evernote" version="Evernote Mac">
<note><title>First note</title><content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note><div>Hello <b>world</b></div></en-note>]]></content>
<created>20130730T205204Z</created></note>
<note><title>Second note</title><content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note><div>Second body</div></en-note>]]></content>
<created>20130730T205300Z</created></note>
</en-export>"""
    documents = load_from_bytes(export.encode(), ".enex", "standard.enex")
    assert [doc.page_content for doc in documents] == [
        "Hello world",
        "Second body",
    ]
    assert [doc.metadata["title"] for doc in documents] == [
        "First note",
        "Second note",
    ]


@pytest.mark.parametrize(
    "doctype_url",
    [
        "http://xml.evernote.com/pub/evernote-export3.dtd",
        "https://xml.evernote.com/pub/evernote-export3.dtd",
        "http://xml.evernote.com/pub/evernote-export4.dtd",
        "https://xml.evernote.com/pub/evernote-export4.dtd",
    ],
)
def test_evernote_accepts_standard_doctype_without_fetching_it(
    doctype_url, monkeypatch
):
    def unexpected_network(*args, **kwargs):
        raise AssertionError("ENEX parsing must not fetch its doctype")

    monkeypatch.setattr("socket.create_connection", unexpected_network)
    monkeypatch.setattr("urllib.request.urlopen", unexpected_network)
    export = f'''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "{doctype_url}">
<en-export><note><title>Standard export</title>
<content><![CDATA[<en-note><p>Exported note text</p></en-note>]]></content>
</note></en-export>'''
    documents = load_from_bytes(export.encode(), ".enex", "standard.enex")
    assert len(documents) == 1
    assert documents[0].page_content == "Exported note text"
    assert documents[0].metadata["title"] == "Standard export"


def test_evernote_keeps_note_attributes_without_script_text():
    export = b"""<en-export><note>
<title>A note</title>
<note-attributes><source-url>https://example.org/item</source-url></note-attributes>
<content><![CDATA[<en-note><p>Visible text</p><script>hidden()</script>
<style>secret { display: none }</style></en-note>]]></content>
</note></en-export>"""
    documents = load_from_bytes(export, ".enex", "safe.enex")
    assert len(documents) == 1
    assert documents[0].page_content == "Visible text"
    assert documents[0].metadata["note-attributes.source-url"] == (
        "https://example.org/item"
    )


def test_evernote_keeps_every_tag_of_a_note():
    export = """<?xml version="1.0" encoding="UTF-8"?>
<en-export><note><title>Tagged</title><tag>work</tag><tag>urgent</tag>
<content><![CDATA[<en-note><div>Body</div></en-note>]]></content></note>
<note><title>Untagged</title>
<content><![CDATA[<en-note><div>Other</div></en-note>]]></content></note></en-export>"""
    documents = load_from_bytes(export.encode(), ".enex", "tags.enex")
    assert documents[0].metadata["tag"] == ["work", "urgent"]
    assert "tag" not in documents[1].metadata


def test_evernote_single_document_separates_its_notes(tmp_path):
    from local_deep_research.document_loaders.evernote_loader import (
        EverNoteLoader,
    )

    path = tmp_path / "notes.enex"
    path.write_text(
        """<?xml version="1.0" encoding="UTF-8"?>
<en-export><note><title>First</title>
<content><![CDATA[<en-note><div>Ends here</div></en-note>]]></content></note>
<note><title>Second</title>
<content><![CDATA[<en-note><div>Starts here</div></en-note>]]></content></note></en-export>""",
        encoding="utf-8",
    )
    [document] = EverNoteLoader(path).lazy_load()
    assert document.page_content == "Ends here\n\nStarts here"