@@ -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,39 @@ 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+ headers : {
582+ "cf-aig-max-attempts" : 3 ,
583+ "cf-aig-retry-delay" : 1000 ,
584+ "cf-aig-backoff" : "exponential" ,
585+ } ,
578586 } ) ;
587+ // ์ธ์ฆ์ ๋ฐ์ธ๋ฉ์ด ํ๋ฏ๋ก ๊ฒ์ดํธ์จ์ด ํ ํฐ์ ์ฃ์ง ์๋๋ค
588+ expect ( request . headers ) . not . toHaveProperty ( "cf-aig-authorization" ) ;
579589 } ) ;
580590} ) ;
581591
0 commit comments