Enhance chunking strategies and graph utilities for section-type transitions
- Implemented WP-26 v1.1: Section-Type-Wechsel erzwingt immer einen neuen Chunk, um konsistente Chunking-Verhalten bei unterschiedlichen section_types zu gewährleisten. - Introduced automatic Intra-Note-Edges zwischen Sektionen mit unterschiedlichen Typen, um semantische Beziehungen zu erfassen. - Updated graph utilities to support automatic edge type derivation based on section transitions. - Added unit tests for section-type changes and automatic edge generation to ensure functionality and reliability.
This commit is contained in:
1 parent
cc258008dc
commit
af3cc0a254
4 files changed
+505
-50
No files matched your search
@@ -1,12 +1,17 @@
|
||||
"""
|
||||
FILE: tests/test_wp26_section_types.py
|
||||
DESCRIPTION: Unit-Tests für WP-26 Phase 1: Section-Types und Intra-Note-Edges
|
||||
VERSION: 1.0.0
|
||||
WP-26 v1.1: Erweitert um Tests für Section-Split und automatische Edges
|
||||
VERSION: 1.1.0
|
||||
"""
|
||||
import pytest
|
||||
from app.core.chunking.chunking_parser import parse_blocks
|
||||
from app.core.chunking.chunking_models import RawBlock, Chunk
|
||||
from app.core.graph.graph_utils import normalize_provenance, _edge
|
||||
from app.core.chunking.chunking_strategies import strategy_by_heading, strategy_sliding_window
|
||||
from app.core.graph.graph_utils import (
|
||||
normalize_provenance, _edge, get_typical_edge_for,
|
||||
load_graph_schema, clear_graph_schema_cache
|
||||
)
|
||||
|
||||
|
||||
class TestSectionTypeRecognition:
|
||||
@@ -261,5 +266,218 @@ class TestNestedEdgeCallouts:
|
||||
assert len(callouts) >= 1
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# WP-26 v1.1: Tests für Section-Type-Wechsel und automatische Chunk-Splits
|
||||
# =============================================================================
|
||||
|
||||
class TestSectionTypeSplit:
|
||||
"""UT-14: Section-Type-Wechsel erzwingt neuen Chunk"""
|
||||
|
||||
def test_section_type_change_forces_new_chunk_by_heading(self):
|
||||
"""Section-Type-Wechsel erzwingt neuen Chunk (by_heading Strategie)"""
|
||||
md = """
|
||||
## Situation ^sit
|
||||
> [!section] experience
|
||||
|
||||
Die Geschichte beginnt hier.
|
||||
|
||||
## Reflexion ^ref
|
||||
> [!section] insight
|
||||
|
||||
Erkenntnisse aus der Situation.
|
||||
"""
|
||||
blocks, _ = parse_blocks(md)
|
||||
|
||||
# Konfiguration: strict=False (würde normalerweise zusammenfassen)
|
||||
config = {
|
||||
"target": 1000, # Hoher Wert, um sicherzustellen, dass nicht wegen Token-Limit gesplittet wird
|
||||
"max": 2000,
|
||||
"split_level": 2,
|
||||
"strict_heading_split": False, # Normalerweise würde zusammengefasst
|
||||
"enable_smart_edge_allocation": True
|
||||
}
|
||||
|
||||
chunks = strategy_by_heading(blocks, config, "test-note")
|
||||
|
||||
# Es sollten mindestens 2 Chunks geben (wegen Section-Type-Wechsel)
|
||||
assert len(chunks) >= 2
|
||||
|
||||
# Prüfe, dass die Chunks unterschiedliche section_types haben
|
||||
section_types = [c.section_type for c in chunks if c.section_type]
|
||||
assert "experience" in section_types
|
||||
assert "insight" in section_types
|
||||
|
||||
def test_same_section_type_follows_normal_behavior(self):
|
||||
"""Gleicher Section-Type folgt normalem Chunking-Verhalten"""
|
||||
md = """
|
||||
## Section A
|
||||
> [!section] experience
|
||||
|
||||
Content A about experience.
|
||||
|
||||
## Section B
|
||||
> [!section] experience
|
||||
|
||||
Content B also about experience.
|
||||
"""
|
||||
blocks, _ = parse_blocks(md)
|
||||
|
||||
# Konfiguration: strict=False (würde zusammenfassen bei gleichem Typ)
|
||||
config = {
|
||||
"target": 1000,
|
||||
"max": 2000,
|
||||
"split_level": 2,
|
||||
"strict_heading_split": False,
|
||||
"enable_smart_edge_allocation": True
|
||||
}
|
||||
|
||||
chunks = strategy_by_heading(blocks, config, "test-note")
|
||||
|
||||
# Bei gleichem section_type und strict=False könnten Chunks zusammengefasst werden
|
||||
# (abhängig von Token-Limits)
|
||||
# Wichtig: Alle Chunks sollten section_type "experience" haben
|
||||
for c in chunks:
|
||||
if c.section_type:
|
||||
assert c.section_type == "experience"
|
||||
|
||||
def test_sliding_window_respects_section_type_change(self):
|
||||
"""sliding_window Strategie respektiert Section-Type-Wechsel"""
|
||||
md = """
|
||||
## Part 1
|
||||
> [!section] experience
|
||||
|
||||
Short content.
|
||||
|
||||
## Part 2
|
||||
> [!section] insight
|
||||
|
||||
Another short content.
|
||||
"""
|
||||
blocks, _ = parse_blocks(md)
|
||||
|
||||
config = {
|
||||
"target": 1000, # Hoher Wert
|
||||
"max": 2000
|
||||
}
|
||||
|
||||
chunks = strategy_sliding_window(blocks, config, "test-note")
|
||||
|
||||
# Bei Section-Type-Wechsel sollte gesplittet werden
|
||||
section_types = [c.section_type for c in chunks if c.section_type]
|
||||
|
||||
# Beide Typen sollten in separaten Chunks sein
|
||||
if len(section_types) >= 2:
|
||||
assert "experience" in section_types or "insight" in section_types
|
||||
|
||||
|
||||
class TestGraphSchemaParser:
|
||||
"""UT-15: Graph-Schema Parser Tests"""
|
||||
|
||||
def test_get_typical_edge_experience_to_insight(self):
|
||||
"""Typischer Edge von experience zu insight ist 'resulted_in'"""
|
||||
# Lade Schema (falls verfügbar)
|
||||
edge = get_typical_edge_for("experience", "insight")
|
||||
|
||||
# Sollte entweder 'resulted_in' oder Fallback 'related_to' sein
|
||||
assert edge in ["resulted_in", "related_to"]
|
||||
|
||||
def test_get_typical_edge_insight_to_decision(self):
|
||||
"""Typischer Edge von insight zu decision"""
|
||||
edge = get_typical_edge_for("insight", "decision")
|
||||
|
||||
# Basierend auf graph_schema.md: foundation_for oder guides
|
||||
assert edge in ["foundation_for", "guides", "related_to"]
|
||||
|
||||
def test_get_typical_edge_fallback(self):
|
||||
"""Fallback auf 'related_to' für unbekannte Typen"""
|
||||
edge = get_typical_edge_for("unknown_type_1", "unknown_type_2")
|
||||
|
||||
# Fallback sollte immer related_to sein
|
||||
assert edge == "related_to"
|
||||
|
||||
def test_get_typical_edge_any_target(self):
|
||||
"""Fallback auf 'any' Target-Regel"""
|
||||
edge = get_typical_edge_for("experience", "unknown_target")
|
||||
|
||||
# Sollte Fallback auf "any"-Regel oder "related_to"
|
||||
assert edge is not None
|
||||
|
||||
|
||||
class TestAutomaticIntraNoteEdges:
|
||||
"""UT-16: Automatische Intra-Note-Edges zwischen Sektionen"""
|
||||
|
||||
def test_edge_payload_has_section_transition(self):
|
||||
"""Edge zwischen Sektionen enthält section_transition Metadaten"""
|
||||
edge = _edge(
|
||||
kind="resulted_in",
|
||||
scope="chunk",
|
||||
source_id="note1#c00",
|
||||
target_id="note1#c01",
|
||||
note_id="note1",
|
||||
extra={
|
||||
"provenance": "rule",
|
||||
"rule_id": "inferred:section_transition",
|
||||
"section_transition": "experience->insight",
|
||||
"is_internal": True
|
||||
}
|
||||
)
|
||||
|
||||
assert edge["is_internal"] is True
|
||||
assert edge["section_transition"] == "experience->insight"
|
||||
assert edge["provenance"] == "rule"
|
||||
|
||||
def test_inferred_section_transition_provenance(self):
|
||||
"""Provenance 'inferred:section_transition' wird korrekt normalisiert"""
|
||||
prov, hint = normalize_provenance("inferred:section_transition")
|
||||
|
||||
assert prov == "rule"
|
||||
assert hint == "schema_default"
|
||||
|
||||
|
||||
class TestRealWorldScenario:
|
||||
"""UT-17: Real-World Szenario - Krebsdiagnose Note"""
|
||||
|
||||
def test_krebsdiagnose_note_structure(self):
|
||||
"""Testet die erwartete Struktur der Krebsdiagnose-Note"""
|
||||
md = """
|
||||
## 📖 Diagnose: Glioblastom ^kontext
|
||||
|
||||
Nach der Operation gab es ein Diagnose-Gespräch.
|
||||
|
||||
## 🎭 Emotions-Check ^emotionen
|
||||
|
||||
Ich reagierte mit Zittern am Körper.
|
||||
|
||||
## 💡 Lektion ^learning
|
||||
> [!section] insight
|
||||
|
||||
Ich habe versucht die nächsten Schritte zu durchdenken.
|
||||
Meine positive Einstellung hat mir geholfen.
|
||||
"""
|
||||
blocks, _ = parse_blocks(md)
|
||||
|
||||
# Konfiguration wie structured_smart_edges
|
||||
config = {
|
||||
"target": 400,
|
||||
"max": 600,
|
||||
"split_level": 2,
|
||||
"strict_heading_split": False,
|
||||
"enable_smart_edge_allocation": True
|
||||
}
|
||||
|
||||
chunks = strategy_by_heading(blocks, config, "krebsdiagnose")
|
||||
|
||||
# Wegen Section-Type-Wechsel (None -> insight) sollte die Lektion
|
||||
# ein separater Chunk sein
|
||||
insight_chunks = [c for c in chunks if c.section_type == "insight"]
|
||||
|
||||
# Mindestens ein Chunk mit section_type "insight"
|
||||
assert len(insight_chunks) >= 1
|
||||
|
||||
# Der insight-Chunk sollte den Lektions-Inhalt enthalten
|
||||
insight_text = insight_chunks[0].text
|
||||
assert "durchdenken" in insight_text.lower() or "positive" in insight_text.lower()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
Reference in new issue
Block a user