"""submit_if_changed: the shared skip-when-unchanged logic behind the CLI's --if-changed flag and the worker scheduler's periodic ping (no network — submit_all/url_signature are stubbed).""" import indexnow def _ok(total=3): return {"submitted": total, "total": total, "batches": 1, "statuses": [200]} def test_skips_when_url_set_unchanged(monkeypatch, tmp_path): monkeypatch.setattr(indexnow, "_STATE_PATH", str(tmp_path / "state.txt")) monkeypatch.setattr(indexnow, "url_signature", lambda: "same-sig") (tmp_path / "state.txt").write_text("same-sig") def boom(*a, **k): raise AssertionError("must not submit when the URL set is unchanged") monkeypatch.setattr(indexnow, "submit_all", boom) assert indexnow.submit_if_changed("https://example.org") is None def test_submits_and_persists_state_when_changed(monkeypatch, tmp_path): state = tmp_path / "state.txt" monkeypatch.setattr(indexnow, "_STATE_PATH", str(state)) monkeypatch.setattr(indexnow, "url_signature", lambda: "new-sig") state.write_text("old-sig") calls = [] monkeypatch.setattr(indexnow, "submit_all", lambda base, **kw: calls.append(base) or _ok()) result = indexnow.submit_if_changed("https://example.org") assert calls == ["https://example.org"] assert result["submitted"] == result["total"] == 3 assert state.read_text() == "new-sig" def test_partial_failure_does_not_persist_state(monkeypatch, tmp_path): """A partial/failed submission must leave the prior state in place so the next attempt retries, rather than silently giving up on the missed URLs.""" state = tmp_path / "state.txt" monkeypatch.setattr(indexnow, "_STATE_PATH", str(state)) monkeypatch.setattr(indexnow, "url_signature", lambda: "new-sig") state.write_text("old-sig") monkeypatch.setattr(indexnow, "submit_all", lambda base, **kw: {"submitted": 1, "total": 3, "batches": 1, "statuses": [429]}) result = indexnow.submit_if_changed("https://example.org") assert result["submitted"] < result["total"] assert state.read_text() == "old-sig" def test_default_base_url_falls_back_to_env(monkeypatch, tmp_path): monkeypatch.setattr(indexnow, "_STATE_PATH", str(tmp_path / "state.txt")) monkeypatch.setattr(indexnow, "url_signature", lambda: "sig") monkeypatch.setenv("THERMOGRAPH_BASE_URL", "https://from-env.example") calls = [] monkeypatch.setattr(indexnow, "submit_all", lambda base, **kw: calls.append(base) or _ok()) indexnow.submit_if_changed() assert calls == ["https://from-env.example"]