gabriel / muse public
test_cmd_whoami.py python
87 lines 2.8 KB
Raw
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa feat: Muse — version control for the agent era Human 156 days 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
14 from muse.core._types import Manifest
15 from tests.cli_test_helper import CliRunner
16
17 cli = None # argparse migration — CliRunner ignores this arg
18
19 runner = CliRunner()
20
21
22 # ---------------------------------------------------------------------------
23 # Helpers
24 # ---------------------------------------------------------------------------
25
26
27 def _init_repo(path: pathlib.Path) -> pathlib.Path:
28 muse = path / ".muse"
29 (muse / "commits").mkdir(parents=True)
30 (muse / "snapshots").mkdir(parents=True)
31 (muse / "objects").mkdir(parents=True)
32 (muse / "refs" / "heads").mkdir(parents=True)
33 (muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8")
34 (muse / "repo.json").write_text(
35 json.dumps({"repo_id": "whoami-test", "domain": "midi"}), encoding="utf-8"
36 )
37 return path
38
39
40 def _env(repo: pathlib.Path) -> Manifest:
41 return {"MUSE_REPO_ROOT": str(repo)}
42
43
44 # ---------------------------------------------------------------------------
45 # Unit: help and basic invocation
46 # ---------------------------------------------------------------------------
47
48
49 def test_whoami_help() -> None:
50 result = runner.invoke(cli, ["whoami", "--help"])
51 assert result.exit_code == 0
52 assert "identity" in result.output.lower()
53
54
55 def test_whoami_exits_cleanly_or_with_user_error(tmp_path: pathlib.Path) -> None:
56 """whoami exits 0 (identity found) or 1 (not found) — never crashes."""
57 _init_repo(tmp_path)
58 result = runner.invoke(cli, ["whoami"], env=_env(tmp_path))
59 # Accept exit 0 (identity on machine) or 1 (no identity) — never any other code.
60 assert result.exit_code in {0, 1}
61
62
63 def test_whoami_json_flag_available() -> None:
64 """--json flag is accepted."""
65 result = runner.invoke(cli, ["whoami", "--help"])
66 assert "--json" in result.output or "-j" in result.output
67
68
69 def test_whoami_all_flag_available() -> None:
70 """--all flag is accepted."""
71 result = runner.invoke(cli, ["whoami", "--help"])
72 assert "--all" in result.output or "-a" in result.output
73
74
75 # ---------------------------------------------------------------------------
76 # Integration: short flags work
77 # ---------------------------------------------------------------------------
78
79
80 def test_whoami_short_flags_accepted(tmp_path: pathlib.Path) -> None:
81 """Both -j and -a flags are accepted without argument errors."""
82 _init_repo(tmp_path)
83 env = _env(tmp_path)
84 env["MUSE_HUB_URL"] = ""
85 result = runner.invoke(cli, ["whoami", "-j", "-a"], env=env)
86 # May fail because no identities, but shouldn't fail due to bad flags.
87 assert "invalid" not in result.output.lower() or result.exit_code != 2
File History 1 commit
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa feat: Muse — version control for the agent era Human 156 days ago