File size: 3,117 Bytes
fd11263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from __future__ import annotations

from pathlib import Path

from nano_python_agents import cli
from nano_python_agents.providers.base import FakeProvider
from nano_python_agents.types import AssistantTurn


def test_listing_packaged_assets_does_not_require_api_key(tmp_path: Path, monkeypatch, capsys) -> None:
    monkeypatch.delenv("NANO_API_KEY", raising=False)

    assert cli.main(["--workspace", str(tmp_path), "--list-prompts"]) == 0
    prompt_output = capsys.readouterr().out
    assert "system  packaged" in prompt_output

    assert cli.main(["--workspace", str(tmp_path), "--list-skills"]) == 0
    skill_output = capsys.readouterr().out
    assert "repo-explore  v1" in skill_output


def test_relative_trusted_directory_is_resolved_from_workspace(
    tmp_path: Path, monkeypatch, capsys
) -> None:
    prompt_dir = tmp_path / "trusted-prompts"
    prompt_dir.mkdir()
    (prompt_dir / "review.md").write_text("review {workspace}", encoding="utf-8")
    (tmp_path / "nano_agent.toml").write_text(
        '[prompts]\ndirectories = ["trusted-prompts"]\n', encoding="utf-8"
    )
    monkeypatch.chdir(tmp_path.parent)

    assert cli.main(["--workspace", str(tmp_path), "--list-prompts"]) == 0
    assert "review  external" in capsys.readouterr().out


def test_prompt_validation_precedes_api_key_validation(tmp_path: Path, monkeypatch, capsys) -> None:
    monkeypatch.delenv("NANO_API_KEY", raising=False)

    result = cli.main(
        ["--workspace", str(tmp_path), "--system-prompt", "missing", "do work"]
    )

    captured = capsys.readouterr()
    assert result == 2
    assert "unknown prompt identifier" in captured.err
    assert "API key" not in captured.err


def test_cli_injects_compact_skill_index_without_body(tmp_path: Path, monkeypatch) -> None:
    captured: dict[str, str] = {}

    class CapturingProvider(FakeProvider):
        def create_turn(self, system_prompt, messages, tools):
            captured["system_prompt"] = system_prompt
            return super().create_turn(system_prompt, messages, tools)

    monkeypatch.setattr(
        cli,
        "_build_provider",
        lambda settings: CapturingProvider([AssistantTurn("done", [], "stop")]),
    )

    assert cli.main(["--workspace", str(tmp_path), "inspect"]) == 0
    assert "## Available Skills" in captured["system_prompt"]
    assert "repo-explore (v1)" in captured["system_prompt"]
    assert "# Repo Explore" not in captured["system_prompt"]


def test_list_tools_does_not_require_api_key_and_lists_native_todo(tmp_path: Path, monkeypatch, capsys) -> None:
    monkeypatch.delenv("NANO_API_KEY", raising=False)
    internal = tmp_path / ".nano_agent"
    internal.mkdir()
    from nano_python_agents.todo import build_contract, write_contract
    write_contract(internal / "todo_contract.json", build_contract("cli-run"))
    (tmp_path / "nano_agent.toml").write_text(
        '[todo]\nenabled = true\nrun_id = "cli-run"\nwriter_session = "A"\n', encoding="utf-8"
    )
    assert cli.main(["--workspace", str(tmp_path), "--list-tools"]) == 0
    assert "todowrite" in capsys.readouterr().out.splitlines()