@@ -2,6 +2,9 @@ import { describe, it, expect } from "bun:test";
22import { handleResponses } from "./responses-handler.js" ;
33import { ResponseStore } from "../responses/store.js" ;
44import type { ProxyConfig } from "../config/types.js" ;
5+ import type * as CaptchaExports from "./captcha.js" ;
6+
7+ type CaptchaModule = typeof CaptchaExports ;
58
69const CONFIG : ProxyConfig = {
710 server : { port : 0 , host : "127.0.0.1" } ,
@@ -156,3 +159,120 @@ describe("handleResponses", () => {
156159 expect ( continuationUpstreamBody ) . toContain ( "second turn" ) ;
157160 } ) ;
158161} ) ;
162+
163+ // /v1/responses used to skip captcha entirely (it passed `undefined` where
164+ // handler.ts passes captcha headers), so every start-plan request came back as
165+ // HTTP 400 {"code":3007,"msg":"captcha verify failed"} while the OpenAI and
166+ // Anthropic routes worked.
167+ describe ( "handleResponses captcha (start-plan)" , ( ) => {
168+ const START_PLAN : ProxyConfig = { ...CONFIG , plan : "start-plan" , provider : "bigmodel" } ;
169+ const PARAM_HEADER = "x-aliyun-captcha-verify-param" ;
170+ const REGION_HEADER = "x-aliyun-captcha-verify-region" ;
171+
172+ /** Fake captcha module: mints sequential tokens, records how many were asked for. */
173+ function fakeCaptcha ( ) : { module : CaptchaModule ; minted : ( ) => number } {
174+ let n = 0 ;
175+ const module = {
176+ RETRY_HEADERS : { PARAM : PARAM_HEADER , REGION : REGION_HEADER } ,
177+ getCaptchaToken : async ( ) => {
178+ n += 1 ;
179+ return { verifyParam : `token-${ n } ` , region : "cn" } ;
180+ } ,
181+ detectCaptchaChallenge : ( resp : Response ) => resp . headers . get ( PARAM_HEADER ) ,
182+ } as unknown as CaptchaModule ;
183+ return { module, minted : ( ) => n } ;
184+ }
185+
186+ it ( "sends a captcha token upstream on start-plan" , async ( ) => {
187+ const seen : ( string | null ) [ ] = [ ] ;
188+ const fetchImpl = ( async ( request : Request ) : Promise < Response > => {
189+ seen . push ( request . headers . get ( PARAM_HEADER ) ) ;
190+ return new Response ( anthropicMsg ( "ok" ) , { status : 200 , headers : { "content-type" : "application/json" } } ) ;
191+ } ) as unknown as typeof fetch ;
192+
193+ const captcha = fakeCaptcha ( ) ;
194+ const resp = await handleResponses ( makeReq ( { model : "glm-5.2" , input : "hi" } ) , {
195+ config : START_PLAN , auth, fetchImpl, captcha : captcha . module ,
196+ } ) ;
197+ expect ( resp . status ) . toBe ( 200 ) ;
198+ expect ( seen ) . toEqual ( [ "token-1" ] ) ;
199+ expect ( captcha . minted ( ) ) . toBe ( 1 ) ;
200+ } ) ;
201+
202+ it ( "retries once with a fresh token when upstream answers in-body 3007" , async ( ) => {
203+ const seen : ( string | null ) [ ] = [ ] ;
204+ const fetchImpl = ( async ( request : Request ) : Promise < Response > => {
205+ seen . push ( request . headers . get ( PARAM_HEADER ) ) ;
206+ if ( seen . length === 1 ) {
207+ return new Response ( JSON . stringify ( { code : 3007 , msg : "captcha verify failed" } ) , {
208+ status : 400 , headers : { "content-type" : "application/json" } ,
209+ } ) ;
210+ }
211+ return new Response ( anthropicMsg ( "recovered" ) , { status : 200 , headers : { "content-type" : "application/json" } } ) ;
212+ } ) as unknown as typeof fetch ;
213+
214+ const captcha = fakeCaptcha ( ) ;
215+ const resp = await handleResponses ( makeReq ( { model : "glm-5.2" , input : "hi" } ) , {
216+ config : START_PLAN , auth, fetchImpl, captcha : captcha . module ,
217+ } ) ;
218+ expect ( resp . status ) . toBe ( 200 ) ;
219+ const body = await resp . json ( ) ;
220+ expect ( body . output [ 0 ] . content [ 0 ] . text ) . toBe ( "recovered" ) ;
221+ // The challenged token is already spent, so the retry must carry a NEW one.
222+ expect ( seen ) . toEqual ( [ "token-1" , "token-2" ] ) ;
223+ } ) ;
224+
225+ it ( "retries when the challenge arrives as a response header" , async ( ) => {
226+ const seen : ( string | null ) [ ] = [ ] ;
227+ const fetchImpl = ( async ( request : Request ) : Promise < Response > => {
228+ seen . push ( request . headers . get ( PARAM_HEADER ) ) ;
229+ if ( seen . length === 1 ) {
230+ return new Response ( "denied" , { status : 403 , headers : { [ PARAM_HEADER ] : "challenge-abc" } } ) ;
231+ }
232+ return new Response ( anthropicMsg ( "ok" ) , { status : 200 , headers : { "content-type" : "application/json" } } ) ;
233+ } ) as unknown as typeof fetch ;
234+
235+ const captcha = fakeCaptcha ( ) ;
236+ const resp = await handleResponses ( makeReq ( { model : "glm-5.2" , input : "hi" } ) , {
237+ config : START_PLAN , auth, fetchImpl, captcha : captcha . module ,
238+ } ) ;
239+ expect ( resp . status ) . toBe ( 200 ) ;
240+ expect ( seen ) . toEqual ( [ "token-1" , "token-2" ] ) ;
241+ } ) ;
242+
243+ it ( "gives up after one retry instead of looping" , async ( ) => {
244+ let calls = 0 ;
245+ const fetchImpl = ( async ( ) : Promise < Response > => {
246+ calls += 1 ;
247+ return new Response ( JSON . stringify ( { code : 3007 , msg : "captcha verify failed" } ) , {
248+ status : 400 , headers : { "content-type" : "application/json" } ,
249+ } ) ;
250+ } ) as unknown as typeof fetch ;
251+
252+ const captcha = fakeCaptcha ( ) ;
253+ const resp = await handleResponses ( makeReq ( { model : "glm-5.2" , input : "hi" } ) , {
254+ config : START_PLAN , auth, fetchImpl, captcha : captcha . module ,
255+ } ) ;
256+ expect ( calls ) . toBe ( 2 ) ;
257+ expect ( resp . status ) . toBe ( 400 ) ;
258+ const body = await resp . json ( ) ;
259+ expect ( body . error . type ) . toBe ( "upstream_error" ) ;
260+ expect ( body . error . message ) . toContain ( "3007" ) ;
261+ } ) ;
262+
263+ it ( "does not touch captcha on coding-plan" , async ( ) => {
264+ const seen : ( string | null ) [ ] = [ ] ;
265+ const fetchImpl = ( async ( request : Request ) : Promise < Response > => {
266+ seen . push ( request . headers . get ( PARAM_HEADER ) ) ;
267+ return new Response ( anthropicMsg ( "ok" ) , { status : 200 , headers : { "content-type" : "application/json" } } ) ;
268+ } ) as unknown as typeof fetch ;
269+
270+ const captcha = fakeCaptcha ( ) ;
271+ const resp = await handleResponses ( makeReq ( { model : "glm-5.2" , input : "hi" } ) , {
272+ config : CONFIG , auth, fetchImpl, captcha : captcha . module ,
273+ } ) ;
274+ expect ( resp . status ) . toBe ( 200 ) ;
275+ expect ( seen ) . toEqual ( [ null ] ) ;
276+ expect ( captcha . minted ( ) ) . toBe ( 0 ) ;
277+ } ) ;
278+ } ) ;
0 commit comments