Repository navigation
Expand file tree
/
Copy pathtest_server_cli.py
More file actions
104 lines (93 loc) · 3.76 KB
/
Copy pathtest_server_cli.py
File metadata and controls
104 lines (93 loc) · 3.76 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
# Copyright (c) 2026 OceanBase.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from datetime import UTC, datetime
from types import TracebackType
from typing import Self
import httpx
import pytest
from typer.testing import CliRunner
import powercontext.client.cli as client_cli
from powercontext.cli.app import create_cli
from powercontext.client import PowerContextClient
from powercontext.http import Capabilities, ExtractionStatus, MemorySearchMode, PreparedContextSchema
from powercontext.server.app import create_app
@pytest.mark.parametrize(
"extraction",
[
pytest.param(None, id="diagnostics-unavailable"),
pytest.param(
ExtractionStatus.model_validate({
"configuration": "configured",
"background": {
"location": "local",
"role": "leader",
"state": "running",
"automatic_processing_enabled": True,
},
"observation": {
"status": "observed",
"since": datetime(2026, 9, 28, tzinfo=UTC),
"last_failure": {
"code": "model_timeout",
"stage": "inference",
"occurred_at": datetime(2026, 9, 29, tzinfo=UTC),
},
},
}),
id="extraction-failed",
),
],
)
def test_capabilities_flow_through_server_sdk_and_cli(
monkeypatch: pytest.MonkeyPatch, extraction: ExtractionStatus | None
) -> None:
server_capabilities = Capabilities(
source_types=["git-commit"],
artifact_families=["memory", "handoff"],
memory_extraction=True,
extraction=extraction,
handoff_generation=True,
search_modes=[MemorySearchMode.FTS],
context_versions=[PreparedContextSchema.POWERCONTEXT_PREPARED_CONTEXT_V1],
)
server_app = create_app(capability_provider=lambda: server_capabilities)
class InProcessClient:
def __init__(self) -> None:
self._http_client: httpx.AsyncClient | None = None
self._sdk: PowerContextClient | None = None
async def __aenter__(self) -> Self:
self._http_client = httpx.AsyncClient(
transport=httpx.ASGITransport(app=server_app),
base_url="http://testserver",
)
self._sdk = PowerContextClient(
"http://testserver", http_client=self._http_client, trust_transport_security=True
)
return self
async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
assert self._http_client is not None
await self._http_client.aclose()
async def get_capabilities(self) -> Capabilities:
assert self._sdk is not None
return await self._sdk.get_capabilities()
monkeypatch.setattr(client_cli, "PowerContextClient", lambda *_args, **_kwargs: InProcessClient())
result = CliRunner().invoke(create_cli([]), ["--json", "capabilities"])
assert result.exit_code == 0
assert json.loads(result.output) == server_capabilities.model_dump(mode="json")