File size: 2,498 Bytes
6a46e44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
tests/test_session.py
─────────────────────
Unit tests for the session architecture.
"""

from __future__ import annotations

import tempfile
from datetime import datetime, timezone
from pathlib import Path

import pytest

from docling_pdf_processor.exceptions import SessionNotFoundError
from docling_pdf_processor.session.manager import SessionManager
from docling_pdf_processor.session.models import SessionStatus
from docling_pdf_processor.session.store import FileSystemSessionStore, InMemorySessionStore


def test_in_memory_store_create_and_get():
    store = InMemorySessionStore()
    session = store.create()
    assert session.status == SessionStatus.CREATED
    assert session.workspace.exists()

    fetched = store.get(session.session_id)
    assert fetched is not None
    assert fetched.session_id == session.session_id


def test_in_memory_store_cleanup_stale():
    store = InMemorySessionStore()
    session = store.create()
    # artificially age the session (do not call save, which would touch it)
    session.last_activity = datetime(2000, 1, 1, tzinfo=timezone.utc)

    removed = store.cleanup_stale(max_age_seconds=1)
    assert removed == 1
    assert store.get(session.session_id) is None


def test_file_system_store_persists_metadata():
    with tempfile.TemporaryDirectory() as tmp:
        base = Path(tmp) / "sessions"
        store = FileSystemSessionStore(base_dir=base)
        session = store.create()
        session.pdf_name = "demo.pdf"
        store.save(session)

        # fresh store instance should read from disk
        store2 = FileSystemSessionStore(base_dir=base)
        fetched = store2.get(session.session_id)
        assert fetched is not None
        assert fetched.pdf_name == "demo.pdf"


def test_session_manager_lifecycle():
    with tempfile.TemporaryDirectory() as tmp:
        base = Path(tmp) / "sessions"
        manager = SessionManager(store=InMemorySessionStore(base_dir=base))
        session = manager.create_session(pdf_name="test.pdf")
        assert session.pdf_name == "test.pdf"

        paths = manager.get_paths(session)
        assert (paths["pdf_dir"]).exists()

        manager.update_status(session.session_id, SessionStatus.READY)
        assert manager.get_session(session.session_id).status == SessionStatus.READY

        manager.cleanup_session(session.session_id)
        with pytest.raises(SessionNotFoundError):
            manager.get_session(session.session_id)