Raven-1/tests/test_cli_skill_commands.py
Dizhan Xue 562c8457c7
fix(config): accept null skill sections when updating the blocklist (#914)
## Summary

The loader accepts a null skillForge section, but block/unblock either
crashed or created a second section under the other spelling. Treat null
as an empty section for a write, retain the existing spelling, and
refuse other invalid section types without overwriting them.

The CLI regression checks block/unblock, case-insensitive idempotence, a
loadable result and an absent unblock that leaves the original bytes
unchanged. The unblock output must not name a key the write did not use.

## Type

- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

- [ ] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed

Native Windows, Python 3.12. PYTHONUTF8=1; the existing environment was
selected through UV_PROJECT_ENVIRONMENT, and PYTHONPATH pointed at this
checkout.

```text
uv run --no-sync pytest -o addopts= tests/test_config_update.py tests/test_cli_skill_commands.py -k "null_section or invalid_section or skill_unblock_removes" -q --tb=short --show-capture=no
```

Result: 10 passed in the targeted selection. The broader two-file
selection previously produced 89 passed and 4 failed at the pre-existing
POSIX permission assertions in config-update tests. Those failures were
reproduced in the main audit; the affected permission-writing code is
unchanged. The relevant-tests box remains unchecked for the broader
selection.

Whole-branch gates:

```powershell
$changed = @(git diff --name-only origin/main...HEAD -- '*.py')
$production = @($changed | Where-Object { $_ -like 'raven/*' })
uv run --no-sync ruff check @changed
uv run --no-sync ruff format --check @changed
uv run --no-sync ty check @production
uv run --no-sync lint-imports
uv run --no-sync pre-commit run --from-ref origin/main --to-ref HEAD
uv run --no-sync python scripts/check_commit_messages.py origin/main..HEAD
uv run --no-sync python scripts/check_source_language.py origin/main...HEAD
uv run --no-sync python scripts/check_large_files.py origin/main...HEAD
git diff --check origin/main...HEAD
```

All listed gates passed. The whole-branch review covered callers,
ownership, guard boundaries, failure paths, regression strength,
architecture and compatibility. No screenshot change applies; directly
affected code documentation is updated where needed. The full repository
suite and Linux/macOS execution were not run locally; the local Docker
Linux engine was unavailable.

## Risk

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

Only an absent or null selected section is initialized. Other invalid
types fail without rewriting the file. Existing dictionaries, spelling
precedence and no-op behavior remain intact. Revert the squash commit to
roll back.

## Related Issues

#500
2026-10-12 19:17:30 +08:00

300 lines
11 KiB
Python

"""CLI tests for ``raven skill``.
Two subcommands are covered with mocked ``SkillService`` so the tests
stay self-contained.
"""
from __future__ import annotations
from pathlib import Path
from types import SimpleNamespace
import pytest
from typer.testing import CliRunner
from raven.cli.commands import app
from raven.config.loader import set_config_path
runner = CliRunner()
@pytest.fixture
def tmp_config(tmp_path: Path) -> Path:
cfg = tmp_path / "config.json"
set_config_path(cfg)
yield cfg
set_config_path(None) # type: ignore[arg-type]
# ============================================================================
# --help surfaces
# ============================================================================
def test_skill_help_lists_all_subcommands() -> None:
r = runner.invoke(app, ["skill", "--help"])
assert r.exit_code == 0
for sub in ("list", "get", "block", "unblock", "remove"):
assert sub in r.stdout, f"missing subcommand in --help: {sub}"
@pytest.mark.parametrize("subcmd", ["list", "get", "block", "unblock", "remove"])
def test_skill_subcommand_help_works(subcmd: str) -> None:
"""Every skill subcommand exposes ``--help`` without crashing."""
r = runner.invoke(app, ["skill", subcmd, "--help"])
assert r.exit_code == 0
# ============================================================================
# skill list / get (mock SkillService)
# ============================================================================
def _make_meta(name: str, source: str = "builtin", desc: str = "stub") -> SimpleNamespace:
return SimpleNamespace(name=name, source=source, description=desc)
def test_skill_list_renders_table(tmp_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""``skill list`` prints a table when the registry returns metas."""
fake_svc = SimpleNamespace(
gather_all_skills=lambda: [_make_meta("alpha"), _make_meta("beta", source="workspace")],
)
monkeypatch.setattr("raven.cli.skill_commands._build_skill_service", lambda: fake_svc)
r = runner.invoke(app, ["skill", "list"])
assert r.exit_code == 0
assert "alpha" in r.stdout
assert "beta" in r.stdout
assert "Skills" in r.stdout # table title
def test_skill_list_empty_message(tmp_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Empty registry prints the ``No skills found`` notice."""
fake_svc = SimpleNamespace(gather_all_skills=lambda: [])
monkeypatch.setattr("raven.cli.skill_commands._build_skill_service", lambda: fake_svc)
r = runner.invoke(app, ["skill", "list"])
assert r.exit_code == 0
assert "No skills found" in r.stdout
def test_skill_list_filters_by_source(tmp_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""``--source workspace`` only shows metas matching that source."""
fake_svc = SimpleNamespace(
gather_all_skills=lambda: [
_make_meta("alpha", source="builtin"),
_make_meta("beta", source="workspace"),
],
)
monkeypatch.setattr("raven.cli.skill_commands._build_skill_service", lambda: fake_svc)
r = runner.invoke(app, ["skill", "list", "--source", "workspace"])
assert r.exit_code == 0
assert "beta" in r.stdout
assert "alpha" not in r.stdout
def test_skill_get_known_skill(tmp_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""``skill get <name>`` prints the metadata fields."""
fake_svc = SimpleNamespace(
get_skill_metadata=lambda _: {"name": "alpha", "source": "builtin"},
load_skill=lambda _: "# SKILL.md body",
)
monkeypatch.setattr("raven.cli.skill_commands._build_skill_service", lambda: fake_svc)
r = runner.invoke(app, ["skill", "get", "alpha"])
assert r.exit_code == 0
assert "alpha" in r.stdout
assert "builtin" in r.stdout
def test_skill_get_unknown_skill_exits_1(tmp_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:
fake_svc = SimpleNamespace(
get_skill_metadata=lambda _: None,
load_skill=lambda _: None,
)
monkeypatch.setattr("raven.cli.skill_commands._build_skill_service", lambda: fake_svc)
r = runner.invoke(app, ["skill", "get", "ghost-skill"])
assert r.exit_code == 1
assert "Skill not found" in r.stdout
def test_skill_get_with_body_renders_markdown(tmp_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""``--with-body`` prints the SKILL.md body section."""
fake_svc = SimpleNamespace(
get_skill_metadata=lambda _: {"source": "builtin"},
load_skill=lambda _: "# Title\nbody text",
)
monkeypatch.setattr("raven.cli.skill_commands._build_skill_service", lambda: fake_svc)
r = runner.invoke(app, ["skill", "get", "alpha", "--with-body"])
assert r.exit_code == 0
assert "SKILL.md" in r.stdout
def test_skill_list_shows_installed_column(tmp_config: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Skills whose dir carries .install-meta.json get an Installed cell
of the form ``YYYY-MM-DD (source)``; others stay blank."""
import json
skill_dir = tmp_path / "foo@v1"
skill_dir.mkdir()
(skill_dir / ".install-meta.json").write_text(
json.dumps({"installed_at": "2026-08-16T00:00:00+00:00", "source": "hub"}),
encoding="utf-8",
)
installed = SimpleNamespace(name="foo", source="workspace", description="d", path=skill_dir / "SKILL.md")
fake_svc = SimpleNamespace(gather_all_skills=lambda: [installed, _make_meta("bare", desc="d")])
monkeypatch.setattr("raven.cli.skill_commands._build_skill_service", lambda: fake_svc)
r = runner.invoke(app, ["skill", "list"])
assert r.exit_code == 0
assert "Installed" in r.stdout
assert "2026-08-16" in r.stdout
assert "(hub)" in r.stdout
def test_skill_list_marks_blocked(tmp_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Names on skillForge.blocklist get a [blocked] marker; the row still
renders (gather_all_skills stays unfiltered for inspection)."""
import json
tmp_config.write_text(
json.dumps({"skillForge": {"blocklist": ["Alpha"]}}),
encoding="utf-8",
)
fake_svc = SimpleNamespace(
gather_all_skills=lambda: [_make_meta("alpha", desc="d"), _make_meta("beta", desc="d")],
)
monkeypatch.setattr("raven.cli.skill_commands._build_skill_service", lambda: fake_svc)
r = runner.invoke(app, ["skill", "list"])
assert r.exit_code == 0
lines = r.stdout.splitlines()
assert any("alpha" in line and "[blocked]" in line for line in lines)
assert not any("beta" in line and "[blocked]" in line for line in lines)
# ============================================================================
# skill block / unblock (on-disk skillForge.blocklist)
# ============================================================================
def _read_blocklist(cfg: Path) -> list[str]:
import json
data = json.loads(cfg.read_text(encoding="utf-8"))
return data.get("skillForge", {}).get("blocklist", [])
def test_skill_block_adds_to_config(tmp_config: Path) -> None:
r = runner.invoke(app, ["skill", "block", "tag-memory"])
assert r.exit_code == 0
assert "tag-memory" in r.stdout
assert _read_blocklist(tmp_config) == ["tag-memory"]
def test_skill_block_is_idempotent_case_insensitive(tmp_config: Path) -> None:
runner.invoke(app, ["skill", "block", "tag-memory"])
r = runner.invoke(app, ["skill", "block", "Tag-Memory"])
assert r.exit_code == 0
assert _read_blocklist(tmp_config) == ["tag-memory"]
def test_skill_unblock_removes_from_config(tmp_config: Path) -> None:
runner.invoke(app, ["skill", "block", "tag-memory"])
runner.invoke(app, ["skill", "block", "other"])
r = runner.invoke(app, ["skill", "unblock", "Tag-Memory"])
assert r.exit_code == 0
assert "skillForge" not in r.stdout
assert _read_blocklist(tmp_config) == ["other"]
@pytest.mark.parametrize("spelling", ["skillForge", "skill_forge"])
def test_skill_block_and_unblock_accept_a_null_section(tmp_config: Path, spelling: str) -> None:
import json
from raven.config.loader import load_config
from raven.config.raven import load_raven_config
tmp_config.write_text(json.dumps({spelling: None}), encoding="utf-8")
load_config(tmp_config)
empty = tmp_config.read_bytes()
assert runner.invoke(app, ["skill", "unblock", "absent"]).exit_code == 0
assert tmp_config.read_bytes() == empty
result = runner.invoke(app, ["skill", "block", "tag-memory"])
assert result.exit_code == 0, result.output
assert json.loads(tmp_config.read_text(encoding="utf-8")) == {spelling: {"blocklist": ["tag-memory"]}}
assert load_raven_config(config_path=tmp_config).skill_forge.blocklist == ["tag-memory"]
before = tmp_config.read_bytes()
assert runner.invoke(app, ["skill", "block", "TAG-MEMORY"]).exit_code == 0
assert tmp_config.read_bytes() == before
result = runner.invoke(app, ["skill", "unblock", "TAG-MEMORY"])
assert result.exit_code == 0
assert "Unblocked" in result.stdout and "skillForge" not in result.stdout
assert json.loads(tmp_config.read_text(encoding="utf-8")) == {spelling: {"blocklist": []}}
def test_skill_block_reports_the_block_it_wrote_on_a_snake_config(tmp_config: Path) -> None:
"""The result line must not name a key this write did not use.
On a snake_case config the write lands on ``skill_forge.blocklist``, so a
line naming ``skillForge.blocklist`` sends the operator to grep for a key
their file does not carry.
"""
import json
tmp_config.write_text(json.dumps({"skill_forge": {}}), encoding="utf-8")
r = runner.invoke(app, ["skill", "block", "tag-memory"])
assert r.exit_code == 0
assert "tag-memory" in r.stdout
assert "skillForge" not in r.stdout
data = json.loads(tmp_config.read_text(encoding="utf-8"))
assert data["skill_forge"]["blocklist"] == ["tag-memory"]
def test_skill_unblock_absent_is_noop(tmp_config: Path) -> None:
r = runner.invoke(app, ["skill", "unblock", "ghost"])
assert r.exit_code == 0
# ============================================================================
# skill remove (installed Hub bundle dirs)
# ============================================================================
def _workspace_config(cfg: Path, tmp_path: Path) -> Path:
import json
ws = tmp_path / "ws"
(ws / "skills" / "hub").mkdir(parents=True)
cfg.write_text(
json.dumps({"agents": {"defaults": {"workspace": str(ws)}}}),
encoding="utf-8",
)
return ws
def test_skill_remove_deletes_matching_bundles(tmp_config: Path, tmp_path: Path) -> None:
ws = _workspace_config(tmp_config, tmp_path)
bundle = ws / "skills" / "hub" / "tag-memory@v1"
bundle.mkdir()
(bundle / "SKILL.md").write_text("x", encoding="utf-8")
other = ws / "skills" / "hub" / "keep-me@v2"
other.mkdir()
r = runner.invoke(app, ["skill", "remove", "Tag-Memory", "--yes"])
assert r.exit_code == 0
assert not bundle.exists()
assert other.exists()
def test_skill_remove_unknown_exits_1(tmp_config: Path, tmp_path: Path) -> None:
_workspace_config(tmp_config, tmp_path)
r = runner.invoke(app, ["skill", "remove", "ghost", "--yes"])
assert r.exit_code == 1
assert "No installed Hub bundle" in r.stdout