brickfrog commited on
Commit
1f40585
·
verified ·
1 Parent(s): 903941a

Upload folder using huggingface_hub

Browse files
tests/__init__.py CHANGED
@@ -1 +0,0 @@
1
- # This file marks tests as a Python package
 
 
tests/test_card_generator.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ankigen_core.card_generator import (
2
+ _parse_model_selection,
3
+ _map_generation_mode_to_subject,
4
+ _build_generation_context,
5
+ format_cards_for_dataframe,
6
+ get_dataframe_columns,
7
+ generate_token_usage_html,
8
+ )
9
+ from ankigen_core.models import Card, CardFront, CardBack
10
+
11
+ # --- _parse_model_selection Tests ---
12
+
13
+
14
+ def test_parse_model_selection():
15
+ assert _parse_model_selection("gpt-5.2-auto") == ("gpt-5.2", None)
16
+ assert _parse_model_selection("gpt-5.2-instant") == ("gpt-5.2", "none")
17
+ assert _parse_model_selection("gpt-5.2-thinking") == ("gpt-5.2", "high")
18
+ assert _parse_model_selection("custom-model") == ("custom-model", None)
19
+ assert _parse_model_selection("") == ("gpt-5.2", None)
20
+
21
+
22
+ # --- _map_generation_mode_to_subject Tests ---
23
+
24
+
25
+ def test_map_generation_mode_to_subject():
26
+ assert _map_generation_mode_to_subject("subject", "Math") == "Math"
27
+ assert _map_generation_mode_to_subject("subject", "") == "general"
28
+ assert _map_generation_mode_to_subject("path", "") == "curriculum_design"
29
+ assert _map_generation_mode_to_subject("text", "") == "content_analysis"
30
+ assert _map_generation_mode_to_subject("unknown", "") == "general"
31
+
32
+
33
+ # --- _build_generation_context Tests ---
34
+
35
+
36
+ def test_build_generation_context():
37
+ assert _build_generation_context("text", "some text") == {
38
+ "source_text": "some text"
39
+ }
40
+ assert _build_generation_context("subject", "ignored") == {}
41
+ assert _build_generation_context("text", "") == {}
42
+
43
+
44
+ # --- format_cards_for_dataframe Tests ---
45
+
46
+
47
+ def test_format_cards_for_dataframe():
48
+ card = Card(
49
+ front=CardFront(question="Q"),
50
+ back=CardBack(answer="A", explanation="E", example="Ex"),
51
+ metadata={
52
+ "prerequisites": ["P1", "P2"],
53
+ "learning_outcomes": ["L1"],
54
+ "difficulty": "beginner",
55
+ "source_url": "http://example.com",
56
+ },
57
+ card_type="cloze",
58
+ )
59
+
60
+ formatted = format_cards_for_dataframe([card], "Test Topic")
61
+ assert len(formatted) == 1
62
+ f_card = formatted[0]
63
+ assert f_card["Index"] == "1"
64
+ assert f_card["Topic"] == "Test Topic"
65
+ assert f_card["Card_Type"] == "cloze"
66
+ assert f_card["Question"] == "Q"
67
+ assert f_card["Prerequisites"] == "P1, P2"
68
+ assert f_card["Learning_Outcomes"] == "L1"
69
+ assert f_card["Difficulty"] == "beginner"
70
+ assert f_card["Source_URL"] == "http://example.com"
71
+
72
+
73
+ def test_format_cards_for_dataframe_missing_metadata():
74
+ card = Card(
75
+ front=CardFront(question="Q"),
76
+ back=CardBack(answer="A", explanation="E", example="Ex"),
77
+ card_type="basic",
78
+ )
79
+ formatted = format_cards_for_dataframe([card], "No Metadata")
80
+ f_card = formatted[0]
81
+ assert f_card["Prerequisites"] == ""
82
+ assert f_card["Difficulty"] == "N/A"
83
+
84
+
85
+ # --- get_dataframe_columns Tests ---
86
+
87
+
88
+ def test_get_dataframe_columns():
89
+ cols = get_dataframe_columns()
90
+ assert isinstance(cols, list)
91
+ assert "Question" in cols
92
+ assert "Answer" in cols
93
+ assert "Topic" in cols
94
+
95
+
96
+ # --- generate_token_usage_html Tests ---
97
+
98
+
99
+ def test_generate_token_usage_html():
100
+ usage = {"total_tokens": 100}
101
+ html = generate_token_usage_html(usage)
102
+ assert "100 tokens" in html
103
+
104
+ html_none = generate_token_usage_html(None)
105
+ assert "No usage data" in html_none
106
+
107
+ html_invalid = generate_token_usage_html("invalid")
108
+ assert "No usage data" in html_invalid
109
+
110
+
111
+ # --- Additional Tests to meet 10+ requirement ---
112
+
113
+
114
+ def test_available_models_constant():
115
+ from ankigen_core.card_generator import AVAILABLE_MODELS
116
+
117
+ assert len(AVAILABLE_MODELS) >= 3
118
+ assert AVAILABLE_MODELS[0]["value"] == "gpt-5.2-auto"
119
+
120
+
121
+ def test_generation_modes_constant():
122
+ from ankigen_core.card_generator import GENERATION_MODES
123
+
124
+ assert len(GENERATION_MODES) >= 1
125
+ assert GENERATION_MODES[0]["value"] == "subject"
126
+
127
+
128
+ def test_format_cards_for_dataframe_empty():
129
+ assert format_cards_for_dataframe([], "Empty") == []
130
+
131
+
132
+ def test_format_cards_for_dataframe_multiple_cards():
133
+ cards = [
134
+ Card(
135
+ front=CardFront(question="Q1"),
136
+ back=CardBack(answer="A1", explanation="E1", example="X1"),
137
+ ),
138
+ Card(
139
+ front=CardFront(question="Q2"),
140
+ back=CardBack(answer="A2", explanation="E2", example="X2"),
141
+ ),
142
+ ]
143
+ formatted = format_cards_for_dataframe(cards, "Multiple", start_index=10)
144
+ assert len(formatted) == 2
145
+ assert formatted[0]["Index"] == "10"
146
+ assert formatted[1]["Index"] == "11"
tests/test_exporters.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import pandas as pd
3
+ import os
4
+ import gradio as gr
5
+ from unittest.mock import patch
6
+ from ankigen_core.exporters import (
7
+ _format_field_as_string,
8
+ _generate_timestamped_filename,
9
+ _validate_non_empty_data,
10
+ export_cards_to_csv,
11
+ export_cards_to_apkg,
12
+ export_cards_from_crawled_content,
13
+ export_dataframe_to_csv,
14
+ export_dataframe_to_apkg,
15
+ )
16
+
17
+ # --- _format_field_as_string Tests ---
18
+
19
+
20
+ def test_format_field_as_string():
21
+ assert _format_field_as_string("test") == "test"
22
+ assert _format_field_as_string(["a", "b"]) == "a, b"
23
+ assert _format_field_as_string(("c", "d")) == "c, d"
24
+ assert _format_field_as_string(None) == ""
25
+ assert _format_field_as_string(float("nan")) == ""
26
+ assert _format_field_as_string(123) == "123"
27
+
28
+
29
+ # --- _generate_timestamped_filename Tests ---
30
+
31
+
32
+ def test_generate_timestamped_filename():
33
+ filename = _generate_timestamped_filename("test", "csv", include_timestamp=False)
34
+ assert filename == "test.csv"
35
+
36
+ filename_ts = _generate_timestamped_filename("test", "csv", include_timestamp=True)
37
+ assert filename_ts.startswith("test_")
38
+ assert filename_ts.endswith(".csv")
39
+ assert len(filename_ts) > len("test.csv")
40
+
41
+
42
+ # --- _validate_non_empty_data Tests ---
43
+
44
+
45
+ def test_validate_non_empty_data():
46
+ _validate_non_empty_data([1], "test") # Should not raise
47
+ _validate_non_empty_data(pd.DataFrame({"a": [1]}), "test") # Should not raise
48
+
49
+ with pytest.raises(ValueError, match="No test provided to export."):
50
+ _validate_non_empty_data(None, "test")
51
+
52
+ with pytest.raises(ValueError, match="No test provided to export."):
53
+ _validate_non_empty_data([], "test")
54
+
55
+ with pytest.raises(ValueError, match="No test available to export."):
56
+ _validate_non_empty_data(pd.DataFrame(), "test")
57
+
58
+
59
+ # --- export_cards_to_csv Tests ---
60
+
61
+
62
+ def test_export_cards_to_csv_success(tmp_path):
63
+ cards = [
64
+ {"front": "Q1", "back": "A1", "tags": "tag1", "note_type": "Basic"},
65
+ {"front": "Q2", "back": "A2"},
66
+ ]
67
+ filename = str(tmp_path / "test.csv")
68
+ result = export_cards_to_csv(cards, filename=filename)
69
+
70
+ assert result == filename
71
+ assert os.path.exists(filename)
72
+ df = pd.read_csv(filename)
73
+ assert len(df) == 2
74
+ assert df.iloc[0]["front"] == "Q1"
75
+ assert df.iloc[1]["note_type"] == "Basic"
76
+
77
+
78
+ def test_export_cards_to_csv_missing_keys(tmp_path):
79
+ cards = [
80
+ {"front": "Q1", "back": "A1"},
81
+ {"only_front": "Q2"}, # Missing 'back', should be skipped
82
+ ]
83
+ filename = str(tmp_path / "test_missing.csv")
84
+ export_cards_to_csv(cards, filename=filename)
85
+
86
+ df = pd.read_csv(filename)
87
+ assert len(df) == 1 # Only one card should be exported
88
+
89
+
90
+ # --- export_cards_to_apkg Tests ---
91
+
92
+
93
+ @patch("genanki.Package.write_to_file")
94
+ def test_export_cards_to_apkg_success(mock_write, tmp_path):
95
+ cards = [
96
+ {"Question": "Q1", "Answer": "A1", "note_type": "Basic"},
97
+ {"Question": "Q2", "Answer": "A2", "note_type": "Cloze"},
98
+ ]
99
+ filename = str(tmp_path / "test.apkg")
100
+ result = export_cards_to_apkg(cards, filename=filename)
101
+
102
+ assert result == filename
103
+ assert mock_write.called
104
+
105
+
106
+ @patch("genanki.Package.write_to_file")
107
+ def test_export_cards_to_apkg_skip_empty_question(mock_write, tmp_path):
108
+ cards = [
109
+ {"Question": "Q1", "Answer": "A1"},
110
+ {"Question": "", "Answer": "A2"}, # Empty question, should skip
111
+ ]
112
+ filename = str(tmp_path / "test_skip.apkg")
113
+ export_cards_to_apkg(cards, filename=filename)
114
+ # genanki objects are a bit hard to inspect, but we mainly check it doesn't crash
115
+
116
+
117
+ def test_export_cards_to_apkg_zero_valid_notes():
118
+ cards = [{"Question": ""}] # No valid notes
119
+ with pytest.raises(gr.Error, match="Failed to create any valid Anki notes"):
120
+ export_cards_to_apkg(cards)
121
+
122
+
123
+ # --- export_cards_from_crawled_content Tests ---
124
+
125
+
126
+ @patch("ankigen_core.exporters.export_cards_to_csv")
127
+ def test_export_cards_from_crawled_content_csv(mock_csv):
128
+ cards = [{"front": "Q", "back": "A"}]
129
+ export_cards_from_crawled_content(cards, export_format="csv")
130
+ mock_csv.assert_called_once()
131
+
132
+
133
+ @patch("ankigen_core.exporters.export_cards_to_apkg")
134
+ def test_export_cards_from_crawled_content_apkg(mock_apkg):
135
+ cards = [{"front": "Q", "back": "A"}]
136
+ export_cards_from_crawled_content(cards, export_format="apkg")
137
+ mock_apkg.assert_called_once()
138
+
139
+
140
+ def test_export_cards_from_crawled_content_unsupported():
141
+ with pytest.raises(ValueError, match="Unsupported export format"):
142
+ export_cards_from_crawled_content([{"f": "b"}], export_format="pdf")
143
+
144
+
145
+ # --- DataFrame Export Tests ---
146
+
147
+
148
+ def test_export_dataframe_to_csv_success(tmp_path):
149
+ df = pd.DataFrame({"Question": ["Q1"], "Answer": ["A1"]})
150
+ # We need to mock gr.Info to avoid issues if it requires a running Gradio app
151
+ with patch("gradio.Info"):
152
+ filename = export_dataframe_to_csv(
153
+ df, filename_suggestion=str(tmp_path / "suggested.csv")
154
+ )
155
+ assert filename is not None
156
+ assert os.path.exists(filename)
157
+
158
+
159
+ def test_export_dataframe_to_csv_empty():
160
+ with pytest.raises(gr.Error, match="No card data available"):
161
+ export_dataframe_to_csv(None)
162
+
163
+
164
+ @patch("ankigen_core.exporters.export_cards_to_apkg")
165
+ def test_export_dataframe_to_apkg_success(mock_apkg):
166
+ df = pd.DataFrame({"Question": ["Q1"], "Answer": ["A1"], "Card_Type": ["Basic"]})
167
+ export_dataframe_to_apkg(df, output_path="test.apkg", deck_name="Test Deck")
168
+ mock_apkg.assert_called_once()
169
+ # Check that cards were processed correctly
170
+ args, kwargs = mock_apkg.call_args
171
+ processed_cards = args[0]
172
+ assert len(processed_cards) == 1
173
+ assert processed_cards[0]["Question"] == "Q1"
174
+ assert processed_cards[0]["note_type"] == "Basic"
tests/test_models.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from pydantic import ValidationError
3
+ from ankigen_core.models import (
4
+ Step,
5
+ Subtopics,
6
+ Topics,
7
+ CardFront,
8
+ CardBack,
9
+ Card,
10
+ CardList,
11
+ ConceptBreakdown,
12
+ CardGeneration,
13
+ LearningSequence,
14
+ )
15
+
16
+
17
+ def test_step_model():
18
+ step = Step(explanation="expl", output="out")
19
+ assert step.explanation == "expl"
20
+ assert step.output == "out"
21
+
22
+ with pytest.raises(ValidationError):
23
+ Step(explanation="expl") # missing output
24
+
25
+
26
+ def test_subtopics_model():
27
+ sub = Subtopics(steps=[Step(explanation="e", output="o")], result=["r1"])
28
+ assert len(sub.steps) == 1
29
+ assert sub.result == ["r1"]
30
+
31
+
32
+ def test_topics_model():
33
+ topics = Topics(result=[Subtopics(steps=[], result=[])])
34
+ assert len(topics.result) == 1
35
+
36
+
37
+ def test_card_front_model():
38
+ cf = CardFront(question="Q?")
39
+ assert cf.question == "Q?"
40
+ assert CardFront().question is None
41
+
42
+
43
+ def test_card_back_model():
44
+ cb = CardBack(answer="A", explanation="E", example="Ex")
45
+ assert cb.answer == "A"
46
+ assert cb.explanation == "E"
47
+ assert cb.example == "Ex"
48
+
49
+
50
+ def test_card_model():
51
+ cf = CardFront(question="Q")
52
+ cb = CardBack(answer="A", explanation="E", example="Ex")
53
+ card = Card(front=cf, back=cb, metadata={"key": "val"}, card_type="cloze")
54
+ assert card.front.question == "Q"
55
+ assert card.card_type == "cloze"
56
+ assert card.metadata["key"] == "val"
57
+
58
+
59
+ def test_card_list_model():
60
+ cf = CardFront(question="Q")
61
+ cb = CardBack(answer="A", explanation="E", example="Ex")
62
+ card = Card(front=cf, back=cb)
63
+ cl = CardList(topic="test", cards=[card])
64
+ assert cl.topic == "test"
65
+ assert len(cl.cards) == 1
66
+
67
+
68
+ def test_concept_breakdown_model():
69
+ cb = ConceptBreakdown(
70
+ main_concept="C",
71
+ prerequisites=["P"],
72
+ learning_outcomes=["L"],
73
+ difficulty_level="beginner",
74
+ )
75
+ assert cb.difficulty_level == "beginner"
76
+
77
+
78
+ def test_card_generation_model():
79
+ cf = CardFront(question="Q")
80
+ cb = CardBack(answer="A", explanation="E", example="Ex")
81
+ card = Card(front=cf, back=cb)
82
+ cg = CardGeneration(
83
+ concept="C", thought_process="T", verification_steps=["V"], card=card
84
+ )
85
+ assert cg.concept == "C"
86
+ assert cg.card.front.question == "Q"
87
+
88
+
89
+ def test_learning_sequence_model():
90
+ ls = LearningSequence(
91
+ topic="T",
92
+ concepts=[],
93
+ cards=[],
94
+ suggested_study_order=[],
95
+ review_recommendations=[],
96
+ )
97
+ assert ls.topic == "T"
tests/test_utils.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import hashlib
3
+ from unittest.mock import MagicMock
4
+ from ankigen_core.utils import (
5
+ ResponseCache,
6
+ RateLimiter,
7
+ strip_html_tags,
8
+ fetch_webpage_text,
9
+ setup_logging,
10
+ get_logger,
11
+ )
12
+ import requests
13
+
14
+ # --- ResponseCache Tests ---
15
+
16
+
17
+ def test_cache_set_get():
18
+ cache = ResponseCache(maxsize=2)
19
+ cache.set("prompt1", "model1", "response1")
20
+ assert cache.get("prompt1", "model1") == "response1"
21
+ assert cache.hits == 1
22
+ assert cache.misses == 0
23
+
24
+
25
+ def test_cache_miss():
26
+ cache = ResponseCache(maxsize=2)
27
+ assert cache.get("nonexistent", "model1") is None
28
+ assert cache.hits == 0
29
+ assert cache.misses == 1
30
+
31
+
32
+ def test_cache_eviction():
33
+ cache = ResponseCache(maxsize=2)
34
+ cache.set("p1", "m1", "r1")
35
+ cache.set("p2", "m2", "r2")
36
+ cache.set("p3", "m3", "r3") # Should evict p1
37
+
38
+ assert cache.get("p1", "m1") is None
39
+ assert cache.get("p2", "m2") == "r2"
40
+ assert cache.get("p3", "m3") == "r3"
41
+
42
+
43
+ def test_cache_lru_update():
44
+ cache = ResponseCache(maxsize=2)
45
+ cache.set("p1", "m1", "r1")
46
+ cache.set("p2", "m2", "r2")
47
+ cache.get("p1", "m1") # p1 is now MRU
48
+ cache.set("p3", "m3", "r3") # Should evict p2
49
+
50
+ assert cache.get("p2", "m2") is None
51
+ assert cache.get("p1", "m1") == "r1"
52
+ assert cache.get("p3", "m3") == "r3"
53
+
54
+
55
+ def test_cache_clear():
56
+ cache = ResponseCache(maxsize=2)
57
+ cache.set("p1", "m1", "r1")
58
+ cache.hits = 5
59
+ cache.misses = 2
60
+ cache.clear()
61
+
62
+ assert cache.get("p1", "m1") is None
63
+ assert cache.hits == 0
64
+ assert cache.misses == 1 # Miss from the get() call above
65
+
66
+
67
+ def test_cache_key_hashing():
68
+ cache = ResponseCache()
69
+ prompt = "test prompt"
70
+ model = "gpt-4"
71
+ expected_key = hashlib.md5(f"{model}:{prompt}".encode("utf-8")).hexdigest()
72
+ assert cache._create_key(prompt, model) == expected_key
73
+
74
+
75
+ # --- RateLimiter Tests ---
76
+
77
+
78
+ def test_rate_limiter_init_invalid():
79
+ with pytest.raises(ValueError, match="Requests per second must be positive."):
80
+ RateLimiter(0)
81
+ with pytest.raises(ValueError, match="Requests per second must be positive."):
82
+ RateLimiter(-1)
83
+
84
+
85
+ def test_rate_limiter_wait(mocker):
86
+ # Mock time.monotonic and time.sleep
87
+ mock_monotonic = mocker.patch("time.monotonic")
88
+ mock_sleep = mocker.patch("time.sleep")
89
+
90
+ # First call: current_time = 10.0, last_request = 0.0
91
+ # Interval = 1.0 (for 1 req/sec)
92
+ # diff = 10.0 >= 1.0, no sleep
93
+ mock_monotonic.side_effect = [10.0, 10.1, 10.2, 10.3]
94
+
95
+ limiter = RateLimiter(1.0)
96
+ limiter.wait()
97
+ assert mock_sleep.call_count == 0
98
+
99
+ # Second call: current_time = 10.2, last_request = 10.1 (from end of first wait)
100
+ # diff = 0.1 < 1.0, sleep for 0.9
101
+ limiter.wait()
102
+ mock_sleep.assert_called_once_with(pytest.approx(0.9))
103
+
104
+
105
+ # --- strip_html_tags Tests ---
106
+
107
+
108
+ def test_strip_html_tags_normal():
109
+ html = "<div>Hello <b>World</b></div>"
110
+ assert strip_html_tags(html) == "Hello World"
111
+
112
+
113
+ def test_strip_html_tags_empty():
114
+ assert strip_html_tags("") == ""
115
+
116
+
117
+ def test_strip_html_tags_non_string():
118
+ assert strip_html_tags(None) == "None"
119
+ assert strip_html_tags(123) == "123"
120
+
121
+
122
+ # --- fetch_webpage_text Tests ---
123
+
124
+
125
+ def test_fetch_webpage_text_success(mocker):
126
+ mock_get = mocker.patch("requests.get")
127
+ mock_response = MagicMock()
128
+ mock_response.text = "<html><body><main>Relevant content</main></body></html>"
129
+ mock_response.status_code = 200
130
+ mock_get.return_value = mock_response
131
+
132
+ result = fetch_webpage_text("http://example.com")
133
+ assert result == "Relevant content"
134
+
135
+
136
+ def test_fetch_webpage_text_article_fallback(mocker):
137
+ mock_get = mocker.patch("requests.get")
138
+ mock_response = MagicMock()
139
+ mock_response.text = "<html><body><article>Article content</article></body></html>"
140
+ mock_get.return_value = mock_response
141
+
142
+ result = fetch_webpage_text("http://example.com")
143
+ assert result == "Article content"
144
+
145
+
146
+ def test_fetch_webpage_text_body_fallback(mocker):
147
+ mock_get = mocker.patch("requests.get")
148
+ mock_response = MagicMock()
149
+ mock_response.text = "<html><body>Body content</body></html>"
150
+ mock_get.return_value = mock_response
151
+
152
+ result = fetch_webpage_text("http://example.com")
153
+ assert result == "Body content"
154
+
155
+
156
+ def test_fetch_webpage_text_network_error(mocker):
157
+ mock_get = mocker.patch("requests.get")
158
+ mock_get.side_effect = requests.exceptions.RequestException("Connection failed")
159
+
160
+ with pytest.raises(ConnectionError, match="Could not fetch URL"):
161
+ fetch_webpage_text("http://example.com")
162
+
163
+
164
+ def test_fetch_webpage_text_empty_content(mocker):
165
+ mock_get = mocker.patch("requests.get")
166
+ mock_response = MagicMock()
167
+ mock_response.text = "<html><body></body></html>"
168
+ mock_get.return_value = mock_response
169
+
170
+ assert fetch_webpage_text("http://example.com") == ""
171
+
172
+
173
+ # --- Logging Tests ---
174
+
175
+
176
+ def test_get_logger_singleton():
177
+ logger1 = get_logger()
178
+ logger2 = get_logger()
179
+ assert logger1 is logger2
180
+
181
+
182
+ def test_setup_logging_idempotent():
183
+ # Calling setup_logging twice should return same logger
184
+ logger1 = setup_logging()
185
+ logger2 = setup_logging()
186
+ assert logger1 is logger2