Files
quality_analyse/verification/test_nao_model_settings.py
lyun ff7bbe3e46 同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。
补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。
统一午夜黑及其他主题的控件细节、中文和业务数字字体。
排除本地业务数据、凭据、预览、发布与验收产物。

验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
2026-10-30 22:40:58 +08:00

110 lines
6.8 KiB
Python

import json
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import nao_model_settings as settings
class ModelSettingsTest(unittest.TestCase):
def test_mimo26_legacy_effort_preserves_switch_but_reports_automatic_strength(self):
with tempfile.TemporaryDirectory() as tmp:
p=Path(tmp)/'config.json'
with patch.object(settings,'SETTINGS_FILE',p):
for enabled in (True,False):
p.write_text(json.dumps(dict(baseUrl='http://localhost/v1',apiKey='synthetic',model='mimo-v2.6-flash',thinkingEnabled=enabled,thinkingEffort='high')))
self.assertEqual(settings.model_configuration()[3],{'enabled':enabled,'effort':'auto','protocol':'thinking'})
def test_thinking_controls_are_loaded_atomically_with_profile_credentials(self):
with tempfile.TemporaryDirectory() as tmp:
p=Path(tmp)/'config.json'
p.write_text(json.dumps(dict(baseUrl='http://localhost/v1',apiKey='synthetic',model='qwen-local',thinkingEnabled=False,thinkingEffort='high')))
with patch.object(settings,'SETTINGS_FILE',p):
result=settings.model_configuration()
self.assertEqual(result[2],'qwen-local')
self.assertEqual(result[3],{'enabled':False,'effort':'high','protocol':'chat_template'})
for value in ('extreme',False):
p.write_text(json.dumps(dict(baseUrl='http://localhost/v1',apiKey='synthetic',model='mimo-model',thinkingEnabled=True,thinkingEffort=value)))
with self.assertRaises(ValueError):settings.model_configuration()
def test_ui_settings_override_legacy_and_refresh_each_read(self):
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / 'config.json'
with patch.object(settings, 'SETTINGS_FILE', p), patch.object(settings, '_legacy_settings') as legacy:
p.write_text(json.dumps(dict(baseUrl='http://localhost:8000/v1', apiKey='private-one', model='local-model')))
self.assertEqual(settings.model_settings(), ('http://localhost:8000/v1', 'private-one', 'local-model'))
p.write_text(json.dumps(dict(baseUrl='http://localhost:8000/v1', apiKey='private-two', model='other-model')))
self.assertEqual(settings.model_settings()[1:], ('private-two', 'other-model'))
legacy.assert_not_called()
def test_missing_ui_config_bootstraps_selected_legacy(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / 'apikey.txt').write_text('https://first.example\nfirst-key\nfirst-model\nhttps://second.example/v1/chat/completions\nBearer selected-key\nsecond-model\n')
selection = root / '.runtime/dbgpt-trial/selected-profile.json'
selection.parent.mkdir(parents=True)
selection.write_text('{"profile":2}')
# A clean checkout has no dbgpt_trial module. Bootstrap must still work.
with patch.object(settings, 'ROOT', root), patch.object(settings, 'SETTINGS_FILE', root / 'absent.json'), patch.dict('os.environ', {}, clear=True), patch.dict(sys.modules, {'dbgpt_trial': None}):
self.assertEqual(settings.model_settings(), ('https://second.example/v1', 'selected-key', 'second-model'))
def test_invalid_saved_config_does_not_silently_use_legacy(self):
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / 'config.json'
p.write_text(json.dumps(dict(baseUrl='http://user:secret@localhost/v1', apiKey='key', model='model')))
with patch.object(settings, 'SETTINGS_FILE', p), patch.object(settings, '_legacy_settings') as legacy:
with self.assertRaises(ValueError): settings.model_settings()
legacy.assert_not_called()
def test_legacy_utf16_and_explicit_config_path(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
p = root / 'selected.txt'
p.write_text('https://model.example/v1\nBEARER selected-key\nprovider/model:latest\n', encoding='utf-16')
with patch.object(settings, 'ROOT', root), patch.object(settings, 'SETTINGS_FILE', root / 'absent.json'), patch.dict('os.environ', {'QUALITY_MODEL_CONFIG': str(p)}):
self.assertEqual(settings.model_settings(), ('https://model.example/v1', 'selected-key', 'provider/model:latest'))
def test_legacy_rejects_invalid_profile_without_exposing_content(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / 'apikey.txt').write_text('https://model.example\nprivate-secret\nmodel\n')
selection = root / '.runtime/dbgpt-trial/selected-profile.json'
selection.parent.mkdir(parents=True)
with patch.object(settings, 'ROOT', root), patch.object(settings, 'SETTINGS_FILE', root / 'absent.json'), patch.dict('os.environ', {}, clear=True):
for profile in (0, 2, True, 1.5, '1'):
with self.subTest(profile=profile):
selection.write_text(json.dumps({'profile': profile}))
with self.assertRaises(ValueError) as error:
settings.model_settings()
self.assertNotIn('private-secret', str(error.exception))
def test_legacy_validation_and_bounded_read(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
p = root / 'apikey.txt'
invalid = [
'https://user:password@model.example\nkey\nmodel',
'https://model.example#fragment\nkey\nmodel',
'https://model.example\nkey with spaces\nmodel',
'https://model.example\nkey\ninvalid model',
'x' * 16385,
]
with patch.object(settings, 'ROOT', root), patch.object(settings, 'SETTINGS_FILE', root / 'absent.json'), patch.dict('os.environ', {}, clear=True):
for content in invalid:
with self.subTest(content_type=invalid.index(content)):
p.write_text(content)
with self.assertRaises(ValueError): settings.model_settings()
def test_dangling_saved_config_symlink_does_not_fall_back(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
p = root / 'config.json'
p.symlink_to(root / 'missing.json')
with patch.object(settings, 'SETTINGS_FILE', p), patch.object(settings, '_legacy_settings') as legacy:
with self.assertRaises(ValueError): settings.model_settings()
legacy.assert_not_called()
if __name__ == '__main__': unittest.main()