|
| 1 | +"""Tests for ScrapeContext dataclass.""" |
| 2 | +import dataclasses |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from custom_components.multiscrape.scrape_context import ScrapeContext |
| 7 | + |
| 8 | +# ============================================================================ |
| 9 | +# Construction Tests |
| 10 | +# ============================================================================ |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.unit |
| 14 | +def test_empty_context(): |
| 15 | + """Test ScrapeContext.empty() creates context with defaults.""" |
| 16 | + ctx = ScrapeContext.empty() |
| 17 | + assert ctx.form_variables == {} |
| 18 | + assert ctx.current_value is None |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.unit |
| 22 | +def test_context_with_form_variables(): |
| 23 | + """Test ScrapeContext preserves form variables.""" |
| 24 | + ctx = ScrapeContext(form_variables={"token": "abc123", "session": "xyz"}) |
| 25 | + assert ctx.form_variables == {"token": "abc123", "session": "xyz"} |
| 26 | + assert ctx.current_value is None |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.unit |
| 30 | +def test_context_with_current_value(): |
| 31 | + """Test ScrapeContext can be constructed with a current value.""" |
| 32 | + ctx = ScrapeContext(current_value="42") |
| 33 | + assert ctx.form_variables == {} |
| 34 | + assert ctx.current_value == "42" |
| 35 | + |
| 36 | + |
| 37 | +# ============================================================================ |
| 38 | +# Immutability Tests |
| 39 | +# ============================================================================ |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.unit |
| 43 | +def test_frozen_cannot_set_form_variables(): |
| 44 | + """Test that ScrapeContext is frozen and fields cannot be mutated.""" |
| 45 | + ctx = ScrapeContext(form_variables={"key": "value"}) |
| 46 | + with pytest.raises(dataclasses.FrozenInstanceError): |
| 47 | + ctx.form_variables = {"other": "thing"} |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.unit |
| 51 | +def test_frozen_cannot_set_current_value(): |
| 52 | + """Test that current_value cannot be mutated on a frozen context.""" |
| 53 | + ctx = ScrapeContext() |
| 54 | + with pytest.raises(dataclasses.FrozenInstanceError): |
| 55 | + ctx.current_value = "new_value" |
| 56 | + |
| 57 | + |
| 58 | +# ============================================================================ |
| 59 | +# with_current_value Tests |
| 60 | +# ============================================================================ |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.unit |
| 64 | +def test_with_current_value_returns_new_instance(): |
| 65 | + """Test with_current_value returns a new context, leaving original unchanged.""" |
| 66 | + original = ScrapeContext(form_variables={"token": "abc"}) |
| 67 | + updated = original.with_current_value("scraped_data") |
| 68 | + |
| 69 | + assert updated is not original |
| 70 | + assert original.current_value is None |
| 71 | + assert updated.current_value == "scraped_data" |
| 72 | + assert updated.form_variables == {"token": "abc"} |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.unit |
| 76 | +def test_with_current_value_preserves_form_variables(): |
| 77 | + """Test with_current_value copies form variables to the new instance.""" |
| 78 | + form_vars = {"a": "1", "b": "2"} |
| 79 | + ctx = ScrapeContext(form_variables=form_vars).with_current_value("val") |
| 80 | + assert ctx.form_variables == {"a": "1", "b": "2"} |
| 81 | + |
| 82 | + |
| 83 | +# ============================================================================ |
| 84 | +# to_template_variables Tests |
| 85 | +# ============================================================================ |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.unit |
| 89 | +def test_to_template_variables_empty(): |
| 90 | + """Test empty context produces empty dict.""" |
| 91 | + assert ScrapeContext.empty().to_template_variables() == {} |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.unit |
| 95 | +def test_to_template_variables_form_only(): |
| 96 | + """Test context with form variables but no current value.""" |
| 97 | + ctx = ScrapeContext(form_variables={"token": "abc", "user": "admin"}) |
| 98 | + result = ctx.to_template_variables() |
| 99 | + assert result == {"token": "abc", "user": "admin"} |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.unit |
| 103 | +def test_to_template_variables_with_current_value(): |
| 104 | + """Test context with current value includes 'value' key.""" |
| 105 | + ctx = ScrapeContext( |
| 106 | + form_variables={"token": "abc"}, |
| 107 | + current_value="42", |
| 108 | + ) |
| 109 | + result = ctx.to_template_variables() |
| 110 | + assert result == {"token": "abc", "value": "42"} |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.unit |
| 114 | +def test_to_template_variables_current_value_overrides_form_variable(): |
| 115 | + """Test that current_value wins when form variables also has a 'value' key.""" |
| 116 | + ctx = ScrapeContext( |
| 117 | + form_variables={"value": "from_form"}, |
| 118 | + current_value="from_scrape", |
| 119 | + ) |
| 120 | + result = ctx.to_template_variables() |
| 121 | + assert result == {"value": "from_scrape"} |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.unit |
| 125 | +def test_to_template_variables_no_value_key_when_none(): |
| 126 | + """Test that 'value' key is NOT added when current_value is None.""" |
| 127 | + ctx = ScrapeContext(form_variables={"token": "abc"}) |
| 128 | + result = ctx.to_template_variables() |
| 129 | + assert "value" not in result |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.unit |
| 133 | +def test_to_template_variables_returns_copy(): |
| 134 | + """Test that modifying the returned dict does not affect the context.""" |
| 135 | + ctx = ScrapeContext(form_variables={"token": "abc"}) |
| 136 | + result = ctx.to_template_variables() |
| 137 | + result["injected"] = "hack" |
| 138 | + assert "injected" not in ctx.form_variables |
0 commit comments