test: guard media understanding calls

This commit is contained in:
Peter Steinberger
2026-05-11 22:28:19 +01:00
parent 95fa1bba60
commit 0f616dc054
3 changed files with 45 additions and 14 deletions

View File

@@ -15,6 +15,23 @@ vi.mock("../media/fetch.js", async () => {
};
});
function requireFetchRemoteMediaInput(): {
url?: unknown;
fetchImpl?: unknown;
maxBytes?: unknown;
ssrfPolicy?: unknown;
} {
const [call] = fetchRemoteMediaMock.mock.calls;
if (!call) {
throw new Error("expected fetchRemoteMedia call");
}
const [input] = call;
if (typeof input !== "object" || input === null || Array.isArray(input)) {
throw new Error("expected fetchRemoteMedia input to be an object");
}
return input;
}
async function withBlockedLocalAttachmentFallback(
prefix: string,
run: (params: { cache: MediaAttachmentCache; fallbackUrl: string }) => Promise<void>,
@@ -65,10 +82,8 @@ describe("media understanding attachment URL fallback", () => {
expect(path.basename(result.path).startsWith("openclaw-media-")).toBe(true);
expect(path.extname(result.path)).toBe(".jpg");
expect(fetchRemoteMediaMock).toHaveBeenCalledTimes(1);
const fetchInput = fetchRemoteMediaMock.mock.calls[0]?.[0] as
| { url?: unknown; fetchImpl?: unknown; maxBytes?: unknown; ssrfPolicy?: unknown }
| undefined;
const fetchImpl = fetchInput?.fetchImpl;
const fetchInput = requireFetchRemoteMediaInput();
const fetchImpl = fetchInput.fetchImpl;
expect(fetchInput).toStrictEqual({
url: fallbackUrl,
fetchImpl,
@@ -95,10 +110,8 @@ describe("media understanding attachment URL fallback", () => {
});
expect(result.buffer.toString()).toBe("fallback-buffer");
expect(fetchRemoteMediaMock).toHaveBeenCalledTimes(1);
const fetchInput = fetchRemoteMediaMock.mock.calls[0]?.[0] as
| { url?: unknown; fetchImpl?: unknown; maxBytes?: unknown; ssrfPolicy?: unknown }
| undefined;
const fetchImpl = fetchInput?.fetchImpl;
const fetchInput = requireFetchRemoteMediaInput();
const fetchImpl = fetchInput.fetchImpl;
expect(fetchInput).toStrictEqual({
url: fallbackUrl,
fetchImpl,

View File

@@ -12,6 +12,18 @@ vi.mock("./shared.js", async () => {
};
});
function requirePostTranscriptionRequest(): { pinDns?: unknown; body?: unknown } {
const [call] = postTranscriptionRequestMock.mock.calls;
if (!call) {
throw new Error("expected postTranscriptionRequest call");
}
const [request] = call;
if (typeof request !== "object" || request === null || Array.isArray(request)) {
throw new Error("expected postTranscriptionRequest params to be an object");
}
return request;
}
import { transcribeOpenAiCompatibleAudio } from "./openai-compatible-audio.js";
afterEach(() => {
@@ -37,10 +49,8 @@ describe("transcribeOpenAiCompatibleAudio pinDns", () => {
});
expect(result.text).toBe("ok");
const request = postTranscriptionRequestMock.mock.calls[0]?.[0] as
| { pinDns?: boolean; body?: unknown }
| undefined;
expect(request?.pinDns).toBe(false);
expect(request?.body).toBeInstanceOf(FormData);
const request = requirePostTranscriptionRequest();
expect(request.pinDns).toBe(false);
expect(request.body).toBeInstanceOf(FormData);
});
});

View File

@@ -46,6 +46,14 @@ vi.mock("./image-runtime.js", () => ({
describeImageWithModel: mocks.describeImageWithModel,
}));
function requireRunCapabilityRequest(): unknown {
const [call] = mocks.runCapability.mock.calls;
if (!call) {
throw new Error("expected runCapability call");
}
return call[0];
}
describe("media-understanding runtime", () => {
afterEach(() => {
mocks.buildProviderRegistry.mockReset();
@@ -200,7 +208,7 @@ describe("media-understanding runtime", () => {
});
expect(mocks.runCapability).toHaveBeenCalledOnce();
expect(mocks.runCapability.mock.calls[0]?.[0]).toEqual({
expect(requireRunCapabilityRequest()).toEqual({
capability: "image",
cfg: {
tools: {