Implement backward propagation of section_type in chunking parser

- Added a new function `_propagate_section_type_backwards` to ensure that the section_type is correctly assigned to all blocks within a heading section, even if the [!section] callout appears later in the text.
- Updated the `parse_blocks` function to call this new method, enhancing the accuracy of section-type assignments.
- Modified chunking strategies to reflect the changes in section-type handling, simplifying logic related to section-type transitions.
- Expanded unit tests to validate the backward propagation of section_type, ensuring comprehensive coverage of the new functionality.
This commit is contained in:
Lars committed 2026-01-25 18:21:17 +01:00
1 parent 553a41df57
commit e86e9f2313
3 files changed
+147 -48

No files matched your search

+71 -2
View File
@@ -2,7 +2,8 @@
FILE: tests/test_wp26_section_types.py
DESCRIPTION: Unit-Tests für WP-26 Phase 1: Section-Types und Intra-Note-Edges
WP-26 v1.1: Erweitert um Tests für Section-Split und automatische Edges
VERSION: 1.1.0
WP-26 v1.3: Erweitert um Tests für rückwirkende section_type Propagation
VERSION: 1.3.0
"""
import pytest
from app.core.chunking.chunking_parser import parse_blocks
@@ -50,6 +51,36 @@ Die Geschichte beginnt hier.
# Block-ID sollte "sit" sein
assert heading_blocks[0].block_id == "sit"
def test_section_type_propagated_backwards_to_heading(self):
"""WP-26 v1.3: section_type wird rückwirkend auf das Heading propagiert."""
md = """
## Lektion ^learning
Einleitender Text ohne section callout.
Noch mehr Text hier...
> [!section] insight
Und dann kommt der eigentliche Insight-Content.
"""
blocks, _ = parse_blocks(md)
# Finde den Heading-Block
heading_blocks = [b for b in blocks if b.kind == "heading"]
assert len(heading_blocks) >= 1
# WP-26 v1.3: Das Heading sollte section_type "insight" haben
# (rückwirkend propagiert, obwohl [!section] später im Abschnitt steht)
assert heading_blocks[0].section_type == "insight", \
f"Heading sollte section_type 'insight' haben, hat aber: {heading_blocks[0].section_type}"
# Alle Paragraphen in dieser Sektion sollten auch section_type "insight" haben
paragraphs = [b for b in blocks if b.kind == "paragraph"]
for p in paragraphs:
assert p.section_type == "insight", \
f"Paragraph sollte section_type 'insight' haben: {p.text[:50]}"
class TestSectionTypeScope:
@@ -475,8 +506,10 @@ Meine positive Einstellung hat mir geholfen.
# Mindestens ein Chunk mit section_type "insight"
assert len(insight_chunks) >= 1
# Der insight-Chunk sollte den Lektions-Inhalt enthalten
# WP-26 v1.2: Der insight-Chunk MUSS die Überschrift "💡 Lektion" enthalten!
# (Nicht nur den Inhalt nach dem [!section] Callout)
insight_text = insight_chunks[0].text
assert "Lektion" in insight_text, f"Überschrift '💡 Lektion' fehlt im insight-Chunk: {insight_text[:100]}"
assert "durchdenken" in insight_text.lower() or "positive" in insight_text.lower()
def test_section_type_change_in_smart_mode_forces_split(self):
@@ -516,6 +549,42 @@ Inhalt C mit section_type "insight".
# Der letzte Chunk sollte section_type "insight" haben
insight_chunks = [c for c in chunks if c.section_type == "insight"]
assert len(insight_chunks) >= 1, "Kein Chunk mit section_type 'insight' gefunden"
def test_heading_belongs_to_new_section_with_section_type(self):
"""WP-26 v1.2: Heading gehört zur neuen Sektion wenn [!section] folgt"""
md = """
## Section A
Inhalt ohne section_type.
## Section B mit Typ
> [!section] insight
Inhalt mit section_type.
"""
blocks, _ = parse_blocks(md)
config = {
"target": 2000,
"max": 4000,
"split_level": 2,
"strict_heading_split": False,
"enable_smart_edge_allocation": True
}
chunks = strategy_by_heading(blocks, config, "test-note")
# Es sollten 2 Chunks geben
assert len(chunks) == 2, f"Erwartet 2 Chunks, bekommen: {len(chunks)}"
# Chunk 1: Section A (section_type = None)
assert chunks[0].section_type is None
assert "Section A" in chunks[0].text
assert "Section B" not in chunks[0].text # Heading B darf NICHT in Chunk 1 sein!
# Chunk 2: Section B (section_type = insight) - MUSS die Überschrift enthalten!
assert chunks[1].section_type == "insight"
assert "Section B mit Typ" in chunks[1].text, "Überschrift 'Section B mit Typ' muss im insight-Chunk sein!"
class TestBlockIdParsing: