from __future__ import annotations import tempfile import unittest from pathlib import Path from backend.dto.common import ApiError, ErrorCode from backend.routes.wardrobe_routes import ( delete_wardrobe_item, import_wardrobe_material, list_wardrobe_items, match_wardrobe, ) 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.material_repository import MaterialRepository from database.repositories.profile_repository import ProfileRepository from database.repositories.wardrobe_repository import WardrobeRepository class WardrobeServiceTests(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.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.ctx = RequestContext.from_astrbot( username="admin", user_id="user_001", permissions={"can_manage_materials"}, ) self.profile = self.profile_service.create_profile({"name": "Nova"}, self.ctx) self.category = self.material_service.create_category({"name": "Upper"}, self.ctx) self.material = self.material_service.create_material( { "category_id": self.category.category_id, "display_name": "White blouse", "image_uri": "storage://materials/white_blouse.png", "prompt": "white blouse, clean collar", "tags": ["white", "upper"], "recognition_status": "confirmed", }, self.ctx, ) def tearDown(self): self.tmp.cleanup() def test_import_list_match_and_delete_wardrobe_item(self): item = self.wardrobe_service.import_material( self.profile.profile_id, { "material_id": self.material.material_id, "wardrobe_category": "top", "tags": ["summer", "white"], "is_favorite": True, }, self.ctx, ) self.assertTrue(item.wardrobe_item_id.startswith("wardrobe_")) items = self.wardrobe_service.list_items(self.profile.profile_id, "top", self.ctx) self.assertEqual([entry.wardrobe_item_id for entry in items], [item.wardrobe_item_id]) matched = self.wardrobe_service.match( self.profile.profile_id, {"categories": ["top"]}, self.ctx, ) self.assertFalse(matched["fallback_used"]) self.assertEqual(matched["selected_materials"][0]["material_id"], self.material.material_id) self.assertIn("white blouse", matched["outfit_prompt"]) self.assertEqual(matched["match_details"]["matched_count"], 1) self.assertEqual(matched["match_details"]["missing_categories"], []) self.assertGreater(matched["selected_materials"][0]["score"], 0) self.assertIn("freshness", matched["selected_materials"][0]["score_breakdown"]) self.assertEqual(self.wardrobe_repository.get(item.wardrobe_item_id).usage_count, 1) deleted = self.wardrobe_service.delete_item(self.profile.profile_id, item.wardrobe_item_id, self.ctx) self.assertEqual(deleted.wardrobe_item_id, item.wardrobe_item_id) self.assertEqual(self.wardrobe_service.list_items(self.profile.profile_id, None, self.ctx), []) def test_match_returns_fallback_strategy_when_no_category_matches(self): matched = self.wardrobe_service.match( self.profile.profile_id, { "categories": ["shoes"], "keywords": ["boots"], "mood": "formal", "weather": "rainy", "season": "winter", "style": "elegant", }, self.ctx, ) self.assertTrue(matched["fallback_used"]) self.assertFalse(matched["fallback_supplement_used"]) self.assertEqual(matched["selected_materials"], []) self.assertEqual(matched["fallback_reason"], "no_matching_materials") self.assertEqual(matched["fallback_strategy"]["name"], "random_prompt_generation") self.assertEqual(matched["fallback_strategy"]["missing_categories"], ["shoes"]) self.assertIn("matching shoes", matched["outfit_prompt"]) self.assertIn("formal", matched["outfit_prompt"]) def test_match_returns_supplement_strategy_for_missing_categories(self): self.wardrobe_service.import_material( self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "top"}, self.ctx, ) matched = self.wardrobe_service.match( self.profile.profile_id, {"categories": ["top", "shoes"], "keywords": ["casual"]}, self.ctx, ) self.assertFalse(matched["fallback_used"]) self.assertTrue(matched["fallback_supplement_used"]) self.assertEqual(matched["fallback_reason"], "missing_categories") self.assertEqual(matched["fallback_strategy"]["missing_categories"], ["shoes"]) self.assertIn("white blouse", matched["outfit_prompt"]) self.assertIn("matching shoes", matched["fallback_prompt"]) def test_match_prefers_candidate_by_tags_season_and_style(self): self.wardrobe_service.import_material( self.profile.profile_id, { "material_id": self.material.material_id, "wardrobe_category": "top", "tags": ["white", "formal"], "season": "summer", "style": "formal", "is_favorite": True, }, self.ctx, ) street_material = self.material_service.create_material( { "category_id": self.category.category_id, "display_name": "Blue hoodie", "image_uri": "storage://materials/blue_hoodie.png", "prompt": "blue hoodie, streetwear layer", "tags": ["blue", "street", "hoodie"], "recognition_status": "confirmed", }, self.ctx, ) self.wardrobe_service.import_material( self.profile.profile_id, { "material_id": street_material.material_id, "wardrobe_category": "top", "tags": ["blue", "street", "hoodie"], "season": "winter", "style": "street", }, self.ctx, ) matched = self.wardrobe_service.match( self.profile.profile_id, { "categories": ["top"], "keywords": ["streetwear"], "tags": ["blue", "hoodie"], "season": "winter", "style": "street", }, self.ctx, ) selected = matched["selected_materials"][0] self.assertEqual(selected["material_id"], street_material.material_id) self.assertEqual(selected["score_breakdown"]["season"], 12) self.assertEqual(selected["score_breakdown"]["style"], 12) self.assertGreaterEqual(selected["score_breakdown"]["tag_overlap"], 16) self.assertEqual(matched["match_details"]["requested_tags"], ["blue", "hoodie", "street", "streetwear", "winter"]) def test_unconfirmed_material_cannot_be_imported(self): pending = self.material_service.create_material( { "category_id": self.category.category_id, "display_name": "Pending", "image_uri": "storage://materials/pending.png", }, self.ctx, ) with self.assertRaises(Exception): self.wardrobe_service.import_material( self.profile.profile_id, {"material_id": pending.material_id, "wardrobe_category": "top"}, self.ctx, ) def test_duplicate_import_returns_stable_validation_error(self): first = self.wardrobe_service.import_material( self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "top"}, self.ctx, ) with self.assertRaises(ApiError) as raised: self.wardrobe_service.import_material( self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "top"}, self.ctx, ) self.assertEqual(raised.exception.error_code, ErrorCode.VALIDATION_FAILED) self.assertEqual(raised.exception.details["wardrobe_item_id"], first.wardrobe_item_id) self.assertEqual(len(self.wardrobe_service.list_items(self.profile.profile_id, "top", self.ctx)), 1) duplicate = import_wardrobe_material( self.ctx, self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "top"}, self.wardrobe_service, ) self.assertFalse(duplicate["ok"]) self.assertEqual(duplicate["error_code"], ErrorCode.VALIDATION_FAILED) def test_archived_material_is_not_used_for_matching(self): self.wardrobe_service.import_material( self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "top"}, self.ctx, ) self.material_service.delete_material(self.material.material_id, self.ctx) matched = self.wardrobe_service.match( self.profile.profile_id, {"categories": ["top"]}, self.ctx, ) self.assertEqual(matched["selected_materials"], []) self.assertTrue(matched["fallback_used"]) self.assertEqual(matched["match_details"]["missing_categories"], ["top"]) def test_cross_user_cannot_access_profile_wardrobe(self): item = self.wardrobe_service.import_material( self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "top"}, self.ctx, ) other_ctx = RequestContext.from_astrbot( username="other", user_id="user_002", permissions={"can_manage_materials"}, ) operations = { "list": lambda: self.wardrobe_service.list_items(self.profile.profile_id, None, other_ctx), "import": lambda: self.wardrobe_service.import_material( self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "bottom"}, other_ctx, ), "match": lambda: self.wardrobe_service.match(self.profile.profile_id, {"categories": ["top"]}, other_ctx), "delete": lambda: self.wardrobe_service.delete_item( self.profile.profile_id, item.wardrobe_item_id, other_ctx, ), } for action, operation in operations.items(): with self.subTest(action=action), self.assertRaises(ApiError) as raised: operation() self.assertEqual(raised.exception.error_code, ErrorCode.RESOURCE_NOT_FOUND) def test_wardrobe_routes_wrap_responses_and_require_permissions(self): imported = import_wardrobe_material( self.ctx, self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "top"}, self.wardrobe_service, ) self.assertTrue(imported["ok"]) wardrobe_item_id = imported["data"]["wardrobe_item_id"] listed = list_wardrobe_items(self.ctx, self.profile.profile_id, {}, self.wardrobe_service) self.assertTrue(listed["ok"]) self.assertEqual(len(listed["data"]), 1) matched = match_wardrobe(self.ctx, self.profile.profile_id, {"categories": ["top"]}, self.wardrobe_service) self.assertTrue(matched["ok"]) self.assertEqual(len(matched["data"]["selected_materials"]), 1) read_only = RequestContext.from_astrbot(username="viewer", user_id="viewer_001") denied = import_wardrobe_material( read_only, self.profile.profile_id, {"material_id": self.material.material_id, "wardrobe_category": "top"}, self.wardrobe_service, ) self.assertFalse(denied["ok"]) self.assertEqual(denied["error_code"], "FORBIDDEN") deleted = delete_wardrobe_item(self.ctx, self.profile.profile_id, wardrobe_item_id, self.wardrobe_service) self.assertTrue(deleted["ok"]) if __name__ == "__main__": unittest.main()