Files
FabledGrimoire/tests/test_integration_rule_surfacing.py
T
bvandeusenandClaude Opus 5.5 0f5ef0be63
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 56s
CI & Build / integration (push) Successful in 1m4s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / Build & push image (push) Successful in 44s
rename(547): the server is grimoire — package, image, DB defaults, backups
Step 2 of the Scribe -> Grimoire rename.

- src/scribe -> src/grimoire, and every import, patch target, module path,
  hypercorn entry point and `python -m grimoire.restore` with it; pyproject,
  uv.lock and the frontend package are named grimoire.
- Image git.fabledsword.com/bvandeusen/fabledgrimoire; CI's Postgres service
  runs as grimoire / grimoire_test.
- Compose files default the DB user, database and (prod) volume to grimoire,
  each overridable from .env, so an install created as Scribe keeps its data
  by setting POSTGRES_USER, POSTGRES_DB and PGDATA_VOLUME.
- Backups are written as grimoire-<ts>.tar with format grimoire-backup-tar.
  Archives named scribe-* with the scribe-backup-tar manifest still restore
  and are still counted at the destination (folder and S3), with tests.
- Exports, the MCP auth realm, the ASGI scope key and the persistent logger
  carry the new name.

Kept on purpose: the secret-sealing key label stays "scribe.secrets.v1",
because every stored secret was derived under it.

Plugin identity (plugin name, hooks, env vars, .scribe marker, skills) is
step 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:50:45 -04:00

144 lines
6.7 KiB
Python

"""Real-Postgres tests for WHICH rules a project's listing names (milestone
307 step 5, narrowed by 394 and 414).
A session receives rules by retrieval, scoped by a rule's home (see
test_integration_rule_scope). This is the other surface — the listing a
planning read carries — and what mocks can't prove about it:
1. A global rule tagged to an area arrives in the listing of a project that
works in that area, and not before. Area matching is DETERMINISTIC: a tag
comparison, never a similarity score.
2. An UNTAGGED global rule is not listed: it applies everywhere and arrives by
retrieval, and listing every one under every project would say nothing.
3. A `co_surfaces` partner arrives with its other half — without it, merging
two rules that must arrive together looks like the only fix — unless the
partner lives on a different project, because an edge is not a way in.
"""
import pytest
import pytest_asyncio
from grimoire.models import async_session
from grimoire.models.project import Project
from grimoire.services import canonical_systems as canonical_svc
from grimoire.services import rulebooks as rulebooks_svc
from grimoire.services import systems as systems_svc
from tests.helpers import ensure_user
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
@pytest_asyncio.fixture
async def world():
"""A project with one rule of its own, and a rulebook of global rules."""
async with async_session() as s:
owner = await ensure_user(s, "surfacing_owner")
project = Project(user_id=owner.id, title="Surfacing target")
elsewhere = Project(user_id=owner.id, title="Another project")
s.add_all([project, elsewhere])
await s.flush()
ids = {"owner": owner.id, "pid": project.id, "elsewhere": elsewhere.id}
await s.commit()
book = await rulebooks_svc.create_rulebook(ids["owner"], "Family standards")
topic = await rulebooks_svc.create_topic(book.id, ids["owner"], "release")
await rulebooks_svc.create_rule(
topic.id, ids["owner"], "dev is home", "Work on dev.",
when_to_apply="before pushing a branch",
)
own = await rulebooks_svc.create_project_rule(
ids["pid"], ids["owner"], "Between batches, keep stacking", "Keep going.",
when_to_apply="when a batch goes green",
)
ids.update({"book": book.id, "topic": topic.id, "own": own.id})
return ids
async def _titles(ids) -> set[str]:
applicable = await rulebooks_svc.get_applicable_rules(ids["pid"], ids["owner"])
return {r["title"] for r in applicable["rules"]}
@pytest.mark.integration
async def test_an_untagged_global_rule_is_not_listed(world):
"""It applies here — and everywhere — so naming it under this project says
nothing a reader can act on. The project's own rule is always listed."""
applicable = await rulebooks_svc.get_applicable_rules(world["pid"], world["owner"])
assert "dev is home" not in await _titles(world)
assert [r["title"] for r in applicable["project_rules"]] == [
"Between batches, keep stacking"]
@pytest.mark.integration
async def test_a_tagged_global_rule_is_listed_for_a_project_that_works_in_its_area(world):
"""The payoff: the tag match carries it in deterministically. The project
reaches the area through its own System's canonical_id — its local NAME is
irrelevant, which is the whole reason the catalog exists."""
area = await canonical_svc.find_by_name("CI & Release")
assert area is not None, "migration 0087 seeds the standard vocabulary"
rule = await rulebooks_svc.create_rule(
world["topic"], world["owner"], "Release tagging", "Derive the tag.",
when_to_apply="when cutting a release",
)
await rulebooks_svc.set_rule_systems(rule.id, world["owner"], [area.id])
# Still absent: the project has no Systems at all yet.
assert "Release tagging" not in await _titles(world)
# The project names the area with its OWN word, mapped to the same canon.
local = await systems_svc.create_system(
world["owner"], world["pid"], "CI & runners", description="ours",
)
await canonical_svc.set_system_canonical(world["owner"], local.id, area.id)
surfaced = await rulebooks_svc.get_applicable_rules(world["pid"], world["owner"])
hit = [r for r in surfaced["rules"] if r["title"] == "Release tagging"]
assert hit, "a tagged global rule must be listed for a project working in that area"
assert [s["name"] for s in hit[0]["systems"]] == ["CI & Release"]
@pytest.mark.integration
async def test_co_surfaces_drags_in_the_half_that_would_have_been_missed(world):
"""Two rules that must arrive together get folded into one because either
could surface without the other. This is the edge that makes that
unnecessary: the partner arrives even though nothing else selected
it, and says why it is here."""
partner = await rulebooks_svc.create_rule(
world["topic"], world["owner"], "Version names are labels",
"A name decides nothing.",
when_to_apply="when naming a build",
)
# Tagged to an area this project does NOT work in, so nothing but the edge
# can bring it in — otherwise this test could pass without the edge.
area = await canonical_svc.find_by_name("CI & Release")
assert area is not None, "migration 0087 seeds the standard vocabulary"
await rulebooks_svc.set_rule_systems(partner.id, world["owner"], [area.id])
assert "Version names are labels" not in await _titles(world), (
"the partner must be unreachable on its own, or this test cannot fail"
)
await rulebooks_svc.add_rule_relation(
world["owner"], world["own"], partner.id, "co_surfaces",
note="they fail together",
)
surfaced = await rulebooks_svc.get_applicable_rules(world["pid"], world["owner"])
hit = [r for r in surfaced["rules"] if r["title"] == "Version names are labels"]
assert hit, "a co_surfaces partner must arrive with its other half"
assert hit[0]["via"] == "co_surfaces"
@pytest.mark.integration
async def test_an_edge_is_not_a_way_into_another_project(world):
"""A partner that lives on a different project belongs to that project.
The edge says the two fail together; it does not make one project's rule
apply to another."""
foreign = await rulebooks_svc.create_project_rule(
world["elsewhere"], world["owner"], "Another project's rule", "Not here.",
when_to_apply="working on the other project",
)
await rulebooks_svc.add_rule_relation(
world["owner"], world["own"], foreign.id, "co_surfaces",
)
surfaced = await rulebooks_svc.get_applicable_rules(world["pid"], world["owner"])
assert "Another project's rule" not in {r["title"] for r in surfaced["rules"]}