245 lines
7.5 KiB
Python
245 lines
7.5 KiB
Python
"""fetch-pfs-comments / fetch-docket-comments must not call reg.gov for
|
|
sealed dockets and must reuse stored document ids for known ones."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from typer.testing import CliRunner
|
|
|
|
from bib.dockets import Docket
|
|
from bib.item import Rule
|
|
from bib.regulations_gov import WalkResult
|
|
from bib.store import Store
|
|
from cli.bib import app
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
def _store() -> Store:
|
|
return Store(":memory:", storage_dir="/tmp/nope")
|
|
|
|
|
|
def _rule_doc():
|
|
doc = MagicMock()
|
|
doc.type = "Proposed Rule"
|
|
doc.publication_date = "2026-07-16"
|
|
doc.dockets = ["CMS-1848-P"]
|
|
doc.html_url = "https://example.com"
|
|
doc.document_number = "2026-1"
|
|
return doc
|
|
|
|
|
|
def _api():
|
|
api = MagicMock()
|
|
api.__enter__ = MagicMock(return_value=api)
|
|
api.__exit__ = MagicMock(return_value=False)
|
|
return api
|
|
|
|
|
|
@patch("bib.regulations_gov.walk_docket", return_value=WalkResult())
|
|
@patch("bib.regulations_gov.Client")
|
|
@patch("bib.translate.federal_register")
|
|
@patch("bib.federalregister.split_docket_ids", return_value=["CMS-1848-P"])
|
|
@patch("bib.federalregister.pfs_rules")
|
|
@patch("bib.connect")
|
|
def test_sealed_docket_skipped_before_any_call(
|
|
mc_connect, mc_pfs, _split, mc_fr, mc_client, mc_walk
|
|
):
|
|
s = _store()
|
|
s.docket_upsert(
|
|
Docket(
|
|
id="CMS-2026-2377",
|
|
rule_cms_id="CMS-1848-P",
|
|
fr_object_id="o",
|
|
sealed_at="2026-10-20T00:00:00Z",
|
|
seal_reason="auto",
|
|
)
|
|
)
|
|
mc_connect.return_value = s
|
|
mc_pfs.return_value = [_rule_doc()]
|
|
api = _api()
|
|
mc_client.return_value = api
|
|
|
|
result = runner.invoke(app, ["fetch-pfs-comments"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert "sealed" in result.output
|
|
mc_fr.assert_not_called() # no rule-metadata fetch
|
|
api.resolve_docket.assert_not_called()
|
|
api.find_documents_in_docket.assert_not_called()
|
|
mc_walk.assert_not_called()
|
|
|
|
|
|
@patch("bib.regulations_gov.walk_docket", return_value=WalkResult(created=2))
|
|
@patch("bib.regulations_gov.Client")
|
|
@patch("bib.translate.federal_register")
|
|
@patch("bib.federalregister.split_docket_ids", return_value=["CMS-1848-P"])
|
|
@patch("bib.federalregister.pfs_rules")
|
|
@patch("bib.connect")
|
|
def test_known_open_docket_walks_without_resolve(
|
|
mc_connect, mc_pfs, _split, mc_fr, mc_client, mc_walk
|
|
):
|
|
s = _store()
|
|
s.docket_upsert(
|
|
Docket(
|
|
id="CMS-2026-2377",
|
|
rule_cms_id="CMS-1848-P",
|
|
fr_object_id="o",
|
|
comment_end_date="2026-09-14",
|
|
)
|
|
)
|
|
mc_connect.return_value = s
|
|
mc_pfs.return_value = [_rule_doc()]
|
|
mc_fr.return_value = Rule(
|
|
title="CY2027 PFS NPRM", url="https://www.federalregister.gov/d/2026-1"
|
|
)
|
|
api = _api()
|
|
mc_client.return_value = api
|
|
|
|
result = runner.invoke(app, ["fetch-pfs-comments"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
api.resolve_docket.assert_not_called()
|
|
api.find_documents_in_docket.assert_not_called()
|
|
mc_walk.assert_called_once()
|
|
assert mc_walk.call_args.args[2].id == "CMS-2026-2377"
|
|
assert "created=2" in result.output
|
|
|
|
|
|
@patch("bib.regulations_gov.walk_docket", return_value=WalkResult())
|
|
@patch("bib.regulations_gov.discover_docket")
|
|
@patch("bib.regulations_gov.Client")
|
|
@patch("bib.translate.federal_register")
|
|
@patch("bib.federalregister.split_docket_ids", return_value=["CMS-1848-P"])
|
|
@patch("bib.federalregister.pfs_rules")
|
|
@patch("bib.connect")
|
|
def test_unknown_docket_is_resolved_once_and_stored(
|
|
mc_connect, mc_pfs, _split, mc_fr, mc_client, mc_disc, mc_walk
|
|
):
|
|
s = _store()
|
|
mc_connect.return_value = s
|
|
mc_pfs.return_value = [_rule_doc()]
|
|
mc_fr.return_value = Rule(
|
|
title="CY2027 PFS NPRM", url="https://www.federalregister.gov/d/2026-1"
|
|
)
|
|
api = _api()
|
|
api.resolve_docket.return_value = "CMS-2026-2377"
|
|
mc_client.return_value = api
|
|
mc_disc.return_value = Docket(
|
|
id="CMS-2026-2377",
|
|
rule_cms_id="CMS-1848-P",
|
|
fr_object_id="o",
|
|
comment_end_date="2026-09-14",
|
|
)
|
|
|
|
result = runner.invoke(app, ["fetch-pfs-comments"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
api.resolve_docket.assert_called_once_with("CMS-1848-P")
|
|
mc_disc.assert_called_once()
|
|
assert s.docket_get("CMS-2026-2377").fr_object_id == "o"
|
|
|
|
|
|
@patch("bib.regulations_gov.walk_docket", return_value=WalkResult())
|
|
@patch("bib.regulations_gov.Client")
|
|
@patch("bib.translate.federal_register")
|
|
@patch("bib.federalregister.split_docket_ids", return_value=["CMS-1848-P"])
|
|
@patch("bib.federalregister.pfs_rules")
|
|
@patch("bib.connect")
|
|
def test_docket_filter_skips_other_known_dockets_without_calls(
|
|
mc_connect, mc_pfs, _split, mc_fr, mc_client, mc_walk
|
|
):
|
|
s = _store()
|
|
s.docket_upsert(
|
|
Docket(id="CMS-2026-2377", rule_cms_id="CMS-1848-P", fr_object_id="o")
|
|
)
|
|
mc_connect.return_value = s
|
|
mc_pfs.return_value = [_rule_doc()]
|
|
api = _api()
|
|
mc_client.return_value = api
|
|
|
|
result = runner.invoke(app, ["fetch-pfs-comments", "--docket", "CMS-2019-0111"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
mc_fr.assert_not_called()
|
|
api.resolve_docket.assert_not_called()
|
|
mc_walk.assert_not_called()
|
|
|
|
|
|
@patch("bib.regulations_gov.walk_docket", return_value=WalkResult())
|
|
@patch("bib.regulations_gov.Client")
|
|
@patch("bib.translate.federal_register")
|
|
@patch("bib.federalregister.split_docket_ids", return_value=["CMS-1848-P"])
|
|
@patch("bib.federalregister.pfs_rules")
|
|
@patch("bib.connect")
|
|
def test_force_walks_sealed_docket(
|
|
mc_connect, mc_pfs, _split, mc_fr, mc_client, mc_walk
|
|
):
|
|
s = _store()
|
|
s.docket_upsert(
|
|
Docket(
|
|
id="CMS-2026-2377",
|
|
rule_cms_id="CMS-1848-P",
|
|
fr_object_id="o",
|
|
sealed_at="2026-10-20T00:00:00Z",
|
|
)
|
|
)
|
|
mc_connect.return_value = s
|
|
mc_pfs.return_value = [_rule_doc()]
|
|
mc_fr.return_value = Rule(
|
|
title="CY2027 PFS NPRM", url="https://www.federalregister.gov/d/2026-1"
|
|
)
|
|
mc_client.return_value = _api()
|
|
|
|
result = runner.invoke(app, ["fetch-pfs-comments", "--force"])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
mc_walk.assert_called_once()
|
|
assert mc_walk.call_args.kwargs["force"] is True
|
|
|
|
|
|
@patch("bib.regulations_gov.walk_docket", return_value=WalkResult(created=1))
|
|
@patch("bib.regulations_gov.discover_docket")
|
|
@patch("bib.regulations_gov.Client")
|
|
@patch("bib.connect")
|
|
def test_fetch_docket_comments_discovers_then_walks(
|
|
mc_connect, mc_client, mc_disc, mc_walk
|
|
):
|
|
s = _store()
|
|
mc_connect.return_value = s
|
|
mc_client.return_value = _api()
|
|
mc_disc.return_value = Docket(
|
|
id="CMS-2026-2377",
|
|
rule_cms_id="CMS-1848-P",
|
|
fr_object_id="o",
|
|
comment_end_date="2026-09-14",
|
|
)
|
|
|
|
result = runner.invoke(
|
|
app, ["fetch-docket-comments", "CMS-2026-2377", "--cms-id", "CMS-1848-P"]
|
|
)
|
|
|
|
assert result.exit_code == 0, result.output
|
|
mc_disc.assert_called_once()
|
|
mc_walk.assert_called_once()
|
|
assert s.docket_get("CMS-2026-2377") is not None
|
|
|
|
|
|
@patch("bib.regulations_gov.walk_docket")
|
|
@patch("bib.regulations_gov.Client")
|
|
@patch("bib.connect")
|
|
def test_fetch_docket_comments_sealed_notice(mc_connect, mc_client, mc_walk):
|
|
s = _store()
|
|
s.docket_upsert(
|
|
Docket(id="CMS-2019-0111", fr_object_id="o", sealed_at="2020-01-01T00:00:00Z")
|
|
)
|
|
mc_connect.return_value = s
|
|
mc_client.return_value = _api()
|
|
|
|
result = runner.invoke(app, ["fetch-docket-comments", "CMS-2019-0111"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "sealed" in result.output
|
|
mc_walk.assert_not_called()
|