Skip to content

Commit eb14ee4

Browse files
committed
fix(brand): return defaults for runtime config
1 parent 0cbbe37 commit eb14ee4

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

src/backend/bisheng/brand/domain/services/brand_service.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async def get_config(self) -> BrandConfig:
105105
saved = BrandConfig.model_validate(json.loads(raw_value))
106106
config = self._merge_with_defaults(saved)
107107
except (json.JSONDecodeError, ValueError) as exc:
108-
logger.warning("Invalid brand_config in DB, using defaults: %s", exc)
108+
logger.warning("Invalid brand_config in DB, using defaults: {}", exc)
109109
await self._hydrate_asset_urls(config)
110110
return config
111111

@@ -211,18 +211,7 @@ async def delete_asset(self, category: BrandAssetCategory, relative_path: str) -
211211
)
212212

213213
async def get_runtime_config(self) -> dict[str, Any]:
214-
raw_value = await self.repository.get_value(BRAND_CONFIG_KEY)
215-
if not raw_value:
216-
return {}
217-
218-
try:
219-
saved = BrandConfig.model_validate(json.loads(raw_value))
220-
config = self._merge_with_defaults(saved)
221-
except (json.JSONDecodeError, ValueError) as exc:
222-
logger.warning("Invalid brand_config in DB, using static defaults for runtime: %s", exc)
223-
return {}
224-
225-
await self._hydrate_asset_urls(config)
214+
config = await self.get_config()
226215
payload_data = config.model_dump(mode="json")
227216
payload_data.pop("linsightAgentName", None)
228217
return payload_data
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json
2+
from types import SimpleNamespace
3+
from unittest.mock import AsyncMock
4+
5+
from bisheng.brand.domain.services.brand_service import BRAND_CONFIG_KEY, BrandService
6+
7+
8+
def build_service(raw_value: str | None) -> tuple[BrandService, AsyncMock]:
9+
get_value = AsyncMock(return_value=raw_value)
10+
repository = SimpleNamespace(get_value=get_value)
11+
return BrandService(repository=repository), get_value
12+
13+
14+
async def test_runtime_config_returns_builtin_brand_when_not_configured() -> None:
15+
service, get_value = build_service(None)
16+
17+
config = await service.get_runtime_config()
18+
19+
assert config["brandName"] == {"zh": "BISHENG", "en": "BISHENG"}
20+
assert config["assets"]["favicon"]["url"] == "/assets/bisheng/favicon.ico"
21+
assert "linsightAgentName" not in config
22+
get_value.assert_awaited_once_with(BRAND_CONFIG_KEY)
23+
24+
25+
async def test_runtime_config_falls_back_to_builtin_brand_for_invalid_saved_config() -> None:
26+
service, _ = build_service("{invalid json")
27+
28+
config = await service.get_runtime_config()
29+
30+
assert config["brandName"] == {"zh": "BISHENG", "en": "BISHENG"}
31+
assert config["assets"]["headerLogoLight"]["url"] == "/assets/bisheng/login-logo-small.png"
32+
33+
34+
async def test_runtime_config_preserves_saved_brand_and_fills_default_assets() -> None:
35+
saved_config = json.dumps({"brandName": {"zh": "定制品牌", "en": "Custom Brand"}})
36+
service, _ = build_service(saved_config)
37+
38+
config = await service.get_runtime_config()
39+
40+
assert config["brandName"] == {"zh": "定制品牌", "en": "Custom Brand"}
41+
assert config["assets"]["favicon"]["url"] == "/assets/bisheng/favicon.ico"

0 commit comments

Comments
 (0)