Files
FabledScribe/tests/test_routes_rulebooks.py
T
bvandeusen 658348f208
CI & Build / Python lint (push) Successful in 3s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Successful in 46s
CI & Build / Build & push image (push) Successful in 1m1s
feat(rules): always_on rulebook flag + Scribe-first prompt
Adds rulebooks.always_on (migration 0058) and a new list_always_on_rules
MCP tool so a session-start eager pull can fetch standing rules without
needing an active-project notion. Updates _INSTRUCTIONS so Claude calls
the new tool at session start and codifies engineering rules in Scribe
rather than CLAUDE.md / auto-memory.

Seeds FabledSword family rulebook to always_on=true on migrate, matching
its design role as the cross-project standards rulebook.

Frontend: badge in RulebookListPane for always-on rulebooks; toggle in
RulebookDetailPane header bound to a new toggleAlwaysOn store action.

This is S1+S2 of the rules-consolidation plan (Scribe task #508). S3
(project-scoped rules) and S4 (enter_project handshake) follow.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 00:56:08 -04:00

82 lines
3.1 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", "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_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))