test_files_preview.py 1.24 KB
from pathlib import Path
import ast
import unittest


def _function_source(path: str, name: str) -> str:
    source = Path(path).read_text(encoding="utf-8")
    tree = ast.parse(source)
    for node in tree.body:
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == name:
            return ast.get_source_segment(source, node) or ""
    raise AssertionError(f"Function not found: {name}")


class FilesPreviewStaticTest(unittest.TestCase):
    def test_pptx_view_uses_existing_preview_without_generating(self):
        view_source = _function_source("src/api/routes/files.py", "view_file")

        self.assertIn("get_existing_office_preview_pdf", view_source)
        self.assertNotIn("get_or_create_office_preview_pdf", view_source)
        self.assertIn("preview_status", view_source)
        self.assertIn("missing", view_source)
        self.assertIn("status_code=202", view_source)

    def test_document_parser_exposes_existing_preview_helper(self):
        parser_source = Path("src/tools/document_parser.py").read_text(encoding="utf-8")

        self.assertIn("def get_existing_office_preview_pdf", parser_source)
        self.assertIn("return str(cache_path)", parser_source)


if __name__ == "__main__":
    unittest.main()