test(product-suite): complete gateway integration
This commit is contained in:
@@ -8,7 +8,7 @@ sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
from _helpers import TempDirMixin
|
||||
|
||||
from app import db, image_studio
|
||||
from app import appconfig, db, image_studio
|
||||
from app import gui
|
||||
|
||||
if gui.QT_IMPORT_ERROR is not None:
|
||||
@@ -18,6 +18,51 @@ from app.gui.workers import ProductSuiteGenerateWorker
|
||||
|
||||
|
||||
class ProductSuiteWorkerTests(TempDirMixin, unittest.TestCase):
|
||||
@staticmethod
|
||||
def _direct_config(temp_dir, db_path):
|
||||
models_path = os.path.join(temp_dir, "ai_models.json")
|
||||
config = {
|
||||
"db_path": db_path,
|
||||
"ai_models_path": models_path,
|
||||
"ai": {
|
||||
"backend": "direct",
|
||||
"default_text_model": "测试文本模型",
|
||||
"default_image_model": "测试图片模型",
|
||||
},
|
||||
}
|
||||
appconfig.save_ai_models_config(
|
||||
{
|
||||
"models": [
|
||||
{
|
||||
"name": "测试文本模型",
|
||||
"category": "text",
|
||||
"enabled": True,
|
||||
"url": "https://text.example.com/v1",
|
||||
"model": "text-model",
|
||||
"api_key": "sk-text-test",
|
||||
"api_type": "chat",
|
||||
"connect_timeout_seconds": 3,
|
||||
"timeout_seconds": 10,
|
||||
"extra_body": {},
|
||||
},
|
||||
{
|
||||
"name": "测试图片模型",
|
||||
"category": "image",
|
||||
"enabled": True,
|
||||
"url": "https://image.example.com/v1",
|
||||
"model": "image-model",
|
||||
"api_key": "sk-image-test",
|
||||
"api_type": "images_edits",
|
||||
"connect_timeout_seconds": 7,
|
||||
"timeout_seconds": 12,
|
||||
"extra_body": {},
|
||||
},
|
||||
]
|
||||
},
|
||||
path=models_path,
|
||||
)
|
||||
return config
|
||||
|
||||
def test_generate_worker_creates_category_jobs_and_forwards_ratio(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
db_path = os.path.join(temp_dir, "cmshopee.db")
|
||||
@@ -83,6 +128,98 @@ class ProductSuiteWorkerTests(TempDirMixin, unittest.TestCase):
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_worker_keeps_frozen_direct_source_in_mixed_project_after_settings_switch(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
db_path = os.path.join(temp_dir, "cmshopee.db")
|
||||
db.init_db(db_path)
|
||||
project = image_studio.create_or_get_project(
|
||||
account_alias="alias",
|
||||
account_slug="alias_slug",
|
||||
item_id="51100639510",
|
||||
path=db_path,
|
||||
)
|
||||
source = image_studio.add_asset(
|
||||
project.id,
|
||||
image_studio.ASSET_KIND_ORIGINAL,
|
||||
local_path=os.path.join(temp_dir, "source.png"),
|
||||
path=db_path,
|
||||
)
|
||||
submitted = image_studio.create_job(
|
||||
project.id,
|
||||
source_asset_id=source.id,
|
||||
prompt="默认网关已提交任务",
|
||||
generation_source=image_studio.GENERATION_SOURCE_CMHUB,
|
||||
provider=image_studio.PROVIDER_CMHUB,
|
||||
path=db_path,
|
||||
)
|
||||
submitted = image_studio.set_job_submitted(
|
||||
submitted.id,
|
||||
"cmhub-task-existing",
|
||||
path=db_path,
|
||||
)
|
||||
config = self._direct_config(temp_dir, db_path)
|
||||
worker = ProductSuiteGenerateWorker(
|
||||
project.id,
|
||||
[
|
||||
{
|
||||
"source_asset_id": source.id,
|
||||
"job_type": "场景图",
|
||||
"prompt": "自定义网关新任务",
|
||||
}
|
||||
],
|
||||
run_token="direct-frozen-run",
|
||||
db_path=db_path,
|
||||
config=config,
|
||||
)
|
||||
|
||||
# 模拟 worker 已创建后,用户在⑤设置切回默认网关并保存。
|
||||
config["ai"]["backend"] = "cmhub"
|
||||
with mock.patch(
|
||||
"app.gui.workers.image_studio_generation.run_jobs",
|
||||
return_value={
|
||||
"total": 1,
|
||||
"success": 0,
|
||||
"failed": 0,
|
||||
"cancelled": 0,
|
||||
"jobs": [],
|
||||
},
|
||||
) as run_jobs:
|
||||
worker.execute()
|
||||
|
||||
new_job = image_studio.get_job(worker.job_ids[0], path=db_path)
|
||||
self.assertEqual(image_studio.GENERATION_SOURCE_DIRECT, new_job.generation_source)
|
||||
self.assertEqual(image_studio.PROVIDER_OPENAI_IMAGES_EDITS, new_job.provider)
|
||||
self.assertIsNone(new_job.task_id)
|
||||
frozen_config = run_jobs.call_args.kwargs["config"]
|
||||
self.assertEqual("direct", appconfig.ai_backend(frozen_config))
|
||||
self.assertEqual(
|
||||
"sk-image-test",
|
||||
frozen_config["_cmshopee_ai_runtime"]["direct_models"][1]["api_key"],
|
||||
)
|
||||
self.assertEqual(
|
||||
[submitted.id],
|
||||
[
|
||||
job.id
|
||||
for job in image_studio.list_resumable_jobs(
|
||||
project_id=project.id,
|
||||
include_failed_downloads=True,
|
||||
path=db_path,
|
||||
)
|
||||
],
|
||||
)
|
||||
self.assertEqual(
|
||||
{
|
||||
image_studio.GENERATION_SOURCE_CMHUB,
|
||||
image_studio.GENERATION_SOURCE_DIRECT,
|
||||
},
|
||||
{
|
||||
job.generation_source
|
||||
for job in image_studio.list_jobs(project.id, path=db_path)
|
||||
},
|
||||
)
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user