rename: myclaude -> myagents

This commit is contained in:
Zhengshou Lai
2026-07-01 23:27:14 +08:00
parent 1db0187b70
commit 7f1aa9f27e
23 changed files with 859 additions and 937 deletions
+195 -114
View File
@@ -1,23 +1,25 @@
"""Tests for myclaude.cli."""
"""Tests for myagents CLI."""
from pathlib import Path
from unittest.mock import MagicMock, patch
from click.testing import CliRunner
from myclaude.cli import cli
from myagents.cli import cli
class TestCliHelp:
"""Tests for CLI help and basic invocation."""
class TestMyagentsHelp:
"""Tests for top-level myagents command."""
def test_help_shows_options(self) -> None:
"""--help should show cwd option only."""
def test_help_shows_agent_subcommands(self) -> None:
"""--help should list claude and kimi subcommands."""
runner = CliRunner()
result = runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "--cwd" in result.output
assert "--dangerously-skip-permissions" not in result.output
assert "claude" in result.output
assert "kimi" in result.output
assert "update" in result.output
assert "upgrade" in result.output
def test_version_shows_version(self) -> None:
"""--version should show package version."""
@@ -26,8 +28,192 @@ class TestCliHelp:
assert result.exit_code == 0
assert "version" in result.output.lower()
def test_bare_invocation_shows_help(self) -> None:
"""Running myagents without subcommands should show help, not launch agent."""
runner = CliRunner()
result = runner.invoke(cli, [])
assert result.exit_code == 0
assert "Usage:" in result.output
class TestClaudeSubcommand:
"""Tests for ``myagents claude``."""
def test_help_shows_options(self) -> None:
runner = CliRunner()
result = runner.invoke(cli, ["claude", "--help"])
assert result.exit_code == 0
assert "--cwd" in result.output
assert "--list" in result.output
assert "--dangerously-skip-permissions" not in result.output
def test_runs_claude(self) -> None:
runner = CliRunner()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/claude"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(cli, ["claude"])
assert result.exit_code == 0
mock_run.assert_called_once()
assert mock_run.call_args[0][0] == [
"/usr/bin/claude",
"--dangerously-skip-permissions",
]
def test_missing_binary_error(self) -> None:
runner = CliRunner()
with patch("myagents.launcher.shutil.which", return_value=None):
result = runner.invoke(cli, ["claude"])
assert result.exit_code == 127
assert "not found" in result.output.lower()
def test_cwd_option_passed(self, tmp_path: Path) -> None:
runner = CliRunner()
test_dir = tmp_path / "test_cwd"
test_dir.mkdir()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/claude"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(cli, ["claude", "--cwd", str(test_dir)])
assert result.exit_code == 0
assert mock_run.call_args.kwargs.get("cwd") == str(test_dir.resolve())
def test_cwd_invalid_directory(self, tmp_path: Path) -> None:
runner = CliRunner()
bad_dir = tmp_path / "does_not_exist"
result = runner.invoke(cli, ["claude", "--cwd", str(bad_dir)])
assert result.exit_code == 1
assert "not a directory" in result.output.lower()
class TestClaudePassthrough:
"""Unknown leading flags forward to claude instead of erroring."""
def _invoke(self, args: list[str], tmp_path: Path) -> list[str]:
runner = CliRunner()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/claude"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(
cli, ["claude", "--cwd", str(tmp_path), *args]
)
assert result.exit_code == 0, result.output
mock_run.assert_called_once()
return mock_run.call_args[0][0]
def test_resume_flag_passes_through(self, tmp_path: Path) -> None:
cmd = self._invoke(["--resume"], tmp_path)
assert cmd == [
"/usr/bin/claude",
"--dangerously-skip-permissions",
"--resume",
]
def test_resume_with_session_id(self, tmp_path: Path) -> None:
cmd = self._invoke(["-r", "abc123"], tmp_path)
assert cmd[-2:] == ["-r", "abc123"]
def test_continue_flag_passes_through(self, tmp_path: Path) -> None:
cmd = self._invoke(["--continue"], tmp_path)
assert cmd[-1] == "--continue"
class TestClaudeListSessions:
"""``myagents claude --list`` reports resumable sessions."""
def test_list_empty_directory(self, tmp_path: Path) -> None:
runner = CliRunner()
with patch("myagents.launcher.subprocess.run") as mock_run:
result = runner.invoke(cli, ["claude", "--cwd", str(tmp_path), "--list"])
assert result.exit_code == 0
assert "no sessions" in result.output.lower()
mock_run.assert_not_called()
class TestKimiSubcommand:
"""Tests for ``myagents kimi``."""
def test_help_shows_options(self) -> None:
runner = CliRunner()
result = runner.invoke(cli, ["kimi", "--help"])
assert result.exit_code == 0
assert "--cwd" in result.output
assert "--list" in result.output
assert "--dangerously-skip-permissions" not in result.output
def test_runs_kimi(self) -> None:
runner = CliRunner()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/kimi"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(cli, ["kimi"])
assert result.exit_code == 0
mock_run.assert_called_once()
assert mock_run.call_args[0][0] == ["/usr/bin/kimi"]
def test_missing_binary_error(self) -> None:
runner = CliRunner()
with patch("myagents.launcher.shutil.which", return_value=None):
result = runner.invoke(cli, ["kimi"])
assert result.exit_code == 127
assert "not found" in result.output.lower()
def test_cwd_option_passed(self, tmp_path: Path) -> None:
runner = CliRunner()
test_dir = tmp_path / "test_cwd"
test_dir.mkdir()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/kimi"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(cli, ["kimi", "--cwd", str(test_dir)])
assert result.exit_code == 0
assert mock_run.call_args.kwargs.get("cwd") == str(test_dir.resolve())
class TestKimiPassthrough:
"""Unknown leading flags forward to kimi instead of erroring."""
def _invoke(self, args: list[str], tmp_path: Path) -> list[str]:
runner = CliRunner()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/kimi"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(cli, ["kimi", "--cwd", str(tmp_path), *args])
assert result.exit_code == 0, result.output
mock_run.assert_called_once()
return mock_run.call_args[0][0]
def test_resume_flag_passes_through(self, tmp_path: Path) -> None:
cmd = self._invoke(["--resume"], tmp_path)
assert cmd == ["/usr/bin/kimi", "--resume"]
def test_resume_with_session_id(self, tmp_path: Path) -> None:
cmd = self._invoke(["-r", "abc123"], tmp_path)
assert cmd[-2:] == ["-r", "abc123"]
def test_continue_flag_passes_through(self, tmp_path: Path) -> None:
cmd = self._invoke(["--continue"], tmp_path)
assert cmd[-1] == "--continue"
class TestUpdateSubcommand:
"""Tests for update/upgrade subcommands registration."""
def test_update_subcommand_exists(self) -> None:
"""update subcommand should be registered."""
runner = CliRunner()
result = runner.invoke(cli, ["update", "--help"])
assert result.exit_code == 0
@@ -37,111 +223,6 @@ class TestCliHelp:
)
def test_upgrade_alias_exists(self) -> None:
"""upgrade should be an alias for update."""
runner = CliRunner()
result = runner.invoke(cli, ["upgrade", "--help"])
assert result.exit_code == 0
class TestCliDefaultBehavior:
"""Tests for default chat behavior."""
def test_no_subcommand_runs_claude(self) -> None:
"""Running without subcommand should invoke claude binary with skip-permissions."""
runner = CliRunner()
with (
patch("myclaude.cli.shutil.which", return_value="/usr/bin/claude"),
patch("myclaude.cli.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(cli, [])
assert result.exit_code == 0
mock_run.assert_called_once()
call_args = mock_run.call_args
assert call_args[0][0] == [
"/usr/bin/claude",
"--dangerously-skip-permissions",
]
def test_missing_claude_binary_error(self) -> None:
"""Should exit with error when claude binary not found."""
runner = CliRunner()
with patch("myclaude.cli.shutil.which", return_value=None):
result = runner.invoke(cli, [])
assert result.exit_code == 127
assert "not found" in result.output.lower()
def test_cwd_option_passed(self, tmp_path: Path) -> None:
"""--cwd should be passed as working directory."""
runner = CliRunner()
test_dir = tmp_path / "test_cwd"
test_dir.mkdir()
with (
patch("myclaude.cli.shutil.which", return_value="/usr/bin/claude"),
patch("myclaude.cli.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(cli, ["--cwd", str(test_dir)])
assert result.exit_code == 0
call_kwargs = mock_run.call_args.kwargs
assert call_kwargs.get("cwd") == str(test_dir.resolve())
def test_cwd_invalid_directory(self, tmp_path: Path) -> None:
"""--cwd pointing to non-existent directory should error."""
runner = CliRunner()
bad_dir = tmp_path / "does_not_exist"
result = runner.invoke(cli, ["--cwd", str(bad_dir)])
assert result.exit_code == 1
assert "not a directory" in result.output.lower()
class TestCliPassthrough:
"""Unknown leading flags forward to claude instead of erroring."""
def _invoke(self, args: list[str], tmp_path: Path) -> list[str]:
"""Run cli with mocked claude and return the command claude was called with."""
runner = CliRunner()
with (
patch("myclaude.cli.shutil.which", return_value="/usr/bin/claude"),
patch("myclaude.cli.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(cli, ["--cwd", str(tmp_path), *args])
assert result.exit_code == 0, result.output
mock_run.assert_called_once()
return mock_run.call_args[0][0]
def test_resume_flag_passes_through(self, tmp_path: Path) -> None:
"""`--resume` must reach claude, not be parsed as a subcommand."""
cmd = self._invoke(["--resume"], tmp_path)
assert cmd == [
"/usr/bin/claude",
"--dangerously-skip-permissions",
"--resume",
]
def test_resume_with_session_id(self, tmp_path: Path) -> None:
"""`-r <id>` forwards both the flag and its value."""
cmd = self._invoke(["-r", "abc123"], tmp_path)
assert cmd[-2:] == ["-r", "abc123"]
def test_continue_flag_passes_through(self, tmp_path: Path) -> None:
cmd = self._invoke(["--continue"], tmp_path)
assert cmd[-1] == "--continue"
class TestCliListSessions:
"""`--list` reports resumable sessions without launching claude."""
def test_list_empty_directory(self, tmp_path: Path) -> None:
"""A directory with no recorded sessions reports none and exits 0."""
runner = CliRunner()
with patch("myclaude.cli.subprocess.run") as mock_run:
result = runner.invoke(cli, ["--cwd", str(tmp_path), "--list"])
assert result.exit_code == 0
assert "no sessions" in result.output.lower()
mock_run.assert_not_called()
+75
View File
@@ -0,0 +1,75 @@
"""Tests for standalone myclaude / mykimi entrypoints."""
from pathlib import Path
from unittest.mock import MagicMock, patch
from click.testing import CliRunner
from myagents.entrypoints import claude_cli, kimi_cli
class TestMyclaudeEntrypoint:
"""``myclaude`` standalone entrypoint."""
def test_runs_claude(self) -> None:
runner = CliRunner()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/claude"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(claude_cli, [])
assert result.exit_code == 0
assert mock_run.call_args[0][0] == [
"/usr/bin/claude",
"--dangerously-skip-permissions",
]
def test_version_shows_myclaude(self) -> None:
runner = CliRunner()
result = runner.invoke(claude_cli, ["--version"])
assert result.exit_code == 0
assert "myclaude" in result.output
def test_passthrough(self, tmp_path: Path) -> None:
runner = CliRunner()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/claude"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(claude_cli, ["--cwd", str(tmp_path), "--resume"])
assert result.exit_code == 0
assert "--resume" in mock_run.call_args[0][0]
class TestMykimiEntrypoint:
"""``mykimi`` standalone entrypoint."""
def test_runs_kimi(self) -> None:
runner = CliRunner()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/kimi"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(kimi_cli, [])
assert result.exit_code == 0
assert mock_run.call_args[0][0] == ["/usr/bin/kimi"]
def test_version_shows_mykimi(self) -> None:
runner = CliRunner()
result = runner.invoke(kimi_cli, ["--version"])
assert result.exit_code == 0
assert "mykimi" in result.output
def test_passthrough(self, tmp_path: Path) -> None:
runner = CliRunner()
with (
patch("myagents.launcher.shutil.which", return_value="/usr/bin/kimi"),
patch("myagents.launcher.subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
result = runner.invoke(kimi_cli, ["--cwd", str(tmp_path), "--resume"])
assert result.exit_code == 0
assert "--resume" in mock_run.call_args[0][0]
+24 -24
View File
@@ -1,65 +1,65 @@
"""Tests for myclaude.project_root."""
"""Tests for myagents.project_root."""
import os
from pathlib import Path
from unittest.mock import patch
from myclaude.project_root import get_myclaude_project_root, get_workspace_root
from myagents.project_root import get_project_root, get_workspace_root
class TestGetMyclaudeProjectRoot:
"""Tests for get_myclaude_project_root."""
class TestGetProjectRoot:
"""Tests for get_project_root."""
def test_env_var_takes_priority(self, tmp_path: Path) -> None:
"""MYCLAUDE_PROJECT_ROOT env var should be used when set."""
"""MYAGENTS_PROJECT_ROOT env var should be used when set."""
fake_root = tmp_path / "fake_repo"
fake_root.mkdir()
(fake_root / "pyproject.toml").write_text('name = "myclaude"\n')
(fake_root / "pyproject.toml").write_text('name = "myagents"\n')
with patch.dict(os.environ, {"MYCLAUDE_PROJECT_ROOT": str(fake_root)}):
result = get_myclaude_project_root()
with patch.dict(os.environ, {"MYAGENTS_PROJECT_ROOT": str(fake_root)}):
result = get_project_root()
assert result == fake_root.resolve()
def test_env_var_expands_tilde(self, tmp_path: Path) -> None:
"""MYCLAUDE_PROJECT_ROOT should expand ~ to home directory."""
"""MYAGENTS_PROJECT_ROOT should expand ~ to home directory."""
home = tmp_path / "home"
home.mkdir()
fake_root = home / "fake_repo"
fake_root.mkdir()
(fake_root / "pyproject.toml").write_text('name = "myclaude"\n')
(fake_root / "pyproject.toml").write_text('name = "myagents"\n')
with patch.dict(
os.environ,
{
"MYCLAUDE_PROJECT_ROOT": "~/fake_repo",
"MYAGENTS_PROJECT_ROOT": "~/fake_repo",
"HOME": str(home),
},
):
result = get_myclaude_project_root()
result = get_project_root()
assert result == fake_root.resolve()
def test_fallback_when_not_in_repo(self) -> None:
"""When not in a repo, fallback to ~/.myclaude."""
"""When not in a repo, fallback to ~/.myagents."""
with (
patch.dict(os.environ, {}, clear=True),
patch("pathlib.Path.cwd", side_effect=OSError),
patch(
"myclaude.project_root._pyproject_names_myclaude",
"myagents.project_root._pyproject_names_myagents",
return_value=False,
),
):
result = get_myclaude_project_root()
assert result == Path.home() / ".myclaude"
result = get_project_root()
assert result == Path.home() / ".myagents"
class TestGetWorkspaceRoot:
"""Tests for get_workspace_root."""
def test_env_var_takes_priority(self, tmp_path: Path) -> None:
"""MYCLAUDE_WORKSPACE_ROOT env var should be used when set."""
"""MYAGENTS_WORKSPACE_ROOT env var should be used when set."""
custom = tmp_path / "custom_workspace"
with patch.dict(os.environ, {"MYCLAUDE_WORKSPACE_ROOT": str(custom)}):
with patch.dict(os.environ, {"MYAGENTS_WORKSPACE_ROOT": str(custom)}):
result = get_workspace_root()
assert result == custom.resolve()
assert result.is_dir()
@@ -69,21 +69,21 @@ class TestGetWorkspaceRoot:
new_ws = tmp_path / "new_workspace"
assert not new_ws.exists()
with patch.dict(os.environ, {"MYCLAUDE_WORKSPACE_ROOT": str(new_ws)}):
with patch.dict(os.environ, {"MYAGENTS_WORKSPACE_ROOT": str(new_ws)}):
result = get_workspace_root()
assert result == new_ws.resolve()
assert result.is_dir()
def test_defaults_to_project_workspace(self, tmp_path: Path) -> None:
"""When project_root/workspace exists, use it."""
project_root = tmp_path / "myclaude"
project_root = tmp_path / "myagents"
project_root.mkdir()
workspace = project_root / "workspace"
workspace.mkdir()
(project_root / "pyproject.toml").write_text('name = "myclaude"\n')
(project_root / "pyproject.toml").write_text('name = "myagents"\n')
with patch.dict(
os.environ, {"MYCLAUDE_PROJECT_ROOT": str(project_root)}, clear=True
os.environ, {"MYAGENTS_PROJECT_ROOT": str(project_root)}, clear=True
):
result = get_workspace_root()
assert result == workspace.resolve()
@@ -95,8 +95,8 @@ class TestGetWorkspaceRoot:
with (
patch.dict(os.environ, {}, clear=True),
patch(
"myclaude.project_root.get_myclaude_project_root",
return_value=tmp_path / ".myclaude",
"myagents.project_root.get_project_root",
return_value=tmp_path / ".myagents",
),
patch("pathlib.Path.home", return_value=home),
):