Core regression / Python core, intervention UI and release configuration (pull_request) Canceled after 0s
Core regression / JavaScript syntax (pull_request) Canceled after 0s
Core regression / Python core, intervention UI and release configuration (push) Canceled after 1m15s
Core regression / JavaScript syntax (push) Canceled after 0s
181 lines
8.6 KiB
Python
181 lines
8.6 KiB
Python
import json
|
||
import sys
|
||
from types import SimpleNamespace
|
||
|
||
import pytest
|
||
|
||
from dx_productivity.dsh import availability
|
||
|
||
|
||
@pytest.fixture(scope='module')
|
||
def runtime_root(tmp_path_factory):
|
||
"""Keep fake runtime metadata outside each read-only configuration root."""
|
||
root = tmp_path_factory.mktemp('availability-unit-runtime')
|
||
package = root / 'node_modules/@deepseek-ai/dsh/package.json'
|
||
package.parent.mkdir(parents=True)
|
||
package.write_text('{}', encoding='utf8')
|
||
return root
|
||
|
||
|
||
@pytest.fixture(autouse=True)
|
||
def isolated_runtime(monkeypatch, runtime_root):
|
||
monkeypatch.setattr(availability, 'integration_root', lambda: runtime_root)
|
||
monkeypatch.setattr(availability, 'node_executable', lambda: 'fixture-node')
|
||
|
||
|
||
@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_host_configuration_requires_harness(tmp_path, monkeypatch):
|
||
host_env(monkeypatch)
|
||
monkeypatch.setattr(availability, 'integration_root', lambda: tmp_path / 'missing-runtime')
|
||
assert availability.check_availability(tmp_path)['code'] == 'runtime_unavailable'
|
||
assert list(tmp_path.iterdir()) == []
|
||
|
||
|
||
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'
|
||
|
||
|
||
@pytest.mark.skipif(sys.platform != 'win32', reason='Windows DPAPI credential protection')
|
||
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
|
||
|
||
|
||
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')
|
||
calls = []
|
||
def timeout(*args, **kwargs):
|
||
calls.append(args)
|
||
assert kwargs['timeout'] == 3
|
||
raise availability.subprocess.TimeoutExpired('fake-secret', 3)
|
||
monkeypatch.setattr(availability.subprocess, 'run', timeout)
|
||
result = availability.check_availability(tmp_path)
|
||
assert result['code'] == 'configuration_timeout'
|
||
assert len(calls) == 2
|
||
assert 'fake-secret' not in json.dumps(result)
|