|
| 1 | +"""Unit tests for EcoPrompt's pure routing-decision helpers. |
| 2 | +
|
| 3 | +These cover the logic that decides *which tier* answers a prompt and how many |
| 4 | +tokens to budget — the core of the energy-efficiency story. They run offline |
| 5 | +(no API calls). Run with: python -m unittest discover -s tests |
| 6 | +""" |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import unittest |
| 10 | + |
| 11 | +# Dummy key so `import main` (which constructs a Groq client) succeeds offline. |
| 12 | +os.environ.setdefault("GROQ_API_KEY", "test_dummy_key") |
| 13 | +# Allow `import main` when tests are run from the repo root. |
| 14 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 15 | + |
| 16 | +import main # noqa: E402 |
| 17 | + |
| 18 | + |
| 19 | +class PromptComplexityScore(unittest.TestCase): |
| 20 | + def test_trivial_prompt_scores_zero(self): |
| 21 | + self.assertEqual(main.prompt_complexity_score("hi"), 0) |
| 22 | + |
| 23 | + def test_short_factual_prompt_is_low(self): |
| 24 | + self.assertLess(main.prompt_complexity_score("what is photosynthesis"), 4) |
| 25 | + |
| 26 | + def test_long_multiclause_prompt_is_high(self): |
| 27 | + prompt = ( |
| 28 | + "Compare and contrast monolithic versus microservices architecture, " |
| 29 | + "because I need a detailed analysis of the tradeoffs" |
| 30 | + ) |
| 31 | + self.assertGreaterEqual(main.prompt_complexity_score(prompt), 4) |
| 32 | + |
| 33 | + def test_realtime_keyword_raises_score(self): |
| 34 | + # "latest"/"today" should push a prompt toward the heavier routes. |
| 35 | + with_realtime = main.prompt_complexity_score("what is the latest news today") |
| 36 | + without = main.prompt_complexity_score("what is the news") |
| 37 | + self.assertGreater(with_realtime, without) |
| 38 | + |
| 39 | + |
| 40 | +class IsSimplePrompt(unittest.TestCase): |
| 41 | + def test_simple_definitional_prompt(self): |
| 42 | + self.assertTrue(main.is_simple_prompt("what is photosynthesis")) |
| 43 | + |
| 44 | + def test_complex_prompt_is_not_simple(self): |
| 45 | + self.assertFalse( |
| 46 | + main.is_simple_prompt( |
| 47 | + "Compare and contrast microservices versus monolith in detail" |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + def test_long_prompt_is_not_simple(self): |
| 52 | + long_prompt = "explain " + "word " * 20 |
| 53 | + self.assertFalse(main.is_simple_prompt(long_prompt)) |
| 54 | + |
| 55 | + |
| 56 | +class SelectMaxTokens(unittest.TestCase): |
| 57 | + def test_code_request_gets_largest_budget(self): |
| 58 | + self.assertEqual( |
| 59 | + main.select_max_tokens("write a python function to sort a list"), 420 |
| 60 | + ) |
| 61 | + |
| 62 | + def test_simple_prompt_gets_smallest_budget(self): |
| 63 | + self.assertEqual(main.select_max_tokens("what is photosynthesis"), 120) |
| 64 | + |
| 65 | + def test_complex_prompt_gets_large_budget(self): |
| 66 | + prompt = ( |
| 67 | + "Compare and contrast monolithic versus microservices architecture, " |
| 68 | + "because I need a detailed analysis of the tradeoffs" |
| 69 | + ) |
| 70 | + self.assertEqual(main.select_max_tokens(prompt), 400) |
| 71 | + |
| 72 | + def test_default_budget(self): |
| 73 | + self.assertEqual(main.select_max_tokens("tell me a fun fact about dogs"), 280) |
| 74 | + |
| 75 | + |
| 76 | +class HostnameHelpers(unittest.TestCase): |
| 77 | + def test_extract_hostname_strips_www_and_lowercases(self): |
| 78 | + self.assertEqual(main.extract_hostname("https://www.BBC.com/news/x"), "bbc.com") |
| 79 | + |
| 80 | + def test_extract_hostname_handles_bad_url(self): |
| 81 | + self.assertEqual(main.extract_hostname("not a url"), "") |
| 82 | + |
| 83 | + def test_hostname_matches_exact(self): |
| 84 | + self.assertTrue(main.hostname_matches("bbc.com", "bbc.com")) |
| 85 | + |
| 86 | + def test_hostname_matches_subdomain(self): |
| 87 | + self.assertTrue(main.hostname_matches("news.bbc.com", "bbc.com")) |
| 88 | + |
| 89 | + def test_hostname_does_not_match_unrelated(self): |
| 90 | + self.assertFalse(main.hostname_matches("example.com", "bbc.com")) |
| 91 | + |
| 92 | + |
| 93 | +class EnergyEstimate(unittest.TestCase): |
| 94 | + def test_energy_formula(self): |
| 95 | + # ((ms/1000) * watts) / 3600 / 1000 |
| 96 | + self.assertAlmostEqual( |
| 97 | + main.estimate_energy_kwh(1000, 50), (1 * 50) / 3600 / 1000, places=12 |
| 98 | + ) |
| 99 | + |
| 100 | + def test_zero_latency_is_zero_energy(self): |
| 101 | + self.assertEqual(main.estimate_energy_kwh(0, 50), 0) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments