@@ -11,6 +11,7 @@ import {
1111 callComplexityAnalysis ,
1212 renderComplexitySection ,
1313} from "./complexity-analysis.js" ;
14+ import { createFakeAi } from "../tests/fakeAi.js" ;
1415
1516const PLAIN_SOURCE = "function solution() { return 0; }" ;
1617
@@ -452,6 +453,8 @@ function solution() {}`;
452453// ── callComplexityAnalysis ────────────────────────
453454
454455describe ( "callComplexityAnalysis" , ( ) => {
456+ const ai = createFakeAi ( ) ;
457+
455458 beforeEach ( ( ) => {
456459 vi . clearAllMocks ( ) ;
457460 } ) ;
@@ -471,7 +474,7 @@ describe("callComplexityAnalysis", () => {
471474 { problemName : "valid-parentheses" , content : PLAIN_SOURCE } ,
472475 ] ;
473476
474- const results = await callComplexityAnalysis ( fileEntries , "fake-key" ) ;
477+ const results = await callComplexityAnalysis ( fileEntries , ai ) ;
475478
476479 expect ( results ) . toHaveLength ( 2 ) ;
477480 expect ( results [ 0 ] . problemName ) . toBe ( "two-sum" ) ;
@@ -485,7 +488,7 @@ describe("callComplexityAnalysis", () => {
485488 await expect (
486489 callComplexityAnalysis (
487490 [ { problemName : "two-sum" , content : PLAIN_SOURCE } ] ,
488- "fake-key"
491+ ai
489492 )
490493 ) . rejects . toThrow ( "OpenAI API error" ) ;
491494 } ) ;
@@ -498,7 +501,7 @@ describe("callComplexityAnalysis", () => {
498501 await expect (
499502 callComplexityAnalysis (
500503 [ { problemName : "two-sum" , content : PLAIN_SOURCE } ] ,
501- "fake-key"
504+ ai
502505 )
503506 ) . rejects . toThrow ( "Empty response from OpenAI" ) ;
504507 } ) ;
@@ -513,7 +516,7 @@ describe("callComplexityAnalysis", () => {
513516 await expect (
514517 callComplexityAnalysis (
515518 [ { problemName : "two-sum" , content : PLAIN_SOURCE } ] ,
516- "fake-key"
519+ ai
517520 )
518521 ) . rejects . toThrow ( "OpenAI returned invalid JSON" ) ;
519522 } ) ;
@@ -530,7 +533,7 @@ describe("callComplexityAnalysis", () => {
530533 { problemName : "longest-prefix" , content : PLAIN_SOURCE } ,
531534 ] ;
532535
533- const results = await callComplexityAnalysis ( fileEntries , "fake-key" ) ;
536+ const results = await callComplexityAnalysis ( fileEntries , ai ) ;
534537
535538 expect ( results ) . toHaveLength ( 1 ) ;
536539 expect ( results [ 0 ] . problemName ) . toBe ( "longest-prefix" ) ;
@@ -550,32 +553,35 @@ describe("callComplexityAnalysis", () => {
550553function solution() { return 0; }` ;
551554 await callComplexityAnalysis (
552555 [ { problemName : "two-sum" , content : source } ] ,
553- "fake-key"
556+ ai
554557 ) ;
555558
556559 expect ( captured ) . toContain ( "L1: \nL2: \nL3: function solution()" ) ;
557560 expect ( captured ) . not . toContain ( "TC: O(n)" ) ;
558561 expect ( captured ) . not . toContain ( "SC: O(1)" ) ;
559562 } ) ;
560563
561- it ( "AI Gateway 인증 헤더와 재시도 헤더를 함께 보낸다" , async ( ) => {
562- let captured = null ;
563- globalThis . fetch = vi . fn ( ) . mockImplementation ( async ( url , opts ) => {
564- captured = opts . headers ;
565- return await makeOpenAIResponse ( [ makeSingleSolutionAnalysis ( "two-sum" ) ] ) ;
566- } ) ;
564+ it ( "dalestudy 게이트웨이의 OpenAI chat/completions 로 재시도 설정과 함께 보낸다" , async ( ) => {
565+ globalThis . fetch = vi
566+ . fn ( )
567+ . mockResolvedValueOnce (
568+ await makeOpenAIResponse ( [ makeSingleSolutionAnalysis ( "two-sum" ) ] )
569+ ) ;
567570
568571 await callComplexityAnalysis (
569572 [ { problemName : "two-sum" , content : PLAIN_SOURCE } ] ,
570- "fake-key"
573+ ai
571574 ) ;
572575
573- expect ( captured ) . toMatchObject ( {
574- "cf-aig-authorization" : "Bearer fake-key" ,
575- "cf-aig-max-attempts" : "3" ,
576- "cf-aig-retry-delay" : "1000" ,
577- "cf-aig-backoff" : "exponential" ,
576+ expect ( ai . gateway ) . toHaveBeenCalledWith ( "dalestudy" ) ;
577+ const request = ai . run . mock . calls [ 0 ] [ 0 ] ;
578+ expect ( request ) . toMatchObject ( {
579+ provider : "openai" ,
580+ endpoint : "chat/completions" ,
581+ config : { maxAttempts : 3 , retryDelay : 1000 , backoff : "exponential" } ,
578582 } ) ;
583+ // 인증은 바인딩이 하므로 게이트웨이 토큰을 싣지 않는다
584+ expect ( request . headers ) . not . toHaveProperty ( "cf-aig-authorization" ) ;
579585 } ) ;
580586} ) ;
581587
0 commit comments