Open-source personal finance manager. Self-hosted, privacy-first.
fix(rules): respect category availability in previews (#1086)
Albert Dolkun33d1962bbfc5
diff --git a/backend/app/services/rule_service.py b/backend/app/services/rule_service.pyindex af25726f..edbebd38 100644--- a/backend/app/services/rule_service.py+++ b/backend/app/services/rule_service.py@@ -1315,6 +1315,10 @@ async def preview_rule(action if isinstance(action, dict) else action.model_dump() for action in actions or []]+ # The save path skips `set_category` at an unavailable target, so a preview that+ # did not would promise a move that saving never makes.+ assignable_categories = await get_assignable_category_ids(session, workspace_id)+matched = 0changed = 0sample: list[RulePreviewItem] = []@@ -1342,6 +1346,7 @@ async def preview_rule(category_already_set=tx.category_id is not Noneand not overwrite_existing_categories,skip_description=_has_manual_description(tx),+ assignable_category_ids=assignable_categories,)will_change = _rule_effect_state(draft) != beforeif will_change:diff --git a/backend/tests/test_new_rules_api.py b/backend/tests/test_new_rules_api.pyindex 8f370bc9..8e171d39 100644--- a/backend/tests/test_new_rules_api.py+++ b/backend/tests/test_new_rules_api.py@@ -1404,3 +1404,47 @@ async def test_create_rule_rejects_unknown_status_value(headers=auth_headers,)assert response.status_code in (400, 422)+++async def test_preview_rule_skips_a_hidden_category_like_the_save_path(+ client: AsyncClient,+ auth_headers,+ session: AsyncSession,+ test_categories,+ test_transactions,+):+ """A rule filing into a hidden category changes nothing, and preview says so.++ Hiding a category does not deactivate the rules pointing at it, so this is+ a reachable state rather than a corner. `apply_rule_actions` drops the+ `set_category` for a hidden target; a preview that did not would promise a+ move that saving never makes.+ """+ target = test_categories[0]+ target.is_hidden = True+ await session.commit()++ body = {+ "conditions_op": "and",+ "conditions": [{"field": "description", "op": "contains", "value": "NETFLIX"}],+ "actions": [{"op": "set_category", "value": str(target.id)}],+ }+ data = (await client.post("/api/rules/preview", json=body, headers=auth_headers)).json()+ assert data["matched"] == 1+ assert data["will_change"] == 0+ item = data["sample"][0]+ assert item["will_change"] is False+ assert item["new_category_id"] == item["current_category_id"] is None++ # Saving agrees: the transaction keeps the category it had.+ created = await client.post(+ "/api/rules",+ json={**body, "name": "Hidden target", "apply_to_existing": True},+ headers=auth_headers,+ )+ assert created.status_code == 201+ txn = (+ await client.get(f"/api/transactions/{item['id']}", headers=auth_headers)+ ).json()+ assert txn["category_id"] is None