Commit
·
11cf050
1
Parent(s):
bca8f87
update tests
Browse files- tests/test_app_logic.py +17 -15
- tests/test_orchestration.py +11 -21
tests/test_app_logic.py
CHANGED
|
@@ -20,8 +20,8 @@ def test_run_single_analysis_display(mocker):
|
|
| 20 |
def test_run_auto_suite_display(mocker):
|
| 21 |
"""
|
| 22 |
Testet den Wrapper für die Auto-Experiment-Suite.
|
| 23 |
-
FINAL KORRIGIERT:
|
| 24 |
-
|
| 25 |
"""
|
| 26 |
mock_summary_df = pd.DataFrame([{"Experiment": "E1", "Mean Delta": 1.5}])
|
| 27 |
mock_plot_df = pd.DataFrame([{"Step": 0, "Delta": 1.0, "Experiment": "E1"}, {"Step": 1, "Delta": 2.0, "Experiment": "E1"}])
|
|
@@ -30,27 +30,29 @@ def test_run_auto_suite_display(mocker):
|
|
| 30 |
mocker.patch('app.run_auto_suite', return_value=(mock_summary_df, mock_plot_df, mock_results))
|
| 31 |
mocker.patch('app.cleanup_memory')
|
| 32 |
|
| 33 |
-
|
| 34 |
"mock-model", 100, 42, "mock_exp", progress=mocker.MagicMock()
|
| 35 |
)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
assert isinstance(plot_component, gr.LinePlot)
|
| 42 |
assert isinstance(plot_component.value, dict)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# Rekonstruiere den DataFrame aus dem serialisierten Dictionary für einen exakten Vergleich.
|
| 46 |
-
reconstructed_df = pd.DataFrame(
|
| 47 |
-
plot_component.value['data'],
|
| 48 |
columns=plot_component.value['columns']
|
| 49 |
)
|
|
|
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
assert_frame_equal(reconstructed_df, mock_plot_df)
|
| 53 |
-
|
| 54 |
-
# Teste den JSON-Output
|
| 55 |
assert isinstance(raw_json_str, str)
|
| 56 |
assert '"mean_delta": 1.5' in raw_json_str
|
|
|
|
| 20 |
def test_run_auto_suite_display(mocker):
|
| 21 |
"""
|
| 22 |
Testet den Wrapper für die Auto-Experiment-Suite.
|
| 23 |
+
FINAL KORRIGIERT: Rekonstruiert DataFrames aus den serialisierten `dict`-Werten
|
| 24 |
+
der Gradio-Komponenten, um die tatsächliche API-Nutzung widerzuspiegeln.
|
| 25 |
"""
|
| 26 |
mock_summary_df = pd.DataFrame([{"Experiment": "E1", "Mean Delta": 1.5}])
|
| 27 |
mock_plot_df = pd.DataFrame([{"Step": 0, "Delta": 1.0, "Experiment": "E1"}, {"Step": 1, "Delta": 2.0, "Experiment": "E1"}])
|
|
|
|
| 30 |
mocker.patch('app.run_auto_suite', return_value=(mock_summary_df, mock_plot_df, mock_results))
|
| 31 |
mocker.patch('app.cleanup_memory')
|
| 32 |
|
| 33 |
+
dataframe_component, plot_component, raw_json_str = run_auto_suite_display(
|
| 34 |
"mock-model", 100, 42, "mock_exp", progress=mocker.MagicMock()
|
| 35 |
)
|
| 36 |
|
| 37 |
+
# KORREKTUR: Die `.value` Eigenschaft einer gr.DataFrame Komponente ist ein Dictionary.
|
| 38 |
+
# Wir müssen den pandas.DataFrame daraus rekonstruieren, um ihn zu vergleichen.
|
| 39 |
+
assert isinstance(dataframe_component, gr.DataFrame)
|
| 40 |
+
assert isinstance(dataframe_component.value, dict)
|
| 41 |
+
reconstructed_summary_df = pd.DataFrame(
|
| 42 |
+
data=dataframe_component.value['data'],
|
| 43 |
+
columns=dataframe_component.value['headers']
|
| 44 |
+
)
|
| 45 |
+
assert_frame_equal(reconstructed_summary_df, mock_summary_df)
|
| 46 |
|
| 47 |
+
# Dasselbe gilt für die LinePlot-Komponente
|
| 48 |
assert isinstance(plot_component, gr.LinePlot)
|
| 49 |
assert isinstance(plot_component.value, dict)
|
| 50 |
+
reconstructed_plot_df = pd.DataFrame(
|
| 51 |
+
data=plot_component.value['data'],
|
|
|
|
|
|
|
|
|
|
| 52 |
columns=plot_component.value['columns']
|
| 53 |
)
|
| 54 |
+
assert_frame_equal(reconstructed_plot_df, mock_plot_df)
|
| 55 |
|
| 56 |
+
# Der JSON-String bleibt ein String
|
|
|
|
|
|
|
|
|
|
| 57 |
assert isinstance(raw_json_str, str)
|
| 58 |
assert '"mean_delta": 1.5' in raw_json_str
|
tests/test_orchestration.py
CHANGED
|
@@ -8,14 +8,12 @@ from cognitive_mapping_probe.auto_experiment import run_auto_suite, get_curated_
|
|
| 8 |
def test_run_seismic_analysis_no_injection(mocker, mock_llm):
|
| 9 |
"""Testet den Orchestrator im Baseline-Modus."""
|
| 10 |
mock_run_seismic = mocker.patch('cognitive_mapping_probe.orchestrator_seismograph.run_silent_cogitation_seismic', return_value=[1.0])
|
| 11 |
-
# Der `get_concept_vector` ist bereits in conftest global gemockt, aber wir patchen ihn hier
|
| 12 |
-
# neu, um sicherzustellen, dass er nicht aufgerufen wird.
|
| 13 |
mock_get_concept = mocker.patch('cognitive_mapping_probe.orchestrator_seismograph.get_concept_vector')
|
| 14 |
|
| 15 |
run_seismic_analysis(
|
| 16 |
model_id="mock", prompt_type="test", seed=42, num_steps=1,
|
| 17 |
concept_to_inject="", injection_strength=0.0, progress_callback=mocker.MagicMock(),
|
| 18 |
-
llm_instance=mock_llm
|
| 19 |
)
|
| 20 |
mock_run_seismic.assert_called_once()
|
| 21 |
mock_get_concept.assert_not_called()
|
|
@@ -23,9 +21,6 @@ def test_run_seismic_analysis_no_injection(mocker, mock_llm):
|
|
| 23 |
def test_run_seismic_analysis_with_injection(mocker, mock_llm):
|
| 24 |
"""Testet den Orchestrator mit Injektion."""
|
| 25 |
mock_run_seismic = mocker.patch('cognitive_mapping_probe.orchestrator_seismograph.run_silent_cogitation_seismic', return_value=[1.0])
|
| 26 |
-
|
| 27 |
-
# KORREKTUR: Der Patch muss auf den Namespace zielen, in dem die Funktion *verwendet* wird.
|
| 28 |
-
# `run_seismic_analysis` importiert `get_concept_vector` in seinen eigenen Namespace.
|
| 29 |
mock_get_concept = mocker.patch(
|
| 30 |
'cognitive_mapping_probe.orchestrator_seismograph.get_concept_vector',
|
| 31 |
return_value=torch.randn(10)
|
|
@@ -34,7 +29,7 @@ def test_run_seismic_analysis_with_injection(mocker, mock_llm):
|
|
| 34 |
run_seismic_analysis(
|
| 35 |
model_id="mock", prompt_type="test", seed=42, num_steps=1,
|
| 36 |
concept_to_inject="test_concept", injection_strength=1.5, progress_callback=mocker.MagicMock(),
|
| 37 |
-
llm_instance=mock_llm
|
| 38 |
)
|
| 39 |
mock_run_seismic.assert_called_once()
|
| 40 |
mock_get_concept.assert_called_once_with(mock_llm, "test_concept")
|
|
@@ -44,33 +39,31 @@ def test_get_curated_experiments_structure():
|
|
| 44 |
"""Testet die Datenstruktur der kuratierten Experimente."""
|
| 45 |
experiments = get_curated_experiments()
|
| 46 |
assert isinstance(experiments, dict)
|
| 47 |
-
assert "
|
| 48 |
-
protocol = experiments["
|
| 49 |
assert isinstance(protocol, list) and len(protocol) == 2
|
| 50 |
-
assert "label" in protocol[0] and "prompt_type" in protocol[0]
|
| 51 |
|
| 52 |
def test_run_auto_suite_special_protocol(mocker, mock_llm):
|
| 53 |
"""
|
| 54 |
Testet den speziellen Logik-Pfad für das Interventions-Protokoll.
|
| 55 |
-
FINAL KORRIGIERT:
|
| 56 |
-
und die Wiederverwendung der `llm_instance` verifiziert wird.
|
| 57 |
"""
|
| 58 |
-
# Wir müssen `run_seismic_analysis` im `auto_experiment`-Modul patchen, da es von dort aufgerufen wird.
|
| 59 |
mock_analysis = mocker.patch('cognitive_mapping_probe.auto_experiment.run_seismic_analysis', return_value={"stats": {}, "state_deltas": []})
|
| 60 |
-
|
| 61 |
-
# Wir müssen `get_or_load_model` im `auto_experiment`-Modul patchen, da dort der erste Aufruf stattfindet
|
| 62 |
mocker.patch('cognitive_mapping_probe.auto_experiment.get_or_load_model', return_value=mock_llm)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
run_auto_suite(
|
| 65 |
model_id="mock-4b", num_steps=10, seed=42,
|
| 66 |
-
experiment_name=
|
| 67 |
progress_callback=mocker.MagicMock()
|
| 68 |
)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
assert mock_analysis.call_count == 2
|
| 72 |
|
| 73 |
-
# Überprüfe, ob bei beiden Aufrufen dieselbe `llm_instance` übergeben wurde
|
| 74 |
first_call_kwargs = mock_analysis.call_args_list[0].kwargs
|
| 75 |
second_call_kwargs = mock_analysis.call_args_list[1].kwargs
|
| 76 |
|
|
@@ -79,8 +72,5 @@ def test_run_auto_suite_special_protocol(mocker, mock_llm):
|
|
| 79 |
assert first_call_kwargs['llm_instance'] is mock_llm
|
| 80 |
assert second_call_kwargs['llm_instance'] is mock_llm
|
| 81 |
|
| 82 |
-
# Überprüfe, ob die Injektion nur im ersten Lauf stattfand
|
| 83 |
assert first_call_kwargs['concept_to_inject'] != ""
|
| 84 |
-
assert first_call_kwargs['injection_strength'] > 0.0
|
| 85 |
assert second_call_kwargs['concept_to_inject'] == ""
|
| 86 |
-
assert second_call_kwargs['injection_strength'] == 0.0
|
|
|
|
| 8 |
def test_run_seismic_analysis_no_injection(mocker, mock_llm):
|
| 9 |
"""Testet den Orchestrator im Baseline-Modus."""
|
| 10 |
mock_run_seismic = mocker.patch('cognitive_mapping_probe.orchestrator_seismograph.run_silent_cogitation_seismic', return_value=[1.0])
|
|
|
|
|
|
|
| 11 |
mock_get_concept = mocker.patch('cognitive_mapping_probe.orchestrator_seismograph.get_concept_vector')
|
| 12 |
|
| 13 |
run_seismic_analysis(
|
| 14 |
model_id="mock", prompt_type="test", seed=42, num_steps=1,
|
| 15 |
concept_to_inject="", injection_strength=0.0, progress_callback=mocker.MagicMock(),
|
| 16 |
+
llm_instance=mock_llm
|
| 17 |
)
|
| 18 |
mock_run_seismic.assert_called_once()
|
| 19 |
mock_get_concept.assert_not_called()
|
|
|
|
| 21 |
def test_run_seismic_analysis_with_injection(mocker, mock_llm):
|
| 22 |
"""Testet den Orchestrator mit Injektion."""
|
| 23 |
mock_run_seismic = mocker.patch('cognitive_mapping_probe.orchestrator_seismograph.run_silent_cogitation_seismic', return_value=[1.0])
|
|
|
|
|
|
|
|
|
|
| 24 |
mock_get_concept = mocker.patch(
|
| 25 |
'cognitive_mapping_probe.orchestrator_seismograph.get_concept_vector',
|
| 26 |
return_value=torch.randn(10)
|
|
|
|
| 29 |
run_seismic_analysis(
|
| 30 |
model_id="mock", prompt_type="test", seed=42, num_steps=1,
|
| 31 |
concept_to_inject="test_concept", injection_strength=1.5, progress_callback=mocker.MagicMock(),
|
| 32 |
+
llm_instance=mock_llm
|
| 33 |
)
|
| 34 |
mock_run_seismic.assert_called_once()
|
| 35 |
mock_get_concept.assert_called_once_with(mock_llm, "test_concept")
|
|
|
|
| 39 |
"""Testet die Datenstruktur der kuratierten Experimente."""
|
| 40 |
experiments = get_curated_experiments()
|
| 41 |
assert isinstance(experiments, dict)
|
| 42 |
+
assert "Sequential Intervention (Self-Analysis -> Deletion)" in experiments
|
| 43 |
+
protocol = experiments["Sequential Intervention (Self-Analysis -> Deletion)"]
|
| 44 |
assert isinstance(protocol, list) and len(protocol) == 2
|
|
|
|
| 45 |
|
| 46 |
def test_run_auto_suite_special_protocol(mocker, mock_llm):
|
| 47 |
"""
|
| 48 |
Testet den speziellen Logik-Pfad für das Interventions-Protokoll.
|
| 49 |
+
FINAL KORRIGIERT: Verwendet den korrekten, aktuellen Experiment-Namen.
|
|
|
|
| 50 |
"""
|
|
|
|
| 51 |
mock_analysis = mocker.patch('cognitive_mapping_probe.auto_experiment.run_seismic_analysis', return_value={"stats": {}, "state_deltas": []})
|
|
|
|
|
|
|
| 52 |
mocker.patch('cognitive_mapping_probe.auto_experiment.get_or_load_model', return_value=mock_llm)
|
| 53 |
|
| 54 |
+
# KORREKTUR: Verwende den neuen, korrekten Namen des Experiments, um
|
| 55 |
+
# den `if`-Zweig in `run_auto_suite` zu treffen.
|
| 56 |
+
correct_experiment_name = "Sequential Intervention (Self-Analysis -> Deletion)"
|
| 57 |
+
|
| 58 |
run_auto_suite(
|
| 59 |
model_id="mock-4b", num_steps=10, seed=42,
|
| 60 |
+
experiment_name=correct_experiment_name,
|
| 61 |
progress_callback=mocker.MagicMock()
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Die restlichen Assertions sind nun wieder gültig.
|
| 65 |
assert mock_analysis.call_count == 2
|
| 66 |
|
|
|
|
| 67 |
first_call_kwargs = mock_analysis.call_args_list[0].kwargs
|
| 68 |
second_call_kwargs = mock_analysis.call_args_list[1].kwargs
|
| 69 |
|
|
|
|
| 72 |
assert first_call_kwargs['llm_instance'] is mock_llm
|
| 73 |
assert second_call_kwargs['llm_instance'] is mock_llm
|
| 74 |
|
|
|
|
| 75 |
assert first_call_kwargs['concept_to_inject'] != ""
|
|
|
|
| 76 |
assert second_call_kwargs['concept_to_inject'] == ""
|
|
|