跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

windows_ccs_context_test.py

73 行 · 4.9 KB历史下载
1"""Host lifecycle tests for actual CCS child dispatch; native Win32 is separate."""
2import contextlib
3import ctypes
4import json
5import os
6from pathlib import Path
7import runpy
8import tempfile
9import subprocess
10import types
11import unittest
12import uuid
13from unittest.mock import MagicMock, patch
14
15with patch.object(ctypes,'WinDLL',create=True,return_value=MagicMock()):
16 platform=runpy.run_path(Path(__file__).with_name('windows_cli_platform.py'))
17actual=platform['launch_ccs_live']
18
19class Unverified(Exception):pass
20class Changed(Exception):pass
21
22class ActualContextTests(unittest.TestCase):
23 def exercise(self, settings, failure=None, resume=False):
24 with tempfile.TemporaryDirectory() as temporary:
25 root=Path(temporary);connection=str(uuid.uuid4());sid=str(uuid.uuid4());folder=root/connection;folder.mkdir()
26 source=folder/'profile.settings.json';source.write_text('{"env":{"ANTHROPIC_API_KEY":"fixture"}}')
27 context={'id':connection,'session_id':sid,'profile':'qa','resume':resume}
28 (folder/'ccs-launch.json').write_text(json.dumps(context))
29 request=folder/('ccs-actual-'+str(uuid.uuid4())+'.json')
30 args=['--settings',str(source),'--session-id',sid,'--setting-sources','user','--model','fixture-model']
31 request.write_text(json.dumps({**context,'actual_args':args,'settings_source':str(source),'expected_settings_source':str(source),'profile_type':'settings'}))
32 state={};events=[];runs=[]
33 @contextlib.contextmanager
34 def state_file(_):yield state
35 def resolve(binary,prefix,args,env,profile,snapshot):
36 if failure:raise failure
37 events.append('resolve');snapshot.write(json.dumps(settings).encode());snapshot.flush();snapshot.seek(0)
38 return binary,[*args,'--settings',snapshot.name],env,{'verified':'fixture'}
39 identity={'Unverified':Unverified,'Changed':Changed,'read_settings':lambda path:json.loads(path.read_text()),
40 'resolve_actual_context':resolve,'validate_shared_mcp':lambda args,paths,**kwargs:events.append('merge-check'),
41 'bind':lambda *args:events.append('bind'),'shared_channels_disabled':lambda:False}
42 class Process:
43 pid=42
44 def __init__(self,args,env):events.append('spawn');runs.append((args,env))
45 def wait(self):return 0
46 def close(self):events.append('closed')
47 channel={'ROOT':root,'__file__':str(Path(__file__).with_name('cli_channel.py')),'identifier':lambda value:str(uuid.UUID(value)),
48 'read_json':lambda path,default=None:json.loads(path.read_text()) if path.exists() else default,
49 'state_file':state_file,'save':lambda path,value:path.write_text(json.dumps(value))}
50 with patch.dict(os.environ,{'CAGE_CLI_BINARY':'trusted-native.exe','CAGE_CLI_IDENTITY_HELPER':'fixture-identity.py','DYNAMIC_PROXY_TOKEN':'fixture-only'}),patch.object(runpy,'run_path',return_value=identity),patch.dict(actual.__globals__,{'CliProcess':Process}):
51 if failure and resume:
52 with self.assertRaisesRegex(ValueError,'暂时无法核验'):actual(str(request),channel)
53 else:self.assertEqual(actual(str(request),channel),0)
54 self.assertFalse(request.exists());self.assertEqual(set(folder.glob('*.settings.json')),{source})
55 return events,runs,state,args
56 def test_failed_merge_leaves_no_persistent_identity_and_keeps_original_child_context(self):
57 events,runs,state,args=self.exercise({'hooks':[]})
58 self.assertNotIn('bind',events);self.assertEqual(runs[0][0][1:],args)
59 self.assertTrue(state['native_ccs_unverified']);self.assertFalse(state['blocked'])
60 self.assertEqual(runs[0][1]['DYNAMIC_PROXY_TOKEN'],'fixture-only')
61 def test_identity_binds_after_all_merge_checks_and_before_actual_spawn(self):
62 events,runs,state,args=self.exercise({'model':'fixture-model','permissions':{'deny':['Bash(fixture)']},'hooks':{}})
63 self.assertLess(events.index('merge-check'),events.index('bind'));self.assertLess(events.index('bind'),events.index('spawn'))
64 self.assertFalse(state['native_ccs_unverified']);self.assertEqual(runs[0][1]['DYNAMIC_PROXY_TOKEN'],'fixture-only')
65 def test_auth_probe_timeout_keeps_new_terminal_original_context_without_binding(self):
66 events,runs,state,args=self.exercise({},subprocess.TimeoutExpired('fixture-native-auth',12))
67 self.assertNotIn('bind',events);self.assertEqual(runs[0][0][1:],args)
68 self.assertTrue(state['native_ccs_unverified'])
69 def test_auth_probe_timeout_refuses_resume_and_cleans_snapshot_before_spawn(self):
70 events,runs,state,args=self.exercise({},subprocess.TimeoutExpired('fixture-native-auth',12),True)
71 self.assertEqual(runs,[]);self.assertNotIn('bind',events)
72
73if __name__=='__main__':unittest.main()