|
1 | | -import { describe, expect, test } from "vite-plus/test"; |
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { Client, ExitCode, REGIONS, type ApiKeyCredential, type Settings } from "bailian-cli-core"; |
| 5 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vite-plus/test"; |
2 | 6 | import { resolvePrimeEndpoint, validatePrimeFlags } from "../src/commands/shared/prime.ts"; |
| 7 | +import videoGenerate from "../src/commands/video/generate.ts"; |
3 | 8 |
|
4 | 9 | function createUrlResolver(baseUrl: string, usesDefaultBaseUrl: boolean) { |
5 | 10 | return { |
@@ -48,3 +53,190 @@ describe("Prime command helpers", () => { |
48 | 53 | ); |
49 | 54 | }); |
50 | 55 | }); |
| 56 | + |
| 57 | +describe("Prime video upload origins", () => { |
| 58 | + const identity = { |
| 59 | + binName: "test-cli", |
| 60 | + version: "test", |
| 61 | + npmPackage: "test-cli", |
| 62 | + clientName: "test-client", |
| 63 | + }; |
| 64 | + const settings: Settings = { |
| 65 | + output: "json", |
| 66 | + outputExplicit: true, |
| 67 | + timeout: 30, |
| 68 | + watermark: true, |
| 69 | + verbose: false, |
| 70 | + quiet: true, |
| 71 | + dryRun: false, |
| 72 | + telemetry: false, |
| 73 | + }; |
| 74 | + const workspaceOrigin = "https://ws_test.cn-beijing.maas.aliyuncs.com"; |
| 75 | + const policy = { |
| 76 | + upload_host: "https://uploads.example.test", |
| 77 | + upload_dir: "temporary", |
| 78 | + oss_access_key_id: "test-access-key", |
| 79 | + signature: "test-signature", |
| 80 | + policy: "test-policy", |
| 81 | + x_oss_object_acl: "private", |
| 82 | + x_oss_forbid_overwrite: "true", |
| 83 | + }; |
| 84 | + const fetchMock = vi.fn<typeof fetch>(); |
| 85 | + let fixtureDir: string; |
| 86 | + let fixturePath: string; |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + fixtureDir = mkdtempSync(join(tmpdir(), "bailian-prime-upload-")); |
| 90 | + fixturePath = join(fixtureDir, "sample.png"); |
| 91 | + writeFileSync(fixturePath, "synthetic media fixture"); |
| 92 | + fetchMock.mockReset(); |
| 93 | + fetchMock.mockImplementation(async (input) => { |
| 94 | + const url = new URL(input instanceof Request ? input.url : input); |
| 95 | + if (url.pathname === "/api/v1/uploads") { |
| 96 | + return Response.json({ data: policy }); |
| 97 | + } |
| 98 | + if (url.origin === policy.upload_host) return new Response(null, { status: 200 }); |
| 99 | + if (url.pathname === "/api/v1/services/aigc/video-generation/video-synthesis") { |
| 100 | + return Response.json({ output: { task_id: "synthetic-task" } }); |
| 101 | + } |
| 102 | + throw new Error(`Unexpected test request: ${url.href}`); |
| 103 | + }); |
| 104 | + vi.stubGlobal("fetch", fetchMock); |
| 105 | + vi.spyOn(process.stdout, "write").mockImplementation(() => true); |
| 106 | + }); |
| 107 | + |
| 108 | + afterEach(() => { |
| 109 | + vi.restoreAllMocks(); |
| 110 | + vi.unstubAllGlobals(); |
| 111 | + rmSync(fixtureDir, { recursive: true, force: true }); |
| 112 | + }); |
| 113 | + |
| 114 | + function runVideo( |
| 115 | + flags: Record<string, unknown>, |
| 116 | + options: { |
| 117 | + baseUrl?: string; |
| 118 | + settingsWorkspaceId?: string; |
| 119 | + credentialSource?: ApiKeyCredential["source"]; |
| 120 | + } = {}, |
| 121 | + ) { |
| 122 | + const baseUrl = options.baseUrl ?? REGIONS.cn; |
| 123 | + const commandSettings = { ...settings, workspaceId: options.settingsWorkspaceId }; |
| 124 | + return videoGenerate.run({ |
| 125 | + identity, |
| 126 | + settings: commandSettings, |
| 127 | + flags: { |
| 128 | + prime: true, |
| 129 | + model: "wan3.0-video", |
| 130 | + prompt: "synthetic video", |
| 131 | + async: true, |
| 132 | + ...flags, |
| 133 | + }, |
| 134 | + client: new Client({ |
| 135 | + identity, |
| 136 | + settings: commandSettings, |
| 137 | + baseUrl, |
| 138 | + baseUrlIsDefault: options.baseUrl === undefined, |
| 139 | + apiCred: { |
| 140 | + token: "synthetic-workspace-key", |
| 141 | + baseUrl, |
| 142 | + source: options.credentialSource ?? "flag", |
| 143 | + }, |
| 144 | + }), |
| 145 | + } as never); |
| 146 | + } |
| 147 | + |
| 148 | + function expectRequestOrigins(options: { |
| 149 | + uploadOrigin: string; |
| 150 | + submitOrigin: string; |
| 151 | + uploadCount?: number; |
| 152 | + }) { |
| 153 | + const { uploadOrigin, submitOrigin, uploadCount = 1 } = options; |
| 154 | + const policyCalls = fetchMock.mock.calls.filter(([input]) => { |
| 155 | + const url = input instanceof Request ? input.url : input.toString(); |
| 156 | + return url.includes("/api/v1/uploads?"); |
| 157 | + }); |
| 158 | + expect(policyCalls).toHaveLength(uploadCount); |
| 159 | + for (const [policyUrl, policyOptions] of policyCalls) { |
| 160 | + expect(policyUrl).toBe(`${uploadOrigin}/api/v1/uploads?action=getPolicy&model=wan3.0-video`); |
| 161 | + expect(new Headers(policyOptions?.headers).get("Authorization")).toBe( |
| 162 | + "Bearer synthetic-workspace-key", |
| 163 | + ); |
| 164 | + } |
| 165 | + const [submitUrl, submitOptions] = fetchMock.mock.calls.at(-1)!; |
| 166 | + expect(submitUrl).toBe(`${submitOrigin}/api/v1/services/aigc/video-generation/video-synthesis`); |
| 167 | + expect(new Headers(submitOptions?.headers).get("Authorization")).toBe( |
| 168 | + "Bearer synthetic-workspace-key", |
| 169 | + ); |
| 170 | + expect(fetchMock).toHaveBeenCalledTimes(uploadCount * 2 + 1); |
| 171 | + } |
| 172 | + |
| 173 | + test.each(["flag", "env"] as const)( |
| 174 | + "uploads local files to the derived workspace with a %s key", |
| 175 | + async (credentialSource) => { |
| 176 | + await runVideo( |
| 177 | + { file: fixturePath, ...(credentialSource === "flag" ? { workspaceId: "ws_test" } : {}) }, |
| 178 | + { |
| 179 | + credentialSource, |
| 180 | + settingsWorkspaceId: credentialSource === "env" ? "ws_test" : undefined, |
| 181 | + }, |
| 182 | + ); |
| 183 | + expectRequestOrigins({ uploadOrigin: workspaceOrigin, submitOrigin: workspaceOrigin }); |
| 184 | + }, |
| 185 | + ); |
| 186 | + |
| 187 | + test("uploads both local keyframes to the derived workspace", async () => { |
| 188 | + await runVideo({ image: fixturePath, lastFrame: fixturePath, workspaceId: "ws_test" }); |
| 189 | + expectRequestOrigins({ |
| 190 | + uploadOrigin: workspaceOrigin, |
| 191 | + submitOrigin: workspaceOrigin, |
| 192 | + uploadCount: 2, |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + test.each([ |
| 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 | + ); |
| 226 | + |
| 227 | + test("rejects a missing default workspace before making an upload request", async () => { |
| 228 | + await expect(runVideo({ file: fixturePath })).rejects.toMatchObject({ |
| 229 | + message: "Workspace ID is required for the default Prime endpoint.", |
| 230 | + }); |
| 231 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 232 | + }); |
| 233 | + |
| 234 | + test("passes an existing media URL through without uploading", async () => { |
| 235 | + await runVideo({ file: "https://media.example.test/video.mp4", workspaceId: "ws_test" }); |
| 236 | + expectRequestOrigins({ |
| 237 | + uploadOrigin: workspaceOrigin, |
| 238 | + submitOrigin: workspaceOrigin, |
| 239 | + uploadCount: 0, |
| 240 | + }); |
| 241 | + }); |
| 242 | +}); |
0 commit comments