Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.
Python agent-skillsai-agentsbook-to-skillcontext-engineeringdocument-processingedtechknowledge-baseknowledge-managementllmpdf-to-markdownragself-studystudy-tools
fix: resolve 3 PR blockers (EPUB regression, batch abort, input order) + tests
Marcello Mendo7495cfbfab38
diff --git a/scripts/extract.py b/scripts/extract.pyindex ef8c30e..867e1de 100644--- a/scripts/extract.py+++ b/scripts/extract.py@@ -7,9 +7,9 @@ Modular entrypoint wrapper.import osimport sys-# Ensure the parent directory of this script's directory (i.e. the repository root)-# is in sys.path so that the modular 'extractor' package can be imported reliably.-sys.path.insert(0, str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))+# Ensure the scripts/ directory (where the 'extractor' package lives) is in sys.path+# so the modular package can be imported reliably regardless of the working directory.+sys.path.insert(0, str(os.path.dirname(os.path.abspath(__file__))))from extractor.utils import maindiff --git a/scripts/extractor/__init__.py b/scripts/extractor/__init__.pyindex ca3871b..825053e 100644--- a/scripts/extractor/__init__.py+++ b/scripts/extractor/__init__.py@@ -1 +1,2 @@from extractor.utils import resolve_input_files, extract_single_file, main+from extractor.exceptions import ExtractionErrordiff --git a/scripts/extractor/exceptions.py b/scripts/extractor/exceptions.pynew file mode 100644index 0000000..14a3f55--- /dev/null+++ b/scripts/extractor/exceptions.py@@ -0,0 +1,2 @@+class ExtractionError(Exception):+ """Raised when a single file cannot be extracted (non-fatal in batch mode)."""diff --git a/scripts/extractor/parsers/docx.py b/scripts/extractor/parsers/docx.pyindex c60fba6..5123319 100644--- a/scripts/extractor/parsers/docx.py+++ b/scripts/extractor/parsers/docx.py@@ -1,6 +1,7 @@import sysimport zipfilefrom extractor.config import supported_formats_message+from extractor.exceptions import ExtractionErrordef extract_docx_with_python_docx(docx_path: str) -> str | None:@@ -53,10 +54,9 @@ def extract_docx(docx_path: str) -> tuple[str, str]:return text, "zipfile-docx"print("FAILED")- print(- "\nERROR: Could not extract text from DOCX.\n"+ raise ExtractionError(+ "Could not extract text from DOCX.\n""Install python-docx for best results:\n"- " pip3 install python-docx",- file=sys.stderr,+ " pip3 install python-docx")- sys.exit(1)+diff --git a/scripts/extractor/parsers/rtf.py b/scripts/extractor/parsers/rtf.pyindex 6a105d8..207a274 100644--- a/scripts/extractor/parsers/rtf.py+++ b/scripts/extractor/parsers/rtf.py@@ -2,6 +2,7 @@ import htmlimport reimport sysfrom extractor.parsers.text import read_text_file+from extractor.exceptions import ExtractionErrordef strip_rtf_fallback(raw: str) -> str:@@ -16,8 +17,7 @@ def strip_rtf_fallback(raw: str) -> str:def extract_rtf(rtf_path: str) -> tuple[str, str]:raw = read_text_file(rtf_path)if raw is None:- print("ERROR: Could not read RTF file", file=sys.stderr)- sys.exit(1)+ raise ExtractionError(f"Could not read RTF file: {rtf_path}")try:from striprtf.striprtf import rtf_to_textdiff --git a/scripts/extractor/utils.py b/scripts/extractor/utils.pyindex 4dfbf4f..491d87a 100644--- a/scripts/extractor/utils.py+++ b/scripts/extractor/utils.py@@ -7,6 +7,8 @@ import shutilimport zipfilefrom pathlib import Path+from extractor.exceptions import ExtractionError+from extractor.config import (OUTPUT_DIR,OUTPUT_TEXT,@@ -106,29 +108,42 @@ def parse_arguments(argv: list[str]) -> tuple[list[str], str, str]:def resolve_input_files(paths: list[str]) -> list[Path]:- """Resolve paths including files, directories, and glob patterns to Path objects."""+ """Resolve paths including files, directories, and glob patterns to Path objects.++ User-given order is preserved for explicit file arguments. Expanded+ results (directories, globs) are sorted deterministically so repeated+ runs produce the same output.+ """resolved = []for path_str in paths:# Check if it has glob wildcardsif any(char in path_str for char in ("*", "?", "[")):glob_matches = glob.glob(path_str, recursive=True)+ # Sort expanded glob results deterministically+ expanded = []for match in glob_matches:p = Path(match)- if p.is_file():- resolved.append(p.resolve())+ if p.is_file() and p.suffix.lower() in SUPPORTED_EXTENSIONS:+ expanded.append(p.resolve())+ expanded.sort(key=lambda x: str(x).lower())+ resolved.extend(expanded)else:p = Path(path_str)if p.is_dir():+ # Sort expanded directory results deterministically+ dir_files = []for root, _, files in os.walk(p):for file in files:file_path = Path(root) / fileif file_path.suffix.lower() in SUPPORTED_EXTENSIONS:- resolved.append(file_path.resolve())+ dir_files.append(file_path.resolve())+ dir_files.sort(key=lambda x: str(x).lower())+ resolved.extend(dir_files)else:# Keep even if it doesn't exist so the error check can report itresolved.append(p.resolve())-- # Deduplicate while preserving order, then sort by file path for consistency++ # Deduplicate while preserving insertion order (user order for explicit files)seen = set()unique_paths = []for path in resolved:@@ -136,8 +151,7 @@ def resolve_input_files(paths: list[str]) -> list[Path]:if resolved_path not in seen:seen.add(resolved_path)unique_paths.append(resolved_path)-- unique_paths.sort(key=lambda x: str(x).lower())+return unique_paths@@ -146,8 +160,7 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: stinput_str = str(input_path)if not input_path.exists():- print(f"ERROR: File not found: {input_str}", file=sys.stderr)- sys.exit(1)+ raise ExtractionError(f"File not found: {input_str}")ext = input_path.suffix.lower()document_format = ext.lstrip(".")@@ -170,33 +183,25 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: stext = ".docx"document_format = "docx"else:- print(- f"ERROR: Unsupported ZIP-based format '{input_path.name}'. Supported: {supported_formats_message()}",- file=sys.stderr,+ raise ExtractionError(+ f"Unsupported ZIP-based format '{input_path.name}'. Supported: {supported_formats_message()}")- sys.exit(1)except (zipfile.BadZipFile, KeyError, OSError):- print(- f"ERROR: Unsupported ZIP-based format '{input_path.name}'. Supported: {supported_formats_message()}",- file=sys.stderr,+ raise ExtractionError(+ f"Unsupported ZIP-based format '{input_path.name}'. Supported: {supported_formats_message()}")- sys.exit(1)else:- print(- f"ERROR: Unsupported format '{ext or '<none>'}'. Supported: {supported_formats_message()}",- file=sys.stderr,+ raise ExtractionError(+ f"Unsupported format '{ext or '<none>'}'. Supported: {supported_formats_message()}")- sys.exit(1)prepare_dependencies(ext, extraction_mode, install_mode)if ext in CALIBRE_EBOOK_EXTENSIONS and not shutil.which("ebook-convert"):- print(+ raise ExtractionError("MOBI/AZW/AZW3 extraction requires Calibre's ebook-convert command. "- "Install Calibre and ensure ebook-convert is on PATH, then rerun this command.",- file=sys.stderr,+ "Install Calibre and ensure ebook-convert is on PATH, then rerun this command.")- sys.exit(1)text = ""method = ""@@ -205,9 +210,11 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: stif ext == ".epub":print(f"Extracting EPUB: {input_str}")- text, method = extract_with_ebooklib(input_str)- if not text or not text.strip():- print("not available")+ text = extract_with_ebooklib(input_str)+ if text and text.strip():+ method = "ebooklib"+ else:+ print("ebooklib not available")print("Trying stdlib zipfile parser...", end=" ", flush=True)text = extract_with_zipfile(input_str)if text and text.strip():@@ -215,15 +222,11 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: stmethod = "zipfile"else:print("FAILED")- print(- "\nERROR: Could not extract text from EPUB.\n"+ raise ExtractionError(+ "Could not extract text from EPUB.\n""Install ebooklib + beautifulsoup4 for best results:\n"- " pip3 install ebooklib beautifulsoup4",- file=sys.stderr,+ " pip3 install ebooklib beautifulsoup4")- sys.exit(1)- else:- method = "ebooklib"pages = count_epub_chapters(input_str)pages_label = "spine_items"elif ext == ".pdf":@@ -262,15 +265,13 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: stprint("OK")else:print("FAILED")- print(- "\nERROR: Could not extract text from PDF.\n"+ raise ExtractionError(+ "Could not extract text from PDF.\n""Install one of: poppler-utils (pdftotext), PyPDF2, or pdfminer.six\n"" sudo apt install poppler-utils\n"" pip3 install PyPDF2\n"- " pip3 install pdfminer.six",- file=sys.stderr,+ " pip3 install pdfminer.six")- sys.exit(1)pages = count_pages(input_str)pages_label = "pages"@@ -278,8 +279,7 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: stprint(f"Extracting text document: {input_str}")text = read_text_file(input_str)if text is None or not text.strip():- print("ERROR: Could not read text document", file=sys.stderr)- sys.exit(1)+ raise ExtractionError(f"Could not read text document: {input_path.name}")method = "plain-text"pages = 0pages_label = "sections"@@ -287,8 +287,7 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: stprint(f"Extracting HTML: {input_str}")text = extract_html_file(input_str)if text is None or not text.strip():- print("ERROR: Could not extract text from HTML", file=sys.stderr)- sys.exit(1)+ raise ExtractionError(f"Could not extract text from HTML: {input_path.name}")method = "html-parser"pages = 0pages_label = "sections"@@ -306,11 +305,9 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: stprint(f"Extracting ebook with Calibre: {input_str}")text = extract_with_ebook_convert(input_str)if text is None or not text.strip():- print(- f"ERROR: Could not extract text from {ext}. Install Calibre and ensure ebook-convert is on PATH.",- file=sys.stderr,+ raise ExtractionError(+ f"Could not extract text from {ext}. Install Calibre and ensure ebook-convert is on PATH.")- sys.exit(1)method = "ebook-convert"pages = 0pages_label = "sections"@@ -358,14 +355,26 @@ def main():extracted_sources = []combined_texts = []+ errors = []for file_path in input_files:- res = extract_single_file(file_path, extraction_mode, install_mode)+ try:+ res = extract_single_file(file_path, extraction_mode, install_mode)+ except ExtractionError as exc:+ print(f"WARNING: Skipping {file_path.name}: {exc}", file=sys.stderr)+ errors.append((file_path, str(exc)))+ continueextracted_sources.append(res)# Format the text with a clear boundaryseparator = f"\n\n{'=' * 80}\nSOURCE: {res['filename']} (Path: {res['source_file']})\n{'=' * 80}\n\n"combined_texts.append(separator + res["text"])++ if not extracted_sources:+ print(f"\nERROR: All {len(errors)} source(s) failed extraction:", file=sys.stderr)+ for path, err in errors:+ print(f" - {path.name}: {err}", file=sys.stderr)+ sys.exit(1)# Combine textsconsolidated_text = "".join(combined_texts).strip()@@ -435,3 +444,7 @@ def main():)print(f"\n Text -> {OUTPUT_TEXT}")print(f" Meta -> {OUTPUT_META}")+ if errors:+ print(f"\n WARNING: {len(errors)} source(s) skipped due to errors:")+ for path, err in errors:+ print(f" - {path.name}: {err}")diff --git a/tests/test_extractor.py b/tests/test_extractor.pynew file mode 100644index 0000000..c08fcc2--- /dev/null+++ b/tests/test_extractor.py@@ -0,0 +1,522 @@+"""+Test suite for the three PR blocker fixes + nits in the extractor package.++Covers:+ Fix #1 — EPUB extraction tuple-unpack regression+ Fix #2 — Batch resilience (ExtractionError instead of sys.exit)+ Fix #3 — Explicit input order preservation+ Nit — Glob results filtered by SUPPORTED_EXTENSIONS+"""++import json+import os+import sys+import textwrap+import zipfile+from pathlib import Path+from unittest import mock++import pytest++# ---------------------------------------------------------------------------+# Bootstrap: make sure the extractor package is importable+# ---------------------------------------------------------------------------+SCRIPTS_DIR = Path(__file__).resolve().parent.parent / "scripts"+sys.path.insert(0, str(SCRIPTS_DIR))++from extractor.exceptions import ExtractionError+from extractor.utils import (+ resolve_input_files,+ extract_single_file,+ parse_arguments,+ estimate_tokens,+ detect_structure,+ main,+)+from extractor.config import SUPPORTED_EXTENSIONS+++# ═══════════════════════════════════════════════════════════════════════════+# Helpers – fixture creation+# ═══════════════════════════════════════════════════════════════════════════++def _make_text_file(path: Path, content: str = "Hello world from test file.") -> Path:+ """Create a plain-text .txt file."""+ path.write_text(content, encoding="utf-8")+ return path+++def _make_md_file(path: Path, content: str = "# Title\n\nSome markdown content.") -> Path:+ """Create a plain-text .md file."""+ path.write_text(content, encoding="utf-8")+ return path+++def _make_html_file(path: Path) -> Path:+ """Create a minimal HTML file."""+ path.write_text(+ "<html><body><h1>Hello</h1><p>Test paragraph.</p></body></html>",+ encoding="utf-8",+ )+ return path+++def _make_minimal_epub(path: Path) -> Path:+ """Create a minimal valid EPUB (zip with mimetype + OPF + one xhtml).++ The xhtml entry name must match the OPF ``href`` exactly because+ the stdlib zipfile parser in ``epub.py`` reads hrefs from the OPF+ and looks them up directly as zip entry names.+ """+ with zipfile.ZipFile(path, "w") as zf:+ zf.writestr("mimetype", "application/epub+zip")+ zf.writestr(+ "content.opf",+ textwrap.dedent("""\+ <?xml version="1.0"?>+ <package xmlns="http://www.idpf.org/2007/opf" version="3.0">+ <metadata/>+ <manifest>+ <item id="ch1" href="chapter1.xhtml" media-type="application/xhtml+xml"/>+ </manifest>+ <spine>+ <itemref idref="ch1"/>+ </spine>+ </package>+ """),+ )+ zf.writestr(+ "chapter1.xhtml",+ "<html><body><p>EPUB chapter one content.</p></body></html>",+ )+ return path+++def _make_minimal_docx(path: Path) -> Path:+ """Create a minimal valid DOCX (ZIP with word/document.xml)."""+ ns = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"+ xml = textwrap.dedent(f"""\+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>+ <w:document xmlns:w="{ns}">+ <w:body>+ <w:p><w:r><w:t>DOCX test paragraph</w:t></w:r></w:p>+ </w:body>+ </w:document>+ """)+ with zipfile.ZipFile(path, "w") as zf:+ zf.writestr("word/document.xml", xml)+ zf.writestr("[Content_Types].xml", '<?xml version="1.0"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"/>')+ return path+++def _make_unsupported_file(path: Path) -> Path:+ """Create a file with an unsupported extension."""+ path.write_bytes(b"unsupported binary junk data")+ return path+++# ═══════════════════════════════════════════════════════════════════════════+# FIX #1 — EPUB extraction no longer does tuple-unpack+# ═══════════════════════════════════════════════════════════════════════════++class TestEpubExtractionFix:+ """Verify that EPUB extraction works without tuple-unpack errors."""++ def test_epub_extract_with_ebooklib_returns_str_or_none(self):+ """extract_with_ebooklib returns str|None, NOT a tuple."""+ from extractor.parsers.epub import extract_with_ebooklib++ # With ebooklib likely not installed in test env → returns None+ result = extract_with_ebooklib("nonexistent.epub")+ assert result is None or isinstance(result, str), (+ f"extract_with_ebooklib should return str|None, got {type(result)}"+ )++ def test_epub_extraction_via_zipfile_fallback(self, tmp_path):+ """EPUB with zipfile fallback should work end-to-end."""+ epub_path = _make_minimal_epub(tmp_path / "test.epub")++ # Mock prepare_dependencies to be a no-op+ with mock.patch("extractor.utils.prepare_dependencies"):+ result = extract_single_file(epub_path, "text", "no")++ assert result["format"] == "epub"+ assert result["extraction_method"] in ("ebooklib", "zipfile")+ assert "EPUB chapter one content" in result["text"]+ assert result["chars"] > 0+ assert result["words"] > 0++ def test_epub_no_tuple_unpack_error(self, tmp_path):+ """The old bug: tuple-unpack of str/None should not happen."""+ epub_path = _make_minimal_epub(tmp_path / "test.epub")++ # Even if ebooklib is absent, this should NOT raise TypeError/ValueError+ with mock.patch("extractor.utils.prepare_dependencies"):+ try:+ result = extract_single_file(epub_path, "text", "no")+ except (TypeError, ValueError) as exc:+ pytest.fail(f"Tuple-unpack regression! Got: {exc}")++ assert result["text"] # some text was extracted+++# ═══════════════════════════════════════════════════════════════════════════+# FIX #2 — Batch resilience (ExtractionError instead of sys.exit)+# ═══════════════════════════════════════════════════════════════════════════++class TestBatchResilience:+ """Verify that a single bad file does NOT abort the entire batch."""++ def test_extract_single_file_raises_on_missing(self, tmp_path):+ """A missing file should raise ExtractionError, not sys.exit."""+ missing = tmp_path / "does_not_exist.txt"+ with pytest.raises(ExtractionError, match="File not found"):+ extract_single_file(missing, "text", "no")++ def test_extract_single_file_raises_on_unsupported(self, tmp_path):+ """An unsupported format should raise ExtractionError, not sys.exit."""+ unsupported = _make_unsupported_file(tmp_path / "data.xyz")+ with pytest.raises(ExtractionError, match="Unsupported format"):+ extract_single_file(unsupported, "text", "no")++ def test_batch_continues_past_bad_files(self, tmp_path):+ """A mix of good + bad files should produce output for the good ones."""+ # Create a valid text file+ good_file = _make_text_file(tmp_path / "good.txt", "Good content here.")+ # Create a file that will fail (unsupported extension, garbage bytes)+ bad_file = _make_unsupported_file(tmp_path / "bad.xyz")++ # Simulate the batch loop from main()+ input_files = [good_file, bad_file]+ extracted = []+ errors = []++ for fp in input_files:+ try:+ with mock.patch("extractor.utils.prepare_dependencies"):+ res = extract_single_file(fp, "text", "no")+ extracted.append(res)+ except ExtractionError as exc:+ errors.append((fp, str(exc)))++ assert len(extracted) == 1, "Good file should have been extracted"+ assert len(errors) == 1, "Bad file should have been recorded as error"+ assert "Good content here" in extracted[0]["text"]++ def test_batch_fails_hard_when_all_fail(self, tmp_path, monkeypatch):+ """If ALL sources fail, main() should sys.exit(1)."""+ bad1 = _make_unsupported_file(tmp_path / "bad1.xyz")+ bad2 = _make_unsupported_file(tmp_path / "bad2.abc")++ monkeypatch.setattr(+ "sys.argv",+ ["extract.py", str(bad1), str(bad2), "--install-missing", "no"],+ )+ monkeypatch.setattr("extractor.utils.prepare_dependencies", lambda *a: None)++ with pytest.raises(SystemExit) as exc_info:+ main()+ assert exc_info.value.code == 1++ def test_main_produces_output_with_partial_failures(self, tmp_path, monkeypatch):+ """main() should produce output even when some files fail."""+ good = _make_text_file(tmp_path / "good.txt", "Partial success content.")+ bad = _make_unsupported_file(tmp_path / "bad.xyz")++ # Point output to tmp+ out_dir = tmp_path / "output"+ monkeypatch.setenv("BOOK_SKILL_WORKDIR", str(out_dir))++ monkeypatch.setattr(+ "sys.argv",+ ["extract.py", str(good), str(bad), "--install-missing", "no"],+ )++ # Need to re-import config constants since they're evaluated at import time+ # So we patch the OUTPUT_* in utils directly+ out_text = out_dir / "full_text.txt"+ out_meta = out_dir / "metadata.json"+ monkeypatch.setattr("extractor.utils.OUTPUT_DIR", out_dir)+ monkeypatch.setattr("extractor.utils.OUTPUT_TEXT", out_text)+ monkeypatch.setattr("extractor.utils.OUTPUT_META", out_meta)+ monkeypatch.setattr("extractor.utils.prepare_dependencies", lambda *a: None)++ main()++ assert out_text.exists(), "full_text.txt should be created"+ assert out_meta.exists(), "metadata.json should be created"+ text = out_text.read_text(encoding="utf-8")+ assert "Partial success content" in text++ meta = json.loads(out_meta.read_text(encoding="utf-8"))+ assert meta["total_sources"] == 1++ def test_extraction_error_is_not_system_exit(self):+ """ExtractionError should NOT be a subclass of SystemExit."""+ assert not issubclass(ExtractionError, SystemExit)+ with pytest.raises(ExtractionError):+ raise ExtractionError("test")+++# ═══════════════════════════════════════════════════════════════════════════+# FIX #3 — Explicit input order preservation+# ═══════════════════════════════════════════════════════════════════════════++class TestInputOrderPreservation:+ """Verify that user-given file order is preserved."""++ def test_explicit_files_preserve_order(self, tmp_path):+ """Files specified explicitly should keep the user's order."""+ f_c = _make_text_file(tmp_path / "charlie.txt", "C")+ f_a = _make_text_file(tmp_path / "alpha.txt", "A")+ f_b = _make_text_file(tmp_path / "bravo.txt", "B")++ # User passes: charlie, alpha, bravo+ result = resolve_input_files([str(f_c), str(f_a), str(f_b)])++ names = [p.name for p in result]+ assert names == ["charlie.txt", "alpha.txt", "bravo.txt"], (+ f"Expected user order, got: {names}"+ )++ def test_explicit_files_reverse_order(self, tmp_path):+ """Reverse alphabetical order should be preserved as-is."""+ f1 = _make_text_file(tmp_path / "note2.md", "two")+ f2 = _make_text_file(tmp_path / "note1.md", "one")++ result = resolve_input_files([str(f1), str(f2)])+ names = [p.name for p in result]+ assert names == ["note2.md", "note1.md"], (+ f"Expected note2 before note1, got: {names}"+ )++ def test_directory_contents_are_sorted(self, tmp_path):+ """Files from directory expansion SHOULD be sorted deterministically."""+ d = tmp_path / "books"+ d.mkdir()+ _make_text_file(d / "zebra.txt", "Z")+ _make_text_file(d / "alpha.txt", "A")+ _make_text_file(d / "middle.txt", "M")++ result = resolve_input_files([str(d)])+ names = [p.name for p in result]+ assert names == sorted(names, key=str.lower), (+ f"Directory contents should be sorted, got: {names}"+ )++ def test_mixed_explicit_and_directory(self, tmp_path):+ """Explicit file order is preserved, directory expansion is sorted within itself."""+ explicit = _make_text_file(tmp_path / "explicit_z.txt", "Z first")++ d = tmp_path / "folder"+ d.mkdir()+ _make_text_file(d / "b_in_dir.txt", "B")+ _make_text_file(d / "a_in_dir.txt", "A")++ result = resolve_input_files([str(explicit), str(d)])+ names = [p.name for p in result]+ # explicit_z should come first, then the dir contents sorted+ assert names[0] == "explicit_z.txt"+ assert names[1:] == ["a_in_dir.txt", "b_in_dir.txt"]++ def test_deduplication_preserves_first_occurrence(self, tmp_path):+ """When a file is mentioned twice, keep the FIRST position."""+ f = _make_text_file(tmp_path / "dup.txt", "dup")+ result = resolve_input_files([str(f), str(f)])+ assert len(result) == 1+ assert result[0].name == "dup.txt"+++# ═══════════════════════════════════════════════════════════════════════════+# NIT — Glob filtering by SUPPORTED_EXTENSIONS+# ═══════════════════════════════════════════════════════════════════════════++class TestGlobFiltering:+ """Verify that glob expansion filters by supported extensions."""++ def test_glob_filters_unsupported_extensions(self, tmp_path):+ """Glob should not include files with unsupported extensions."""+ _make_text_file(tmp_path / "notes.txt", "good")+ _make_unsupported_file(tmp_path / "image.png")+ _make_unsupported_file(tmp_path / "data.csv")++ pattern = str(tmp_path / "*")+ result = resolve_input_files([pattern])++ extensions = {p.suffix.lower() for p in result}+ assert extensions <= SUPPORTED_EXTENSIONS, (+ f"Unsupported extensions found in glob results: {extensions - SUPPORTED_EXTENSIONS}"+ )+ names = [p.name for p in result]+ assert "notes.txt" in names+ assert "image.png" not in names+ assert "data.csv" not in names++ def test_glob_includes_supported_extensions(self, tmp_path):+ """Glob should include all supported file types."""+ _make_text_file(tmp_path / "readme.md", "# README")+ _make_html_file(tmp_path / "page.html")+ _make_text_file(tmp_path / "notes.txt", "notes")++ pattern = str(tmp_path / "*")+ result = resolve_input_files([pattern])++ names = {p.name for p in result}+ assert "readme.md" in names+ assert "page.html" in names+ assert "notes.txt" in names++ def test_glob_results_are_sorted(self, tmp_path):+ """Glob expansion results should be sorted deterministically."""+ _make_text_file(tmp_path / "z_file.txt", "z")+ _make_text_file(tmp_path / "a_file.txt", "a")+ _make_text_file(tmp_path / "m_file.txt", "m")++ pattern = str(tmp_path / "*.txt")+ result = resolve_input_files([pattern])+ names = [p.name for p in result]+ assert names == sorted(names, key=str.lower)+++# ═══════════════════════════════════════════════════════════════════════════+# Additional edge-case tests+# ═══════════════════════════════════════════════════════════════════════════++class TestParseArguments:+ """Basic tests for argument parsing."""++ def test_basic_parsing(self):+ paths, mode, _ = parse_arguments(+ ["extract.py", "book.pdf", "--mode", "text", "--install-missing", "no"]+ )+ assert paths == ["book.pdf"]+ assert mode == "text"++ def test_multiple_inputs(self):+ paths, mode, _ = parse_arguments(+ ["extract.py", "a.pdf", "b.epub", "c.txt"]+ )+ assert paths == ["a.pdf", "b.epub", "c.txt"]+ assert mode == "text" # default++ def test_technical_mode(self):+ paths, mode, _ = parse_arguments(+ ["extract.py", "a.pdf", "--mode", "technical"]+ )+ assert mode == "technical"++ def test_invalid_mode_defaults_to_text(self):+ _, mode, _ = parse_arguments(+ ["extract.py", "a.pdf", "--mode", "invalid"]+ )+ assert mode == "text"+++class TestEstimateTokens:+ """Tests for token estimation."""++ def test_empty_string(self):+ assert estimate_tokens("") == 0++ def test_known_word_count(self):+ text = " ".join(["word"] * 100)+ tokens = estimate_tokens(text)+ # 100 words / 0.75 ≈ 133+ assert tokens == 133+++class TestDetectStructure:+ """Tests for structure detection."""++ def test_detects_chapters(self):+ text = "Chapter 1 Introduction\nSome text.\nChapter 2 Details\nMore text."+ result = detect_structure(text)+ assert result["chapters_detected"] == 2++ def test_detects_toc(self):+ text = "Table of Contents\n1. Intro\n2. Body"+ result = detect_structure(text)+ assert result["has_toc"] is True++ def test_no_toc(self):+ text = "Just some regular text without any structure."+ result = detect_structure(text)+ assert result["has_toc"] is False+++class TestTextExtraction:+ """Tests for plain-text file extraction."""++ def test_extract_txt_file(self, tmp_path):+ txt = _make_text_file(tmp_path / "simple.txt", "Simple text content for testing.")++ with mock.patch("extractor.utils.prepare_dependencies"):+ result = extract_single_file(txt, "text", "no")++ assert result["format"] == "txt"+ assert result["extraction_method"] == "plain-text"+ assert "Simple text content" in result["text"]++ def test_extract_md_file(self, tmp_path):+ md = _make_md_file(tmp_path / "notes.md", "# My Notes\n\nSome notes here.")++ with mock.patch("extractor.utils.prepare_dependencies"):+ result = extract_single_file(md, "text", "no")++ assert result["format"] == "md"+ assert "My Notes" in result["text"]+++class TestHtmlExtraction:+ """Tests for HTML file extraction."""++ def test_extract_html_file(self, tmp_path):+ html_file = _make_html_file(tmp_path / "page.html")++ with mock.patch("extractor.utils.prepare_dependencies"):+ result = extract_single_file(html_file, "text", "no")++ assert result["format"] == "html"+ assert result["extraction_method"] == "html-parser"+ assert "Test paragraph" in result["text"]+++class TestDocxExtraction:+ """Tests for DOCX extraction via the zipfile fallback."""++ def test_extract_docx_zipfile_fallback(self, tmp_path):+ docx = _make_minimal_docx(tmp_path / "test.docx")++ with mock.patch("extractor.utils.prepare_dependencies"):+ result = extract_single_file(docx, "text", "no")++ assert result["format"] == "docx"+ assert "DOCX test paragraph" in result["text"]+++class TestResolveInputFiles:+ """Additional edge-case tests for resolve_input_files."""++ def test_nonexistent_file_kept_for_error_reporting(self, tmp_path):+ """A nonexistent explicit path is kept so extract_single_file can report it."""+ fake = tmp_path / "nonexistent.pdf"+ result = resolve_input_files([str(fake)])+ assert len(result) == 1+ assert result[0].name == "nonexistent.pdf"++ def test_empty_directory_returns_empty(self, tmp_path):+ d = tmp_path / "empty"+ d.mkdir()+ result = resolve_input_files([str(d)])+ assert result == []++ def test_directory_only_picks_supported(self, tmp_path):+ d = tmp_path / "mixed"+ d.mkdir()+ _make_text_file(d / "readme.txt", "hi")+ _make_unsupported_file(d / "photo.jpg")++ result = resolve_input_files([str(d)])+ names = [p.name for p in result]+ assert "readme.txt" in names+ assert "photo.jpg" not in names