File size: 2,954 Bytes
532111b
 
9d8bbd6
532111b
 
9d8bbd6
 
 
 
 
532111b
9d8bbd6
532111b
9d8bbd6
 
 
 
532111b
 
9d8bbd6
 
 
532111b
9d8bbd6
 
532111b
9d8bbd6
 
 
 
532111b
9d8bbd6
 
 
532111b
9d8bbd6
532111b
 
d7d27a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d8bbd6
 
 
532111b
9d8bbd6
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pytest

from dataloader import convert_uploaded_documents


def test_uploaded_markdown_and_text_are_formatted_without_disk_output(tmp_path):
    markdown = tmp_path / "resume.md"
    text = tmp_path / "projects.txt"
    markdown.write_text("# Resume", encoding="utf-8")
    text.write_text("Project work", encoding="utf-8")

    profile = convert_uploaded_documents([str(markdown), str(text)])

    assert "# Source: resume" in profile
    assert "# Resume" in profile
    assert "# Source: projects" in profile
    assert "Project work" in profile


def test_uploaded_pdf_is_converted_to_markdown(tmp_path, monkeypatch):
    import sys
    from types import SimpleNamespace

    pdf = tmp_path / "resume.pdf"
    pdf.write_bytes(b"fake pdf")

    class FakeConverter:
        def convert(self, path):
            assert path == str(pdf)
            return SimpleNamespace(markdown="# Converted resume")

    monkeypatch.setitem(
        sys.modules, "markitdown", SimpleNamespace(MarkItDown=FakeConverter)
    )

    assert "# Converted resume" in convert_uploaded_documents([str(pdf)])


@pytest.mark.parametrize("markitdown_result", ["", "raise"])
def test_uploaded_pdf_falls_back_to_rapidocr(tmp_path, monkeypatch, markitdown_result):
    import sys
    from types import SimpleNamespace
    import dataloader

    pdf = tmp_path / "scanned.pdf"
    pdf.write_bytes(b"fake pdf")

    class FakeConverter:
        def convert(self, _path):
            if markitdown_result == "raise":
                raise ValueError("No text layer")
            return SimpleNamespace(markdown="  ")

    class FakeBitmap:
        def to_numpy(self):
            return object()

    class FakePage:
        def render(self, scale):
            assert scale == 2
            return FakeBitmap()

        def close(self):
            pass

    class FakePdfDocument:
        def __init__(self, path):
            assert path == str(pdf)

        def __len__(self):
            return 2

        def __getitem__(self, _index):
            return FakePage()

        def close(self):
            pass

    class FakeOCR:
        def __call__(self, _image):
            return SimpleNamespace(txts=("recognized text",))

    monkeypatch.setitem(
        sys.modules, "markitdown", SimpleNamespace(MarkItDown=FakeConverter)
    )
    monkeypatch.setitem(
        sys.modules,
        "pypdfium2",
        SimpleNamespace(PdfDocument=FakePdfDocument),
    )
    dataloader._get_ocr_engine.cache_clear()
    monkeypatch.setattr(dataloader, "_get_ocr_engine", FakeOCR)

    profile = convert_uploaded_documents([str(pdf)])

    assert profile.count("recognized text") == 2


def test_uploaded_unsupported_file_type_is_rejected(tmp_path):
    unsupported = tmp_path / "profile.exe"
    unsupported.write_text("not a document", encoding="utf-8")

    with pytest.raises(ValueError, match="Unsupported file type"):
        convert_uploaded_documents([str(unsupported)])