"""回归:CK 列表按用户粘贴的账号名顺序排序,而不是按「最近拿到 CK 的时间」。 2026-10-04 CK 解耦后,列表单位从「登录记录」变成「账号」,排序键也跟着从 ``login_tasks.finished_at`` 换成 ``accounts.cookie_updated_at``。 """ from datetime import UTC, datetime, timedelta from tests.support.db import get_test_engine, make_test_session from web.backend.models import Account, User from web.backend.routers.cookies import _order_cookie_accounts class TestCustomCookieOrder: def setup_method(self): self.engine = get_test_engine() self.db = make_test_session(self.engine) self.user = User(username="admin", password_hash="hash", role="super_admin") self.db.add(self.user) self.db.flush() now = datetime.now(UTC) self.accounts = {} for index, name in enumerate(("account-a", "account-b", "account-c")): account = Account( username=name, password="p", email="e", email_password="ep", cookie=f"ck-{name}", # 拿到 CK 的时间特意与用户输入顺序相反。 cookie_updated_at=now + timedelta(minutes=index), ) self.accounts[name] = account self.db.add(account) self.db.commit() def teardown_method(self): self.db.close() def test_custom_cookie_order_follows_input_not_updated_time(self): selected_names = ["account-c", "account-a", "account-b"] accounts = _order_cookie_accounts( self.db.query(Account).filter(Account.username.in_(selected_names)), selected_names, ).all() assert [account.username for account in accounts] == selected_names def test_default_order_is_most_recent_cookie_first(self): accounts = _order_cookie_accounts(self.db.query(Account), []).all() assert [account.username for account in accounts] == [ "account-c", "account-b", "account-a", ]