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


def test_docs_pipeline_builds_new_d0_to_d5_layers() -> None:
    pipeline = DocsIndexingPipeline()
    content = """---
id: api.billing.create_invoice
type: api_method
name: create_invoice
title: Create Invoice API
module: billing
layer: application
status: draft
updated_at: 2026-03-23
tags: [billing, api]
entities: [Invoice]
parent: billing_api
children: []
links:
  - type: related_api
    target: api.billing.validate_invoice
---
# Summary

Creates an invoice in billing.

# Details

## Описание

Создает счет на оплату.

## Сценарий

**Название:**
Create invoice

**Предусловия:**
- billing service is available

**Триггер:**
- client sends create invoice request

**Основной сценарий:**
1. Validate payload.
2. Create invoice.

**Альтернативный сценарий:**
1. Reject invalid payload.

**Обработка ошибок:**
1. Return validation error.

**Постусловие:**
- Invoice is created.

## Контракт

### Входные параметры

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

### Выходные параметры

| field | type | required |
| --- | --- | --- |
| invoice_id | string | yes |

## Ошибки

| status | error | client action |
| --- | --- | --- |
| 400 | invalid_amount | fix request |
"""
    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_DOC_CHUNKS in layers
    assert RagLayer.DOCS_DOCUMENT_CATALOG in layers
    assert RagLayer.DOCS_FACT_INDEX in layers
    assert RagLayer.DOCS_ENTITY_CATALOG in layers
    assert RagLayer.DOCS_WORKFLOW_INDEX in layers
    assert RagLayer.DOCS_RELATION_GRAPH in layers

    catalog_doc = next(doc for doc in docs if doc.layer == RagLayer.DOCS_DOCUMENT_CATALOG)
    assert catalog_doc.metadata["document_id"] == "api.billing.create_invoice"
    assert catalog_doc.metadata["module"] == "billing"

    fact_texts = [doc.text for doc in docs if doc.layer == RagLayer.DOCS_FACT_INDEX]
    assert any("has_field amount" in text for text in fact_texts)
    assert any("field_required amount:yes" in text for text in fact_texts)
    assert any("returns_error invalid_amount" in text for text in fact_texts)

    entity_doc = next(doc for doc in docs if doc.layer == RagLayer.DOCS_ENTITY_CATALOG)
    assert entity_doc.metadata["entity_name"] == "Invoice"

    workflow_doc = next(doc for doc in docs if doc.layer == RagLayer.DOCS_WORKFLOW_INDEX)
    assert workflow_doc.metadata["workflow_name"] == "Create invoice"

    relation_targets = [doc.metadata["target_id"] for doc in docs if doc.layer == RagLayer.DOCS_RELATION_GRAPH]
    assert "billing_api" in relation_targets
    assert "api.billing.validate_invoice" in relation_targets

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