"""Route-level tests for the rulebooks blueprint. Mirrors the structural-tests pattern in tests/test_events_routes.py: covers blueprint registration, callable handlers, and service-signature contracts. Full HTTP integration requires a live DB and auth machinery that the unit-test environment doesn't provide. """ import inspect def test_rulebooks_blueprint_registered(): from scribe.routes.rulebooks import rulebooks_bp assert rulebooks_bp.name == "rulebooks" assert rulebooks_bp.url_prefix == "/api" def test_rulebooks_blueprint_registered_in_app(): from scribe.app import create_app app = create_app() assert "rulebooks" in app.blueprints def test_rulebook_handlers_callable(): from scribe.routes import rulebooks as rb_routes for name in ( "list_rulebooks", "create_rulebook", "get_rulebook", "update_rulebook", "delete_rulebook", ): assert callable(getattr(rb_routes, name)) def test_topic_handlers_callable(): from scribe.routes import rulebooks as rb_routes for name in ( "list_topics", "create_topic", "update_topic", "delete_topic", ): assert callable(getattr(rb_routes, name)) def test_service_signatures_require_user_id(): """Routes must call services with user_id — verify the contract.""" from scribe.services import rulebooks as svc for fn_name in ( "create_rulebook", "list_rulebooks", "get_rulebook", "update_rulebook", "delete_rulebook", "find_rulebook_by_title", "create_topic", "list_topics", "get_topic", "update_topic", "delete_topic", "create_rule", "create_project_rule", "list_rules", "list_always_on_rules", "get_rule", "update_rule", "delete_rule", "subscribe_project", "unsubscribe_project", "get_applicable_rules", "suppress_rule_for_project", "unsuppress_rule_for_project", "suppress_topic_for_project", "unsuppress_topic_for_project", ): sig = inspect.signature(getattr(svc, fn_name)) assert "user_id" in sig.parameters, f"{fn_name} missing user_id param" def test_rule_model_carries_project_id_and_topic_id_nullable(): """Migration 0059 made topic_id nullable and added project_id.""" from scribe.models.rulebook import Rule assert "project_id" in Rule.__table__.columns assert Rule.__table__.columns["topic_id"].nullable is True assert Rule.__table__.columns["project_id"].nullable is True def test_create_project_rule_route_exists(): """POST /api/projects//rules — the frontend fast-path endpoint.""" from scribe.routes import rulebooks as rb_routes assert callable(getattr(rb_routes, "create_project_rule")) def test_suppression_route_handlers_exist(): """The 4 suppression endpoint handlers are registered as Python callables.""" from scribe.routes import rulebooks as rb_routes for name in ( "suppress_project_rule", "unsuppress_project_rule", "suppress_project_topic", "unsuppress_project_topic", ): assert callable(getattr(rb_routes, name)), f"missing route handler: {name}" def test_suppression_association_tables_declared(): """Migration 0060 created two new association tables; the models module must declare matching Table() objects so the rest of the service layer can reference them via .c..""" from scribe.models import rulebook as rb_models for tbl_name in ("project_rule_suppressions", "project_topic_suppressions"): tbl = getattr(rb_models, tbl_name, None) assert tbl is not None, f"models.rulebook missing {tbl_name}" cols = {c.name for c in tbl.columns} assert "project_id" in cols assert "rule_id" in cols or "topic_id" in cols def test_rulebook_model_carries_always_on(): """Migration 0058 added rulebooks.always_on — verify the model declares it.""" from scribe.models.rulebook import Rulebook assert "always_on" in Rulebook.__table__.columns col = Rulebook.__table__.columns["always_on"] assert col.nullable is False def test_update_rulebook_route_accepts_always_on(): """PATCH /api/rulebooks/ must pass always_on through to the service. The handler filters body keys against a whitelist; that whitelist needs to include always_on or toggling from the UI silently drops the field. """ import inspect as _inspect from scribe.routes import rulebooks as rb_routes src = _inspect.getsource(rb_routes.update_rulebook) assert "always_on" in src, "update_rulebook handler missing always_on in field whitelist" def test_rule_and_subscription_handlers_callable(): from scribe.routes import rulebooks as rb_routes for name in ( "list_rules", "create_rule", "get_rule", "update_rule", "delete_rule", "subscribe_project", "unsubscribe_project", "get_project_rules", ): assert callable(getattr(rb_routes, name))