"""部分尺寸的尺寸标注图:仅传入已知方向,绝不补造缺失方向。""" from __future__ import annotations import sys import unittest from pathlib import Path SERVER_DIR = Path(__file__).resolve().parents[1] if str(SERVER_DIR) not in sys.path: sys.path.insert(0, str(SERVER_DIR)) from api.suite import _with_dimensions from schemas.suite import ProductDimensions from services.prompts.common import build_context from services.prompts.gpt import _task_size_chart class PartialDimensionsTests(unittest.TestCase): def test_only_known_direction_is_passed_to_prompt_context(self) -> None: raw = _with_dimensions( {"title": "测试商品", "params": [ {"key": "产品尺寸", "value": "12.5×8×4 cm"}, {"key": "材质", "value": "棉"}, ]}, ProductDimensions(width=8, height=4, unit="cm"), required=True, ) self.assertEqual(raw["dimensions"], {"width": 8, "height": 4, "unit": "cm"}) self.assertEqual(raw["params"], [{"key": "材质", "value": "棉"}]) context = build_context(raw) prompt = _task_size_chart(context, "en") self.assertIn("width: 8.0 cm; height: 4.0 cm", prompt) self.assertIn("render exactly 2 measurement arrows and labels, only for: width, height", prompt) self.assertIn("Do not draw, label, or show an arrow for: length", prompt) self.assertNotIn("12.5×8×4", prompt) def test_size_chart_still_requires_at_least_one_dimension(self) -> None: with self.assertRaisesRegex(Exception, "至少需要填写一项"): _with_dimensions({"title": "测试商品"}, ProductDimensions(unit="mm"), required=True) if __name__ == "__main__": unittest.main()