Files

44 lines
1.8 KiB
Python
Raw Permalink Normal View History

2026-11-16 20:45:22 +08:00
from __future__ import annotations
import unittest
from pixiv_app.core.cookie_import import cookies_to_playwright, parse_cookie_text
class XCookieImportTests(unittest.TestCase):
def test_netscape_x_cookie_export_is_playwright_compatible(self) -> None:
text = """# Netscape HTTP Cookie File
.x.com\tTRUE\t/\tTRUE\t1819195756\tguest_id\tv1%3Aexample
x.com\tFALSE\t/\tFALSE\t0\tlang\tzh-CN
.x.com\tTRUE\t/\tTRUE\t1809869781\tauth_token\tsecret-auth-token
.x.com\tTRUE\t/\tTRUE\t1809869782\tct0\tsecret-csrf-token
"""
cookies = parse_cookie_text(text)
playwright_cookies = cookies_to_playwright(cookies, ("x.com", "twitter.com"))
names = {item["name"] for item in playwright_cookies}
self.assertIn("auth_token", names)
self.assertIn("ct0", names)
self.assertIn("guest_id", names)
self.assertIn("lang", names)
lang_cookie = next(item for item in playwright_cookies if item["name"] == "lang")
self.assertNotIn("expires", lang_cookie)
def test_x_cookie_conversion_drops_invalid_items_and_session_expiry(self) -> None:
prepared = cookies_to_playwright(
[
{"name": "", "value": "bad", "domain": ".x.com"},
{"name": "lang", "value": "zh-CN", "domain": "x.com", "path": "/", "expires": 0},
{"name": "ct0", "value": "token", "domain": ".x.com", "path": "/", "expires": "1809869782"},
],
("x.com",),
)
self.assertEqual({item["name"] for item in prepared}, {"lang", "ct0"})
self.assertNotIn("expires", next(item for item in prepared if item["name"] == "lang"))
self.assertEqual(next(item for item in prepared if item["name"] == "ct0")["expires"], 1809869782)
if __name__ == "__main__":
unittest.main()