|
| 1 | +import importlib.util |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import types |
| 6 | +import unittest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 11 | +MODULE_PATH = REPO_ROOT / "agents" / "s_full.py" |
| 12 | + |
| 13 | + |
| 14 | +def load_s_full_module(temp_cwd: Path): |
| 15 | + fake_anthropic = types.ModuleType("anthropic") |
| 16 | + |
| 17 | + class FakeAnthropic: |
| 18 | + def __init__(self, *args, **kwargs): |
| 19 | + self.messages = types.SimpleNamespace(create=None) |
| 20 | + |
| 21 | + fake_dotenv = types.ModuleType("dotenv") |
| 22 | + setattr(fake_anthropic, "Anthropic", FakeAnthropic) |
| 23 | + setattr(fake_dotenv, "load_dotenv", lambda override=True: None) |
| 24 | + |
| 25 | + previous_anthropic = sys.modules.get("anthropic") |
| 26 | + previous_dotenv = sys.modules.get("dotenv") |
| 27 | + previous_cwd = Path.cwd() |
| 28 | + spec = importlib.util.spec_from_file_location("s_full_under_test", MODULE_PATH) |
| 29 | + if spec is None or spec.loader is None: |
| 30 | + raise RuntimeError(f"Unable to load {MODULE_PATH}") |
| 31 | + module = importlib.util.module_from_spec(spec) |
| 32 | + |
| 33 | + sys.modules["anthropic"] = fake_anthropic |
| 34 | + sys.modules["dotenv"] = fake_dotenv |
| 35 | + try: |
| 36 | + os.chdir(temp_cwd) |
| 37 | + os.environ.setdefault("MODEL_ID", "test-model") |
| 38 | + spec.loader.exec_module(module) |
| 39 | + return module |
| 40 | + finally: |
| 41 | + os.chdir(previous_cwd) |
| 42 | + if previous_anthropic is None: |
| 43 | + sys.modules.pop("anthropic", None) |
| 44 | + else: |
| 45 | + sys.modules["anthropic"] = previous_anthropic |
| 46 | + if previous_dotenv is None: |
| 47 | + sys.modules.pop("dotenv", None) |
| 48 | + else: |
| 49 | + sys.modules["dotenv"] = previous_dotenv |
| 50 | + |
| 51 | + |
| 52 | +class BackgroundManagerTests(unittest.TestCase): |
| 53 | + def test_check_returns_running_placeholder_when_result_is_none(self): |
| 54 | + with tempfile.TemporaryDirectory() as tmp: |
| 55 | + module = load_s_full_module(Path(tmp)) |
| 56 | + manager = module.BackgroundManager() |
| 57 | + manager.tasks["abc123"] = { |
| 58 | + "status": "running", |
| 59 | + "command": "sleep 1", |
| 60 | + "result": None, |
| 61 | + } |
| 62 | + |
| 63 | + self.assertEqual(manager.check("abc123"), "[running] (running)") |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + unittest.main() |
0 commit comments