2026-07-05 10:03:11 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from backend.routes.integration_routes import list_integration_logs, private_companion_daily_outfit
|
|
|
|
|
from backend.services.integration_service import IntegrationService
|
|
|
|
|
from backend.services.material_service import MaterialService
|
|
|
|
|
from backend.services.profile_service import ProfileService
|
|
|
|
|
from backend.services.wardrobe_service import WardrobeService
|
|
|
|
|
from core.audit_service import AuditService
|
|
|
|
|
from core.identity import RequestContext
|
|
|
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
|
|
|
|
from database.repositories.integration_repository import IntegrationRepository
|
|
|
|
|
from database.repositories.material_repository import MaterialRepository
|
|
|
|
|
from database.repositories.profile_repository import ProfileRepository
|
|
|
|
|
from database.repositories.wardrobe_repository import WardrobeRepository
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IntegrationServiceTests(unittest.TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
|
|
|
self.database = Database(
|
|
|
|
|
DatabaseSettings(sqlite=SqliteSettings(path=str(Path(self.tmp.name) / "imagestudio.db")))
|
|
|
|
|
)
|
|
|
|
|
self.database.initialize()
|
|
|
|
|
self.audit_service = AuditService(self.database)
|
|
|
|
|
self.profile_repository = ProfileRepository(self.database)
|
|
|
|
|
self.material_repository = MaterialRepository(self.database)
|
|
|
|
|
self.wardrobe_repository = WardrobeRepository(self.database)
|
|
|
|
|
self.integration_repository = IntegrationRepository(self.database)
|
|
|
|
|
self.profile_service = ProfileService(self.profile_repository, self.audit_service)
|
|
|
|
|
self.material_service = MaterialService(self.material_repository, self.audit_service)
|
|
|
|
|
self.wardrobe_service = WardrobeService(
|
|
|
|
|
self.wardrobe_repository,
|
|
|
|
|
self.profile_repository,
|
|
|
|
|
self.material_repository,
|
|
|
|
|
self.audit_service,
|
|
|
|
|
)
|
|
|
|
|
self.integration_service = IntegrationService(self.wardrobe_service, self.integration_repository)
|
|
|
|
|
self.ctx = RequestContext.from_astrbot(
|
|
|
|
|
username="companion",
|
|
|
|
|
user_id="companion_001",
|
|
|
|
|
permissions={"can_manage_materials"},
|
|
|
|
|
)
|
|
|
|
|
self.profile = self.profile_service.create_profile({"name": "Nova"}, self.ctx)
|
|
|
|
|
category = self.material_service.create_category({"name": "Upper"}, self.ctx)
|
|
|
|
|
material = self.material_service.create_material(
|
|
|
|
|
{
|
|
|
|
|
"category_id": category.category_id,
|
|
|
|
|
"display_name": "White blouse",
|
|
|
|
|
"image_uri": "storage://materials/white_blouse.png",
|
|
|
|
|
"prompt": "white blouse, clean collar",
|
|
|
|
|
"tags": ["white", "top"],
|
|
|
|
|
"recognition_status": "confirmed",
|
|
|
|
|
},
|
|
|
|
|
self.ctx,
|
|
|
|
|
)
|
|
|
|
|
self.wardrobe_service.import_material(
|
|
|
|
|
self.profile.profile_id,
|
|
|
|
|
{"material_id": material.material_id, "wardrobe_category": "top"},
|
|
|
|
|
self.ctx,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
self.tmp.cleanup()
|
|
|
|
|
|
|
|
|
|
def test_daily_outfit_uses_wardrobe_match_and_records_log(self):
|
|
|
|
|
response = self.integration_service.daily_outfit(
|
|
|
|
|
{
|
|
|
|
|
"profile_id": self.profile.profile_id,
|
|
|
|
|
"mood": "relaxed",
|
|
|
|
|
"weather": "sunny",
|
|
|
|
|
"keywords": ["top"],
|
|
|
|
|
"request_source": "astrbot_plugin_private_companion",
|
|
|
|
|
},
|
|
|
|
|
self.ctx,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertTrue(response["matched"])
|
|
|
|
|
self.assertIn("white blouse", response["final_prompt"])
|
|
|
|
|
logs = self.integration_service.list_integration_logs("astrbot_plugin_private_companion")
|
|
|
|
|
self.assertEqual(len(logs), 1)
|
|
|
|
|
self.assertEqual(logs[0]["action"], "daily_outfit")
|
|
|
|
|
|
|
|
|
|
def test_daily_outfit_fallback_when_no_materials_match(self):
|
|
|
|
|
response = self.integration_service.daily_outfit(
|
|
|
|
|
{
|
|
|
|
|
"profile_id": self.profile.profile_id,
|
|
|
|
|
"mood": "formal",
|
|
|
|
|
"weather": "rainy",
|
|
|
|
|
"categories": ["shoes"],
|
|
|
|
|
},
|
|
|
|
|
self.ctx,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertFalse(response["matched"])
|
|
|
|
|
self.assertTrue(response["fallback_used"])
|
|
|
|
|
self.assertIn("formal", response["final_prompt"])
|
|
|
|
|
|
|
|
|
|
def test_integration_routes_wrap_responses(self):
|
|
|
|
|
response = private_companion_daily_outfit(
|
|
|
|
|
self.ctx,
|
|
|
|
|
{"profile_id": self.profile.profile_id, "keywords": ["top"]},
|
|
|
|
|
self.integration_service,
|
|
|
|
|
)
|
|
|
|
|
self.assertTrue(response["ok"])
|
|
|
|
|
self.assertTrue(response["data"]["matched"])
|
|
|
|
|
|
|
|
|
|
logs = list_integration_logs(self.ctx, {}, self.integration_service)
|
|
|
|
|
self.assertTrue(logs["ok"])
|
|
|
|
|
self.assertEqual(len(logs["data"]), 1)
|
|
|
|
|
|
|
|
|
|
bad_source = private_companion_daily_outfit(
|
|
|
|
|
self.ctx,
|
|
|
|
|
{"profile_id": self.profile.profile_id, "request_source": "unknown_plugin"},
|
|
|
|
|
self.integration_service,
|
|
|
|
|
)
|
|
|
|
|
self.assertFalse(bad_source["ok"])
|
|
|
|
|
self.assertEqual(bad_source["error_code"], "INTEGRATION_TOKEN_INVALID")
|
|
|
|
|
|
2026-07-05 15:28:28 +08:00
|
|
|
failed_logs = list_integration_logs(
|
|
|
|
|
self.ctx,
|
|
|
|
|
{"integration_name": "unknown_plugin", "status": "failed"},
|
|
|
|
|
self.integration_service,
|
|
|
|
|
)
|
|
|
|
|
self.assertTrue(failed_logs["ok"])
|
|
|
|
|
self.assertEqual(len(failed_logs["data"]), 1)
|
|
|
|
|
self.assertEqual(failed_logs["data"][0]["response"]["error_code"], "INTEGRATION_TOKEN_INVALID")
|
|
|
|
|
|
|
|
|
|
def test_daily_outfit_validation_failure_records_failed_log(self):
|
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
|
self.integration_service.daily_outfit({"keywords": ["top"]}, self.ctx)
|
|
|
|
|
|
|
|
|
|
logs = self.integration_service.list_integration_logs(status="failed")
|
|
|
|
|
self.assertEqual(len(logs), 1)
|
|
|
|
|
self.assertEqual(logs[0]["action"], "daily_outfit")
|
|
|
|
|
self.assertEqual(logs[0]["response"]["error_code"], "VALIDATION_FAILED")
|
|
|
|
|
|
2026-07-05 19:35:22 +08:00
|
|
|
def test_integration_log_limit_is_normalized(self):
|
|
|
|
|
for index in range(3):
|
|
|
|
|
self.integration_repository.record(
|
|
|
|
|
{
|
|
|
|
|
"integration_log_id": f"log_{index}",
|
|
|
|
|
"integration_name": "manual",
|
|
|
|
|
"action": "sync",
|
|
|
|
|
"status": "succeeded",
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(len(self.integration_repository.list(limit=0)), 1)
|
|
|
|
|
self.assertEqual(len(self.integration_repository.list(limit=-10)), 1)
|
|
|
|
|
self.assertEqual(len(self.integration_repository.list(limit=999)), 3)
|
|
|
|
|
|
2026-07-05 10:03:11 +08:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|