43a860c3ac
Rules can now belong to either a rulebook topic OR a single project,
enforced by a CHECK constraint (exactly-one of topic_id/project_id).
Adds the create_project_rule MCP tool + REST endpoint, surfaces
project-scoped rules in get_project/get_task/start_planning under a
new project_rules field, and adds a project Rules tab section with an
inline create form so the operator can author project rules from the
UI without rulebook ceremony.
- migration 0059: rules.project_id (FK projects ON DELETE CASCADE),
topic_id now nullable, CHECK ck_rule_topic_xor_project, index on
project_id
- model: Rule gains project_id; to_dict exposes it
- service: create_project_rule with project-ownership guard; list_rules
with project_id filter UNIONs subscription-derived + project-scoped;
get_applicable_rules adds a project_rules field; get_rule / update_rule
/ delete_rule fetch via a shared _fetch_owned_rule that handles both
rulebook and project ownership paths
- trash: project delete cascades to project-scoped rules
- MCP: create_project_rule tool registered; _INSTRUCTIONS mentions both
create_rule and create_project_rule paths
- REST: POST /api/projects/<id>/rules (statement required, title derived
if omitted)
- frontend: Rule type gains nullable topic_id + project_id; createProjectRule
client; ProjectRulesTab.vue gains a "Project rules" section with inline
create form and per-rule expand/delete
- tests: register count → 18; create_project_rule unit tests (required
fields, title derivation, explicit-title pass-through); applicable_rules
shape tests now include project_rules; trash cascade test updated to
expect 5 executions
S1+S2 (always_on flag + Scribe-first prompt) shipped in 658348f.
S4 (enter_project handshake) follows.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
97 lines
3.7 KiB
Python
97 lines
3.7 KiB
Python
"""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 fabledassistant.routes.rulebooks import rulebooks_bp
|
|
assert rulebooks_bp.name == "rulebooks"
|
|
assert rulebooks_bp.url_prefix == "/api"
|
|
|
|
|
|
def test_rulebooks_blueprint_registered_in_app():
|
|
from fabledassistant.app import create_app
|
|
app = create_app()
|
|
assert "rulebooks" in app.blueprints
|
|
|
|
|
|
def test_rulebook_handlers_callable():
|
|
from fabledassistant.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 fabledassistant.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 fabledassistant.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",
|
|
):
|
|
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 fabledassistant.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/<id>/rules — the frontend fast-path endpoint."""
|
|
from fabledassistant.routes import rulebooks as rb_routes
|
|
assert callable(getattr(rb_routes, "create_project_rule"))
|
|
|
|
|
|
def test_rulebook_model_carries_always_on():
|
|
"""Migration 0058 added rulebooks.always_on — verify the model declares it."""
|
|
from fabledassistant.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/<id> 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 fabledassistant.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 fabledassistant.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))
|