/ tests / unit / cli / test_persistence.py
test_persistence.py
 1  import os
 2  import shutil
 3  from pathlib import Path
 4  
 5  import pytest
 6  from click.testing import CliRunner
 7  
 8  from cli.main import cli
 9  
10  
11  @pytest.fixture
12  def project_dir(tmp_path):
13      """Create a temporary project directory for testing"""
14      project_path = tmp_path / "test_project"
15      project_path.mkdir()
16      original_cwd = Path.cwd()
17      os.chdir(project_path)
18      runner = CliRunner()
19      runner.invoke(
20          cli,
21          ["init", "--skip", "--agent-name", "MyOrchestrator"],
22          catch_exceptions=False,
23      )
24      yield project_path
25      os.chdir(original_cwd)
26      shutil.rmtree(project_path)
27  
28  
29  def test_add_agent_idempotency(project_dir):
30      """
31      Test that running 'add agent' multiple times for the same agent updates the config correctly.
32      """
33      runner = CliRunner()
34      # First run
35      result1 = runner.invoke(
36          cli,
37          [
38              "add",
39              "agent",
40              "idempotentAgent",
41              "--session-service-type",
42              "sql",
43              "--instruction",
44              "First instruction",
45              "--skip",
46          ],
47          catch_exceptions=False,
48      )
49      assert result1.exit_code == 0, f"First run failed: {result1.output}"
50      agent_config_path = (
51          project_dir / "configs" / "agents" / "idempotent_agent_agent.yaml"
52      )
53      with open(agent_config_path) as f:
54          content1 = f.read()
55      assert "First instruction" in content1
56  
57      # Second run with different instruction
58      result2 = runner.invoke(
59          cli,
60          [
61              "add",
62              "agent",
63              "idempotentAgent",
64              "--session-service-type",
65              "sql",
66              "--instruction",
67              "Second instruction",
68              "--skip",
69          ],
70          catch_exceptions=False,
71      )
72      assert result2.exit_code == 0, f"Second run failed: {result2.output}"
73      with open(agent_config_path) as f:
74          content2 = f.read()
75      assert "Second instruction" in content2
76      assert "First instruction" not in content2