"""Isolated bilingual prompt, stale-write, and actual-provider-body checks. No real models/GPU jobs.""" import base64,concurrent.futures,json,os,pathlib,re,sqlite3,subprocess,threading,time,urllib.request,urllib.error from http.server import BaseHTTPRequestHandler,ThreadingHTTPServer root=pathlib.Path(__file__).resolve().parent.parent data=root/'integration-work'/('english-test-'+time.strftime('%Y%m%d-%H%M%S'));data.mkdir() stage=root/'integration-work/stage-4.0.11/app';dbfile=data/'huobao.sqlite3' source=(root/'huobao-source/desktop/build/backend.mjs').read_text('utf-8') source=source.replace('const require = __hbCreateRequire(import.meta.url)','const require = __hbCreateRequire('+json.dumps(str(stage/'resources/app.asar/build/backend.mjs'))+')',1) source=re.sub(r'^import (\w+) from "(better-sqlite3|sharp)";',r'const \1 = require("\2");',source,flags=re.M) source=source.replace('target.hostname === "192.168.110.111" && ["18792", "18794"].includes(target.port)','target.hostname === "127.0.0.1" && target.port === "18941"') source=re.sub(r'(url\d*)\.hostname === "192\.168\.110\.111" && \["18792", "18794"\]\.includes\(\1.port\)',r'\1.hostname === "127.0.0.1" && \1.port === "18941"',source) source=re.sub(r'&& !!taskRouting\(\{ provider: config\d*\.provider, taskId \}\)','&& !!taskId',source) (data/'backend.mjs').write_text(source,'utf-8') events=[];translations=[];mode='ok';entered=threading.Event();release=threading.Event();process=None batch_mode=False;active=0;peak=0;counter_lock=threading.Lock() class Mock(BaseHTTPRequestHandler): def log_message(self,*args):pass def reply(self,status,body): raw=json.dumps(body).encode();self.send_response(status);self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(raw) def do_POST(self): global active,peak body=json.loads(self.rfile.read(int(self.headers['Content-Length']))) if self.path.endswith('/chat/completions'): if body.get('tools'): if any(m.get('role')=='tool' for m in body['messages']):message={'role':'assistant','content':'Bilingual storyboard saved.'};finish='stop' else: name=next(t['function']['name'] for t in body['tools'] if 'save' in t['function']['name'].lower() and 'storyboard' in t['function']['name'].lower()) en=shot(1)['video_prompt_en'] args={'storyboards':[{'shot_number':4,'description':'【镜头1】暴雨中,乔雨灵说:「别开门!」【镜头2】镜头靠近四楼宿舍。','video_prompt':shot(1)['video_prompt'],'video_prompt_en':en,'duration':9,'character_ids':[1],'scene_id':1,'continuity_mode':'none'}]} if batch_mode: args['storyboards']=[{**args['storyboards'][0],'shot_number':40+i,'video_prompt_en':'invalid English without the literal dialogue'} for i in range(6)] args['replace_existing']=mode=='invalid' message={'role':'assistant','content':None,'tool_calls':[{'id':'save_bilingual_fixture','type':'function','function':{'name':name,'arguments':json.dumps(args)}}]};finish='tool_calls' self.reply(200,{'id':'fixture','object':'chat.completion','created':1,'model':'fixture-translator','choices':[{'index':0,'message':message,'finish_reason':finish}],'usage':{'prompt_tokens':1,'completion_tokens':1,'total_tokens':2}});return translations.append(body) if batch_mode: with counter_lock:active+=1;peak=max(peak,active) time.sleep(.2) with counter_lock:active-=1 if mode=='unavailable':self.reply(503,{'error':'fixture'});return if mode=='delay':entered.set();release.wait(6) text='Cinematic rain and camera movement. '+ ' '.join(('Dialogue: '+x if x=='__HB_KEEP_0__' else x) for x in re.findall(r'__HB_KEEP_\d+__',body['messages'][-1]['content'])) if mode=='invalid':text='Wrong translation without protected tokens' self.reply(200,{'choices':[{'message':{'content':json.dumps({'translation':text})}}]});return events.append((self.path,body)) if self.path.endswith('/validate'):self.reply(200,{'valid':True}) elif self.path.endswith('/cancel'):self.reply(200,{'task':{'id':self.path.split('/')[-2],'status':'cancelled'}}) else:self.reply(200,{'task_id':'new-'+str(len(events))}) def do_GET(self):self.reply(200,{'task':{'id':self.path.split('/')[-1],'status':'running','worker_state':'running'}}) server=ThreadingHTTPServer(('127.0.0.1',18941),Mock);threading.Thread(target=server.serve_forever,daemon=True).start() env={**os.environ,'ELECTRON_RUN_AS_NODE':'1','PORT':'18940','HUOBAO_DATA_DIR':str(data),'SQLITE_PATH':str(dbfile),'FRONTEND_DIST':str(root/'huobao-source/frontend/.output/public'),'WORKSPACE_PATH':str(stage/'resources/workspace-template')} log=(data/'backend.log').open('w',encoding='utf-8') def api(method,path,body=None,expected=200): req=urllib.request.Request('http://127.0.0.1:18940/api/v1'+path,method=method,data=None if body is None else json.dumps(body).encode(),headers={'Content-Type':'application/json'}) try: with urllib.request.urlopen(req,timeout=30) as r:status=r.status;result=json.load(r) except urllib.error.HTTPError as e:status=e.code;result=json.load(e) assert status==expected,(path,status,result) return result.get('data',result) def sql(query,args=()): with sqlite3.connect(dbfile) as db:db.row_factory=sqlite3.Row;return [dict(r) for r in db.execute(query,args)] def shot(i):return sql('select * from storyboards where id=?',(i,))[0] def start(): global process process=subprocess.Popen([str(stage/'HuobaoDrama.exe'),str(data/'backend.mjs')],cwd=data,env=env,stdout=log,stderr=log,creationflags=subprocess.CREATE_NO_WINDOW) for _ in range(150): assert process.poll() is None try:api('GET','/health');return except OSError:time.sleep(.1) raise AssertionError('Backend failed to start') cn='出场人物:@乔雨灵;场景:@四楼宿舍。\n0-3秒:暴雨中,@乔雨灵 抬头,说:「别开门!」\n3-9秒:镜头缓慢靠近 @四楼宿舍,无字幕。' png='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aN1kAAAAASUVORK5CYII=' try: start();ts='2026-11-12T00:00:00Z' sql('insert into dramas(id,title,created_at,updated_at) values(1,?,?,?)',('fixture',ts,ts)) sql('insert into episodes(id,drama_id,episode_number,title,created_at,updated_at) values(1,1,1,?,?,?)',('fixture',ts,ts)) for id,service,provider,model in [(1,'video','minimax','MiniMax-H3'),(2,'text','openai','fixture-translator')]: sql('insert into ai_service_configs(id,service_type,provider,name,base_url,api_key,model,is_active,created_at,updated_at) values(?,?,?,?,?,?,?,1,?,?)',(id,service,provider,'fixture','http://127.0.0.1:18941','test-key',json.dumps([model]),ts,ts)) sql('insert into characters(id,drama_id,name,created_at,updated_at) values(1,1,?,?,?)',('乔雨灵',ts,ts)) sql("insert into scenes(id,drama_id,location,time,prompt,created_at,updated_at) values(1,1,?,'night','rain',?,?)",('四楼宿舍',ts,ts)) for i in [1,2,3]:sql('insert into storyboards(id,episode_id,storyboard_number,video_prompt,created_at,updated_at) values(?,1,?,?,?,?)',(i,i,cn,ts,ts)) sql('update storyboards set scene_id=1') sql('insert into episode_scenes(episode_id,scene_id,created_at) values(1,1,?)',(ts,)) row=api('POST','/storyboards/1/english-prompt',{}) assert row['video_prompt_en_status']=='ready' and '「别开门!」' in row['video_prompt_en'] and '@乔雨灵' in row['video_prompt_en'] and '0-3s:' in row['video_prompt_en'] n=len(translations);api('POST','/storyboards/1/english-prompt',{});assert len(translations)==n assert shot(1)['video_prompt']==cn # Manual EN validation rejects modified dialogue and stale-source edits. api('PUT','/storyboards/1',{'video_prompt_en':row['video_prompt_en'].replace('别开门','快开门'),'source_prompt':cn},400) api('PUT','/storyboards/1',{'video_prompt_en':row['video_prompt_en']+' No new dialogue.','source_prompt':cn}) api('PUT','/storyboards/1',{'video_prompt':cn+' 暗蓝色光线。'}) assert api('GET','/episodes/1/storyboards')[0]['video_prompt_en_status']=='stale' api('PUT','/storyboards/1',{'video_prompt_en':row['video_prompt_en'],'source_prompt':cn},400) old=shot(1)['video_prompt_en'];mode='invalid' api('POST','/storyboards/1/english-prompt',{},400);assert shot(1)['video_prompt_en']==old # Translation failure must not cancel an existing shot or create a provider job. sql('insert into sys_task(id,type,storyboard_id,provider,model,status,stage,task_id,config_id,config_base_url,created_at,updated_at) values(10,?,?,?,?,?,?,?,?,?,?,?)',('video',2,'minimax','MiniMax-H3','processing','running','old-2',1,'http://127.0.0.1:18941',ts,ts)) mode='unavailable';before=len(events) api('POST','/tasks',{'type':'video','storyboard_id':2,'model':'MiniMax-H3','config_id':1,'prompt':cn,'replace_task_id':10,'reference_image_urls':[png]},400) assert len(events)==before and sql('select status from sys_task where id=10')[0]['status']=='processing' # Reuse EN, map asset reference indexes, translate continuity, and submit exactly the preflight prompt. mode='ok';sql("update storyboards set continuity_start='暴雨夜窗前无人' where id=2") result=api('POST','/tasks',{'type':'video','storyboard_id':2,'drama_id':1,'model':'MiniMax-H3','config_id':1,'prompt':cn.replace('@乔雨灵','@图片2乔雨灵').replace('@四楼宿舍','@图片1四楼宿舍'),'replace_task_id':10,'reference_image_urls':[png,png]},201) for _ in range(80): if len(events)>=before+3:break time.sleep(.1) assert [x[0] for x in events[before:]]==['/v2/video_generation/validate','/v2/video_generation/old-2/cancel','/v2/video_generation'] sent=events[-1][1]['content'][0]['text'];assert sent==events[before][1]['content'][0]['text'] assert '「别开门!」' in sent and '@图片2乔雨灵' in sent and '@图片1四楼宿舍' in sent assert '暴雨夜窗前无人' not in sent and '画面衔接约束' not in sent saved=sql('select prompt,params from sys_task where id=?',(result['id'],))[0] assert saved['prompt']==sent and json.loads(saved['params'])['promptLanguage']=='en' # An in-flight translation may never overwrite a new Chinese edit. mode='delay' with concurrent.futures.ThreadPoolExecutor() as pool: f=pool.submit(api,'POST','/storyboards/3/english-prompt',{},400);assert entered.wait(3) api('PUT','/storyboards/3',{'video_prompt':cn+' 新内容。'});release.set();f.result() assert shot(3)['video_prompt_en'] is None and '新内容' in shot(3)['video_prompt'] # Batch fill EN only preserves all original CN and media/task records. mode='ok';before=len(events);originals=[shot(i)['video_prompt'] for i in [1,2,3]] batch=api('POST','/episodes/1/generate-video-prompts',{'english_only':True});assert batch['total']==2 for _ in range(100): state=api('GET','/episodes/1/video-prompts-status') if state['status']!='running':break time.sleep(.1) assert state['failed']==0 and state['completed']==2,state assert len(events)==before and [shot(i)['video_prompt'] for i in [1,2,3]]==originals assert all(r['video_prompt_en_status']=='ready' for r in api('GET','/episodes/1/storyboards')) # New storyboard Agent tool persists CN and EN together, without changing prior shots. api('POST','/agent/storyboard_breaker/chat',{'drama_id':1,'episode_id':1,'message':'Save the next bilingual storyboard.','config_id':2}) created=sql('select * from storyboards where storyboard_number=4')[0] assert created['video_prompt_en'] and created['video_prompt_en_source_hash'] and '「别开门!」' in created['video_prompt_en'] # Broadcast labels are canonicalized; numbers inside speech remain literal. broadcast='0-3秒:老式广播音响起:「0-3秒:别开门!」\n3-9秒:雨水落下。' sql('insert into storyboards(id,episode_id,storyboard_number,video_prompt,created_at,updated_at) values(30,1,30,?,?,?)',(broadcast,ts,ts)) translated=api('POST','/storyboards/30/english-prompt',{})['video_prompt_en'] assert 'Dialogue: 「0-3秒:别开门!」' in translated and '0-3s:' in translated # English possessives around Chinese asset identifiers are not literal speech. original="0-3秒:@乔雨灵 的手搭在 @乔雨灵 的肩上;系统音:「停了。NG,两次。」" sql('insert into storyboards(id,episode_id,storyboard_number,video_prompt,created_at,updated_at) values(31,1,31,?,?,?)',(original,ts,ts)) candidate="0-3s: @乔雨灵's hand rests on @乔雨灵's shoulder; a system voice announces: 「停了。NG,两次。」" api('PUT','/storyboards/31',{'video_prompt_en':candidate,'source_prompt':original}) assert "@乔雨灵's" in shot(31)['video_prompt_en'] and 'Dialogue: 「停了。NG,两次。」' in shot(31)['video_prompt_en'] # Direct Doubao translations disable thinking and respect the selected model. Batch fallback is bounded at 3. sql('insert into ai_service_configs(id,service_type,provider,name,base_url,api_key,model,is_active,created_at,updated_at) values(3,?,?,?,?,?,?,1,?,?)',('text','volcengine','fixture','http://127.0.0.1:18941','test-key',json.dumps(['doubao-seed-fixture-default']),ts,ts)) batch_mode=True;before=len(translations) api('POST','/agent/storyboard_breaker/chat',{'drama_id':1,'episode_id':1,'message':'Save 6 bilingual shots.','config_id':3,'model':'doubao-seed-fixture-selected'}) saved=sql('select * from storyboards where storyboard_number between 40 and 45 order by storyboard_number') assert len(saved)==6 and peak==3,(len(saved),peak) assert len(translations[before:])==6 assert all(b['thinking']=={'type':'disabled'} and b['model']=='doubao-seed-fixture-selected' for b in translations[before:]) assert all('「别开门!」' in x['video_prompt_en'] and x['video_prompt_en_source_hash'] for x in saved) # A failed concurrent batch must not clear existing shots, including when replace_existing=true. snapshot=sql('select * from storyboards order by id');mode='invalid' api('POST','/agent/storyboard_breaker/chat',{'drama_id':1,'episode_id':1,'message':'Attempt replacement.','config_id':3,'model':'doubao-seed-fixture-selected'}) assert sql('select * from storyboards order by id')==snapshot mode='ok';batch_mode=False process.terminate();process.wait(10);process=None;start() assert all(r['video_prompt_en_status']=='ready' for r in api('GET','/episodes/1/storyboards')) print(json.dumps({'passed':True,'literal_dialogue_preserved':True,'reference_mapping_preserved':True,'actual_submission_english':True,'continuity_english':True,'failure_before_cancel':True,'stale_write_protection':True,'batch_preserves_originals':True,'restart_persistence':True,'fixture':str(data)})) finally: if process:process.terminate();process.wait(10) server.shutdown();log.close()