Spaces:
Sleeping
Sleeping
Download src/tests/runtime/test_execution_progress.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 8.57 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/tests/runtime/test_execution_progress.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/tests/runtime/test_execution_progress.py
-
curl -L -o test_execution_progress.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/tests/runtime/test_execution_progress.py
8.57 kB
| import hashlib | |
| import json | |
| import threading | |
| import time | |
| import unittest | |
| from pathlib import Path | |
| from types import SimpleNamespace | |
| from unittest.mock import patch | |
| from src.apps.workbench.development import progress | |
| from src.apps.workbench.project_files import materialize, execution_record, project_path | |
| from src.modules.skill.domain.models import SkillError, digest | |
| from src.tests.runtime.fixtures import workspace, setup, USER, OTHER | |
| class ExecutionProgressTests(unittest.TestCase): | |
| def test_short_feedback_uses_recorded_stage_and_stops_at_terminal_state(self): | |
| from src.modules.orchestrator.feedback import MESSAGES | |
| self.assertTrue(all(0<len(text)<=20 for text in MESSAGES.values())) | |
| run={'created_at':0,'project_id':'test-project'} | |
| events=[{'stage':'code_review','status':'started','timestamp':10,'detail':''}] | |
| self.assertEqual(progress(run,'working',events,99)['short_message'],'') | |
| self.assertEqual(progress(run,'working',events,100)['short_message'],'正在审查代码') | |
| for state in ('verified','failed','cancelled','unknown'): | |
| self.assertEqual(progress(run,state,events,200)['short_message'],'') | |
| def test_checkpoint_audits_actual_state_once_without_model_calls(self): | |
| clock=[1000.0] | |
| with workspace() as folder: | |
| runtime,request,model,_=setup(folder,clock=lambda:clock[0]) | |
| run=runtime.create(USER,request,'checkpoint-test') | |
| runtime.store.mutate(run['id'],lambda r:r.update(status='running',claim='c',lease_until=1400)) | |
| runtime.activity_interval=.01 | |
| with runtime.activity(run['id'],'c','build'): | |
| clock[0]=1101 | |
| deadline=time.monotonic()+1 | |
| while not any(e['status']=='audit' for e in runtime.store.stages(run['id'])) and time.monotonic()<deadline:time.sleep(.01) | |
| time.sleep(.04) | |
| records=runtime.store.stages(run['id']) | |
| audits=[e for e in records if e['status']=='audit'] | |
| self.assertEqual(len(audits),1) | |
| data=json.loads(audits[0]['detail']) | |
| self.assertTrue(data['lease_valid']) | |
| self.assertEqual(data['model_pending'],0) | |
| self.assertFalse(data['process']['observed']) | |
| self.assertEqual(len([e for e in records if e['status']=='checkpoint']),1) | |
| self.assertEqual(runtime.store.calls(run['id']),[]) | |
| def test_elapsed_and_terminal_result_are_derived_from_actual_records(self): | |
| run={'created_at':100} | |
| events=[{'stage':'build','status':'started','timestamp':110,'detail':''}, | |
| {'stage':'build','status':'active','timestamp':125,'detail':'lease heartbeat'}] | |
| live=progress(run,'working',events,130) | |
| self.assertEqual(live['elapsed_seconds'],30) | |
| self.assertEqual(live['stage_elapsed_seconds'],20) | |
| self.assertEqual(live['last_heartbeat_at'],125) | |
| self.assertIn('正在',live['headline']) | |
| self.assertNotIn('通过',live['headline']) | |
| events.append({'stage':'acceptance','status':'waiting','timestamp':140,'detail':''}) | |
| a=progress(run,'verified',events,150);b=progress(run,'verified',events,999) | |
| self.assertEqual(a['elapsed_seconds'],b['elapsed_seconds']) | |
| self.assertEqual(a['elapsed_seconds'],40) | |
| self.assertEqual(progress(run,'unknown',events,999)['headline'],'本次结果尚未确认') | |
| def test_live_lease_heartbeat_stops_after_work_and_cancel(self): | |
| with workspace() as folder: | |
| runtime,request,_,_=setup(folder);run=runtime.create(USER,request,'heartbeat-test') | |
| runtime.store.mutate(run['id'],lambda r:r.update(status='running',claim='c',lease_until=time.time()+20)) | |
| runtime.activity_interval=.02 | |
| with runtime.activity(run['id'],'c','build'): | |
| deadline=time.monotonic()+1 | |
| while not runtime.store.stages(run['id']) and time.monotonic()<deadline:time.sleep(.01) | |
| self.assertEqual(runtime.store.stages(run['id'])[-1]['status'],'active') | |
| count=len(runtime.store.stages(run['id']));time.sleep(.05) | |
| self.assertEqual(len(runtime.store.stages(run['id'])),count) | |
| runtime.store.mutate(run['id'],lambda r:r.update(cancel_requested=True)) | |
| with runtime.activity(run['id'],'c','build'):time.sleep(.05) | |
| self.assertEqual(len(runtime.store.stages(run['id'])),count) | |
| def engine(self, folder): | |
| run_id='run-'+'a'*32 | |
| source='class App {}\n' | |
| delivery={'verified':True,'scenario':'erp','source_digest':digest({'src/main/java/demo/App.java':source}), | |
| 'files':[{'path':'src/main/java/demo/App.java','content':source,'change':'modified','additions':2,'deletions':1,'origin':'model-generated'}]} | |
| engine=SimpleNamespace(root=Path(folder),lock=threading.RLock()) | |
| def job(actor,key): | |
| if actor!=USER:raise SkillError('RUN_NOT_FOUND','not found',404) | |
| return {'id':run_id,'source':{'source_digest':delivery['source_digest']},'evidence':{'digest':'test'},'model_calls':[],'timeline':[]} | |
| engine.job=job | |
| return engine,delivery,run_id | |
| def test_materialized_project_is_exact_idempotent_and_never_overwrites_edits(self): | |
| with workspace() as folder: | |
| engine,delivery,key=self.engine(folder) | |
| with patch('src.apps.workbench.deliverables.get_delivery',return_value=delivery): | |
| first=materialize(engine,USER,key);second=materialize(engine,USER,key) | |
| self.assertEqual(first,second) | |
| root=Path(first['directory']);self.assertTrue(root.is_relative_to(Path(folder)/'projects')) | |
| for name,expected in first['sha256'].items():self.assertEqual(hashlib.sha256((root/name).read_bytes()).hexdigest(),expected) | |
| self.assertIn('/preview-launch/',first['preview_url']) | |
| java=root/'src/main/java/demo/App.java';java.write_text('user changed',encoding='utf-8') | |
| with self.assertRaises(SkillError) as error:materialize(engine,USER,key) | |
| self.assertEqual(error.exception.code,'PROJECT_SNAPSHOT_MODIFIED') | |
| self.assertEqual(java.read_text(),'user changed') | |
| def test_unverified_and_unsafe_project_paths_cannot_write(self): | |
| with workspace() as folder: | |
| engine,delivery,key=self.engine(folder) | |
| for name in ('../escape','C:/escape','src/file:stream','src\\escape'): | |
| bad={**delivery,'files':[{**delivery['files'][0],'path':name}]} | |
| with patch('src.apps.workbench.deliverables.get_delivery',return_value=bad): | |
| with self.assertRaises(SkillError):materialize(engine,USER,key) | |
| with patch('src.apps.workbench.deliverables.get_delivery',return_value={**delivery,'verified':False}): | |
| with self.assertRaises(SkillError):materialize(engine,USER,key) | |
| self.assertFalse(project_path(engine,USER,key).exists()) | |
| def test_presentation_upgrade_creates_a_revision_and_preserves_previous_user_edits(self): | |
| with workspace() as folder: | |
| engine,delivery,key=self.engine(folder) | |
| with patch('src.apps.workbench.deliverables.get_delivery',return_value=delivery): | |
| with patch('src.apps.workbench.project_files.digest',return_value='a'*64): | |
| first=materialize(engine,USER,key) | |
| old=Path(first['directory'])/'src/main/java/demo/App.java' | |
| old.write_text('user edit',encoding='utf-8') | |
| with patch('src.apps.workbench.project_files.digest',return_value='b'*64): | |
| second=materialize(engine,USER,key) | |
| self.assertNotEqual(first['directory'],second['directory']) | |
| self.assertEqual(old.read_text(encoding='utf-8'),'user edit') | |
| self.assertEqual((Path(second['directory'])/'src/main/java/demo/App.java').read_text(),delivery['files'][0]['content']) | |
| def test_execution_record_is_read_only_hash_bound_and_access_checked(self): | |
| with workspace() as folder: | |
| engine,_,key=self.engine(folder) | |
| record=execution_record(engine,USER,key) | |
| self.assertEqual(digest(record['record']),record['digest']) | |
| self.assertFalse(record['human_acceptance']) | |
| self.assertFalse((Path(folder)/'projects').exists()) | |
| with self.assertRaises(SkillError):execution_record(engine,OTHER,key) | |
| if __name__=='__main__':unittest.main() | |