Files
xc_agent_platform_demo/tests/test_config_params.py

48 lines
2.5 KiB
Python
Raw Normal View History

import json
import re
import unittest
import os
from unittest.mock import patch
from pathlib import Path
from src.config_params import build_config_params, config_params_source, GAP
from src.build_config_client import BuildConfigError
from scripts.dry_run import _render
class TestConfigParams(unittest.TestCase):
def setUp(self):
env = patch.dict(os.environ, {}, clear=True)
env.start()
self.addCleanup(env.stop)
def test_source_selection_and_failure(self):
with patch("urllib.request.urlopen") as network:
self.assertEqual(config_params_source(), "sample")
build_config_params()
network.assert_not_called()
self.assertIn("discovered via capture", GAP)
with patch.dict(os.environ, {"CONFIG_PARAMS_SOURCE": "build-config"}):
with self.assertRaisesRegex(BuildConfigError, "missing/invalid credential"):
build_config_params("gpu")
with patch.dict(os.environ, {"ALLOW_BUILD_CONFIG": "1", "XC_TOKEN": "test-secret"}), patch("src.config_params.build_config", return_value="api: yaml") as client:
self.assertEqual(config_params_source(), "build-config")
self.assertEqual(build_config_params("gpu"), "api: yaml")
client.assert_called_once_with("gpu", "vllm", "text-generation")
with patch.dict(os.environ, {"CONFIG_PARAMS_SOURCE": "build-config"}), patch("src.config_params.build_config", side_effect=BuildConfigError("transport/timeout")):
with self.assertRaisesRegex(BuildConfigError, "transport/timeout"):
build_config_params("gpu")
def test_official_sample_exact(self):
scrape = Path('/workspace/modelhub-adapt/logs/api-docs-scrape-2026-10-16.md')
if scrape.is_file():
body = json.loads(re.search(r'```json\n(.*?)\n```', scrape.read_text(), re.S)[1])
self.assertEqual(build_config_params(), body['configParams'])
self.assertIn('max_model_len: 4096', build_config_params())
self.assertIn('framework: vllm', build_config_params())
self.assertIn('NOT production-ready', GAP)
def test_report_draft_and_gap(self):
report = _render([{'model_address': 'org/a', 'try_next_gpus': ['壁砺']}], 'test', 'fixture')
for value in ('代提候选 / 需 Victor 确认', 'configParams source', 'configParams_source=sample', 'GAP:', 'truncated', 'max_model_len: 4096', 'perf_order=unset', 'order_source=experience'):
self.assertIn(value, report)