|
| 1 | +""" |
| 2 | +Unit tests for JSONSummaryReporter |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import json |
| 7 | +import datetime |
| 8 | + |
| 9 | +from krkn_ai.reporter.json_summary_reporter import JSONSummaryReporter |
| 10 | +from krkn_ai.models.app import CommandRunResult, FitnessResult |
| 11 | +from krkn_ai.models.scenario.scenario_dummy import DummyScenario |
| 12 | + |
| 13 | + |
| 14 | +class TestJSONSummaryReporter: |
| 15 | + def _create_results(self, gen_id, start_score, count, scenario, now): |
| 16 | + results = {} |
| 17 | + for i in range(count): |
| 18 | + sid = (gen_id * 100) + i |
| 19 | + score = float(start_score + (i * 10)) |
| 20 | + res = CommandRunResult( |
| 21 | + generation_id=gen_id, |
| 22 | + scenario_id=sid, |
| 23 | + scenario=scenario, |
| 24 | + cmd="test", |
| 25 | + log="test", |
| 26 | + returncode=0, |
| 27 | + start_time=now, |
| 28 | + end_time=now, |
| 29 | + fitness_result=FitnessResult(fitness_score=score), |
| 30 | + ) |
| 31 | + results[sid] = res |
| 32 | + return results |
| 33 | + |
| 34 | + def test_generate_summary_content(self, minimal_config): |
| 35 | + now = datetime.datetime(2023, 1, 1, 12, 0, 0) |
| 36 | + cc = minimal_config.cluster_components |
| 37 | + scenario = DummyScenario(cluster_components=cc) |
| 38 | + |
| 39 | + gen0 = self._create_results(0, 0, 3, scenario, now) |
| 40 | + gen1 = self._create_results(1, 40, 3, scenario, now) |
| 41 | + |
| 42 | + pop = {**gen0, **gen1} |
| 43 | + best = [gen0[2], gen1[102]] |
| 44 | + |
| 45 | + reporter = JSONSummaryReporter( |
| 46 | + run_uuid="test-run", |
| 47 | + config=minimal_config, |
| 48 | + seen_population=pop, |
| 49 | + best_of_generation=best, |
| 50 | + start_time=now, |
| 51 | + end_time=now + datetime.timedelta(seconds=100), |
| 52 | + completed_generations=2, |
| 53 | + seed=123 |
| 54 | + ) |
| 55 | + |
| 56 | + summary = reporter.generate_summary() |
| 57 | + |
| 58 | + assert summary["run_id"] == "test-run" |
| 59 | + assert summary["duration_seconds"] == 100.0 |
| 60 | + assert summary["summary"]["total_scenarios_executed"] == 6 |
| 61 | + assert summary["summary"]["best_fitness_score"] == 60.0 |
| 62 | + |
| 63 | + assert summary["fitness_progression"][0]["average"] == 10.0 |
| 64 | + assert summary["fitness_progression"][1]["average"] == 50.0 |
| 65 | + assert summary["fitness_progression"][1]["best"] == 60.0 |
| 66 | + |
| 67 | + def test_best_scenarios_ranking(self, minimal_config): |
| 68 | + now = datetime.datetime.now() |
| 69 | + cc = minimal_config.cluster_components |
| 70 | + scenario = DummyScenario(cluster_components=cc) |
| 71 | + |
| 72 | + pop = self._create_results(0, 0, 15, scenario, now) |
| 73 | + |
| 74 | + reporter = JSONSummaryReporter( |
| 75 | + run_uuid="test", |
| 76 | + config=minimal_config, |
| 77 | + seen_population=pop, |
| 78 | + best_of_generation=[], |
| 79 | + ) |
| 80 | + |
| 81 | + best = reporter.generate_summary()["best_scenarios"] |
| 82 | + assert len(best) == 10 |
| 83 | + assert best[0]["rank"] == 1 |
| 84 | + assert best[0]["fitness_score"] == 140.0 |
| 85 | + assert best[0]["parameters"]["end"] == 10 |
| 86 | + |
| 87 | + def test_empty_population(self, minimal_config): |
| 88 | + reporter = JSONSummaryReporter( |
| 89 | + run_uuid="empty", |
| 90 | + config=minimal_config, |
| 91 | + seen_population={}, |
| 92 | + best_of_generation=[], |
| 93 | + ) |
| 94 | + summary = reporter.generate_summary() |
| 95 | + assert summary["summary"]["total_scenarios_executed"] == 0 |
| 96 | + assert summary["best_scenarios"] == [] |
| 97 | + |
| 98 | + def test_save_json_file(self, minimal_config, temp_output_dir): |
| 99 | + reporter = JSONSummaryReporter( |
| 100 | + run_uuid="save-test", |
| 101 | + config=minimal_config, |
| 102 | + seen_population={}, |
| 103 | + best_of_generation=[], |
| 104 | + ) |
| 105 | + reporter.save(temp_output_dir) |
| 106 | + |
| 107 | + path = os.path.join(temp_output_dir, "results.json") |
| 108 | + assert os.path.exists(path) |
| 109 | + with open(path, "r") as f: |
| 110 | + content = json.load(f) |
| 111 | + assert content["run_id"] == "save-test" |
0 commit comments