from __future__ import annotations import tempfile import unittest from unittest.mock import patch import os from pathlib import Path from pixiv_app.core.auth import SessionStore from pixiv_app.core.gallery_service import LocalGalleryService from pixiv_app.core.library import PixivLibrary from pixiv_app.core.origin_follow_sync import OriginFollow, OriginFollowSyncResult, OriginFollowSyncService from pixiv_app.core.paths import app_session_file class OriginFollowSyncLocalTest(unittest.TestCase): def test_pixiv_origin_follow_upsert_is_idempotent(self) -> None: with tempfile.TemporaryDirectory() as tmp: db_path = Path(tmp) / "library.db" service = OriginFollowSyncService(db_path=str(db_path)) result = OriginFollowSyncResult(platform="pixiv") follows = [ OriginFollow( platform="pixiv", external_id="12345", name="Sakura Artist", handle="sakura_artist", avatar_url="https://i.pximg.net/user-profile/img.jpg", profile_url="https://www.pixiv.net/users/12345", ) ] service._upsert_pixiv_local(follows, result) second = OriginFollowSyncResult(platform="pixiv") service._upsert_pixiv_local(follows, second) self.assertEqual(result.created, 1) self.assertEqual(second.skipped, 1) library = PixivLibrary(db_path) try: rows = library.connection.execute("SELECT artist_id, enabled FROM followed_artists").fetchall() self.assertEqual(len(rows), 1) self.assertEqual(int(rows[0]["artist_id"]), 12345) self.assertEqual(int(rows[0]["enabled"]), 1) finally: library.close() def test_gallery_sync_reads_shared_app_session_cookie(self) -> None: old_data_dir = os.environ.get("SAKURA_APP_DATA_DIR") try: with tempfile.TemporaryDirectory() as tmp: os.environ["SAKURA_APP_DATA_DIR"] = tmp db_path = Path(tmp) / "library.db" SessionStore(app_session_file()).save({"cookie": "PHPSESSID=fake"}) library = PixivLibrary(db_path) try: library.set_followed_artist(12345, True, "Sakura Artist") finally: library.close() service = LocalGalleryService(db_path=db_path) with patch("pixiv_app.core.gallery_service.fetch_user_work_ids", return_value=(12345, [])) as mocked: result = service.sync_followed_artists(download=False) self.assertEqual(result["artists"][0]["artist_id"], 12345) self.assertEqual(mocked.call_args.kwargs["cookie"], "PHPSESSID=fake") finally: if old_data_dir is None: os.environ.pop("SAKURA_APP_DATA_DIR", None) else: os.environ["SAKURA_APP_DATA_DIR"] = old_data_dir def test_gallery_sync_keeps_new_pixiv_work_local(self) -> None: old_data_dir = os.environ.get("SAKURA_APP_DATA_DIR") try: with tempfile.TemporaryDirectory() as tmp: os.environ["SAKURA_APP_DATA_DIR"] = tmp db_path = Path(tmp) / "library.db" SessionStore(app_session_file()).save({"cookie": "PHPSESSID=fake"}) library = PixivLibrary(db_path) try: library.set_followed_artist(12345, True, "Sakura Artist") finally: library.close() service = LocalGalleryService(db_path=db_path) with patch("pixiv_app.core.gallery_service.fetch_user_work_ids", return_value=(12345, [67890])): result = service.sync_followed_artists(download=False) self.assertEqual(result["artists"][0]["new_count"], 1) self.assertEqual(result["artists"][0]["alerts"], 0) self.assertFalse(result["artists"][0]["cloud_synced"]) finally: if old_data_dir is None: os.environ.pop("SAKURA_APP_DATA_DIR", None) else: os.environ["SAKURA_APP_DATA_DIR"] = old_data_dir if __name__ == "__main__": unittest.main()