gabriel / muse public
test_cmd_whoami.py python
85 lines 2.8 KB
86000da9 fix: replace typer CliRunner with argparse-compatible test helper Gabriel Cardona <gabriel@tellurstori.com> 1d ago
1 """Tests for ``muse whoami``.
2
3 Covers: no hub configured, no identity stored, identity found (text and JSON),
4 all-hubs listing, --help exits 0.
5 """
6
7 from __future__ import annotations
8
9 import json
10 import pathlib
11
12 import pytest
13 from tests.cli_test_helper import CliRunner
14
15 cli = None # argparse migration — CliRunner ignores this arg
16
17 runner = CliRunner()
18
19
20 # ---------------------------------------------------------------------------
21 # Helpers
22 # ---------------------------------------------------------------------------
23
24
25 def _init_repo(path: pathlib.Path) -> pathlib.Path:
26 muse = path / ".muse"
27 (muse / "commits").mkdir(parents=True)
28 (muse / "snapshots").mkdir(parents=True)
29 (muse / "objects").mkdir(parents=True)
30 (muse / "refs" / "heads").mkdir(parents=True)
31 (muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8")
32 (muse / "repo.json").write_text(
33 json.dumps({"repo_id": "whoami-test", "domain": "midi"}), encoding="utf-8"
34 )
35 return path
36
37
38 def _env(repo: pathlib.Path) -> dict[str, str]:
39 return {"MUSE_REPO_ROOT": str(repo)}
40
41
42 # ---------------------------------------------------------------------------
43 # Unit: help and basic invocation
44 # ---------------------------------------------------------------------------
45
46
47 def test_whoami_help() -> None:
48 result = runner.invoke(cli, ["whoami", "--help"])
49 assert result.exit_code == 0
50 assert "identity" in result.output.lower()
51
52
53 def test_whoami_exits_cleanly_or_with_user_error(tmp_path: pathlib.Path) -> None:
54 """whoami exits 0 (identity found) or 1 (not found) — never crashes."""
55 _init_repo(tmp_path)
56 result = runner.invoke(cli, ["whoami"], env=_env(tmp_path))
57 # Accept exit 0 (identity on machine) or 1 (no identity) — never any other code.
58 assert result.exit_code in {0, 1}
59
60
61 def test_whoami_json_flag_available() -> None:
62 """--json flag is accepted."""
63 result = runner.invoke(cli, ["whoami", "--help"])
64 assert "--json" in result.output or "-j" in result.output
65
66
67 def test_whoami_all_flag_available() -> None:
68 """--all flag is accepted."""
69 result = runner.invoke(cli, ["whoami", "--help"])
70 assert "--all" in result.output or "-a" in result.output
71
72
73 # ---------------------------------------------------------------------------
74 # Integration: short flags work
75 # ---------------------------------------------------------------------------
76
77
78 def test_whoami_short_flags_accepted(tmp_path: pathlib.Path) -> None:
79 """Both -j and -a flags are accepted without argument errors."""
80 _init_repo(tmp_path)
81 env = _env(tmp_path)
82 env["MUSE_HUB_URL"] = ""
83 result = runner.invoke(cli, ["whoami", "-j", "-a"], env=env)
84 # May fail because no identities, but shouldn't fail due to bad flags.
85 assert "invalid" not in result.output.lower() or result.exit_code != 2