Files
dxdesigner-part-replacement/tests/dsh/test_native_runtime.py
T
阿白叔 f8f07d30d2
Core regression / Python core, intervention UI and release configuration (push) Failing after 2m42s
Core regression / JavaScript syntax (push) Successful in 6s
Core regression / Python core, intervention UI and release configuration (pull_request) Canceled after 1m17s
Core regression / JavaScript syntax (pull_request) Canceled after 0s
merge(main): resolve PR19 review conflicts and run intervention regressions in CI
2026-11-09 11:35:29 +08:00

73 lines
3.6 KiB
Python

import json
import sys
import pytest
from dx_productivity.dsh.native_runtime import launch_environment, write_context
from dx_productivity.dsh.snapshot import TaskSnapshot
def test_context_publication_has_revision_and_no_unrelated_progress_fields(tmp_path):
value = write_context(tmp_path, TaskSnapshot('t', 'paused', {'completed': 2, 'api_key': 'secret'}))
assert value['revision']
assert 'secret' not in json.dumps(value)
assert json.loads((tmp_path / 'task-context.json').read_text(encoding='utf8')) == value
def test_launch_does_not_inherit_unrelated_credentials_or_user_harness_home(tmp_path, monkeypatch):
monkeypatch.setenv('OPENAI_API_KEY', 'unrelated-secret')
monkeypatch.setenv('DSH_HOME', 'unrelated-home')
env = launch_environment(tmp_path)
assert 'OPENAI_API_KEY' not in env
assert 'DSH_HOME' not in env
assert env['DX_DSH_HOME'].startswith(str(tmp_path))
def test_selected_native_credential_is_forwarded_without_unrelated_secrets(tmp_path, monkeypatch):
monkeypatch.setenv('FIXTURE_SELECTED_API_KEY', 'fake-selected-key')
monkeypatch.setenv('FIXTURE_UNRELATED_API_KEY', 'fake-unrelated-key')
monkeypatch.setenv('OPENAI_API_KEY', 'fake-openai-key')
env = launch_environment(tmp_path, native_model=True, credential_refs=['FIXTURE_SELECTED_API_KEY'])
assert env['FIXTURE_SELECTED_API_KEY'] == 'fake-selected-key'
assert 'FIXTURE_UNRELATED_API_KEY' not in env
assert 'OPENAI_API_KEY' not in env
def test_proven_native_route_can_launch_with_corrupt_unused_host_metadata(tmp_path, monkeypatch):
for name in ('DSH_API_BASE', 'DSH_MODEL', 'DSH_API_KEY'):
monkeypatch.delenv(name, raising=False)
(tmp_path / 'provider.json').write_text('{invalid-metadata', encoding='utf8')
monkeypatch.setenv('FIXTURE_SELECTED_API_KEY', 'fake-native-key')
env = launch_environment(tmp_path, native_model=True, credential_refs=['FIXTURE_SELECTED_API_KEY'])
assert env['FIXTURE_SELECTED_API_KEY'] == 'fake-native-key'
assert env['DX_DSH_HOME'] == str(tmp_path / 'harness-home')
with pytest.raises(Exception):
launch_environment(tmp_path)
def test_invalid_native_credential_reference_does_not_expand_environment_allowlist(tmp_path, monkeypatch):
monkeypatch.setenv('FIXTURE_UNRELATED_API_KEY', 'fake-unrelated-key')
env = launch_environment(tmp_path, native_model=True, credential_refs=[None, 'invalid-ref', 'A' * 129])
assert 'FIXTURE_UNRELATED_API_KEY' not in env
@pytest.mark.skipif(sys.platform != 'win32', reason='Windows file sharing lock')
def test_context_publication_survives_a_real_windows_reader_lock(tmp_path):
import ctypes
from ctypes import wintypes
from threading import Timer
write_context(tmp_path, TaskSnapshot('t', 'draft'))
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
kernel.CreateFileW.argtypes = (wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, ctypes.c_void_p,
wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE)
kernel.CreateFileW.restype = wintypes.HANDLE
kernel.CloseHandle.argtypes = (wintypes.HANDLE,)
handle = kernel.CreateFileW(str(tmp_path / 'task-context.json'), 0x80000000, 3, None, 3, 0, None)
assert handle != ctypes.c_void_p(-1).value
release = Timer(.03, lambda: kernel.CloseHandle(handle))
release.start()
try:
value = write_context(tmp_path, TaskSnapshot('t', 'failed', issue_code='controlled'))
assert json.loads((tmp_path / 'task-context.json').read_text())['revision'] == value['revision']
assert not list(tmp_path.glob('context-*.tmp'))
finally:
release.join()