11import { mkdtempSync , rmSync , writeFileSync } from "node:fs" ;
22import { tmpdir } from "node:os" ;
33import { join } from "node:path" ;
4- import { Client , REGIONS , type ApiKeyCredential , type Settings } from "bailian-cli-core" ;
4+ import { Client , ExitCode , REGIONS , type ApiKeyCredential , type Settings } from "bailian-cli-core" ;
55import { afterEach , beforeEach , describe , expect , test , vi } from "vite-plus/test" ;
66import { resolvePrimeEndpoint , validatePrimeFlags } from "../src/commands/shared/prime.ts" ;
77import videoGenerate from "../src/commands/video/generate.ts" ;
@@ -145,20 +145,25 @@ describe("Prime video upload origins", () => {
145145 } as never ) ;
146146 }
147147
148- function expectUploadOrigin ( origin : string , uploadCount = 1 ) {
148+ function expectRequestOrigins ( options : {
149+ uploadOrigin : string ;
150+ submitOrigin : string ;
151+ uploadCount ?: number ;
152+ } ) {
153+ const { uploadOrigin, submitOrigin, uploadCount = 1 } = options ;
149154 const policyCalls = fetchMock . mock . calls . filter ( ( [ input ] ) => {
150155 const url = input instanceof Request ? input . url : input . toString ( ) ;
151156 return url . includes ( "/api/v1/uploads?" ) ;
152157 } ) ;
153158 expect ( policyCalls ) . toHaveLength ( uploadCount ) ;
154159 for ( const [ policyUrl , policyOptions ] of policyCalls ) {
155- expect ( policyUrl ) . toBe ( `${ origin } /api/v1/uploads?action=getPolicy&model=wan3.0-video` ) ;
160+ expect ( policyUrl ) . toBe ( `${ uploadOrigin } /api/v1/uploads?action=getPolicy&model=wan3.0-video` ) ;
156161 expect ( new Headers ( policyOptions ?. headers ) . get ( "Authorization" ) ) . toBe (
157162 "Bearer synthetic-workspace-key" ,
158163 ) ;
159164 }
160165 const [ submitUrl , submitOptions ] = fetchMock . mock . calls . at ( - 1 ) ! ;
161- expect ( submitUrl ) . toBe ( `${ origin } /api/v1/services/aigc/video-generation/video-synthesis` ) ;
166+ expect ( submitUrl ) . toBe ( `${ submitOrigin } /api/v1/services/aigc/video-generation/video-synthesis` ) ;
162167 expect ( new Headers ( submitOptions ?. headers ) . get ( "Authorization" ) ) . toBe (
163168 "Bearer synthetic-workspace-key" ,
164169 ) ;
@@ -175,24 +180,49 @@ describe("Prime video upload origins", () => {
175180 settingsWorkspaceId : credentialSource === "env" ? "ws_test" : undefined ,
176181 } ,
177182 ) ;
178- expectUploadOrigin ( workspaceOrigin ) ;
183+ expectRequestOrigins ( { uploadOrigin : workspaceOrigin , submitOrigin : workspaceOrigin } ) ;
179184 } ,
180185 ) ;
181186
182187 test ( "uploads both local keyframes to the derived workspace" , async ( ) => {
183188 await runVideo ( { image : fixturePath , lastFrame : fixturePath , workspaceId : "ws_test" } ) ;
184- expectUploadOrigin ( workspaceOrigin , 2 ) ;
189+ expectRequestOrigins ( {
190+ uploadOrigin : workspaceOrigin ,
191+ submitOrigin : workspaceOrigin ,
192+ uploadCount : 2 ,
193+ } ) ;
185194 } ) ;
186195
187196 test . each ( [
188- REGIONS . intl ,
189- REGIONS . us ,
190- "https://configured-workspace.ap-southeast-1.maas.aliyuncs.com" ,
191- "https://proxy.example.test" ,
192- ] ) ( "keeps the configured origin %s without requiring a workspace" , async ( baseUrl ) => {
193- await runVideo ( { file : fixturePath } , { baseUrl } ) ;
194- expectUploadOrigin ( baseUrl ) ;
195- } ) ;
197+ [ REGIONS . cn , REGIONS . cn ] ,
198+ [ REGIONS . intl , REGIONS . intl ] ,
199+ [
200+ "https://configured-workspace.cn-beijing.maas.aliyuncs.com" ,
201+ "https://configured-workspace.cn-beijing.maas.aliyuncs.com" ,
202+ ] ,
203+ [
204+ "https://configured-workspace.ap-southeast-1.maas.aliyuncs.com" ,
205+ "https://configured-workspace.ap-southeast-1.maas.aliyuncs.com" ,
206+ ] ,
207+ [ "https://proxy.example.test" , "https://proxy.example.test" ] ,
208+ ] ) (
209+ "routes configured origin %s through upload service %s without requiring a workspace" ,
210+ async ( baseUrl , uploadOrigin ) => {
211+ await runVideo ( { file : fixturePath } , { baseUrl } ) ;
212+ expectRequestOrigins ( { uploadOrigin, submitOrigin : baseUrl } ) ;
213+ } ,
214+ ) ;
215+
216+ test . each ( [ REGIONS . us , "https://configured-workspace.us-east-1.maas.aliyuncs.com" ] ) (
217+ "rejects unsupported local upload origin %s before submitting" ,
218+ async ( baseUrl ) => {
219+ await expect ( runVideo ( { file : fixturePath } , { baseUrl } ) ) . rejects . toMatchObject ( {
220+ exitCode : ExitCode . USAGE ,
221+ hint : expect . stringContaining ( "HTTPS media URL" ) ,
222+ } ) ;
223+ expect ( fetchMock ) . not . toHaveBeenCalled ( ) ;
224+ } ,
225+ ) ;
196226
197227 test ( "rejects a missing default workspace before making an upload request" , async ( ) => {
198228 await expect ( runVideo ( { file : fixturePath } ) ) . rejects . toMatchObject ( {
@@ -203,6 +233,10 @@ describe("Prime video upload origins", () => {
203233
204234 test ( "passes an existing media URL through without uploading" , async ( ) => {
205235 await runVideo ( { file : "https://media.example.test/video.mp4" , workspaceId : "ws_test" } ) ;
206- expectUploadOrigin ( workspaceOrigin , 0 ) ;
236+ expectRequestOrigins ( {
237+ uploadOrigin : workspaceOrigin ,
238+ submitOrigin : workspaceOrigin ,
239+ uploadCount : 0 ,
240+ } ) ;
207241 } ) ;
208242} ) ;
0 commit comments