"""Tests for dream parser.""" from core.parser import _parse_text_format, _fallback_entity_extraction, _strip_thinking def test_parse_text_format_basic(): """Test text format parsing with standard output.""" text = """地点:水晶城市 — 漂浮的世界,由光点组成 人物:银发精灵守望者 — 守护者与引导者 物品:水晶碎片 — 散落的光源与线索 情绪:希望、期待 总结:梦见在水晶城市中寻找碎片""" result = _parse_text_format(text) assert result is not None assert len(result["entities"]) == 3 assert result["entities"][0]["name"] == "水晶城市" assert result["entities"][0]["type"] == "location" assert result["entities"][1]["type"] == "npc" assert "希望" in result["emotions"] assert "水晶" in result["summary"] def test_parse_text_format_with_thinking(): """Test that thinking tags are stripped before parsing.""" text = """ 一些推理过程... 地点:深海图书馆 — 沉没在海底的古老图书馆 情绪:神秘 总结:探索深海图书馆""" result = _parse_text_format(text) assert result is not None assert len(result["entities"]) == 1 assert result["entities"][0]["name"] == "深海图书馆" def test_parse_text_format_no_entities(): """Test that empty text returns None.""" result = _parse_text_format("这是一段不相关的文本") assert result is None def test_strip_thinking(): """Test thinking tag removal.""" text = "推理过程\n实际内容" assert _strip_thinking(text) == "实际内容" def test_fallback_extraction_from_dream(): """Test regex-based fallback entity extraction from dream text.""" dream = "昨晚梦见自己在一座漂浮的水晶城市中飞翔" entities = _fallback_entity_extraction("", dream) assert len(entities) >= 1 def test_fallback_extraction_no_entities(): """Test fallback with no recognizable entities.""" entities = _fallback_entity_extraction("", "I just had a vague dream") assert isinstance(entities, list) if __name__ == "__main__": test_parse_text_format_basic() test_parse_text_format_with_thinking() test_parse_text_format_no_entities() test_strip_thinking() test_fallback_extraction_from_dream() test_fallback_extraction_no_entities() print("All parser tests passed!")