|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from click.testing import CliRunner |
| 16 | +from pathlib import Path |
| 17 | +from veadk.cli.cli_create import create, _generate_files |
| 18 | + |
| 19 | + |
| 20 | +def test_create_agent_with_options(): |
| 21 | + runner = CliRunner() |
| 22 | + with runner.isolated_filesystem() as temp_dir: |
| 23 | + result = runner.invoke( |
| 24 | + create, ["--agent-name", "test-agent", "--ark-api-key", "test-key"] |
| 25 | + ) |
| 26 | + assert result.exit_code == 0 |
| 27 | + |
| 28 | + agent_folder = Path(temp_dir) / "test-agent" |
| 29 | + assert agent_folder.exists() |
| 30 | + |
| 31 | + config_path = agent_folder / "config.yaml" |
| 32 | + assert config_path.exists() |
| 33 | + config_content = config_path.read_text() |
| 34 | + assert "api_key: test-key" in config_content |
| 35 | + |
| 36 | + agent_init_path = agent_folder / "test-agent" / "__init__.py" |
| 37 | + assert agent_init_path.exists() |
| 38 | + |
| 39 | + agent_py_path = agent_folder / "test-agent" / "agent.py" |
| 40 | + assert agent_py_path.exists() |
| 41 | + |
| 42 | + |
| 43 | +def test_create_agent_overwrite_existing_directory(): |
| 44 | + runner = CliRunner() |
| 45 | + with runner.isolated_filesystem() as temp_dir: |
| 46 | + # First, create the agent |
| 47 | + runner.invoke( |
| 48 | + create, ["--agent-name", "test-agent", "--ark-api-key", "test-key"] |
| 49 | + ) |
| 50 | + |
| 51 | + # Attempt to create it again, but cancel the overwrite |
| 52 | + result = runner.invoke( |
| 53 | + create, |
| 54 | + ["--agent-name", "test-agent", "--ark-api-key", "test-key"], |
| 55 | + input="n\n", |
| 56 | + ) |
| 57 | + assert "Operation cancelled" in result.output |
| 58 | + |
| 59 | + # Attempt to create it again, and confirm the overwrite |
| 60 | + result = runner.invoke( |
| 61 | + create, |
| 62 | + ["--agent-name", "test-agent", "--ark-api-key", "new-key"], |
| 63 | + input="y\n", |
| 64 | + ) |
| 65 | + assert result.exit_code == 0 |
| 66 | + agent_folder = Path(temp_dir) / "test-agent" |
| 67 | + config_path = agent_folder / "config.yaml" |
| 68 | + config_content = config_path.read_text() |
| 69 | + assert "api_key: new-key" in config_content |
| 70 | + |
| 71 | + |
| 72 | +def test_generate_files(tmp_path: Path): |
| 73 | + agent_name = "test-agent" |
| 74 | + api_key = "test-key" |
| 75 | + target_dir = tmp_path / agent_name |
| 76 | + |
| 77 | + _generate_files(agent_name, api_key, target_dir) |
| 78 | + |
| 79 | + agent_code_dir = target_dir / agent_name |
| 80 | + assert agent_code_dir.is_dir() |
| 81 | + |
| 82 | + config_file = target_dir / "config.yaml" |
| 83 | + assert config_file.is_file() |
| 84 | + content = config_file.read_text() |
| 85 | + assert f"api_key: {api_key}" in content |
| 86 | + |
| 87 | + init_file = agent_code_dir / "__init__.py" |
| 88 | + assert init_file.is_file() |
| 89 | + |
| 90 | + agent_file = agent_code_dir / "agent.py" |
| 91 | + assert agent_file.is_file() |
| 92 | + |
| 93 | + |
| 94 | +def test_prompt_for_ark_api_key_enter_now(): |
| 95 | + runner = CliRunner() |
| 96 | + with runner.isolated_filesystem(): |
| 97 | + result = runner.invoke(create, input="test-agent\n1\nmy-secret-key\n") |
| 98 | + assert result.exit_code == 0 |
| 99 | + assert "my-secret-key" in (Path("test-agent") / "config.yaml").read_text() |
| 100 | + |
| 101 | + |
| 102 | +def test_prompt_for_ark_api_key_configure_later(): |
| 103 | + runner = CliRunner() |
| 104 | + with runner.isolated_filesystem(): |
| 105 | + result = runner.invoke(create, input="test-agent\n2\n") |
| 106 | + assert result.exit_code == 0 |
| 107 | + assert "api_key: " in (Path("test-agent") / "config.yaml").read_text() |
| 108 | + |
| 109 | + |
| 110 | +def test_create_agent_with_prompts(): |
| 111 | + runner = CliRunner() |
| 112 | + with runner.isolated_filesystem() as temp_dir: |
| 113 | + result = runner.invoke(create, input="test-agent\n1\ntest-key\n") |
| 114 | + assert result.exit_code == 0 |
| 115 | + |
| 116 | + agent_folder = Path(temp_dir) / "test-agent" |
| 117 | + assert agent_folder.exists() |
| 118 | + |
| 119 | + config_path = agent_folder / "config.yaml" |
| 120 | + assert config_path.exists() |
| 121 | + config_content = config_path.read_text() |
| 122 | + assert "api_key: test-key" in config_content |
| 123 | + |
| 124 | + agent_init_path = agent_folder / "test-agent" / "__init__.py" |
| 125 | + assert agent_init_path.exists() |
| 126 | + |
| 127 | + agent_py_path = agent_folder / "test-agent" / "agent.py" |
| 128 | + assert agent_py_path.exists() |
0 commit comments