mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 07:40:44 +00:00
test: share comfy provider fixtures
This commit is contained in:
@@ -1,29 +1,21 @@
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import * as providerAuth from "openclaw/plugin-sdk/provider-auth-runtime";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
_setComfyFetchGuardForTesting,
|
||||
buildComfyImageGenerationProvider,
|
||||
} from "./image-generation-provider.js";
|
||||
import {
|
||||
buildComfyConfig,
|
||||
mockComfyCloudJobResponses,
|
||||
mockComfyProviderApiKey,
|
||||
parseComfyJsonBody,
|
||||
} from "./test-helpers.js";
|
||||
|
||||
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
|
||||
fetchWithSsrFGuardMock: vi.fn(),
|
||||
}));
|
||||
|
||||
function parseJsonBody(call: number): Record<string, unknown> {
|
||||
const request = fetchWithSsrFGuardMock.mock.calls[call - 1]?.[0];
|
||||
expect(request?.init?.body).toBeTruthy();
|
||||
return JSON.parse(String(request.init.body)) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function buildComfyConfig(config: Record<string, unknown>): OpenClawConfig {
|
||||
return {
|
||||
models: {
|
||||
providers: {
|
||||
comfy: config,
|
||||
},
|
||||
},
|
||||
} as unknown as OpenClawConfig;
|
||||
return parseComfyJsonBody(fetchWithSsrFGuardMock, call);
|
||||
}
|
||||
|
||||
describe("comfy image-generation provider", () => {
|
||||
@@ -234,59 +226,16 @@ describe("comfy image-generation provider", () => {
|
||||
});
|
||||
|
||||
it("uses cloud endpoints, auth headers, and partner-node extra_data", async () => {
|
||||
vi.spyOn(providerAuth, "resolveApiKeyForProvider").mockResolvedValue({
|
||||
apiKey: "comfy-test-key",
|
||||
source: "env",
|
||||
mode: "api-key",
|
||||
});
|
||||
mockComfyProviderApiKey();
|
||||
_setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
|
||||
fetchWithSsrFGuardMock
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(JSON.stringify({ prompt_id: "cloud-job-1" }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
release: vi.fn(async () => {}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(JSON.stringify({ status: "completed" }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
release: vi.fn(async () => {}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(
|
||||
JSON.stringify({
|
||||
"cloud-job-1": {
|
||||
outputs: {
|
||||
"9": {
|
||||
images: [{ filename: "cloud.png", subfolder: "", type: "output" }],
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
},
|
||||
),
|
||||
release: vi.fn(async () => {}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(null, {
|
||||
status: 302,
|
||||
headers: { location: "https://cdn.example.com/cloud.png" },
|
||||
}),
|
||||
release: vi.fn(async () => {}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(Buffer.from("cloud-data"), {
|
||||
status: 200,
|
||||
headers: { "content-type": "image/png" },
|
||||
}),
|
||||
release: vi.fn(async () => {}),
|
||||
});
|
||||
mockComfyCloudJobResponses(fetchWithSsrFGuardMock, {
|
||||
body: Buffer.from("cloud-data"),
|
||||
contentType: "image/png",
|
||||
filename: "cloud.png",
|
||||
outputKind: "images",
|
||||
promptId: "cloud-job-1",
|
||||
redirectLocation: "https://cdn.example.com/cloud.png",
|
||||
});
|
||||
|
||||
const provider = buildComfyImageGenerationProvider();
|
||||
const result = await provider.generateImage({
|
||||
|
||||
99
extensions/comfy/test-helpers.ts
Normal file
99
extensions/comfy/test-helpers.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import * as providerAuth from "openclaw/plugin-sdk/provider-auth-runtime";
|
||||
import { expect, vi } from "vitest";
|
||||
|
||||
type FetchGuardMock = ReturnType<typeof vi.fn>;
|
||||
|
||||
type FetchGuardRequest = {
|
||||
init?: {
|
||||
body?: unknown;
|
||||
};
|
||||
};
|
||||
|
||||
type ComfyCloudJobResponseOptions = {
|
||||
body: BodyInit;
|
||||
contentType: string;
|
||||
filename: string;
|
||||
outputKind: "gifs" | "images";
|
||||
promptId: string;
|
||||
redirectLocation: string;
|
||||
};
|
||||
|
||||
export function buildComfyConfig(config: Record<string, unknown>): OpenClawConfig {
|
||||
return {
|
||||
models: {
|
||||
providers: {
|
||||
comfy: config,
|
||||
},
|
||||
},
|
||||
} as unknown as OpenClawConfig;
|
||||
}
|
||||
|
||||
export function parseComfyJsonBody(
|
||||
fetchWithSsrFGuardMock: FetchGuardMock,
|
||||
call: number,
|
||||
): Record<string, unknown> {
|
||||
const request = fetchWithSsrFGuardMock.mock.calls[call - 1]?.[0] as FetchGuardRequest | undefined;
|
||||
expect(request?.init?.body).toBeTruthy();
|
||||
return JSON.parse(String(request.init.body)) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function mockComfyProviderApiKey(apiKey = "comfy-test-key") {
|
||||
return vi.spyOn(providerAuth, "resolveApiKeyForProvider").mockResolvedValue({
|
||||
apiKey,
|
||||
source: "env",
|
||||
mode: "api-key",
|
||||
});
|
||||
}
|
||||
|
||||
export function mockComfyCloudJobResponses(
|
||||
fetchWithSsrFGuardMock: FetchGuardMock,
|
||||
options: ComfyCloudJobResponseOptions,
|
||||
) {
|
||||
fetchWithSsrFGuardMock
|
||||
.mockResolvedValueOnce(fetchGuardJson({ prompt_id: options.promptId }))
|
||||
.mockResolvedValueOnce(fetchGuardJson({ status: "completed" }))
|
||||
.mockResolvedValueOnce(
|
||||
fetchGuardJson({
|
||||
[options.promptId]: {
|
||||
outputs: {
|
||||
"9": {
|
||||
[options.outputKind]: [{ filename: options.filename, subfolder: "", type: "output" }],
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
fetchGuardResponse(
|
||||
new Response(null, {
|
||||
status: 302,
|
||||
headers: { location: options.redirectLocation },
|
||||
}),
|
||||
),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
fetchGuardResponse(
|
||||
new Response(options.body, {
|
||||
status: 200,
|
||||
headers: { "content-type": options.contentType },
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function fetchGuardJson(body: unknown) {
|
||||
return fetchGuardResponse(
|
||||
new Response(JSON.stringify(body), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function fetchGuardResponse(response: Response) {
|
||||
return {
|
||||
response,
|
||||
release: vi.fn(async () => {}),
|
||||
};
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import * as providerAuth from "openclaw/plugin-sdk/provider-auth-runtime";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { expectExplicitVideoGenerationCapabilities } from "../../test/helpers/media-generation/provider-capability-assertions.js";
|
||||
import {
|
||||
buildComfyConfig,
|
||||
mockComfyCloudJobResponses,
|
||||
mockComfyProviderApiKey,
|
||||
parseComfyJsonBody,
|
||||
} from "./test-helpers.js";
|
||||
import {
|
||||
_setComfyFetchGuardForTesting,
|
||||
buildComfyVideoGenerationProvider,
|
||||
@@ -12,19 +16,7 @@ const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
|
||||
}));
|
||||
|
||||
function parseJsonBody(call: number): Record<string, unknown> {
|
||||
const request = fetchWithSsrFGuardMock.mock.calls[call - 1]?.[0];
|
||||
expect(request?.init?.body).toBeTruthy();
|
||||
return JSON.parse(String(request.init.body)) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function buildComfyConfig(config: Record<string, unknown>): OpenClawConfig {
|
||||
return {
|
||||
models: {
|
||||
providers: {
|
||||
comfy: config,
|
||||
},
|
||||
},
|
||||
} as unknown as OpenClawConfig;
|
||||
return parseComfyJsonBody(fetchWithSsrFGuardMock, call);
|
||||
}
|
||||
|
||||
describe("comfy video-generation provider", () => {
|
||||
@@ -158,59 +150,16 @@ describe("comfy video-generation provider", () => {
|
||||
});
|
||||
|
||||
it("uses cloud endpoints for video workflows", async () => {
|
||||
vi.spyOn(providerAuth, "resolveApiKeyForProvider").mockResolvedValue({
|
||||
apiKey: "comfy-test-key",
|
||||
source: "env",
|
||||
mode: "api-key",
|
||||
});
|
||||
mockComfyProviderApiKey();
|
||||
_setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
|
||||
fetchWithSsrFGuardMock
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(JSON.stringify({ prompt_id: "cloud-video-1" }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
release: vi.fn(async () => {}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(JSON.stringify({ status: "completed" }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
release: vi.fn(async () => {}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(
|
||||
JSON.stringify({
|
||||
"cloud-video-1": {
|
||||
outputs: {
|
||||
"9": {
|
||||
gifs: [{ filename: "cloud.mp4", subfolder: "", type: "output" }],
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
},
|
||||
),
|
||||
release: vi.fn(async () => {}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(null, {
|
||||
status: 302,
|
||||
headers: { location: "https://cdn.example.com/cloud.mp4" },
|
||||
}),
|
||||
release: vi.fn(async () => {}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
response: new Response(Buffer.from("cloud-video-data"), {
|
||||
status: 200,
|
||||
headers: { "content-type": "video/mp4" },
|
||||
}),
|
||||
release: vi.fn(async () => {}),
|
||||
});
|
||||
mockComfyCloudJobResponses(fetchWithSsrFGuardMock, {
|
||||
body: Buffer.from("cloud-video-data"),
|
||||
contentType: "video/mp4",
|
||||
filename: "cloud.mp4",
|
||||
outputKind: "gifs",
|
||||
promptId: "cloud-video-1",
|
||||
redirectLocation: "https://cdn.example.com/cloud.mp4",
|
||||
});
|
||||
|
||||
const provider = buildComfyVideoGenerationProvider();
|
||||
const result = await provider.generateVideo({
|
||||
|
||||
Reference in New Issue
Block a user