-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathtest_workspace_commands.py
More file actions
184 lines (143 loc) · 6.5 KB
/
test_workspace_commands.py
File metadata and controls
184 lines (143 loc) · 6.5 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""Tests for workspace CLI commands."""
import tempfile
from pathlib import Path
import pytest
from typer.testing import CliRunner
import basic_memory.config
from basic_memory.cli.app import app
from basic_memory.config import BasicMemoryConfig, ConfigManager
from basic_memory.schemas.cloud import WorkspaceInfo
# Importing the cloud package registers workspace_app on cloud_app.
import basic_memory.cli.commands.cloud as cloud_cmd # noqa: F401
import basic_memory.cli.commands.cloud.workspace as workspace_cmd # noqa: F401
@pytest.fixture
def runner():
return CliRunner()
def test_workspace_list_prints_available_workspaces(runner, monkeypatch):
async def fake_get_available_workspaces(context=None):
return [
WorkspaceInfo(
tenant_id="11111111-1111-1111-1111-111111111111",
workspace_type="personal",
name="Personal",
role="owner",
),
WorkspaceInfo(
tenant_id="22222222-2222-2222-2222-222222222222",
workspace_type="organization",
name="Team",
role="editor",
),
]
monkeypatch.setattr(workspace_cmd, "get_available_workspaces", fake_get_available_workspaces)
result = runner.invoke(app, ["cloud", "workspace", "list"])
assert result.exit_code == 0
assert "Available Workspaces" in result.stdout
assert "Personal" in result.stdout
assert "Team" in result.stdout
# Tenant ID may be truncated by Rich table rendering
assert "11111111" in result.stdout
def test_workspace_list_requires_oauth_login_message(runner, monkeypatch):
async def fail_get_available_workspaces(context=None): # pragma: no cover
raise RuntimeError("Workspace discovery requires OAuth login. Run 'bm cloud login' first.")
monkeypatch.setattr(workspace_cmd, "get_available_workspaces", fail_get_available_workspaces)
result = runner.invoke(app, ["cloud", "workspace", "list"])
assert result.exit_code == 1
assert "Workspace discovery requires OAuth login" in result.stdout
assert "bm cloud login" in result.stdout
class TestWorkspaceSetDefault:
"""Tests for 'bm cloud workspace set-default' command."""
@pytest.fixture(autouse=True)
def _setup_config(self, monkeypatch):
"""Set up a temp config for each test."""
self.temp_dir = tempfile.mkdtemp()
temp_path = Path(self.temp_dir)
config_dir = temp_path / ".basic-memory"
config_dir.mkdir(parents=True, exist_ok=True)
monkeypatch.setenv("HOME", str(temp_path))
monkeypatch.setenv("BASIC_MEMORY_CONFIG_DIR", str(config_dir))
basic_memory.config._CONFIG_CACHE = None
basic_memory.config._CONFIG_MTIME = None
basic_memory.config._CONFIG_SIZE = None
config_manager = ConfigManager()
test_config = BasicMemoryConfig(
projects={"main": {"path": str(temp_path / "main")}},
)
config_manager.save_config(test_config)
def test_set_default_workspace_by_name(self, runner, monkeypatch):
async def fake_get_available_workspaces(context=None):
return [
WorkspaceInfo(
tenant_id="11111111-1111-1111-1111-111111111111",
workspace_type="personal",
name="Personal",
role="owner",
),
]
monkeypatch.setattr(
workspace_cmd, "get_available_workspaces", fake_get_available_workspaces
)
result = runner.invoke(app, ["cloud", "workspace", "set-default", "Personal"])
assert result.exit_code == 0
assert "Default workspace set" in result.stdout
assert "Personal" in result.stdout
# Verify config was updated
basic_memory.config._CONFIG_CACHE = None
basic_memory.config._CONFIG_MTIME = None
basic_memory.config._CONFIG_SIZE = None
config = ConfigManager().config
assert config.default_workspace == "11111111-1111-1111-1111-111111111111"
def test_set_default_workspace_by_tenant_id(self, runner, monkeypatch):
async def fake_get_available_workspaces(context=None):
return [
WorkspaceInfo(
tenant_id="22222222-2222-2222-2222-222222222222",
workspace_type="organization",
name="Team",
role="editor",
),
]
monkeypatch.setattr(
workspace_cmd, "get_available_workspaces", fake_get_available_workspaces
)
result = runner.invoke(
app, ["cloud", "workspace", "set-default", "22222222-2222-2222-2222-222222222222"]
)
assert result.exit_code == 0
assert "Default workspace set" in result.stdout
def test_set_default_workspace_not_found(self, runner, monkeypatch):
async def fake_get_available_workspaces(context=None):
return [
WorkspaceInfo(
tenant_id="11111111-1111-1111-1111-111111111111",
workspace_type="personal",
name="Personal",
role="owner",
),
]
monkeypatch.setattr(
workspace_cmd, "get_available_workspaces", fake_get_available_workspaces
)
result = runner.invoke(app, ["cloud", "workspace", "set-default", "Nonexistent"])
assert result.exit_code == 1
assert "not found" in result.stdout
def test_set_default_workspace_no_workspaces(self, runner, monkeypatch):
async def fake_get_available_workspaces(context=None):
return []
monkeypatch.setattr(
workspace_cmd, "get_available_workspaces", fake_get_available_workspaces
)
result = runner.invoke(app, ["cloud", "workspace", "set-default", "Personal"])
assert result.exit_code == 1
assert "No accessible workspaces" in result.stdout
def test_set_default_workspace_oauth_error(self, runner, monkeypatch):
async def fail_get_available_workspaces(context=None): # pragma: no cover
raise RuntimeError(
"Workspace discovery requires OAuth login. Run 'bm cloud login' first."
)
monkeypatch.setattr(
workspace_cmd, "get_available_workspaces", fail_get_available_workspaces
)
result = runner.invoke(app, ["cloud", "workspace", "set-default", "Personal"])
assert result.exit_code == 1
assert "OAuth login" in result.stdout