Files
FabledScribe/tests/test_routes_design_systems.py
T
bvandeusen b0a7d9e89b
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 7s
CI & Build / integration (push) Successful in 16s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Successful in 42s
CI & Build / Build & push image (push) Successful in 34s
feat(design-systems): the master sheet — purpose tokens, not per-element values
Operator's new requirement (#2299, architecture in #2296): a design system does
not just hold tokens, it generates and manages a master CSS sheet. That settles
the milestone's open "authority mechanism" question — the record is
authoritative because the stylesheet comes out of it.

**The sheet is shaped by purpose and styles no elements.** It declares custom
properties, grouped by what they mean, and contains no `.btn-primary`, no
`table`, no `input`. That is the design, not a shortcut: a sheet that styled
elements would restate the same handful of values once per element and grow with
the UI, where purpose-named values are stated once and reused. Components live
as SNIPPETS that reference these names — a surface that already exists and
already carries prose, locations, drift checks, merge and write-path recall.

A token named after an element (`--fs-button-bg`) is the smell that the two have
been mixed; a purpose name (`--fs-action-primary`) is reused across all of them.

Alongside the CSS the endpoint returns what the text cannot say for itself:
which tokens are still valueless, and which VALUES are declared under more than
one name. The second is the operator's "reuse consistent values" constraint made
checkable — and it reports rather than refuses, because a design system
legitimately aligns colours on purpose ("Success = Moss, by design") and only a
human knows which case it is.

Mode maps to selector the way the codebase already does it: base on the root
selector, every other mode layered on `[data-theme="…"]`. The root selector is a
PARAMETER — #251 recorded that a container-scoped preview cannot use `:root`, so
hardcoding it would have made the generator useless to the preview surface.

A token the rulebook names but states no value for is emitted as a commented-out
declaration IN ITS GROUP rather than dropped. Its absence is the finding, and a
comment puts that finding where the reader already is.

Values are validated, not escaped, and this is a real boundary rather than
tidiness: design systems are shareable records (rule #47), so `red; } body {
display: none` in a system shared with you would otherwise inject CSS into your
page. A value containing `{ } ; @ < >`, a comment delimiter or a newline is
REFUSED and rendered as a comment saying so — rejecting beats stripping, since a
partially-sanitised value is one the operator never wrote and the sheet's whole
claim is that it is the record.

Not in scope, and deliberately: serving this as the app's actual stylesheet.
Generating and exposing a sheet is reversible; swapping theme.css for a
generated one is not, and it should be an explicit call rather than a side
effect.
2026-07-30 21:42:08 -04:00

118 lines
4.6 KiB
Python

"""Structural tests for the design-systems blueprint + the parity guard.
Modelled on tests/test_routes_snippets.py. The enumerations below are
deliberate: relaxing one to a pattern would stop it catching the thing it was
written for, which is a capability landing on one surface and not the other.
"""
import inspect
def test_design_systems_blueprint_registered():
from scribe.routes.design_systems import design_systems_bp
assert design_systems_bp.name == "design_systems"
assert design_systems_bp.url_prefix == "/api"
def test_design_systems_blueprint_registered_in_app():
from scribe.app import create_app
app = create_app()
assert "design_systems" in app.blueprints
def test_route_handlers_callable():
from scribe.routes import design_systems as routes
for name in (
"list_design_systems", "create_design_system", "get_design_system",
"update_design_system", "delete_design_system", "resolve_design_system",
"import_design_system", "get_design_system_stylesheet",
"list_design_tokens", "create_design_token",
"update_design_token", "delete_design_token", "set_project_design_system",
):
assert callable(getattr(routes, name))
def test_every_endpoint_is_reachable_on_the_app():
"""The handlers existing is not the same as them being routed. This catches
a decorator that was copied without its path changing — two handlers on one
rule, where the second silently never runs."""
from scribe.app import create_app
app = create_app()
rules = {
str(r.rule) for r in app.url_map.iter_rules()
if r.endpoint.startswith("design_systems.")
}
assert rules == {
"/api/design-systems",
"/api/design-systems/<int:design_system_id>",
"/api/design-systems/<int:design_system_id>/resolved",
"/api/design-systems/<int:design_system_id>/import",
"/api/design-systems/<int:design_system_id>/stylesheet",
"/api/design-systems/<int:design_system_id>/tokens",
"/api/design-tokens/<int:token_id>",
"/api/projects/<int:project_id>/design-system",
}
def test_service_functions_take_user_id():
"""Routes must call the services with user_id — verify the contract (#33)."""
from scribe.services import design_systems as svc
for fn_name in (
"create_design_system", "list_design_systems", "get_design_system",
"update_design_system", "delete_design_system", "resolve_design_system",
"create_token", "list_tokens", "update_token", "delete_token",
"set_project_design_system", "import_from_rulebook",
"stylesheet_for_system",
):
fn = getattr(svc, fn_name)
assert callable(fn)
assert "user_id" in inspect.signature(fn).parameters
def test_agent_and_web_surfaces_stay_at_parity():
"""The MCP tools and the REST routes are two callers of one service; a
capability on one has to exist on the other (rule #33).
This guard exists because the snippet surfaces drifted apart once — MCP had
no delete, the web side had no near-duplicate gate — and neither failed
anything until someone went looking.
"""
from scribe.mcp.tools import design_systems as tools
from scribe.routes import design_systems as routes
for name in (
"create_design_system", "list_design_systems", "get_design_system",
"resolve_design_system", "update_design_system", "delete_design_system",
"create_design_token", "list_design_tokens", "update_design_token",
"delete_design_token", "set_project_design_system",
"get_design_system_stylesheet",
):
assert callable(getattr(tools, name)), f"MCP tool missing: {name}"
assert callable(getattr(routes, name)), f"REST route missing: {name}"
# Import is the one verb whose handler names differ between the surfaces
# (the tool says what it reads FROM; the route is already under the system),
# so the loop above can't pair it. It still has to exist on both.
assert callable(tools.import_design_system_from_rulebook)
assert callable(routes.import_design_system)
def test_every_mcp_tool_in_the_module_is_registered():
"""A tool written but never registered is invisible to an agent, and nothing
else in the codebase would notice."""
from scribe.mcp.tools import design_systems as tools
registered = []
class _Recorder:
def tool(self, name):
registered.append(name)
return lambda fn: fn
tools.register(_Recorder())
public = {
name for name, obj in vars(tools).items()
if inspect.iscoroutinefunction(obj) and not name.startswith("_")
}
assert set(registered) == public