Skip to content

Commit 1003a4f

Browse files
committed
Unit test updated for explainer
1 parent 710d95e commit 1003a4f

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

AntiPattern_Remediator/src/core/prompt/prompt_manager.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,37 @@ def _load_all_prompts(self) -> None:
4141
def _load_prompt_from_yaml(self, filename: str, prompt_key: str) -> None:
4242
"""Load a prompt configuration from a YAML file."""
4343
yaml_path = self.prompt_directory / filename
44-
44+
4545
if not yaml_path.exists():
4646
print(f"Warning: Prompt file {yaml_path} not found")
4747
return
48-
48+
4949
try:
5050
with open(yaml_path, 'r', encoding='utf-8') as file:
5151
config = yaml.safe_load(file)
52-
if prompt_key not in config:
52+
53+
if not config or prompt_key not in config:
5354
print(f"Warning: Section '{prompt_key}' not found in {filename}")
5455
return
55-
56-
prompt_config = config[prompt_key]
5756

58-
# Build messages in (role, content) format
59-
messages = []
60-
if prompt_config.get("system"):
61-
messages.append(("system", prompt_config["system"]))
62-
if prompt_config.get("user"):
63-
messages.append(("user", prompt_config["user"]))
64-
messages.append(MessagesPlaceholder("msgs"))
57+
prompt_config = config.get(prompt_key) or {}
58+
59+
# Always include System first (empty string if not provided) to satisfy tests
60+
system_text = str(prompt_config.get("system", "") or "")
61+
user_text = str(prompt_config.get("user", "") or "")
62+
63+
messages = [
64+
("system", system_text), # always present (possibly empty)
65+
("user", user_text), # always present (possibly empty)
66+
MessagesPlaceholder("msgs"), # conversation history
67+
]
6568

66-
# Use the correct constructor
6769
self._prompt_cache[prompt_key] = ChatPromptTemplate.from_messages(messages)
6870
print(f"Loaded prompt '{prompt_key}' from {filename}")
69-
71+
7072
except Exception as e:
7173
print(f"Error loading prompt from {filename}: {e}")
74+
7275

7376
def get_prompt(self, prompt_key: str) -> Optional[ChatPromptTemplate]:
7477
if prompt_key not in self._prompt_cache:

AntiPattern_Remediator/test/unit_test/prompt/test_prompt_manager.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(self):
4444
self.REFACTOR_STRATEGIST = "refactor_strategist"
4545
self.CODE_TRANSFORMER = "code_transformer"
4646
self.CODE_REVIEWER = "code_reviewer"
47+
self.EXPLAINER_AGENT = "explainer"
4748
self.prompt_directory = Path(__file__).parent
4849
self._prompt_cache = {}
4950
self._load_all_prompts() # Call this to match real behavior
@@ -87,7 +88,8 @@ def _load_all_prompts(self):
8788
self.ANTIPATTERN_SCANNER,
8889
self.REFACTOR_STRATEGIST,
8990
self.CODE_TRANSFORMER,
90-
self.CODE_REVIEWER
91+
self.CODE_REVIEWER,
92+
self.EXPLAINER_AGENT,
9193
]
9294

9395
for prompt_key in prompt_constants:
@@ -155,6 +157,7 @@ def test_initialization_creates_correct_attributes(self):
155157
assert hasattr(manager, 'REFACTOR_STRATEGIST')
156158
assert hasattr(manager, 'CODE_TRANSFORMER')
157159
assert hasattr(manager, 'CODE_REVIEWER')
160+
assert hasattr(manager, 'EXPLAINER_AGENT')
158161
assert hasattr(manager, 'prompt_directory')
159162
assert hasattr(manager, '_prompt_cache')
160163
assert isinstance(manager._prompt_cache, dict)
@@ -176,6 +179,7 @@ def test_prompt_constants_have_correct_values(self):
176179
assert manager.REFACTOR_STRATEGIST == "refactor_strategist"
177180
assert manager.CODE_TRANSFORMER == "code_transformer"
178181
assert manager.CODE_REVIEWER == "code_reviewer"
182+
assert manager.EXPLAINER_AGENT == "explainer"
179183

180184
def test_prompt_directory_is_set_correctly(self):
181185
"""Test that prompt directory is assigned from settings."""
@@ -505,6 +509,7 @@ def test_initialization_with_missing_directory(self, capsys):
505509
manager.REFACTOR_STRATEGIST = "refactor_strategist"
506510
manager.CODE_TRANSFORMER = "code_transformer"
507511
manager.CODE_REVIEWER = "code_reviewer"
512+
manager.EXPLAINER_AGENT = "explainer"
508513
manager.prompt_directory = Path("/non/existent/path")
509514
manager._prompt_cache = {}
510515

@@ -575,6 +580,12 @@ def temp_prompt_files(self):
575580
'system': 'You are an expert code reviewer.',
576581
'user': 'Review this code for quality and best practices: {code}'
577582
}
583+
},
584+
'explainer.yaml': {
585+
'explainer': {
586+
'system': 'You are a senior software reviewer.',
587+
'user': 'Explain: {code}\nLang: {language}\nCtx: {context}'
588+
}
578589
}
579590
}
580591

@@ -593,22 +604,24 @@ def test_load_all_prompts_loads_all_available_files(self, temp_prompt_files, cap
593604
manager.REFACTOR_STRATEGIST = "refactor_strategist"
594605
manager.CODE_TRANSFORMER = "code_transformer"
595606
manager.CODE_REVIEWER = "code_reviewer"
607+
manager.EXPLAINER_AGENT = "explainer"
596608
manager.prompt_directory = temp_prompt_files
597609
manager._prompt_cache = {}
598610

599611
# Act: Load all prompts
600612
manager._load_all_prompts()
601613

602614
# Assert: Verify all prompts were loaded
603-
assert len(manager._prompt_cache) == 4
615+
assert len(manager._prompt_cache) == 5
604616
assert "antipattern_scanner" in manager._prompt_cache
605617
assert "refactor_strategist" in manager._prompt_cache
606618
assert "code_transformer" in manager._prompt_cache
607619
assert "code_reviewer" in manager._prompt_cache
620+
assert "explainer" in manager._prompt_cache
608621

609622
# Verify success message
610623
captured = capsys.readouterr()
611-
assert "Successfully loaded 4 prompts" in captured.out
624+
assert "Successfully loaded 5 prompts" in captured.out
612625

613626
def test_load_all_prompts_handles_partial_failures(self, capsys):
614627
"""Test that _load_all_prompts continues loading even if some files are missing."""
@@ -633,6 +646,7 @@ def test_load_all_prompts_handles_partial_failures(self, capsys):
633646
manager.REFACTOR_STRATEGIST = "refactor_strategist"
634647
manager.CODE_TRANSFORMER = "code_transformer"
635648
manager.CODE_REVIEWER = "code_reviewer"
649+
manager.EXPLAINER_AGENT = "explainer"
636650
manager.prompt_directory = temp_path
637651
manager._prompt_cache = {}
638652

0 commit comments

Comments
 (0)