mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-13 15:47:28 +00:00
test: expose provider media test helpers
This commit is contained in:
@@ -1,118 +0,0 @@
|
||||
import type { VideoGenerationResult } from "openclaw/plugin-sdk/video-generation";
|
||||
import { expect, vi } from "vitest";
|
||||
|
||||
type ClearableMock = {
|
||||
mockClear(): unknown;
|
||||
};
|
||||
|
||||
type ResettableMock = {
|
||||
mockReset(): unknown;
|
||||
};
|
||||
|
||||
type ResolvableMock = {
|
||||
mockResolvedValue(value: unknown): unknown;
|
||||
};
|
||||
|
||||
type ChainableResolvedValueMock = ResettableMock & {
|
||||
mockResolvedValueOnce(value: unknown): ChainableResolvedValueMock;
|
||||
};
|
||||
|
||||
export type DashscopeVideoProviderMocks = {
|
||||
resolveApiKeyForProviderMock: ClearableMock;
|
||||
postJsonRequestMock: ResettableMock & ResolvableMock;
|
||||
fetchWithTimeoutMock: ChainableResolvedValueMock;
|
||||
assertOkOrThrowHttpErrorMock: ClearableMock;
|
||||
resolveProviderHttpRequestConfigMock: ClearableMock;
|
||||
};
|
||||
|
||||
export function resetDashscopeVideoProviderMocks(mocks: DashscopeVideoProviderMocks): void {
|
||||
mocks.resolveApiKeyForProviderMock.mockClear();
|
||||
mocks.postJsonRequestMock.mockReset();
|
||||
mocks.fetchWithTimeoutMock.mockReset();
|
||||
mocks.assertOkOrThrowHttpErrorMock.mockClear();
|
||||
mocks.resolveProviderHttpRequestConfigMock.mockClear();
|
||||
}
|
||||
|
||||
export function mockSuccessfulDashscopeVideoTask(
|
||||
mocks: Pick<DashscopeVideoProviderMocks, "postJsonRequestMock" | "fetchWithTimeoutMock">,
|
||||
params: {
|
||||
requestId?: string;
|
||||
taskId?: string;
|
||||
taskStatus?: string;
|
||||
videoUrl?: string;
|
||||
} = {},
|
||||
): void {
|
||||
const {
|
||||
requestId = "req-1",
|
||||
taskId = "task-1",
|
||||
taskStatus = "SUCCEEDED",
|
||||
videoUrl = "https://example.com/out.mp4",
|
||||
} = params;
|
||||
mocks.postJsonRequestMock.mockResolvedValue({
|
||||
response: {
|
||||
json: async () => ({
|
||||
request_id: requestId,
|
||||
output: {
|
||||
task_id: taskId,
|
||||
},
|
||||
}),
|
||||
},
|
||||
release: vi.fn(async () => {}),
|
||||
});
|
||||
mocks.fetchWithTimeoutMock
|
||||
.mockResolvedValueOnce({
|
||||
json: async () => ({
|
||||
output: {
|
||||
task_status: taskStatus,
|
||||
results: [{ video_url: videoUrl }],
|
||||
},
|
||||
}),
|
||||
headers: new Headers(),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
arrayBuffer: async () => Buffer.from("mp4-bytes"),
|
||||
headers: new Headers({ "content-type": "video/mp4" }),
|
||||
});
|
||||
}
|
||||
|
||||
export function expectDashscopeVideoTaskPoll(
|
||||
fetchWithTimeoutMock: ChainableResolvedValueMock,
|
||||
params: {
|
||||
baseUrl?: string;
|
||||
taskId?: string;
|
||||
timeoutMs?: number;
|
||||
} = {},
|
||||
): void {
|
||||
const {
|
||||
baseUrl = "https://dashscope-intl.aliyuncs.com",
|
||||
taskId = "task-1",
|
||||
timeoutMs = 120_000,
|
||||
} = params;
|
||||
expect(fetchWithTimeoutMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
`${baseUrl}/api/v1/tasks/${taskId}`,
|
||||
expect.objectContaining({ method: "GET" }),
|
||||
timeoutMs,
|
||||
fetch,
|
||||
);
|
||||
}
|
||||
|
||||
export function expectSuccessfulDashscopeVideoResult(
|
||||
result: VideoGenerationResult,
|
||||
params: {
|
||||
requestId?: string;
|
||||
taskId?: string;
|
||||
taskStatus?: string;
|
||||
} = {},
|
||||
): void {
|
||||
const { requestId = "req-1", taskId = "task-1", taskStatus = "SUCCEEDED" } = params;
|
||||
expect(result.videos).toHaveLength(1);
|
||||
expect(result.videos[0]?.mimeType).toBe("video/mp4");
|
||||
expect(result.metadata).toEqual(
|
||||
expect.objectContaining({
|
||||
requestId,
|
||||
taskId,
|
||||
taskStatus,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
import { expect } from "vitest";
|
||||
import { listSupportedMusicGenerationModes } from "../../../src/music-generation/capabilities.js";
|
||||
import type {
|
||||
MusicGenerationProviderPlugin,
|
||||
VideoGenerationProviderPlugin,
|
||||
} from "../../../src/plugins/types.js";
|
||||
import { listSupportedVideoGenerationModes } from "../../../src/video-generation/capabilities.js";
|
||||
|
||||
function hasPositiveModeLimit(
|
||||
value: number | undefined,
|
||||
valuesByModel: Readonly<Record<string, number>> | undefined,
|
||||
): boolean {
|
||||
return (
|
||||
(value ?? 0) > 0 ||
|
||||
Object.values(valuesByModel ?? {}).some(
|
||||
(modelValue) => Number.isFinite(modelValue) && modelValue > 0,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function expectExplicitVideoGenerationCapabilities(
|
||||
provider: VideoGenerationProviderPlugin,
|
||||
): void {
|
||||
expect(
|
||||
provider.capabilities.generate,
|
||||
`${provider.id} missing generate capabilities`,
|
||||
).toBeDefined();
|
||||
expect(
|
||||
provider.capabilities.imageToVideo,
|
||||
`${provider.id} missing imageToVideo capabilities`,
|
||||
).toBeDefined();
|
||||
expect(
|
||||
provider.capabilities.videoToVideo,
|
||||
`${provider.id} missing videoToVideo capabilities`,
|
||||
).toBeDefined();
|
||||
|
||||
const supportedModes = listSupportedVideoGenerationModes(provider);
|
||||
const imageToVideo = provider.capabilities.imageToVideo;
|
||||
const videoToVideo = provider.capabilities.videoToVideo;
|
||||
|
||||
if (imageToVideo?.enabled) {
|
||||
expect(
|
||||
hasPositiveModeLimit(imageToVideo.maxInputImages, imageToVideo.maxInputImagesByModel),
|
||||
`${provider.id} imageToVideo.enabled requires maxInputImages or maxInputImagesByModel`,
|
||||
).toBe(true);
|
||||
expect(supportedModes).toContain("imageToVideo");
|
||||
}
|
||||
if (videoToVideo?.enabled) {
|
||||
expect(
|
||||
hasPositiveModeLimit(videoToVideo.maxInputVideos, videoToVideo.maxInputVideosByModel),
|
||||
`${provider.id} videoToVideo.enabled requires maxInputVideos or maxInputVideosByModel`,
|
||||
).toBe(true);
|
||||
expect(supportedModes).toContain("videoToVideo");
|
||||
}
|
||||
}
|
||||
|
||||
export function expectExplicitMusicGenerationCapabilities(
|
||||
provider: MusicGenerationProviderPlugin,
|
||||
): void {
|
||||
expect(
|
||||
provider.capabilities.generate,
|
||||
`${provider.id} missing generate capabilities`,
|
||||
).toBeDefined();
|
||||
expect(provider.capabilities.edit, `${provider.id} missing edit capabilities`).toBeDefined();
|
||||
|
||||
const edit = provider.capabilities.edit;
|
||||
if (!edit) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (edit.enabled) {
|
||||
expect(
|
||||
edit.maxInputImages ?? 0,
|
||||
`${provider.id} edit.enabled requires maxInputImages`,
|
||||
).toBeGreaterThan(0);
|
||||
expect(listSupportedMusicGenerationModes(provider)).toContain("edit");
|
||||
} else {
|
||||
expect(listSupportedMusicGenerationModes(provider)).toEqual(["generate"]);
|
||||
}
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
import type {
|
||||
pollProviderOperationJson,
|
||||
resolveProviderHttpRequestConfig,
|
||||
sanitizeConfiguredModelProviderRequest,
|
||||
} from "openclaw/plugin-sdk/provider-http";
|
||||
import { afterEach, vi } from "vitest";
|
||||
|
||||
type ResolveProviderHttpRequestConfigParams = Parameters<
|
||||
typeof resolveProviderHttpRequestConfig
|
||||
>[0];
|
||||
type PollProviderOperationJsonParams = Parameters<typeof pollProviderOperationJson>[0];
|
||||
type SanitizeConfiguredModelProviderRequestParams = Parameters<
|
||||
typeof sanitizeConfiguredModelProviderRequest
|
||||
>[0];
|
||||
|
||||
const providerHttpMocks = vi.hoisted(() => ({
|
||||
resolveApiKeyForProviderMock: vi.fn(async () => ({ apiKey: "provider-key" })),
|
||||
postJsonRequestMock: vi.fn(),
|
||||
fetchWithTimeoutMock: vi.fn(),
|
||||
pollProviderOperationJsonMock: vi.fn(),
|
||||
assertOkOrThrowHttpErrorMock: vi.fn(async (_response: Response, _label: string) => {}),
|
||||
assertOkOrThrowProviderErrorMock: vi.fn(async (_response: Response, _label: string) => {}),
|
||||
sanitizeConfiguredModelProviderRequestMock: vi.fn(
|
||||
(request: SanitizeConfiguredModelProviderRequestParams) => request,
|
||||
),
|
||||
resolveProviderHttpRequestConfigMock: vi.fn((params: ResolveProviderHttpRequestConfigParams) => ({
|
||||
baseUrl: params.baseUrl ?? params.defaultBaseUrl,
|
||||
allowPrivateNetwork: params.allowPrivateNetwork === true,
|
||||
headers: new Headers(params.defaultHeaders),
|
||||
dispatcherPolicy: undefined,
|
||||
})),
|
||||
}));
|
||||
|
||||
providerHttpMocks.pollProviderOperationJsonMock.mockImplementation(
|
||||
async (params: PollProviderOperationJsonParams) => {
|
||||
for (let attempt = 0; attempt < params.maxAttempts; attempt += 1) {
|
||||
const response = await providerHttpMocks.fetchWithTimeoutMock(
|
||||
params.url,
|
||||
{
|
||||
method: "GET",
|
||||
headers: params.headers,
|
||||
},
|
||||
params.defaultTimeoutMs,
|
||||
params.fetchFn,
|
||||
);
|
||||
await providerHttpMocks.assertOkOrThrowHttpErrorMock(response, params.requestFailedMessage);
|
||||
const payload = await response.json();
|
||||
if (params.isComplete(payload)) {
|
||||
return payload;
|
||||
}
|
||||
const failureMessage = params.getFailureMessage?.(payload);
|
||||
if (failureMessage) {
|
||||
throw new Error(failureMessage);
|
||||
}
|
||||
}
|
||||
throw new Error(params.timeoutMessage);
|
||||
},
|
||||
);
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/provider-auth-runtime", () => ({
|
||||
resolveApiKeyForProvider: providerHttpMocks.resolveApiKeyForProviderMock,
|
||||
}));
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/provider-http", () => ({
|
||||
assertOkOrThrowHttpError: providerHttpMocks.assertOkOrThrowHttpErrorMock,
|
||||
assertOkOrThrowProviderError: providerHttpMocks.assertOkOrThrowProviderErrorMock,
|
||||
createProviderOperationDeadline: ({
|
||||
label,
|
||||
timeoutMs,
|
||||
}: {
|
||||
label: string;
|
||||
timeoutMs?: number;
|
||||
}) => ({
|
||||
label,
|
||||
timeoutMs,
|
||||
}),
|
||||
fetchWithTimeout: providerHttpMocks.fetchWithTimeoutMock,
|
||||
pollProviderOperationJson: providerHttpMocks.pollProviderOperationJsonMock,
|
||||
postJsonRequest: providerHttpMocks.postJsonRequestMock,
|
||||
resolveProviderOperationTimeoutMs: ({ defaultTimeoutMs }: { defaultTimeoutMs: number }) =>
|
||||
defaultTimeoutMs,
|
||||
resolveProviderHttpRequestConfig: providerHttpMocks.resolveProviderHttpRequestConfigMock,
|
||||
sanitizeConfiguredModelProviderRequest:
|
||||
providerHttpMocks.sanitizeConfiguredModelProviderRequestMock,
|
||||
waitProviderOperationPollInterval: async () => {},
|
||||
}));
|
||||
|
||||
export function getProviderHttpMocks() {
|
||||
return providerHttpMocks;
|
||||
}
|
||||
|
||||
export function installProviderHttpMockCleanup(): void {
|
||||
afterEach(() => {
|
||||
providerHttpMocks.resolveApiKeyForProviderMock.mockClear();
|
||||
providerHttpMocks.postJsonRequestMock.mockReset();
|
||||
providerHttpMocks.fetchWithTimeoutMock.mockReset();
|
||||
providerHttpMocks.pollProviderOperationJsonMock.mockClear();
|
||||
providerHttpMocks.assertOkOrThrowHttpErrorMock.mockClear();
|
||||
providerHttpMocks.assertOkOrThrowProviderErrorMock.mockClear();
|
||||
providerHttpMocks.sanitizeConfiguredModelProviderRequestMock.mockClear();
|
||||
providerHttpMocks.resolveProviderHttpRequestConfigMock.mockClear();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user