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_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()