58 lines
2.3 KiB
TypeScript
58 lines
2.3 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { sha256Hex, signVolcengineRequest } from "@/lib/volcengine/signature";
|
|
import { queryVisualTask, submitVisualTask, type VisualClientConfig } from "@/lib/volcengine/visual-client";
|
|
|
|
describe("Volcengine Visual signing", () => {
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("creates canonical request and signed headers", () => {
|
|
const signed = signVolcengineRequest({
|
|
method: "POST",
|
|
endpoint: "https://visual.volcengineapi.com",
|
|
query: {
|
|
Version: "2022-08-31",
|
|
Action: "CVSync2AsyncSubmitTask"
|
|
},
|
|
body: JSON.stringify({ req_key: "legacy-model", prompt: "test" }),
|
|
accessKeyId: "ak",
|
|
secretAccessKey: "sk",
|
|
region: "cn-north-1",
|
|
service: "cv",
|
|
date: new Date("2026-05-28T00:00:00Z")
|
|
});
|
|
expect(signed.url).toBe("https://visual.volcengineapi.com/?Action=CVSync2AsyncSubmitTask&Version=2022-08-31");
|
|
expect(signed.headers.Authorization).toContain("HMAC-SHA256 Credential=ak/20260528/cn-north-1/cv/request");
|
|
expect(signed.headers["X-Content-Sha256"]).toBe(sha256Hex(JSON.stringify({ req_key: "legacy-model", prompt: "test" })));
|
|
expect(signed.canonicalRequest).toContain("content-type;host;x-content-sha256;x-date");
|
|
});
|
|
|
|
it("uses the dedicated Seedream 4.6 submit and query protocol", async () => {
|
|
const urls: string[] = [];
|
|
vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => {
|
|
const url = String(input);
|
|
urls.push(url);
|
|
return new Response(JSON.stringify({ code: 10000, data: { task_id: "task-1", status: "queued" } }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
}));
|
|
const config: VisualClientConfig = {
|
|
accessKeyId: "ak",
|
|
secretAccessKey: "sk",
|
|
region: "cn-north-1",
|
|
service: "cv",
|
|
endpoint: "https://visual.volcengineapi.com"
|
|
};
|
|
|
|
await submitVisualTask({ req_key: "jimeng_seedream46_cvtob", prompt: "draw", scale: 50 }, config);
|
|
await queryVisualTask({ req_key: "jimeng_seedream46_cvtob", task_id: "task-1" }, config);
|
|
|
|
expect(urls).toEqual([
|
|
"https://visual.volcengineapi.com/?Action=JimengSeedream46CVToBSubmitTask&Version=2024-06-06",
|
|
"https://visual.volcengineapi.com/?Action=JimengSeedream46CVToBGetResult&Version=2024-06-06"
|
|
]);
|
|
});
|
|
});
|