Add resource upload APIs
This commit is contained in:
@ -61,14 +61,21 @@ class FakeAsyncClient:
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
|
||||
async def post(self, path: str, json: dict | None = None) -> FakeResponse:
|
||||
self.calls.append(("post", self.api_key, self.headers, path, json))
|
||||
async def post(self, path: str, json: dict | None = None, files: dict | None = None) -> FakeResponse:
|
||||
if files and "file" in files:
|
||||
uploaded = files["file"]
|
||||
files = {"file": uploaded[0] if isinstance(uploaded, tuple) else uploaded}
|
||||
self.calls.append(("post", self.api_key, self.headers, path, json, files))
|
||||
return self.responses.pop(0)
|
||||
|
||||
async def get(self, path: str) -> FakeResponse:
|
||||
self.calls.append(("get", self.api_key, self.headers, path, None))
|
||||
return self.responses.pop(0)
|
||||
|
||||
async def delete(self, path: str, params: dict | None = None) -> FakeResponse:
|
||||
self.calls.append(("delete", self.api_key, self.headers, path, params))
|
||||
return self.responses.pop(0)
|
||||
|
||||
|
||||
def test_openviking_rejects_unknown_user_credentials():
|
||||
store = FakeStore()
|
||||
@ -126,7 +133,7 @@ def test_openviking_create_user_creates_isolated_admin_account():
|
||||
},
|
||||
),
|
||||
]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -152,6 +159,7 @@ def test_openviking_create_user_creates_isolated_admin_account():
|
||||
{},
|
||||
"/api/v1/admin/accounts",
|
||||
{"account_id": "userA_account", "admin_user_id": "userA"},
|
||||
None,
|
||||
),
|
||||
]
|
||||
|
||||
@ -175,7 +183,7 @@ def test_openviking_create_user_creates_account_even_when_admin_workspace_exists
|
||||
},
|
||||
)
|
||||
]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -201,6 +209,7 @@ def test_openviking_create_user_creates_account_even_when_admin_workspace_exists
|
||||
{},
|
||||
"/api/v1/admin/accounts",
|
||||
{"account_id": "userB_account", "admin_user_id": "userB"},
|
||||
None,
|
||||
)
|
||||
]
|
||||
|
||||
@ -210,7 +219,7 @@ def test_openviking_user_key_auth_is_used_for_session_create():
|
||||
client.root_key = "root-key"
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"session_id": "sess-2"}})]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -229,6 +238,7 @@ def test_openviking_user_key_auth_is_used_for_session_create():
|
||||
{},
|
||||
"/api/v1/sessions",
|
||||
{"session_id": "sess-2"},
|
||||
None,
|
||||
)
|
||||
]
|
||||
|
||||
@ -237,7 +247,7 @@ def test_openviking_find_uses_current_identity_memory_scope():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -255,6 +265,7 @@ def test_openviking_find_uses_current_identity_memory_scope():
|
||||
{},
|
||||
"/api/v1/search/find",
|
||||
{"query": "咖啡", "target_uri": "viking://user/tom/memories/", "limit": 5},
|
||||
None,
|
||||
)
|
||||
]
|
||||
|
||||
@ -263,7 +274,7 @@ def test_openviking_search_uses_fixed_user_memory_target_with_level_and_score_th
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -287,6 +298,7 @@ def test_openviking_search_uses_fixed_user_memory_target_with_level_and_score_th
|
||||
"level": 3,
|
||||
"score_threshold": 0.7,
|
||||
},
|
||||
None,
|
||||
)
|
||||
]
|
||||
|
||||
@ -295,7 +307,7 @@ def test_openviking_search_accepts_custom_target_uri():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -326,6 +338,7 @@ def test_openviking_search_accepts_custom_target_uri():
|
||||
"level": 3,
|
||||
"score_threshold": 0.7,
|
||||
},
|
||||
None,
|
||||
)
|
||||
]
|
||||
|
||||
@ -334,7 +347,7 @@ def test_openviking_profile_search_uses_user_memory_target_and_level():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"memories": []}})]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -357,6 +370,7 @@ def test_openviking_profile_search_uses_user_memory_target_and_level():
|
||||
"level": 2,
|
||||
"target_uri": "viking://user/memories",
|
||||
},
|
||||
None,
|
||||
)
|
||||
]
|
||||
|
||||
@ -376,7 +390,7 @@ def test_openviking_get_session_context_uses_user_key_auth():
|
||||
},
|
||||
)
|
||||
]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -420,7 +434,7 @@ def test_openviking_commit_keeps_no_recent_live_messages():
|
||||
},
|
||||
)
|
||||
]
|
||||
client._client = lambda api_key, extra_headers=None: FakeAsyncClient( # type: ignore[method-assign]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
@ -446,6 +460,115 @@ def test_openviking_commit_keeps_no_recent_live_messages():
|
||||
{},
|
||||
"/api/v1/sessions/sess-1/commit",
|
||||
{"keep_recent_count": 0},
|
||||
None,
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_openviking_upload_temp_file_posts_multipart(tmp_path):
|
||||
path = tmp_path / "report.pdf"
|
||||
path.write_bytes(b"pdf-bytes")
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [
|
||||
FakeResponse(
|
||||
200,
|
||||
{"status": "ok", "result": {"temp_file_id": "upload_report.pdf"}},
|
||||
)
|
||||
]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
extra_headers or {},
|
||||
)
|
||||
credential = client.user_credential("tom-key", "tom")
|
||||
|
||||
result = asyncio.run(client.upload_temp_file(credential, path))
|
||||
|
||||
assert result == {"status": "ok", "result": {"temp_file_id": "upload_report.pdf"}}
|
||||
assert calls == [
|
||||
(
|
||||
"post",
|
||||
"tom-key",
|
||||
{},
|
||||
"/api/v1/resources/temp_upload",
|
||||
None,
|
||||
{"file": "report.pdf"},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_openviking_add_resource_posts_url_payload():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"uri": "viking://resources/tom/images/photo.png"}})]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
extra_headers or {},
|
||||
)
|
||||
credential = client.user_credential("tom-key", "tom")
|
||||
|
||||
result = asyncio.run(
|
||||
client.add_resource(
|
||||
credential,
|
||||
path="https://example.com/photo.png",
|
||||
to="viking://resources/tom/images/photo.png",
|
||||
reason="上传远程图片",
|
||||
wait=True,
|
||||
directly_upload_media=True,
|
||||
)
|
||||
)
|
||||
|
||||
assert result == {"status": "ok", "result": {"uri": "viking://resources/tom/images/photo.png"}}
|
||||
assert calls == [
|
||||
(
|
||||
"post",
|
||||
"tom-key",
|
||||
{},
|
||||
"/api/v1/resources",
|
||||
{
|
||||
"path": "https://example.com/photo.png",
|
||||
"to": "viking://resources/tom/images/photo.png",
|
||||
"reason": "上传远程图片",
|
||||
"wait": True,
|
||||
"directly_upload_media": True,
|
||||
},
|
||||
None,
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_openviking_delete_resource_sends_uri_and_recursive_flag():
|
||||
client = OpenVikingMemorySystemClient(store=FakeStore())
|
||||
calls = []
|
||||
responses = [FakeResponse(200, {"status": "ok", "result": {"estimated_deleted_count": 4}})]
|
||||
client._client = lambda api_key, extra_headers=None, json_content_type=True: FakeAsyncClient( # type: ignore[method-assign]
|
||||
calls,
|
||||
responses,
|
||||
api_key,
|
||||
extra_headers or {},
|
||||
)
|
||||
credential = client.user_credential("tom-key", "tom")
|
||||
|
||||
result = asyncio.run(
|
||||
client.delete_resource(
|
||||
credential,
|
||||
uri="viking://resources/tom/files/report.pdf",
|
||||
recursive=True,
|
||||
)
|
||||
)
|
||||
|
||||
assert result == {"status": "ok", "result": {"estimated_deleted_count": 4}}
|
||||
assert calls == [
|
||||
(
|
||||
"delete",
|
||||
"tom-key",
|
||||
{},
|
||||
"/api/v1/fs",
|
||||
{"uri": "viking://resources/tom/files/report.pdf", "recursive": "true"},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user