Files
dj/integration-work/test_storyboard_jobs.py
T
2026-11-13 11:46:25 +08:00

153 lines
12 KiB
Python

"""Isolated async storyboard lifecycle, cancellation and atomic replacement regression tests."""
import contextlib,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
OUT=ROOT/'integration-work'/('storyboard-jobs-test-'+time.strftime('%Y%m%d-%H%M%S'));OUT.mkdir()
(ROOT/'integration-work/current-storyboard-jobs-test.txt').write_text(str(OUT),'utf-8')
STAGE=ROOT/'integration-work/stage-4.0.11/app';DB=OUT/'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)
(OUT/'backend.mjs').write_text(source,'utf-8')
release=threading.Event();held=threading.Event();mode='hold';calls=[];process=None
def shot(i,bad=False):
return {'shot_number':i,'source_ids':['S01P01'],'title':'New '+str(i),'duration':9,'description':'【镜头1】角色站在房间。','character_ids':[1],'scene_id':999 if bad else 1,'video_prompt':'0-9秒:@测试角色 站在 @房间,保持静止。','video_prompt_en':'0-9s: @测试角色 stands in @房间, motionless.'}
class Mock(BaseHTTPRequestHandler):
def log_message(self,*a):pass
def reply(self,value):
raw=json.dumps(value).encode()
try:self.send_response(200);self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(raw)
except (BrokenPipeError,ConnectionResetError,ConnectionAbortedError):pass
def do_POST(self):
body=json.loads(self.rfile.read(int(self.headers['Content-Length'])));which=body['model'];calls.append(which)
assert body.get('tools'),'Unexpected translation call'
assert any('每批最多 4 个分镜' in str(m.get('content','')) for m in body['messages'] if m['role']=='system'),'Tracked batch limit must be in system instructions'
tools=[t['function']['name'] for t in body['tools']]
read=next(t for t in tools if 'read' in t.lower());save=next(t for t in tools if 'save' in t.lower())
n=sum(m['role']=='tool' for m in body['messages'])
context={}
for m in body['messages']:
if m['role']=='tool':
try:
value=json.loads(m.get('content','{}'))
if 'current_section' in value:context=value
except (ValueError,TypeError):pass
args={};name=read
if which=='empty':message={'role':'assistant','content':'Nothing saved.'};finish='stop'
else:
if n==1:
name=save;args={'replace_existing':True,'storyboards':[shot(i) for i in range(1,5)]}
if which=='reject':args['storyboards'][0]['video_prompt']='0-6秒:@测试角色 站在 @房间。'
elif n==2 and which in ('hold','cancel','restart','changed','timeout','bad'):
held.set();release.wait(90);name=save;args={'storyboards':[shot(5,which=='bad'),shot(6)]}
elif n>=2 and which=='reject':
name=save;args={'replace_existing':True,'storyboards':[shot(1)]};args['storyboards'][0]['video_prompt']='0-6秒:@测试角色 站在 @房间。'
elif n==2:name=save;args={'storyboards':[shot(5),shot(6)]}
if which=='inplace' and n==1:
assert not any('update' in t.lower() or 'workspace' in t.lower() for t in tools),'Tracked jobs expose only context and staging tools'
name='updateStoryboard';args={'storyboard_id':rows()[0]['id'],'title':'must not overwrite'}
if which=='toolerror' and n>=1:
name=save;args={'replace_existing':True,'storyboards':[shot(1,True)]}
if which=='voice' and n==1:
args['storyboards'][0]['video_prompt']='0-9秒:#测试角色音色说:「安静。」'
args['storyboards'][0]['video_prompt_en']='0-9s: #测试角色音色 says: 「安静。」'
if which=='ending' and n==1:
args['storyboards'][1]['description']='角色说:「我们在这扇门后结束本集故事,不要再继续编写新的剧情了!」'
if which=='multisection' and n in (1,2):
base=context['next_shot_number']
numbers=range(base,base+4) if n==1 else range(base+4,base+6)
args={'storyboards':[{**shot(i),'source_ids':[context['current_section']['id']+'P01']} for i in numbers]}
args['replace_existing']=True # repeated per-scene flags must never erase staged drafts
if which=='gap' and n>=1:
name=save;args={'storyboards':[shot(6)]}
if which in ('skip','rushed') and n>=1:
name=save;args={'storyboards':[shot(1)]}
if which=='skip':args['storyboards'][0]['source_ids']=['S02P01']
else:args['storyboards'][0]['video_prompt']='0-4秒:@测试角色说:「'+'先检查窗户是否关好再整理图纸清点工具确认材料数量然后才可以开始修理屋顶。'*3+'」\n4-9秒:大家点头。'
if (n>=3 and which not in ('reject','toolerror','skip','rushed','gap')) or (which=='inplace' and n>=2):message={'role':'assistant','content':'All saved.'};finish='stop'
else:message={'role':'assistant','content':None,'tool_calls':[{'id':f'call_{n}','type':'function','function':{'name':name,'arguments':json.dumps(args)}}]};finish='tool_calls'
self.reply({'id':'fixture','object':'chat.completion','created':1,'model':which,'choices':[{'index':0,'message':message,'finish_reason':finish}],'usage':{'prompt_tokens':1,'completion_tokens':1,'total_tokens':2}})
server=ThreadingHTTPServer(('127.0.0.1',18985),Mock);threading.Thread(target=server.serve_forever,daemon=True).start()
env={**os.environ,'ELECTRON_RUN_AS_NODE':'1','PORT':'18984','HUOBAO_DATA_DIR':str(OUT),'SQLITE_PATH':str(DB),'FRONTEND_DIST':str(ROOT/'huobao-source/frontend/.output/public'),'WORKSPACE_PATH':str(STAGE/'resources/workspace-template'),'HUOBAO_STORYBOARD_TIMEOUT_MS':'90000' if os.getenv('STORYBOARD_UI_TEST')=='1' else '20000'}
def sql(q,args=()):
with contextlib.closing(sqlite3.connect(DB)) as d,d:d.row_factory=sqlite3.Row;return [dict(r) for r in d.execute(q,args)]
def api(method,path,body=None):
req=urllib.request.Request('http://127.0.0.1:18984/api/v1'+path,method=method,data=None if body is None else json.dumps(body).encode(),headers={'Content-Type':'application/json'})
with urllib.request.urlopen(req,timeout=8) as r:v=json.load(r)
return v.get('data',v)
def wait(fn,seconds=12):
end=time.monotonic()+seconds
while time.monotonic()<end:
value=fn()
if value:return value
time.sleep(.1)
raise AssertionError('Timed out')
def state():return api('GET','/agent/storyboard_breaker/jobs/1')
def send(which):return api('POST','/agent/storyboard_breaker/jobs',{'episode_id':1,'drama_id':1,'config_id':1,'model':which,'message':'Create the entire storyboard.'})
def rows():return sql('select * from storyboards where episode_id=1 order by id')
def start():
global process
log=(OUT/'backend.log').open('a',encoding='utf-8')
process=subprocess.Popen([str(STAGE/'HuobaoDrama.exe'),str(OUT/'backend.mjs')],cwd=OUT,env=env,stdout=log,stderr=log,creationflags=subprocess.CREATE_NO_WINDOW)
for _ in range(100):
assert process.poll() is None
try:api('GET','/health');return
except OSError:time.sleep(.15)
raise AssertionError('Backend failed')
def stop():
global process
process.terminate();process.wait(10);process=None
try:
start();ts='2026-11-12T00:00:00Z'
sql("insert into app_settings(key,value,updated_at) values('tours_seen',?,?) on conflict(key) do update set value=excluded.value",(json.dumps(['episode']),ts))
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,content,script_content,created_at,updated_at) values(1,1,1,?,?,?,?,?)',('fixture','戏'*450,'戏'*450,ts,ts))
sql("insert into ai_service_configs(id,service_type,provider,name,base_url,api_key,model,is_active,created_at,updated_at) values(1,'text','openai','fixture','http://127.0.0.1:18985','fixture','[\"hold\"]',1,?,?)",(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','room',?,?)",('房间',ts,ts))
sql('insert into storyboards(id,episode_id,storyboard_number,title,video_url,duration,created_at,updated_at) values(1,1,1,?,?,12,?,?)',('old preserved','static/videos/old.mp4',ts,ts))
original=rows();started=time.monotonic();job=send('hold');assert time.monotonic()-started<2
wait(lambda:state()['completed']==4);assert rows()==original and 'drafts' not in state()
try:
api('DELETE','/storyboards/1');raise AssertionError('Deletion during split must be rejected')
except urllib.error.HTTPError as error:assert error.code==400
assert rows()==original
assert send('hold')['id']==job['id'],'Duplicate start must reuse current job'
if os.getenv('STORYBOARD_UI_TEST')=='1':
ui=subprocess.run(['node',str(ROOT/'integration-work/browser-test/verify_storyboard_jobs_ui.cjs')],cwd=ROOT/'integration-work/browser-test',env={**os.environ,'STORYBOARD_FIXTURE':str(OUT)},capture_output=True,text=True,encoding='utf-8',timeout=40)
print(ui.stdout);assert ui.returncode==0,ui.stderr
release.set();wait(lambda:state()['status']=='done');assert len(rows())==6 and all(r['video_prompt_en'] for r in rows())
baseline=rows()
if os.getenv('STORYBOARD_UI_TEST')=='1':
stop();env['HUOBAO_STORYBOARD_TIMEOUT_MS']='20000';start()
for which in ('cancel','changed','bad','timeout','restart'):
release.clear();held.clear();job=send(which);wait(lambda:state()['completed']==4);assert rows()==baseline
if which=='cancel':
api('POST','/agent/storyboard_breaker/jobs/1/cancel',{'id':job['id']});release.set();time.sleep(.4);assert state()['status']=='cancelled' and rows()==baseline
elif which=='changed':
sql("update storyboards set title='user edit' where id=?",(baseline[0]['id'],));release.set();wait(lambda:state()['status']=='error');assert rows()[0]['title']=='user edit';baseline=rows()
elif which=='bad':release.set();wait(lambda:state()['status']=='error');assert rows()==baseline
elif which=='timeout':wait(lambda:state()['status']=='error',25);release.set();time.sleep(.4);assert rows()==baseline
else:
stop();release.set();start();assert state()['status']=='error' and rows()==baseline
print('PASS',which,flush=True)
for which in ('empty','reject','inplace','toolerror'):
send(which);wait(lambda:state()['status']=='error');assert rows()==baseline;print('PASS',which,flush=True)
if which=='toolerror':assert state()['steps']<=4 and state().get('lastValidation'),state()
send('voice');wait(lambda:state()['status']=='done');assert '#测试角色' not in rows()[0]['video_prompt'];print('PASS invented voice normalization',flush=True)
baseline=rows()
terminal='角色:(低声)我们在这扇门后结束本集故事,不要再继续编写新的剧情了!'
sql('update episodes set content=?,script_content=? where id=1',('戏'*450+'\n'+terminal,'戏'*450+'\n'+terminal))
send('ending');wait(lambda:state()['status']=='error');assert rows()==baseline;print('PASS ending cannot bypass missing source coverage or timing',flush=True)
script='## S01 | Room | Day\n\n'+'甲'*450+'\n\n## S02 | Garden | Day\n\n'+'乙'*450
sql('update episodes set content=?,script_content=? where id=1',(script,script))
for which in ('skip','rushed','gap'):
send(which);wait(lambda:state()['status']=='error');assert rows()==baseline
assert {'skip':'不能跳场','rushed':'对白至少需要','gap':'分镜编号缺少'}[which] in state()['error'];print('PASS',which,flush=True)
send('multisection');wait(lambda:state()['status']=='done');assert len(rows())==12 and state()['sectionIndex']==state()['sectionCount']==2;print('PASS full multi-section completion',flush=True)
print(json.dumps({'passed':True,'checks':['immediate response','draft progress','duplicate admission','atomic bilingual replacement','cancel late-write guard','concurrent edit preservation','invalid binding rollback','deadline','restart recovery','empty result failure','validation retry cap']}),flush=True)
finally:
release.set()
if process:stop()
server.shutdown()