Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pageindex/page_index_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import json
import re
import os
try:
if __package__:
from .utils import *
except:
else:
# Run as a script (no parent package): resolve utils from the same directory.
from utils import *

async def get_node_summary(node, summary_token_threshold=200, model=None):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_page_index_md.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import subprocess
import sys
import unittest
from pathlib import Path

from pageindex.page_index_md import extract_nodes_from_markdown


class UtilsImportFallbackTest(unittest.TestCase):
def test_script_mode_import_resolves_sibling_utils(self):
# Without a parent package the module must fall back to `from utils import *`.
module_dir = Path(__file__).resolve().parents[1] / "pageindex"
result = subprocess.run(
[sys.executable, "-c", "import page_index_md; print(page_index_md.count_tokens.__name__)"],
cwd=module_dir,
capture_output=True,
text=True,
timeout=120,
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.strip(), "count_tokens")

def test_package_import_does_not_swallow_errors(self):
source = (Path(__file__).resolve().parents[1] / "pageindex" / "page_index_md.py").read_text()
self.assertNotIn("except:", source)


class ExtractNodesFromMarkdownTest(unittest.TestCase):
def test_skips_bold_heading_with_only_whitespace(self):
nodes, _ = extract_nodes_from_markdown("** **\n**Valid heading**")
Expand Down