"""Unit tests for portfolio priority sorting — AP1.8a.""" from steering.portfolio.priority import sort_candidates_by_portfolio_rank def test_sort_candidates_by_portfolio_rank(): candidates = [ {"kind": "a", "initiative_id": "low"}, {"kind": "b", "initiative_id": "high"}, {"kind": "c", "initiative_id": "mid"}, ] rank_map = {"high": 0, "mid": 1, "low": 2} sorted_items = sort_candidates_by_portfolio_rank(candidates, rank_map) assert [c["initiative_id"] for c in sorted_items] == ["high", "mid", "low"] assert sorted_items[0]["portfolio_rank"] == 0 def test_null_rank_sorts_last(): candidates = [ {"initiative_id": "unknown"}, {"initiative_id": "ranked"}, ] rank_map = {"ranked": 0} sorted_items = sort_candidates_by_portfolio_rank(candidates, rank_map) assert sorted_items[0]["initiative_id"] == "ranked" def test_portfolio_reorder_api(client): from tests.test_initiatives_actions import _auth, _create_initiative, _login from tests.factories import provision_user_in_tenant user = provision_user_in_tenant(tenant_role="member") token = _login(client, user) first = _create_initiative(client, token, title="Alpha").json() second = _create_initiative(client, token, title="Beta").json() reorder = client.post( "/api/workspace/portfolio/reorder", json={"initiative_ids": [second["id"], first["id"]]}, headers=_auth(token), ) assert reorder.status_code == 200 ordered = reorder.json() assert ordered[0]["id"] == second["id"] assert ordered[0]["portfolio_rank"] == 0 assert ordered[1]["portfolio_rank"] == 1