from __future__ import annotations

import importlib.util
from pathlib import Path

MIGRATION_PATH = (
    Path(__file__).resolve().parents[1]
    / "alembic"
    / "versions"
    / "20260717_0027_knowledge_tenant_security.py"
)


def test_knowledge_security_migration_has_fixed_ancestry_and_fail_closed_downgrade() -> None:
    spec = importlib.util.spec_from_file_location("knowledge_security_0027", MIGRATION_PATH)
    assert spec is not None and spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)

    assert module.revision == "20260717_0027"
    assert module.down_revision == "20260717_0026"
    source = MIGRATION_PATH.read_text(encoding="utf-8")
    assert "knowledge_onlyoffice_sessions" in source
    assert "fk_knowledge_onlyoffice_sessions_tenant" in source
    assert "ck_knowledge_onlyoffice_sessions_scope_tenant" in source
    assert "OnlyOffice session evidence exists" in source
