File size: 6,675 Bytes
b65f9e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from __future__ import annotations

import asyncio
from pathlib import Path

import httpx

from zai2api.account_pool import AccountPool
from zai2api.config import Settings
from zai2api.db import Database
from zai2api.zai_client import SessionState, UpstreamResult


class FakeClient:
    def __init__(self, *, name: str, answer: str, fail_status: int | None = None):
        self._name = name
        self._answer = answer
        self._fail_status = fail_status

    async def ensure_session(self, force_refresh: bool = False) -> SessionState:
        return SessionState(
            token=f"session-{self._name}",
            user_id=f"user-{self._name}",
            name=self._name,
            email=f"{self._name}@example.com",
            role="user",
        )

    async def verify_completion_version(self) -> int:
        return 2

    async def collect_prompt(
        self,
        *,
        prompt: str,
        model: str,
        enable_thinking: bool,
        auto_web_search: bool,
    ) -> UpstreamResult:
        if self._fail_status is not None:
            request = httpx.Request("POST", "https://example.com")
            response = httpx.Response(self._fail_status, request=request)
            raise httpx.HTTPStatusError("boom", request=request, response=response)
        return UpstreamResult(
            answer_text=f"{self._answer}:{prompt}",
            reasoning_text="reasoning",
            usage={"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
            finish_reason="stop",
        )

    async def stream_prompt(self, **_: object):
        if False:
            yield None

    async def aclose(self) -> None:
        return None


def make_settings(tmp_path: Path, **overrides: object) -> Settings:
    base = Settings(
        host="127.0.0.1",
        port=8000,
        log_level="info",
        zai_base_url="https://chat.z.ai",
        zai_jwt=None,
        zai_session_token=None,
        default_model="glm-5",
        request_timeout=120.0,
        database_path=str(tmp_path / "state.db"),
        panel_password_env=None,
        api_password_env=None,
        admin_cookie_name="zai2api_admin_session",
        admin_session_ttl_hours=24,
        admin_cookie_secure=False,
    )
    for key, value in overrides.items():
        setattr(base, key, value)
    return base


def test_account_pool_rotates_enabled_accounts(tmp_path: Path) -> None:
    settings = make_settings(tmp_path)
    db = Database(settings.database_path)
    db.initialize()
    first = db.upsert_account(
        jwt="jwt-a",
        session_token="token-a",
        user_id="user-a",
        email="a@example.com",
        name="a",
    )
    second = db.upsert_account(
        jwt="jwt-b",
        session_token="token-b",
        user_id="user-b",
        email="b@example.com",
        name="b",
    )

    def client_factory(jwt: str | None, session_token: str | None) -> FakeClient:
        if session_token == "token-a":
            return FakeClient(name="a", answer="alpha")
        if session_token == "token-b":
            return FakeClient(name="b", answer="beta")
        raise AssertionError(f"unexpected session token: {session_token}")

    pool = AccountPool(settings, db, client_factory=client_factory)
    first_result = asyncio.run(
        pool.collect_prompt(
            prompt="hello",
            model="glm-5",
            enable_thinking=True,
            auto_web_search=False,
        )
    )
    second_result = asyncio.run(
        pool.collect_prompt(
            prompt="hello",
            model="glm-5",
            enable_thinking=True,
            auto_web_search=False,
        )
    )

    assert first_result.answer_text == "alpha:hello"
    assert second_result.answer_text == "beta:hello"
    assert db.get_account(first.id).status == "active"
    assert db.get_account(second.id).status == "active"


def test_account_pool_disables_unauthorized_account(tmp_path: Path) -> None:
    settings = make_settings(tmp_path)
    db = Database(settings.database_path)
    db.initialize()
    bad = db.upsert_account(
        jwt="jwt-bad",
        session_token="token-bad",
        user_id="user-bad",
        email="bad@example.com",
        name="bad",
    )
    db.upsert_account(
        jwt="jwt-good",
        session_token="token-good",
        user_id="user-good",
        email="good@example.com",
        name="good",
    )

    def client_factory(jwt: str | None, session_token: str | None) -> FakeClient:
        if session_token == "token-bad":
            return FakeClient(name="bad", answer="bad", fail_status=401)
        if session_token == "token-good":
            return FakeClient(name="good", answer="good")
        raise AssertionError(f"unexpected session token: {session_token}")

    pool = AccountPool(settings, db, client_factory=client_factory)
    result = asyncio.run(
        pool.collect_prompt(
            prompt="hello",
            model="glm-5",
            enable_thinking=True,
            auto_web_search=False,
        )
    )

    bad_account = db.get_account(bad.id)
    assert result.answer_text == "good:hello"
    assert bad_account is not None
    assert bad_account.enabled is False
    assert bad_account.status == "invalid"


def test_register_jwt_persists_account(tmp_path: Path) -> None:
    settings = make_settings(tmp_path)
    db = Database(settings.database_path)
    db.initialize()

    def client_factory(jwt: str | None, session_token: str | None) -> FakeClient:
        assert jwt == "fresh-jwt"
        return FakeClient(name="fresh", answer="fresh")

    pool = AccountPool(settings, db, client_factory=client_factory)
    account = asyncio.run(pool.register_jwt("fresh-jwt"))

    assert account.user_id == "user-fresh"
    assert account.session_token == "session-fresh"
    assert db.count_accounts() == 1


def test_check_account_can_reenable_account(tmp_path: Path) -> None:
    settings = make_settings(tmp_path)
    db = Database(settings.database_path)
    db.initialize()
    account = db.upsert_account(
        jwt="jwt-a",
        session_token="token-a",
        user_id="user-a",
        email="a@example.com",
        name="a",
        enabled=False,
        status="invalid",
        last_error="HTTP 401",
        failure_count=2,
    )

    def client_factory(jwt: str | None, session_token: str | None) -> FakeClient:
        return FakeClient(name="a", answer="alpha")

    pool = AccountPool(settings, db, client_factory=client_factory)
    updated = asyncio.run(pool.check_account(account.id))

    assert updated.enabled is True
    assert updated.status == "active"
    assert updated.failure_count == 0