80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
"""世谱 API 集成测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import httpx
|
|
|
|
|
|
async def _default_character_id(auth_client: httpx.AsyncClient) -> str:
|
|
resp = await auth_client.get("/api/v1/characters")
|
|
assert resp.status_code == 200
|
|
return resp.json()["items"][0]["id"]
|
|
|
|
|
|
async def test_create_list_get_lorebook(auth_client: httpx.AsyncClient) -> None:
|
|
character_id = await _default_character_id(auth_client)
|
|
|
|
resp = await auth_client.post(
|
|
"/api/v1/lorebooks",
|
|
json={"name": "测试世谱", "character_id": character_id, "scope": "character"},
|
|
)
|
|
assert resp.status_code == 200
|
|
book = resp.json()
|
|
book_id = book["id"]
|
|
assert book["name"] == "测试世谱"
|
|
assert book["scope"] == "character"
|
|
|
|
entry_resp = await auth_client.post(
|
|
f"/api/v1/lorebooks/{book_id}/entries",
|
|
json={"keys": ["猫"], "content": "她喜欢猫。", "position": "after_char"},
|
|
)
|
|
assert entry_resp.status_code == 200
|
|
assert entry_resp.json()["keys"] == ["猫"]
|
|
|
|
listed = await auth_client.get(f"/api/v1/lorebooks?character_id={character_id}")
|
|
assert listed.status_code == 200
|
|
assert any(b["id"] == book_id for b in listed.json())
|
|
|
|
got = await auth_client.get(f"/api/v1/lorebooks/{book_id}")
|
|
assert got.status_code == 200
|
|
assert len(got.json()["entries"]) == 1
|
|
|
|
|
|
async def test_update_entry(auth_client: httpx.AsyncClient) -> None:
|
|
character_id = await _default_character_id(auth_client)
|
|
book = (await auth_client.post("/api/v1/lorebooks", json={"character_id": character_id})).json()
|
|
entry = (
|
|
await auth_client.post(
|
|
f"/api/v1/lorebooks/{book['id']}/entries",
|
|
json={"keys": ["旧"], "content": "旧"},
|
|
)
|
|
).json()
|
|
patched = await auth_client.patch(
|
|
f"/api/v1/lorebooks/{book['id']}/entries/{entry['id']}",
|
|
json={"keys": ["新"], "content": "新内容"},
|
|
)
|
|
assert patched.status_code == 200
|
|
assert patched.json()["keys"] == ["新"]
|
|
assert patched.json()["content"] == "新内容"
|
|
|
|
|
|
async def test_delete_lorebook_archives(auth_client: httpx.AsyncClient) -> None:
|
|
character_id = await _default_character_id(auth_client)
|
|
book = (
|
|
await auth_client.post("/api/v1/lorebooks", json={"character_id": character_id})
|
|
).json()
|
|
dele = await auth_client.delete(f"/api/v1/lorebooks/{book['id']}")
|
|
assert dele.status_code == 200
|
|
# archived 后不再可见
|
|
got = await auth_client.get(f"/api/v1/lorebooks/{book['id']}")
|
|
assert got.status_code == 404
|
|
|
|
|
|
async def test_other_owner_isolated(auth_client: httpx.AsyncClient) -> None:
|
|
character_id = await _default_character_id(auth_client)
|
|
book = (
|
|
await auth_client.post("/api/v1/lorebooks", json={"character_id": character_id})
|
|
).json()
|
|
# book id 对当前用户可见
|
|
assert (await auth_client.get(f"/api/v1/lorebooks/{book['id']}")).status_code == 200
|