2026-11-10 23:56:50 +08:00
import { describe , expect , it , vi } from 'vitest'
import { createSSRApp , defineComponent , h } from 'vue'
import { renderToString } from 'vue/server-renderer'
import { createPinia } from 'pinia'
import RunCard from './RunCard.vue'
import type { RunDTO , RunSnapshot , StepDTO } from '../v2types'
vi . mock ( './AssetPreview.vue' , ( ) = > ( { default : { render : ( ) = > null } } ) )
vi . mock ( './JobDetail.vue' , ( ) = > ( { default : { render : ( ) = > null } } ) )
const run :RunDTO = { id : 'run' , draft_id : 'draft' , draft_revision :1 , state : 'running' , desired_state : 'run' , execution_mode : 'interactive' , version :1 , result_assets : [ ] , created_at : 'now' , completed_steps :0 , total_steps :4 }
function step ( key :string ) : StepDTO { return { id :key , run_id :run.id , step_key :key , kind :key === 'compose' ? 'video.compose' : 'video.generate' , state : 'waiting' , selected_job_id :null , explicit_attempt :0 , dependencies : [ ] , semantic_input_hash : 'hash' } }
async function render ( snapshot :RunSnapshot ) { const app = createSSRApp ( RunCard , { snapshot } ) ; app . use ( createPinia ( ) ) ; app . component ( 'ElButton' , defineComponent ( { setup ( _ , { slots } ) { return ( ) = > h ( 'button' , slots . default ? . ( ) ) } } ) ) ; app . component ( 'ElTag' , { render : ( ) = > null } ) ; return renderToString ( app ) }
describe ( 'RunCard presentation' , ( ) = > {
it ( 'shows shots one to three then compose even if API steps are lexicographically ordered, without mutating the snapshot' , async ( ) = > {
const snapshot :RunSnapshot = { run , steps : [ 'compose' , 'shot_1' , 'shot_2' , 'shot_3' ] . map ( step ) , jobs : [ ] }
const html = await render ( snapshot ) , labels = [ '镜头一' , '镜头二' , '镜头三' , '成片渲染' ]
expect ( labels . map ( label = > html . indexOf ( label ) ) ) . toEqual ( [ . . . labels . map ( label = > html . indexOf ( label ) ) ] . sort ( ( a , b ) = > a - b ) )
expect ( snapshot . steps . map ( s = > s . step_key ) ) . toEqual ( [ 'compose' , 'shot_1' , 'shot_2' , 'shot_3' ] )
} )
it ( 'gives a code-only failed step a visible Chinese error notice' , async ( ) = > {
const html = await render ( { run : { . . . run , error : { code : 'step_failed' , step_key : 'shot_2' } } , steps : [ ] , jobs : [ ] } )
expect ( html ) . toContain ( 'class="notice danger">镜头二执行失败,请查看子任务详情。' )
} )
2026-11-11 00:36:30 +08:00
it ( 'sorts arbitrary shot numbers numerically before the final render' , async ( ) = > {
const snapshot :RunSnapshot = { run , steps : [ 'compose' , 'shot_10' , 'shot_2' , 'shot_1' , 'shot_9' ] . map ( step ) , jobs : [ ] }
const html = await render ( snapshot ) , labels = [ '镜头一' , '镜头二' , '镜头9' , '镜头10' , '成片渲染' ]
const positions = labels . map ( label = > html . indexOf ( ` <strong> ${ label } </strong> ` ) )
expect ( positions . every ( index = > index >= 0 ) ) . toBe ( true )
expect ( positions ) . toEqual ( [ . . . positions ] . sort ( ( a , b ) = > a - b ) )
expect ( snapshot . steps [ 0 ] ? . step_key ) . toBe ( 'compose' )
} )
2026-11-10 23:56:50 +08:00
it . each ( [ [ 'storage_insufficient' , '存储空间不足' ] , [ 'owner_disabled' , '账户已被管理员停用' ] , [ 'dependency_changed' , '依赖素材或阶段已变更' ] ] ) ( 'explains backend code %s with no message' , async ( code , text ) = > {
expect ( await render ( { run : { . . . run , error : { code , message : ' ' } } , steps : [ ] , jobs : [ ] } ) ) . toContain ( text )
} )
it ( 'uses a meaningful generic notice for unknown error codes and preserves explicit messages' , async ( ) = > {
expect ( await render ( { run : { . . . run , error : { code : 'new_error' } } , steps : [ ] , jobs : [ ] } ) ) . toContain ( '工作流暂时无法继续,请查看任务详情或同步后重试。' )
expect ( await render ( { run : { . . . run , error : { code : 'step_failed' , message : '素材缺失' } } , steps : [ ] , jobs : [ ] } ) ) . toContain ( 'class="notice danger">素材缺失' )
} )
} )