File size: 2,407 Bytes
364907d
 
a45e333
364907d
 
a45e333
 
 
 
 
 
 
 
364907d
a45e333
 
 
 
 
 
364907d
 
a45e333
 
 
 
 
 
 
 
 
364907d
a45e333
 
364907d
 
a45e333
 
 
364907d
 
 
a45e333
 
 
 
364907d
 
a45e333
 
 
 
 
 
 
 
364907d
a45e333
 
364907d
 
 
a45e333
 
 
 
 
 
364907d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""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 = """<think>
一些推理过程...
</think>
地点:深海图书馆 — 沉没在海底的古老图书馆
情绪:神秘
总结:探索深海图书馆"""
    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 = "<think>推理过程</think>\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!")