from app.modules.rag.contracts.enums import RagLayer
from app.modules.rag.indexing.docs.pipeline import DocsIndexingPipeline


def test_docs_pipeline_builds_catalog_facts_sections_and_policy() -> None:
    pipeline = DocsIndexingPipeline()
    content = """---
id: api.billing.create_invoice
type: policy
domain: billing
links:
  calls_api:
    - api.billing.validate_invoice
tags: [billing]
status: active
---
# Create Invoice

## Spec Summary

Creates an invoice in billing.

## Request Contract

| field | type | required | validation |
| --- | --- | --- | --- |
| amount | decimal | yes | > 0 |

## Error Matrix

| status | error | client action |
| --- | --- | --- |
| 400 | invalid_amount | fix request |

## Rules

- metric: billing.invoice.created
- rule: amount must be positive
"""
    docs = pipeline.index_file(
        repo_id="acme/proj",
        commit_sha="abc123",
        path="docs/billing/create_invoice.md",
        content=content,
    )

    layers = {doc.layer for doc in docs}
    assert RagLayer.DOCS_MODULE_CATALOG in layers
    assert RagLayer.DOCS_FACT_INDEX in layers
    assert RagLayer.DOCS_SECTION_INDEX in layers
    assert RagLayer.DOCS_POLICY_INDEX in layers

    module_doc = next(doc for doc in docs if doc.layer == RagLayer.DOCS_MODULE_CATALOG)
    assert module_doc.metadata["module_id"] == "api.billing.create_invoice"
    assert module_doc.metadata["type"] == "policy"

    fact_texts = [doc.text for doc in docs if doc.layer == RagLayer.DOCS_FACT_INDEX]
    assert any("calls_api" in text for text in fact_texts)
    assert any("has_field" in text for text in fact_texts)
    assert any("returns_error" in text for text in fact_texts)

    section_doc = next(doc for doc in docs if doc.layer == RagLayer.DOCS_SECTION_INDEX)
    assert section_doc.metadata["section_path"]
