From fc51e2a7c9e008e0741f36712c4c4e8ada2ded6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B8=85?= Date: Wed, 2 Sep 2026 11:49:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A7=E9=AA=8C=E6=94=B6=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=A1=A5=E9=BD=90Cookie=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- acceptance_service.py | 34 +++++++++++++++++++++++++++++++ tests/test_acceptance_creation.py | 28 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/acceptance_service.py b/acceptance_service.py index 991c56b..2b1eb86 100644 --- a/acceptance_service.py +++ b/acceptance_service.py @@ -1640,6 +1640,40 @@ def _task_dict(conn, task) -> dict[str, Any]: FROM acceptance_task_resources WHERE task_id=? ORDER BY resource_order""", (task["id"],), ).fetchall() + has_custom_cookie = conn.execute( + """SELECT 1 FROM acceptance_task_secrets + WHERE task_id=? AND secret_type='custom_cookie' LIMIT 1""", + (task["id"],), + ).fetchone() + if (not task_resources and not has_custom_cookie + and task["scheduled_from_task_id"] is None + and task["acceptance_method"] in {"data-analysis", "combined"}): + try: + current_resources = get_wen_resources() + credential_ids = list(dict.fromkeys( + str(item).strip() for item in current_resources.get("credential_ids", []) + if str(item).strip() + )) + conn.executemany( + """INSERT OR IGNORE INTO acceptance_task_resources + (task_id,resource_type,resource_id,resource_order,created_at) + VALUES (?,'cookie',?,?,?)""", + [(task["id"], credential_id, index, now_iso()) + for index, credential_id in enumerate(credential_ids)], + ) + if credential_ids: + conn.execute( + "UPDATE acceptance_tasks SET cookie_count=?,updated_at=? WHERE id=?", + (len(credential_ids), now_iso(), task["id"]), + ) + task_resources = conn.execute( + """SELECT resource_type,resource_id,resource_order + FROM acceptance_task_resources WHERE task_id=? ORDER BY resource_order""", + (task["id"],), + ).fetchall() + except Exception: + # 详情读取不能因为 Cookie 池短暂不可用而整体失败。 + pass cookie_attempts = conn.execute( """SELECT credential_id,keyword,status,attempt_number,error_message, started_at,completed_at diff --git a/tests/test_acceptance_creation.py b/tests/test_acceptance_creation.py index 5d92ea9..a9ee2a2 100644 --- a/tests/test_acceptance_creation.py +++ b/tests/test_acceptance_creation.py @@ -80,6 +80,34 @@ class AcceptanceCreationTest(unittest.TestCase): self.assertEqual(task["schedule_start_time"], "20:00") self.assertIsNotNone(task["next_run_at"]) self.assertEqual(task["keyword_runs"], []) + self.assertEqual( + [item["resource_id"] for item in task["resource_progress"] if item["resource_type"] == "cookie"], + ["c1", "c2"], + ) + + @patch("acceptance_service.get_wen_resources") + def test_legacy_root_task_backfills_current_cookie_pool(self, resources): + resources.return_value = {"cookie_count": 2, "device_count": 3, "credential_ids": ["pool-1", "pool-2"]} + now = now_iso() + with connection() as conn: + task_id = conn.execute( + """INSERT INTO acceptance_tasks + (public_task_id,name,acceptance_method,status,progress_percent,device_count, + created_by_id,created_by_name,created_at,updated_at,cookie_count) + VALUES ('legacy-root','旧项目','combined','completed',100,3,'u','u',?,?,2)""", + (now, now), + ).lastrowid + conn.execute( + "INSERT INTO acceptance_keywords(task_id,keyword_order,keyword,created_at) VALUES (?,0,'山姆会员卡',?)", + (task_id, now), + ) + + task = get_acceptance_task("legacy-root") + + self.assertEqual( + [item["resource_id"] for item in task["resource_progress"] if item["resource_type"] == "cookie"], + ["pool-1", "pool-2"], + ) @patch("acceptance_service.threading.Thread", _ThreadWithoutStart) @patch("acceptance_service.sync_dashboard_acceptance_project")