import pytest from src.nodes.entity_extractor import extract_entities from src.state import init_state def test_extract_order_id_32_char(): """Test extraction of 32-character hex order ID""" state = init_state("Where is order ef4e1688abcd1234ef4e1688abcd1234?") result = extract_entities(state) assert result["last_order_id"] == "ef4e1688abcd1234ef4e1688abcd1234" def test_extract_short_order_id(): """Test extraction of short order ID format""" state = init_state("Check status of ORD-12345") result = extract_entities(state) assert result["last_order_id"] == "ORD-12345" def test_pronoun_resolution_that(): """Test pronoun 'that' resolves to previous order ID""" state = init_state("What about that?") state["last_order_id"] = "previous-order-123" result = extract_entities(state) assert result["last_order_id"] == "previous-order-123" assert any("pronoun" in log.lower() for log in result["error_log"]) def test_pronoun_resolution_it(): """Test pronoun 'it' resolves to session context""" state = init_state("When will it arrive?") state["session_context"] = {"order_id": "session-order-456"} result = extract_entities(state) assert result["last_order_id"] == "session-order-456" def test_no_pronoun_without_context(): """Test pronoun without context doesn't create false positive""" state = init_state("What is that?") result = extract_entities(state) assert result.get("last_order_id") is None def test_extract_category_electronics(): """Test category extraction for electronics""" state = init_state("Show me electronics products") result = extract_entities(state) assert result["last_category"] == "electronics" def test_extract_category_computers(): """Test category extraction for computers""" state = init_state("I need a new computer") result = extract_entities(state) # Should match 'computer' which maps to computers category assert result["last_category"] in ["computer", "computers"] def test_no_category_in_generic_query(): """Test no category extracted from generic query""" state = init_state("Where is my order?") result = extract_entities(state) assert result.get("last_category") is None def test_explicit_order_id_overrides_pronoun(): """Test explicit order ID takes precedence over pronoun""" state = init_state("Check that order: abc123def456abc123def456abc123de") state["last_order_id"] = "old-order-999" result = extract_entities(state) assert result["last_order_id"] == "abc123def456abc123def456abc123de" def test_case_insensitive_order_id(): """Test order ID extraction is case insensitive""" state = init_state("ORDER-ABCD1234") result = extract_entities(state) assert result["last_order_id"] is not None