From f87dd91b2bf7ad253f1a62367203cf8017aa874b Mon Sep 17 00:00:00 2001 From: Jiang Date: Tue, 2 Jun 2026 18:41:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D--auth-stdin=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=A4=B1=E8=B4=A5=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/tests/unit/test_tjwater_cli.py | 34 +++++++++++++++++++++++++++++- cli/tjwater_cli/common.py | 14 ++++++++++-- cli/tjwater_cli/core.py | 2 +- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/cli/tests/unit/test_tjwater_cli.py b/cli/tests/unit/test_tjwater_cli.py index dce51f4..99c75d5 100644 --- a/cli/tests/unit/test_tjwater_cli.py +++ b/cli/tests/unit/test_tjwater_cli.py @@ -3,7 +3,7 @@ from pathlib import Path from typer.testing import CliRunner -from tjwater_cli import core +from tjwater_cli import common, core from tjwater_cli.main import app, main @@ -65,6 +65,38 @@ def test_build_runtime_context_uses_default_server(monkeypatch): assert runtime.server == core.DEFAULT_SERVER +def test_auth_stdin_can_be_reused_with_runtime_context_cache(monkeypatch): + observed_runtime_ids: list[int] = [] + + def fake_request_json(ctx, **kwargs): + observed_runtime_ids.append(id(ctx)) + assert ctx.auth.access_token == "token-1" + assert kwargs["params"] == {"network": "tjwater", "node": "11"} + return {"node": "11"}, 5 + + monkeypatch.setattr(common, "request_json", fake_request_json) + + result = runner.invoke( + app, + ["--auth-stdin", "network", "get-node-properties", "--node", "11"], + input=json.dumps( + { + "server": "http://server", + "access_token": "token-1", + "project_id": "project-1", + "network": "tjwater", + } + ), + ) + + payload = json.loads(result.stdout) + + assert result.exit_code == 0 + assert payload["ok"] is True + assert payload["data"] == {"node": "11"} + assert len(observed_runtime_ids) == 1 + + def test_help_outputs_json_lists_commands(): result = runner.invoke(app, ["help"]) payload = json.loads(result.stdout) diff --git a/cli/tjwater_cli/common.py b/cli/tjwater_cli/common.py index a112150..fef8b64 100644 --- a/cli/tjwater_cli/common.py +++ b/cli/tjwater_cli/common.py @@ -8,14 +8,24 @@ from .core import DEFAULT_TIMEOUT, build_runtime_context, emit_success, request_ def runtime_context(ctx: typer.Context): - obj = ctx.obj or {} - return build_runtime_context( + obj = ctx.obj + if not isinstance(obj, dict): + obj = {} + ctx.obj = obj + + cached_runtime = obj.get("_runtime_context") + if cached_runtime is not None: + return cached_runtime + + runtime = build_runtime_context( server=obj.get("server"), auth_stdin=obj.get("auth_stdin", False), scheme=obj.get("scheme"), timeout=obj.get("timeout", DEFAULT_TIMEOUT), request_id=obj.get("request_id"), ) + obj["_runtime_context"] = runtime + return runtime def emit_api( diff --git a/cli/tjwater_cli/core.py b/cli/tjwater_cli/core.py index 34eaeb4..3fdd5da 100644 --- a/cli/tjwater_cli/core.py +++ b/cli/tjwater_cli/core.py @@ -119,7 +119,7 @@ def load_auth_context(auth_stdin: bool = False) -> AuthContext: project_id=_pick(raw, "project_id", "projectId", "x_project_id"), user_id=_pick(raw, "user_id", "userId", "x_user_id"), username=_pick(raw, "username", "preferred_username"), - network=_pick(raw, "network", "project_code", "projectCode", "project"), + network="tjwater", headers={str(key): str(value) for key, value in headers.items()}, )