import pytest from ldap2nextcloud.planner import Desired, GuardError, Plan, check_guards, compute_plan from ldap2nextcloud.state import StateRow def desired(*items): return {href: Desired(href, f"k-{href}", fp) for href, fp in items} def test_new_contact_is_created(): plan = compute_plan(desired(("a.vcf", "f1")), {}, {}) assert plan.create == ["a.vcf"] and not plan.update and not plan.delete def test_changed_fingerprint_is_updated(): state = {"a.vcf": StateRow("a.vcf", "k", "old", "e1")} plan = compute_plan(desired(("a.vcf", "f1")), {"a.vcf": "e1"}, state) assert plan.update == ["a.vcf"] def test_server_etag_drift_is_updated(): state = {"a.vcf": StateRow("a.vcf", "k", "f1", "e1")} plan = compute_plan(desired(("a.vcf", "f1")), {"a.vcf": "e2"}, state) assert plan.update == ["a.vcf"] def test_unchanged_contact(): state = {"a.vcf": StateRow("a.vcf", "k", "f1", "e1")} plan = compute_plan(desired(("a.vcf", "f1")), {"a.vcf": "e1"}, state) assert plan.unchanged == ["a.vcf"] assert not (plan.create or plan.update or plan.verify or plan.delete) def test_missing_state_is_verified(): plan = compute_plan(desired(("a.vcf", "f1")), {"a.vcf": "e1"}, {}) assert plan.verify == ["a.vcf"] def test_unknown_server_card_is_deleted(): plan = compute_plan(desired(("a.vcf", "f1")), {"a.vcf": "e1", "ldap2vcard-x.vcf": "e"}, {}) assert plan.delete == ["ldap2vcard-x.vcf"] def test_state_without_server_card_is_recreated(): state = {"a.vcf": StateRow("a.vcf", "k", "f1", "e1")} plan = compute_plan(desired(("a.vcf", "f1")), {}, state) assert plan.create == ["a.vcf"] def test_lists_are_sorted(): plan = compute_plan(desired(("b.vcf", "1"), ("a.vcf", "2")), {}, {}) assert plan.create == ["a.vcf", "b.vcf"] def deletes(n): return Plan(create=[], update=[], verify=[], delete=[f"{i}.vcf" for i in range(n)], unchanged=[]) def test_guard_empty_ldap_always_aborts(): with pytest.raises(GuardError): check_guards(0, 1270, Plan([], [], [], [], []), 0.10, force=True, initial=True) def test_guard_too_many_deletions(): with pytest.raises(GuardError): check_guards(1142, 1270, deletes(128), 0.10, False, False) def test_guard_deletions_under_ratio_pass(): check_guards(1143, 1270, deletes(127), 0.10, False, False) def test_guard_force_allows_mass_deletion(): check_guards(100, 1270, deletes(1170), 0.10, True, False) def test_guard_initial_allows_mass_deletion(): check_guards(1270, 1270, deletes(1270), 0.10, False, True) def test_guard_empty_server_has_no_ratio_issue(): check_guards(10, 0, deletes(0), 0.10, False, False)