Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/sai_cli/project_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,24 @@ def render_project_compute(result: ProjectCompute, *, args: Namespace) -> str:
value = result.values
if "items" in value:
if value["schema_version"] == "collaboration.projects/v1":
lines = [f"{row['label']} · {row['organization_label']} · {row['code']}\n {row['project_id']}" for row in value["items"]]
lines = []
for row in value["items"]:
command = follow_up_command(args, "projects", "members", row["project_id"], "--format", "table")
lines.append(f"{row['label']} · {row['organization_label']} · {row['code']}\n {terminal_log(command)}")
else:
roles = {"owner": "负责人", "admin": "管理员", "write": "编辑文件", "read": "查看文件"}
states = {"active": "", "scheduled": " · 尚未生效", "expired": " · 已过期"}
lines = [f"{row['username']} · {'计算已启用' if row['compute_enabled'] else '计算未启用'} · {roles[row['role']]}{states[row['membership_status']]}\n {row['membership_id']}" for row in value["items"]]
lines = []
for row in value["items"]:
command = follow_up_command(
args, "projects", "member", value["project"]["project_id"], row["membership_id"],
"--site", value["site_id"], "--format", "table",
)
compute = "计算已启用" if row["compute_enabled"] else "计算未启用"
lines.append(
f"{row['username']} · {compute} · {roles[row['role']]}{states[row['membership_status']]}\n"
f" {terminal_log(command)}"
)
if value["has_more"]:
words = ["projects", args.projects_command]
if args.projects_command == "members":
Expand Down
42 changes: 42 additions & 0 deletions tests/test_project_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
from pathlib import Path
import shlex
from urllib.parse import parse_qs, urlsplit

import pytest

Expand Down Expand Up @@ -84,6 +85,47 @@ def reply(call,count):
assert len(calls)==2


def test_listed_project_and_member_open_the_selected_permission_preview(tmp_path):
projects = sample("collaboration-projects")
selected = projects["items"][0]
projects["items"].insert(0, {**selected, "project_id": "project-a", "organization_label": "Other team"})
members = sample("project-members")
members["items"].insert(0, {**members["items"][0], "membership_id": "member-a", "username": "another"})
detail = sample("project-member")
members["site_id"] = detail["site_id"] = detail["sites"][0]["site_id"] = "site-b"
responses = iter((projects, members, detail))

def run(words):
output, error = StringIO(), StringIO()
assert main(words, environ={"SAI_TOKEN": "test-user"}, stdout=output, stderr=error) == 0, error.getvalue()
return output.getvalue()

def command_under(display, label):
lines = display.splitlines()
command = shlex.split(lines[lines.index(label) + 1])
assert command[0] == "sai"
return command[1:]

with http_endpoint(lambda call, count: (200, next(responses))) as (origin, calls):
config = tmp_path / "team profile.toml"
config.write_text(f'[profiles.research]\nbase_url = {json.dumps(origin)}\nsite_id = "site-b"\n')
config.chmod(0o600)
listing = run(["projects", "list", "--query", "Research", "--format", "table",
"--config", str(config), "--profile", "research"])
roster = run(command_under(listing, "Research · Research team · proj-example"))
member = command_under(roster, "researcher · 计算未启用 · 编辑文件")
assert member[member.index("--site") + 1] == "site-b"
preview = run(member)

assert [(call["method"], urlsplit(call["path"]).path) for call in calls] == [
("GET", "/v1/account/cli/projects"),
("GET", "/v1/account/cli/projects/project-example/members"),
("GET", "/v1/account/cli/projects/project-example/members/member-example"),
]
assert all(parse_qs(urlsplit(call["path"]).query)["site_id"] == ["site-b"] for call in calls[1:])
assert "付款组织:Research team" in preview and "确认启用:" in preview


@pytest.mark.parametrize("failure", ["unavailable", "invalid_response", "state_changed", "member_pending", "idempotency_conflict"])
def test_apply_requires_request_key_and_does_not_retry_a_failed_write(tmp_path, failure):
def reply(call,_):
Expand Down
Loading