"""KD-6b vault UI — seven-tier test matrix (§KD-6a.F)."""
from __future__ import annotations
import asyncio
import pathlib
import secrets
from datetime import datetime, timezone
from unittest.mock import patch
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from musehub.services.knowtation_view import (
KnowtationViewAdapter,
KnowtationViewResult,
VaultUIState,
compute_harmony_diff,
frontmatter_panel_html,
harmony_diff_html,
is_vault_domain_active,
map_mcp_reason,
render_note_html,
render_wikilinks,
split_frontmatter,
structured_delta_to_view_model,
validate_note_path,
)
from musehub.services.knowtation_view.path_grammar import PathGrammarError
SMOKE_ROOT = pathlib.Path("/Users/aaronrenecarvajal/MUSE_HUB/knowtation-vault-smoke")
SMOKE_NOTE = "projects/kd-smoke/overview.md"
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture
def smoke_root() -> pathlib.Path:
if not SMOKE_ROOT.is_dir():
pytest.skip("knowtation-vault-smoke fixture not present")
return SMOKE_ROOT
async def _seed_knowtation_repo(
db: AsyncSession,
*,
owner: str = "kd6owner",
slug: str = "kd6-vault",
):
from musehub.core.genesis import compute_identity_id, compute_repo_id
from musehub.db.musehub_domain_models import MusehubDomain
from musehub.db.musehub_repo_models import MusehubRepo
domain = MusehubDomain(
domain_id=f"sha256:{secrets.token_hex(32)}",
author_slug="aaronrene",
slug="knowtation",
display_name="Knowtation",
description="Test domain",
version="0.1.0",
viewer_type="generic",
capabilities={},
)
db.add(domain)
await db.flush()
created = datetime.now(tz=timezone.utc)
owner_id = compute_identity_id(owner.encode())
repo = MusehubRepo(
repo_id=compute_repo_id(owner_id, slug, "knowtation", created.isoformat()),
name=slug,
owner=owner,
slug=slug,
visibility="public",
owner_user_id=owner_id,
marketplace_domain_id=domain.domain_id,
domain_id="knowtation",
created_at=created,
updated_at=created,
)
db.add(repo)
await db.commit()
await db.refresh(repo)
await db.refresh(domain)
return repo, domain
# ---------------------------------------------------------------------------
# Tier 1 — Unit
# ---------------------------------------------------------------------------
@pytest.mark.tier1
class TestVaultUnit:
def test_gate_active_for_knowtation_scoped_id(self) -> None:
assert is_vault_domain_active({"scoped_id": "@aaronrene/knowtation"}) is True
def test_gate_inactive_for_code_domain(self) -> None:
assert is_vault_domain_active({"scoped_id": "@gabriel/muse"}) is False
assert is_vault_domain_active(None) is False
def test_path_grammar_rejects_traversal(self) -> None:
with pytest.raises(PathGrammarError) as exc:
validate_note_path("../secret.md")
assert exc.value.reason == "PATH_INVALID"
def test_path_grammar_rejects_non_note(self) -> None:
with pytest.raises(PathGrammarError):
validate_note_path("data/index.sqlite")
def test_wikilink_resolves_and_broken_class(self) -> None:
raw = "See [[linked-target]] and [[missing-note]]"
out = render_wikilinks(
raw,
owner="o",
repo_slug="r",
note_paths=frozenset({"linked-target.md"}),
)
assert "vault-wikilink" in out
assert "broken" in out
def test_frontmatter_split_and_panel_no_script(self) -> None:
raw = "---\ntitle: T\ntags: [a]\n---\n# Body\n"
fm, body = split_frontmatter(raw)
assert fm["title"] == "T"
assert "Body" in body
panel = frontmatter_panel_html(fm)
assert "\n**bold**", owner="o", repo_slug="r")
assert "