/ tests / unit / cli / test_init_cmd.py
test_init_cmd.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      # Store the original CWD and change to the new project directory
17      original_cwd = Path.cwd()
18      os.chdir(project_path)
19      yield project_path
20      # Restore the original CWD and clean up the temp directory
21      os.chdir(original_cwd)
22      shutil.rmtree(project_path)
23  
24  
25  def test_init_default_db_generation(project_dir):
26      """
27      Test that the init command generates the default SQLite database files
28      and configures the .env file correctly.
29      """
30      runner = CliRunner()
31      # Run the init command with non-interactive flags
32      result = runner.invoke(
33          cli,
34          ["init", "--skip", "--agent-name", "MyOrchestrator"],
35          catch_exceptions=False,
36      )
37  
38      assert result.exit_code == 0, (
39          f"CLI command failed with exit code {result.exit_code}: {result.output}"
40      )
41  
42      # Verify that the .env file is configured correctly
43      env_file = project_dir / ".env"
44      assert env_file.exists(), ".env file was not created"
45  
46  
47  @pytest.mark.xfail(reason="This test needs to be reviewed and fixed.")
48  def test_init_external_db_url_no_file_creation(project_dir, mocker):
49      """
50      Test that the init command with a non-sqlite custom database URL does NOT
51      create local .db files.
52      """
53      mocker.patch("cli.commands.init_cmd.database_step.create_engine")
54      runner = CliRunner()
55      custom_db_url = "postgresql://user:pass@host/db"
56  
57      result = runner.invoke(
58          cli,
59          [
60              "init",
61              "--skip",
62              f"--web-ui-gateway-database-url={custom_db_url}",
63          ],
64          catch_exceptions=False,
65      )
66  
67      assert result.exit_code == 0, (
68          f"CLI command failed with exit code {result.exit_code}: {result.output}"
69      )
70      assert not (project_dir / "data" / "webui_gateway.db").exists(), (
71          "webui_gateway.db should not have been created"
72      )
73  
74      env_file = project_dir / ".env"
75      with open(env_file) as f:
76          env_content = f.read()
77          assert f'WEB_UI_GATEWAY_DATABASE_URL="{custom_db_url}"' in env_content