11// Comfy tests cover image generation provider plugin behavior.
22import type { LookupAddress } from "node:dns" ;
33import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime" ;
4- import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime" ;
54import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
65import { buildComfyImageGenerationProvider } from "./image-generation-provider.js" ;
76import {
@@ -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+
2030type 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 ] ;
3242type RealGuardFetchImpl = NonNullable < RealGuardParams [ "fetchImpl" ] > ;
3343type RealGuardLookupFn = NonNullable < RealGuardParams [ "lookupFn" ] > ;
3444type 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
220234describe ( "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" ,
0 commit comments