Files
stack/tests/dev/test_migrate_fr_txt.py
kert d726dca50e
All checks were successful
CI / lint (push) Successful in 40s
CI / notebooks-smoke (push) Successful in 1m37s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
Deploy / llm (push) Has been skipped
Deploy / mc (push) Has been skipped
Infra CI / notebooks (push) Successful in 1m5s
Infra CI / zotero (push) Successful in 13s
Infra CI / docs (push) Successful in 1m22s
Infra CI / api (push) Successful in 1m15s
Infra CI / llm (push) Successful in 44s
Infra CI / mc (push) Successful in 15s
Deploy / report (push) Successful in 13s
CI / test (push) Successful in 17m19s
Notebooks Integration / notebooks-integration (push) Successful in 7m27s
Zotero Sync / zotero-sync (push) Successful in 1m10s
Package Supply Chain / pkg-supply-chain (push) Successful in 59s
refactor(llm,rex): normalize FR text at capture; index-time strip becomes guard (refs #617)
2026-08-13 20:04:38 -04:00

98 lines
3.3 KiB
Python

"""Tests for dev/scripts/migrate_fr_txt.py — one-off migration that
normalizes already-downloaded FR ``.txt`` files (refs #617).
"""
from __future__ import annotations
import importlib.util
import sys
from pathlib import Path
# Load the script as a module — it lives outside the src/ tree.
_SCRIPT = Path(__file__).resolve().parents[2] / "dev" / "scripts" / "migrate_fr_txt.py"
_spec = importlib.util.spec_from_file_location("_migrate_fr_txt", _SCRIPT)
assert _spec and _spec.loader
_migrate_fr_txt = importlib.util.module_from_spec(_spec)
sys.modules["_migrate_fr_txt"] = _migrate_fr_txt
_spec.loader.exec_module(_migrate_fr_txt)
WRAPPED = (
"<html><head><title>x</title></head><body><pre>\n"
"The Secretary proposes to amend 42 CFR part 414.\n"
"</pre></body></html>"
)
class TestFindCandidates:
def test_finds_only_html_wrapped_files(self, tmp_path):
(tmp_path / "wrapped.txt").write_text(WRAPPED)
(tmp_path / "plain.txt").write_text("Already plain.")
(tmp_path / "ignored.pdf").write_bytes(b"%PDF-1.4")
found = _migrate_fr_txt.find_candidates(tmp_path)
assert [p.name for p in found] == ["wrapped.txt"]
def test_empty_dir_yields_nothing(self, tmp_path):
assert _migrate_fr_txt.find_candidates(tmp_path) == []
class TestMigrateOne:
def test_dry_run_leaves_file_untouched(self, tmp_path):
txt = tmp_path / "2025-13271.txt"
txt.write_text(WRAPPED)
row = _migrate_fr_txt.migrate_one(txt, dry_run=True)
assert row["changed"] is False
assert txt.read_text() == WRAPPED
assert not (tmp_path / "2025-13271.html").exists()
def test_real_run_writes_html_sibling_and_cleans_txt(self, tmp_path):
txt = tmp_path / "2025-13271.txt"
txt.write_text(WRAPPED)
row = _migrate_fr_txt.migrate_one(txt, dry_run=False)
assert row["changed"] is True
assert row["before"] == len(WRAPPED.encode())
html_sibling = tmp_path / "2025-13271.html"
assert html_sibling.read_text() == WRAPPED
cleaned = txt.read_text()
assert "<html>" not in cleaned
assert "The Secretary proposes to amend 42 CFR part 414." in cleaned
assert row["after"] == len(cleaned.encode())
class TestMain:
def test_dry_run_reports_without_writing(self, tmp_path, capsys):
txt = tmp_path / "2025-13271.txt"
txt.write_text(WRAPPED)
rc = _migrate_fr_txt.main(["--dir", str(tmp_path), "--dry-run"])
assert rc == 0
assert txt.read_text() == WRAPPED
out = capsys.readouterr().out
assert "2025-13271.txt" in out
def test_real_run_migrates_matching_files(self, tmp_path):
txt = tmp_path / "2025-13271.txt"
txt.write_text(WRAPPED)
(tmp_path / "plain.txt").write_text("Nothing to do here.")
rc = _migrate_fr_txt.main(["--dir", str(tmp_path)])
assert rc == 0
assert (tmp_path / "2025-13271.html").exists()
assert "<html>" not in txt.read_text()
assert not (tmp_path / "plain.html").exists()
def test_no_candidates_is_a_clean_noop(self, tmp_path, capsys):
(tmp_path / "plain.txt").write_text("Nothing wrapped here.")
rc = _migrate_fr_txt.main(["--dir", str(tmp_path)])
assert rc == 0
assert "No HTML-wrapped" in capsys.readouterr().out