|
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vite-plus/test"; |
| 5 | +import { Client } from "../src/client/client.ts"; |
| 6 | +import { REGIONS, type Settings } from "../src/config/schema.ts"; |
| 7 | +import { ExitCode } from "../src/errors/codes.ts"; |
| 8 | +import { resolveFileUrl, uploadFile } from "../src/files/upload.ts"; |
| 9 | + |
| 10 | +const identity = { |
| 11 | + binName: "test-cli", |
| 12 | + version: "test", |
| 13 | + npmPackage: "test-cli", |
| 14 | + clientName: "test-client", |
| 15 | +}; |
| 16 | +const settings: Settings = { |
| 17 | + output: "json", |
| 18 | + outputExplicit: true, |
| 19 | + timeout: 30, |
| 20 | + watermark: true, |
| 21 | + verbose: false, |
| 22 | + quiet: true, |
| 23 | + dryRun: false, |
| 24 | + telemetry: false, |
| 25 | +}; |
| 26 | +const policy = { |
| 27 | + upload_host: "https://uploads.example.test", |
| 28 | + upload_dir: "temporary", |
| 29 | + oss_access_key_id: "test-access-key", |
| 30 | + signature: "test-signature", |
| 31 | + policy: "test-policy", |
| 32 | + x_oss_object_acl: "private", |
| 33 | + x_oss_forbid_overwrite: "true", |
| 34 | +}; |
| 35 | +const fetchMock = vi.fn<typeof fetch>(); |
| 36 | +let fixtureDir: string; |
| 37 | +let fixturePath: string; |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + fixtureDir = mkdtempSync(join(tmpdir(), "bailian-regional-upload-")); |
| 41 | + fixturePath = join(fixtureDir, "sample.mp4"); |
| 42 | + writeFileSync(fixturePath, "synthetic media fixture"); |
| 43 | + fetchMock.mockReset(); |
| 44 | + fetchMock.mockResolvedValueOnce(Response.json({ data: policy })); |
| 45 | + fetchMock.mockResolvedValueOnce(new Response(null, { status: 200 })); |
| 46 | + vi.stubGlobal("fetch", fetchMock); |
| 47 | +}); |
| 48 | + |
| 49 | +afterEach(() => { |
| 50 | + vi.unstubAllGlobals(); |
| 51 | + rmSync(fixtureDir, { recursive: true, force: true }); |
| 52 | +}); |
| 53 | + |
| 54 | +function makeClient(baseUrl: string): Client { |
| 55 | + return new Client({ |
| 56 | + identity, |
| 57 | + settings, |
| 58 | + baseUrl: REGIONS.cn, |
| 59 | + apiCred: { token: "synthetic-profile-key", baseUrl, source: "config" }, |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +describe("regional temporary file uploads", () => { |
| 64 | + test.each([ |
| 65 | + REGIONS.cn, |
| 66 | + REGIONS.intl, |
| 67 | + REGIONS.us, |
| 68 | + "https://ws-example.ap-southeast-1.maas.aliyuncs.com", |
| 69 | + "https://proxy.example.test", |
| 70 | + ])("pairs the upload policy host with the credential for %s", async (baseUrl) => { |
| 71 | + await expect(makeClient(baseUrl).uploadFile(fixturePath, "wan3.0-video")).resolves.toBe( |
| 72 | + "oss://temporary/sample.mp4", |
| 73 | + ); |
| 74 | + |
| 75 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 76 | + const [policyUrl, policyOptions] = fetchMock.mock.calls[0]!; |
| 77 | + expect(policyUrl).toBe(`${baseUrl}/api/v1/uploads?action=getPolicy&model=wan3.0-video`); |
| 78 | + expect(new Headers(policyOptions?.headers).get("Authorization")).toBe( |
| 79 | + "Bearer synthetic-profile-key", |
| 80 | + ); |
| 81 | + const [uploadUrl, uploadOptions] = fetchMock.mock.calls[1]!; |
| 82 | + expect(uploadUrl).toBe(policy.upload_host); |
| 83 | + expect(uploadOptions?.method).toBe("POST"); |
| 84 | + expect((uploadOptions?.body as FormData).get("key")).toBe("temporary/sample.mp4"); |
| 85 | + expect(new Headers(uploadOptions?.headers).has("Authorization")).toBe(false); |
| 86 | + }); |
| 87 | + |
| 88 | + test("keeps the CN default for existing direct uploadFile callers", async () => { |
| 89 | + await uploadFile({ |
| 90 | + apiKey: "synthetic-profile-key", |
| 91 | + model: "wan3.0-video", |
| 92 | + filePath: fixturePath, |
| 93 | + identity, |
| 94 | + }); |
| 95 | + expect(fetchMock.mock.calls[0]![0]).toBe( |
| 96 | + `${REGIONS.cn}/api/v1/uploads?action=getPolicy&model=wan3.0-video`, |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + test("normalizes a supplied upload base URL using the shared origin contract", async () => { |
| 101 | + const options = { |
| 102 | + identity, |
| 103 | + baseUrl: `${REGIONS.intl}/compatible-mode/v1/?unused=value#fragment`, |
| 104 | + }; |
| 105 | + await resolveFileUrl(fixturePath, "synthetic-profile-key", "wan3.0-video", options); |
| 106 | + expect(fetchMock.mock.calls[0]![0]).toBe( |
| 107 | + `${REGIONS.intl}/api/v1/uploads?action=getPolicy&model=wan3.0-video`, |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + test("preserves unsupported-service errors without retrying another region", async () => { |
| 112 | + fetchMock.mockReset(); |
| 113 | + const response = '{"code":"UnsupportedOperation","message":"Temporary uploads unavailable"}'; |
| 114 | + fetchMock.mockResolvedValueOnce(new Response(response, { status: 404 })); |
| 115 | + await expect( |
| 116 | + makeClient(REGIONS.intl).uploadFile(fixturePath, "wan3.0-video"), |
| 117 | + ).rejects.toMatchObject({ |
| 118 | + exitCode: ExitCode.GENERAL, |
| 119 | + message: `Failed to get upload policy (HTTP 404, ${REGIONS.intl}/api/v1/uploads): ${response}`, |
| 120 | + }); |
| 121 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 122 | + expect(fetchMock.mock.calls[0]![0]).toBe( |
| 123 | + `${REGIONS.intl}/api/v1/uploads?action=getPolicy&model=wan3.0-video`, |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + test.each([ |
| 128 | + "https://media.example.test/sample.mp4", |
| 129 | + "oss://existing/sample.mp4", |
| 130 | + "data:image/png;base64,AQID", |
| 131 | + ])("passes %s through without credentials or requests", async (source) => { |
| 132 | + const client = new Client({ identity, settings, baseUrl: REGIONS.intl }); |
| 133 | + await expect(client.uploadFile(source, "wan3.0-video")).resolves.toBe(source); |
| 134 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments