Skip to content

Commit c5ba4ef

Browse files
authored
refactor(plugins): remove media test globals (#122636)
1 parent 85230cf commit c5ba4ef

11 files changed

Lines changed: 57 additions & 134 deletions

‎extensions/comfy/comfy.live.test.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api";
55
import { isLiveTestEnabled, readLiveTestConfig } from "openclaw/plugin-sdk/test-live";
66
import { beforeAll, describe, expect, it } from "vitest";
77
import plugin from "./index.js";
8-
import { getComfyConfigForTesting } from "./test-support.js";
98
import { isComfyCapabilityConfigured } from "./workflow-runtime.js";
109

1110
const LIVE =
@@ -123,9 +122,4 @@ describeLive("comfy live", () => {
123122
expect(result.tracks[0]?.mimeType.startsWith("audio/")).toBe(true);
124123
expect(result.tracks[0]?.buffer.byteLength).toBeGreaterThan(512);
125124
}, 180_000);
126-
127-
it("documents the effective comfy config shape for live debugging", () => {
128-
const comfyConfig = getComfyConfigForTesting(cfg as never);
129-
expect(typeof comfyConfig).toBe("object");
130-
});
131125
});

‎extensions/comfy/image-generation-provider.test.ts‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Comfy tests cover image generation provider plugin behavior.
22
import type { LookupAddress } from "node:dns";
33
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
4-
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
54
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
65
import { buildComfyImageGenerationProvider } from "./image-generation-provider.js";
76
import {
@@ -11,12 +10,23 @@ import {
1110
mockComfyProviderApiKey,
1211
parseComfyJsonBody,
1312
} from "./test-helpers.js";
14-
import { setComfyFetchGuardForTesting } from "./test-support.js";
1513

16-
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
14+
type FetchWithSsrFGuard = (typeof import("openclaw/plugin-sdk/ssrf-runtime"))["fetchWithSsrFGuard"];
15+
16+
const { fetchWithSsrFGuardMock, ssrfGuardState } = vi.hoisted(() => ({
1717
fetchWithSsrFGuardMock: vi.fn(),
18+
ssrfGuardState: {} as { actual?: FetchWithSsrFGuard },
1819
}));
1920

21+
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => {
22+
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>();
23+
ssrfGuardState.actual = actual.fetchWithSsrFGuard;
24+
return {
25+
...actual,
26+
fetchWithSsrFGuard: fetchWithSsrFGuardMock,
27+
};
28+
});
29+
2030
type FetchGuardRequest = {
2131
url?: unknown;
2232
auditContext?: unknown;
@@ -28,7 +38,7 @@ type FetchGuardRequest = {
2838
body?: BodyInit | null;
2939
};
3040
};
31-
type RealGuardParams = Parameters<typeof fetchWithSsrFGuard>[0];
41+
type RealGuardParams = Parameters<FetchWithSsrFGuard>[0];
3242
type RealGuardFetchImpl = NonNullable<RealGuardParams["fetchImpl"]>;
3343
type RealGuardLookupFn = NonNullable<RealGuardParams["lookupFn"]>;
3444
type RealGuardHarness = {
@@ -206,9 +216,13 @@ function installRealComfyFetchGuard(options: RealComfyFetchOptions): RealGuardHa
206216
});
207217
};
208218

209-
setComfyFetchGuardForTesting(async (params) => {
219+
const actualFetchWithSsrFGuard = ssrfGuardState.actual;
220+
if (!actualFetchWithSsrFGuard) {
221+
throw new Error("expected actual SSRF guard");
222+
}
223+
fetchWithSsrFGuardMock.mockImplementation(async (params) => {
210224
guardCalls.push(params);
211-
return await fetchWithSsrFGuard({
225+
return await actualFetchWithSsrFGuard({
212226
...params,
213227
fetchImpl,
214228
lookupFn,
@@ -219,11 +233,12 @@ function installRealComfyFetchGuard(options: RealComfyFetchOptions): RealGuardHa
219233

220234
describe("comfy image-generation provider", () => {
221235
beforeEach(() => {
236+
fetchWithSsrFGuardMock.mockReset();
222237
vi.clearAllMocks();
223238
});
224239

225240
afterEach(() => {
226-
setComfyFetchGuardForTesting(null);
241+
fetchWithSsrFGuardMock.mockReset();
227242
vi.unstubAllEnvs();
228243
vi.restoreAllMocks();
229244
});
@@ -353,7 +368,6 @@ describe("comfy image-generation provider", () => {
353368
});
354369

355370
it("submits a local workflow, waits for history, and downloads images", async () => {
356-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
357371
fetchWithSsrFGuardMock
358372
.mockResolvedValueOnce({
359373
response: new Response(JSON.stringify({ prompt_id: "local-prompt-1" }), {
@@ -441,7 +455,6 @@ describe("comfy image-generation provider", () => {
441455
});
442456

443457
it("honors local private-network access for service-discovery hostnames", async () => {
444-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
445458
mockLocalImageResponses("compose-prompt-1");
446459

447460
const provider = buildComfyImageGenerationProvider();
@@ -468,7 +481,6 @@ describe("comfy image-generation provider", () => {
468481
});
469482

470483
it("keeps local public-looking hostnames strict without explicit private-network access", async () => {
471-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
472484
mockLocalImageResponses("public-host-prompt-1");
473485

474486
const provider = buildComfyImageGenerationProvider();
@@ -492,7 +504,6 @@ describe("comfy image-generation provider", () => {
492504
});
493505

494506
it("keeps cloud service-discovery hostnames strict without explicit private-network access", async () => {
495-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
496507
mockComfyCloudJobResponses(fetchWithSsrFGuardMock, {
497508
body: Buffer.from("cloud-data"),
498509
contentType: "image/png",
@@ -525,7 +536,6 @@ describe("comfy image-generation provider", () => {
525536
});
526537

527538
it("honors explicit cloud private-network access for service-discovery hostnames", async () => {
528-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
529539
mockComfyCloudJobResponses(fetchWithSsrFGuardMock, {
530540
body: Buffer.from("cloud-data"),
531541
contentType: "image/png",
@@ -787,7 +797,6 @@ describe("comfy image-generation provider", () => {
787797
});
788798

789799
it("caps oversized local workflow timeouts", async () => {
790-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
791800
const nowSpy = vi.spyOn(Date, "now");
792801
nowSpy
793802
.mockReturnValueOnce(0)
@@ -836,7 +845,6 @@ describe("comfy image-generation provider", () => {
836845
});
837846

838847
it("rejects generated image downloads that exceed the configured media cap", async () => {
839-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
840848
fetchWithSsrFGuardMock
841849
.mockResolvedValueOnce({
842850
response: new Response(JSON.stringify({ prompt_id: "local-prompt-1" }), {
@@ -893,7 +901,6 @@ describe("comfy image-generation provider", () => {
893901
});
894902

895903
it("reports malformed local workflow submit JSON as a provider error", async () => {
896-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
897904
const release = vi.fn(async () => {});
898905
fetchWithSsrFGuardMock.mockResolvedValueOnce({
899906
response: new Response("{ nope", {
@@ -923,7 +930,6 @@ describe("comfy image-generation provider", () => {
923930
});
924931

925932
it("bounds oversized local workflow submit responses and releases the request", async () => {
926-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
927933
const chunk = new Uint8Array(1024 * 1024);
928934
const totalBytes = 32 * chunk.length;
929935
let bytesPulled = 0;
@@ -971,7 +977,6 @@ describe("comfy image-generation provider", () => {
971977
});
972978

973979
it("uploads reference images for local edit workflows", async () => {
974-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
975980
fetchWithSsrFGuardMock
976981
.mockResolvedValueOnce({
977982
response: new Response(JSON.stringify({ name: "upload.png" }), {
@@ -1059,7 +1064,6 @@ describe("comfy image-generation provider", () => {
10591064

10601065
it("uses cloud endpoints, auth headers, and partner-node extra_data", async () => {
10611066
mockComfyProviderApiKey();
1062-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
10631067
mockComfyCloudJobResponses(fetchWithSsrFGuardMock, {
10641068
body: Buffer.from("cloud-data"),
10651069
contentType: "image/png",
@@ -1120,7 +1124,6 @@ describe("comfy image-generation provider", () => {
11201124

11211125
it("uses plugin config env SecretRef auth for cloud workflows", async () => {
11221126
vi.stubEnv("COMFY_TEST_API_KEY", "comfy-secret-ref-key");
1123-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
11241127
mockComfyCloudJobResponses(fetchWithSsrFGuardMock, {
11251128
body: Buffer.from("cloud-data"),
11261129
contentType: "image/png",
@@ -1158,7 +1161,6 @@ describe("comfy image-generation provider", () => {
11581161
it("uses provider auth fallback for cloud workflows without plugin config API keys", async () => {
11591162
vi.stubEnv("COMFY_API_KEY", "stale-env-key");
11601163
mockComfyProviderApiKey("profile-key");
1161-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
11621164
mockComfyCloudJobResponses(fetchWithSsrFGuardMock, {
11631165
body: Buffer.from("cloud-data"),
11641166
contentType: "image/png",

‎extensions/comfy/music-generation-provider.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
import { expectExplicitMusicGenerationCapabilities } from "openclaw/plugin-sdk/provider-test-contracts";
33
import { afterEach, describe, expect, it, vi } from "vitest";
44
import { buildComfyMusicGenerationProvider } from "./music-generation-provider.js";
5-
import { setComfyFetchGuardForTesting } from "./test-support.js";
65

76
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
87
fetchWithSsrFGuardMock: vi.fn(),
98
}));
109

10+
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => ({
11+
...(await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>()),
12+
fetchWithSsrFGuard: fetchWithSsrFGuardMock,
13+
}));
14+
1115
describe("comfy music-generation provider", () => {
1216
afterEach(() => {
13-
setComfyFetchGuardForTesting(null);
17+
fetchWithSsrFGuardMock.mockReset();
1418
vi.clearAllMocks();
1519
});
1620

@@ -23,7 +27,6 @@ describe("comfy music-generation provider", () => {
2327
});
2428

2529
it("runs a music workflow and returns audio outputs", async () => {
26-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
2730
fetchWithSsrFGuardMock
2831
.mockResolvedValueOnce({
2932
response: new Response(JSON.stringify({ prompt_id: "music-job-1" }), {
@@ -101,7 +104,6 @@ describe("comfy music-generation provider", () => {
101104
});
102105

103106
it("rejects generated music downloads that exceed the configured media cap", async () => {
104-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
105107
fetchWithSsrFGuardMock
106108
.mockResolvedValueOnce({
107109
response: new Response(JSON.stringify({ prompt_id: "music-job-1" }), {

‎extensions/comfy/test-support.ts‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎extensions/comfy/video-generation-provider.test.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import {
77
mockComfyProviderApiKey,
88
parseComfyJsonBody,
99
} from "./test-helpers.js";
10-
import { setComfyFetchGuardForTesting } from "./test-support.js";
1110
import { buildComfyVideoGenerationProvider } from "./video-generation-provider.js";
1211

1312
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
1413
fetchWithSsrFGuardMock: vi.fn(),
1514
}));
1615

16+
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => ({
17+
...(await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>()),
18+
fetchWithSsrFGuard: fetchWithSsrFGuardMock,
19+
}));
20+
1721
function parseJsonBody(call: number): Record<string, unknown> {
1822
return parseComfyJsonBody(fetchWithSsrFGuardMock, call);
1923
}
@@ -89,11 +93,12 @@ function generateLocalVideo(outputNodeId?: string) {
8993

9094
describe("comfy video-generation provider", () => {
9195
beforeEach(() => {
96+
fetchWithSsrFGuardMock.mockReset();
9297
vi.clearAllMocks();
9398
});
9499

95100
afterEach(() => {
96-
setComfyFetchGuardForTesting(null);
101+
fetchWithSsrFGuardMock.mockReset();
97102
vi.restoreAllMocks();
98103
});
99104

@@ -118,7 +123,6 @@ describe("comfy video-generation provider", () => {
118123
});
119124

120125
it("submits a local workflow, waits for history, and downloads videos", async () => {
121-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
122126
fetchWithSsrFGuardMock
123127
.mockResolvedValueOnce({
124128
response: new Response(JSON.stringify({ prompt_id: "local-video-1" }), {
@@ -205,7 +209,6 @@ describe("comfy video-generation provider", () => {
205209
});
206210

207211
it("returns only MP4 video entries from mixed images buckets", async () => {
208-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
209212
mockLocalVideoResponses({
210213
promptId: "local-video-mixed",
211214
outputs: {
@@ -243,7 +246,6 @@ describe("comfy video-generation provider", () => {
243246
});
244247

245248
it("accepts uppercase WEBM names from the images bucket", async () => {
246-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
247249
mockLocalVideoResponses({
248250
promptId: "local-video-webm",
249251
outputs: {
@@ -272,7 +274,6 @@ describe("comfy video-generation provider", () => {
272274
});
273275

274276
it("rejects images-only workflow output for video generation", async () => {
275-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
276277
mockLocalVideoResponses({
277278
promptId: "local-video-images-only",
278279
outputs: {
@@ -292,7 +293,6 @@ describe("comfy video-generation provider", () => {
292293
});
293294

294295
it("preserves legacy videos bucket output without filename filtering", async () => {
295-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
296296
mockLocalVideoResponses({
297297
promptId: "local-video-legacy",
298298
outputs: {
@@ -318,7 +318,6 @@ describe("comfy video-generation provider", () => {
318318
});
319319

320320
it("rejects generated video downloads that exceed the configured media cap", async () => {
321-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
322321
fetchWithSsrFGuardMock
323322
.mockResolvedValueOnce({
324323
response: new Response(JSON.stringify({ prompt_id: "local-video-1" }), {
@@ -378,7 +377,6 @@ describe("comfy video-generation provider", () => {
378377

379378
it("uses cloud endpoints for video workflows", async () => {
380379
mockComfyProviderApiKey();
381-
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
382380
mockComfyCloudJobResponses(fetchWithSsrFGuardMock, {
383381
body: Buffer.from("cloud-video-data"),
384382
contentType: "video/mp4",

‎extensions/comfy/workflow-runtime.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,6 @@ type ComfyWorkflowResult = {
111111
outputNodeIds: string[];
112112
};
113113

114-
let comfyFetchGuard = fetchWithSsrFGuard;
115-
116-
function setComfyFetchGuardForTesting(impl: typeof fetchWithSsrFGuard | null): void {
117-
comfyFetchGuard = impl ?? fetchWithSsrFGuard;
118-
}
119-
120-
if (process.env.VITEST === "true") {
121-
Reflect.set(globalThis, Symbol.for("openclaw.comfyTestApi"), {
122-
getConfig: getComfyConfig,
123-
setFetchGuard: setComfyFetchGuardForTesting,
124-
});
125-
}
126-
127114
function readConfigInteger(config: ComfyProviderConfig, key: string): number | undefined {
128115
const value = config[key];
129116
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
@@ -327,7 +314,7 @@ async function readJsonResponse<T>(params: {
327314
auditContext: string;
328315
errorPrefix: string;
329316
}): Promise<T> {
330-
const { response, release } = await comfyFetchGuard({
317+
const { response, release } = await fetchWithSsrFGuard({
331318
url: params.url,
332319
init: params.init,
333320
timeoutMs: params.timeoutMs,
@@ -581,7 +568,7 @@ async function downloadOutputFile(params: {
581568
const viewPath = params.mode === "cloud" ? "/api/view" : "/view";
582569
const auditContext = `comfy-${params.capability}-download`;
583570

584-
const firstResponse = await comfyFetchGuard({
571+
const firstResponse = await fetchWithSsrFGuard({
585572
url: `${params.baseUrl}${viewPath}?${query.toString()}`,
586573
init: {
587574
method: "GET",

0 commit comments

Comments
 (0)