Core regression / Python 3.12 core, intervention UI and release configuration (push) Failing after 20s
Core regression / JavaScript syntax (push) Successful in 4s
Core regression / Python 3.12 core, intervention UI and release configuration (pull_request) Failing after 20s
Core regression / JavaScript syntax (pull_request) Successful in 3s
185 lines
9.3 KiB
Python
185 lines
9.3 KiB
Python
import json
|
||
import sys
|
||
from types import SimpleNamespace
|
||
|
||
import pytest
|
||
|
||
from dx_productivity.dsh import availability
|
||
|
||
|
||
@pytest.fixture(autouse=True)
|
||
def isolated_environment(monkeypatch):
|
||
for key in ('DSH_API_BASE', 'DSH_MODEL', 'DSH_API_KEY', 'DX_DSH_MODELS', 'DEEPSEEK_API_KEY', 'FIXTURE_NATIVE_KEY'):
|
||
monkeypatch.delenv(key, raising=False)
|
||
|
||
|
||
def host_env(monkeypatch):
|
||
for key, value in {'DSH_API_BASE': 'https://example.test/v1', 'DSH_MODEL': 'fixture',
|
||
'DSH_API_KEY': 'fake-credential-never-return'}.items():
|
||
monkeypatch.setenv(key, value)
|
||
|
||
|
||
def test_fresh_install_is_disabled_without_files_or_node(tmp_path, monkeypatch):
|
||
def unexpected(*args, **kwargs):
|
||
pytest.fail('Fresh configuration must not spawn a process')
|
||
monkeypatch.setattr(availability.subprocess, 'run', unexpected)
|
||
monkeypatch.setitem(sys.modules, 'win32crypt', None)
|
||
assert availability.check_availability(tmp_path) == {
|
||
'available': False, 'code': 'missing_configuration', 'message': '未配置 AI 模型或 API Key;AI 功能已停用。'}
|
||
assert list(tmp_path.iterdir()) == []
|
||
|
||
|
||
def test_environment_host_configuration_is_available_and_never_returned(tmp_path, monkeypatch):
|
||
host_env(monkeypatch)
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['available'] is True, result
|
||
assert result['code'] == 'available'
|
||
assert result['source'] == 'host'
|
||
assert result['environment_refs'] == ['DSH_API_KEY']
|
||
assert 'fake-credential' not in json.dumps(result)
|
||
assert list(tmp_path.iterdir()) == []
|
||
|
||
|
||
def test_corrupt_host_configuration_returns_safe_error(tmp_path):
|
||
config = tmp_path / 'dsh'
|
||
config.mkdir()
|
||
(config / 'provider.json').write_text('{fake-secret: invalid', encoding='utf8')
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['available'] is False
|
||
assert result['code'] == 'invalid_configuration'
|
||
assert 'fake-secret' not in json.dumps(result)
|
||
|
||
|
||
def test_native_profile_is_checked_without_host_key(tmp_path, monkeypatch):
|
||
patch = tmp_path / 'dsh/harness-home/profiles/dx-task/cordis.patch.yml'
|
||
patch.parent.mkdir(parents=True)
|
||
patch.write_text('[]', encoding='utf8')
|
||
def native_check(*args, **kwargs):
|
||
return SimpleNamespace(returncode=0, stdout='{"available":true,"code":"available","source":"native","environment_refs":["NATIVE_API_KEY"]}', stderr='')
|
||
monkeypatch.setattr(availability.subprocess, 'run', native_check)
|
||
assert availability.check_availability(tmp_path)['available'] is True
|
||
assert patch.read_text(encoding='utf8') == '[]'
|
||
|
||
|
||
def test_host_configuration_requires_node(tmp_path, monkeypatch):
|
||
host_env(monkeypatch)
|
||
def missing_node():
|
||
raise RuntimeError('private error text')
|
||
monkeypatch.setattr(availability, 'node_executable', missing_node)
|
||
assert availability.check_availability(tmp_path)['code'] == 'runtime_unavailable'
|
||
|
||
|
||
def test_native_only_credential_store_is_checked_without_profile_patch(tmp_path, monkeypatch):
|
||
credentials = tmp_path / 'dsh/harness-home/.credentials.yaml'
|
||
credentials.parent.mkdir(parents=True)
|
||
credentials.write_text('version: 1\nrefs: {}', encoding='utf8')
|
||
monkeypatch.setattr(availability.subprocess, 'run', lambda *a, **k: SimpleNamespace(returncode=0,
|
||
stdout='{"available":true,"code":"available","source":"native","environment_refs":[]}', stderr=''))
|
||
assert availability.check_availability(tmp_path)['source'] == 'native'
|
||
|
||
|
||
def test_dpapi_host_configuration_is_available_without_writes(tmp_path):
|
||
from dx_productivity.dsh.provider_config import save_provider
|
||
directory = tmp_path / 'dsh'
|
||
save_provider(directory, base_url='https://example.test/v1', models=['fixture'],
|
||
default_model='fixture', api_key='fake-protected-key')
|
||
before = {path.name: path.read_bytes() for path in directory.iterdir()}
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['source'] == 'host'
|
||
assert 'fake-protected-key' not in json.dumps(result)
|
||
assert {path.name: path.read_bytes() for path in directory.iterdir()} == before
|
||
|
||
|
||
@pytest.mark.parametrize('explicit_default', [True, False])
|
||
def test_real_native_route_survives_corrupt_unused_host_metadata(tmp_path, explicit_default):
|
||
directory = tmp_path / 'dsh'
|
||
patch = directory / 'harness-home/profiles/dx-task/cordis.patch.yml'
|
||
patch.parent.mkdir(parents=True)
|
||
(directory / 'provider.json').write_text('{invalid-unused-host', encoding='utf8')
|
||
rows = [
|
||
{'id': 'llm-pi-ai', 'config': {'providers': {'native': {'api': 'openai-completions',
|
||
'baseURL': 'https://example.invalid/v1', 'apiKeyEnv': 'FIXTURE_NATIVE_KEY',
|
||
'models': [{'id': 'fixture-model'}]}}}},
|
||
{'id': 'agent-default-model', 'config': {'provider': 'native', 'model': 'fixture-model'}}]
|
||
patch.write_text(json.dumps(rows if explicit_default else rows[:1]), encoding='utf8')
|
||
credentials = directory / 'harness-home/.credentials.yaml'
|
||
credentials.write_text(json.dumps({'version': 1, 'refs': {'FIXTURE_NATIVE_KEY': 'fake-native-key'}}), encoding='utf8')
|
||
before = {path: path.read_bytes() for path in directory.rglob('*') if path.is_file()}
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['available'] is True, result
|
||
assert result['source'] == 'native'
|
||
assert result['environment_refs'] == []
|
||
assert result['model_selection'] == {'provider': 'native', 'model': 'fixture-model'}
|
||
assert 'fake-native-key' not in json.dumps(result)
|
||
assert {path: path.read_bytes() for path in directory.rglob('*') if path.is_file()} == before
|
||
|
||
|
||
def test_ambient_official_native_key_works_before_dsh_directory_exists(tmp_path, monkeypatch):
|
||
monkeypatch.setenv('DEEPSEEK_API_KEY', 'fake-official-native-key')
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['available'] is True, result
|
||
assert result['source'] == 'native'
|
||
assert result['environment_refs'] == ['DEEPSEEK_API_KEY']
|
||
assert not (tmp_path / 'dsh').exists()
|
||
|
||
|
||
def test_native_extra_child_fields_are_discarded(tmp_path, monkeypatch):
|
||
credentials = tmp_path / 'dsh/harness-home/.credentials.yaml'
|
||
credentials.parent.mkdir(parents=True)
|
||
credentials.write_text('{}', encoding='utf8')
|
||
output = json.dumps({'available': True, 'code': 'available', 'source': 'native',
|
||
'environment_refs': ['FIXTURE_NATIVE_KEY'], 'key': 'fake-do-not-return'})
|
||
monkeypatch.setattr(availability.subprocess, 'run', lambda *a, **k:
|
||
SimpleNamespace(returncode=0, stdout=output, stderr=''))
|
||
assert 'fake-do-not-return' not in json.dumps(availability.check_availability(tmp_path))
|
||
|
||
|
||
@pytest.mark.parametrize('selection', [None, [], 7, {'provider': 'p'},
|
||
{'provider': 'p', 'model': 'm', 'reasoningEffort': None}, {'provider': 'p', 'model': '\n'}])
|
||
def test_invalid_native_model_route_is_rejected(tmp_path, monkeypatch, selection):
|
||
credentials = tmp_path / 'dsh/harness-home/.credentials.yaml'
|
||
credentials.parent.mkdir(parents=True)
|
||
credentials.write_text('{}', encoding='utf8')
|
||
output = {'available': True, 'code': 'available', 'source': 'native',
|
||
'environment_refs': [], 'model_selection': selection}
|
||
monkeypatch.setattr(availability.subprocess, 'run', lambda *a, **k:
|
||
SimpleNamespace(returncode=0, stdout=json.dumps(output), stderr=''))
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['available'] is (selection is None)
|
||
|
||
|
||
def test_native_reasoning_selection_is_preserved_without_extra_fields(tmp_path, monkeypatch):
|
||
credentials = tmp_path / 'dsh/harness-home/.credentials.yaml'
|
||
credentials.parent.mkdir(parents=True)
|
||
credentials.write_text('{}', encoding='utf8')
|
||
output = {'available': True, 'code': 'available', 'source': 'native', 'environment_refs': [],
|
||
'model_selection': {'provider': 'p', 'model': 'm', 'reasoningEffort': 'high', 'secret': 'do-not-return'}}
|
||
monkeypatch.setattr(availability.subprocess, 'run', lambda *a, **k:
|
||
SimpleNamespace(returncode=0, stdout=json.dumps(output), stderr=''))
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['model_selection'] == {'provider': 'p', 'model': 'm', 'reasoningEffort': 'high'}
|
||
assert 'do-not-return' not in json.dumps(result)
|
||
|
||
|
||
@pytest.mark.parametrize('output', ['secret not json', '{"available":true,"code":"secret"}'])
|
||
def test_native_invalid_output_is_fixed_safe_message(tmp_path, monkeypatch, output):
|
||
patch = tmp_path / 'dsh/harness-home/profiles/dx-task/cordis.patch.yml'
|
||
patch.parent.mkdir(parents=True)
|
||
patch.write_text('[]', encoding='utf8')
|
||
monkeypatch.setattr(availability.subprocess, 'run', lambda *a, **k:
|
||
SimpleNamespace(returncode=0, stdout=output, stderr='another-secret'))
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['code'] == 'runtime_unavailable'
|
||
assert 'secret' not in json.dumps(result)
|
||
|
||
|
||
def test_native_timeout_is_safe_and_bounded(tmp_path, monkeypatch):
|
||
patch = tmp_path / 'dsh/harness-home/profiles/dx-task/cordis.patch.yml'
|
||
patch.parent.mkdir(parents=True)
|
||
patch.write_text('[]', encoding='utf8')
|
||
def timeout(*args, **kwargs):
|
||
assert kwargs['timeout'] <= 10
|
||
raise availability.subprocess.TimeoutExpired('fake-secret', 8)
|
||
monkeypatch.setattr(availability.subprocess, 'run', timeout)
|
||
assert availability.check_availability(tmp_path)['code'] == 'runtime_unavailable'
|