@@ -55,7 +55,7 @@ async function run() {
5555 apiKey : 'mock-api-key' ,
5656 } ) ;
5757
58- // Test 1: Verify .withResponse() method exists and can be called
58+ // Verify .withResponse() method exists and can be called
5959 const result = client . chat . completions . create ( {
6060 model : 'gpt-4' ,
6161 messages : [ { role : 'user' , content : 'Test withResponse' } ] ,
@@ -67,20 +67,83 @@ async function run() {
6767 }
6868
6969 // Call .withResponse() and verify structure
70- const { data } = await result . withResponse ( ) ;
70+ const withResponseResult = await result . withResponse ( ) ;
7171
72- if ( ! data ) {
72+ // Verify all three properties exist
73+ if ( ! withResponseResult . data ) {
7374 throw new Error ( '.withResponse() did not return data' ) ;
7475 }
76+ if ( ! withResponseResult . response ) {
77+ throw new Error ( '.withResponse() did not return response' ) ;
78+ }
79+ if ( withResponseResult . request_id === undefined ) {
80+ throw new Error ( '.withResponse() did not return request_id' ) ;
81+ }
82+
83+ // Verify data structure matches expected OpenAI response
84+ const { data } = withResponseResult ;
85+ if ( data . id !== 'chatcmpl-withresponse' ) {
86+ throw new Error ( `Expected data.id to be 'chatcmpl-withresponse', got '${ data . id } '` ) ;
87+ }
88+ if ( data . choices [ 0 ] . message . content !== 'Testing .withResponse() method!' ) {
89+ throw new Error ( `Expected specific content, got '${ data . choices [ 0 ] . message . content } '` ) ;
90+ }
91+ if ( data . usage . total_tokens !== 20 ) {
92+ throw new Error ( `Expected 20 total tokens, got ${ data . usage . total_tokens } ` ) ;
93+ }
94+
95+ // Verify response is a Response object with correct headers
96+ if ( ! ( withResponseResult . response instanceof Response ) ) {
97+ throw new Error ( 'response is not a Response object' ) ;
98+ }
99+ if ( withResponseResult . response . headers . get ( 'x-request-id' ) !== 'req_withresponse_test' ) {
100+ throw new Error (
101+ `Expected x-request-id header 'req_withresponse_test', got '${ withResponseResult . response . headers . get ( 'x-request-id' ) } '` ,
102+ ) ;
103+ }
104+
105+ // Verify request_id matches the header
106+ if ( withResponseResult . request_id !== 'req_withresponse_test' ) {
107+ throw new Error ( `Expected request_id 'req_withresponse_test', got '${ withResponseResult . request_id } '` ) ;
108+ }
75109
76- // Test 2: Verify regular await still works
77- const result2 = await client . chat . completions . create ( {
110+ // Test 2: Verify .asResponse() method works
111+ const result2 = client . chat . completions . create ( {
78112 model : 'gpt-4' ,
79- messages : [ { role : 'user' , content : 'Test regular await ' } ] ,
113+ messages : [ { role : 'user' , content : 'Test asResponse ' } ] ,
80114 } ) ;
81115
82- if ( ! result2 || result2 . id !== 'chatcmpl-withresponse' ) {
83- throw new Error ( 'Regular await failed' ) ;
116+ // Verify method exists
117+ if ( typeof result2 . asResponse !== 'function' ) {
118+ throw new Error ( '.asResponse() method does not exist' ) ;
119+ }
120+
121+ // Call .asResponse() and verify it returns raw Response
122+ const rawResponse = await result2 . asResponse ( ) ;
123+
124+ if ( ! ( rawResponse instanceof Response ) ) {
125+ throw new Error ( '.asResponse() did not return a Response object' ) ;
126+ }
127+
128+ // Verify response has correct status
129+ if ( rawResponse . status !== 200 ) {
130+ throw new Error ( `Expected status 200, got ${ rawResponse . status } ` ) ;
131+ }
132+
133+ // Verify response headers
134+ if ( rawResponse . headers . get ( 'x-request-id' ) !== 'req_withresponse_test' ) {
135+ throw new Error (
136+ `Expected x-request-id header 'req_withresponse_test', got '${ rawResponse . headers . get ( 'x-request-id' ) } '` ,
137+ ) ;
138+ }
139+
140+ // Verify we can manually parse the body
141+ const body = await rawResponse . json ( ) ;
142+ if ( body . id !== 'chatcmpl-withresponse' ) {
143+ throw new Error ( `Expected body.id 'chatcmpl-withresponse', got '${ body . id } '` ) ;
144+ }
145+ if ( body . choices [ 0 ] . message . content !== 'Testing .withResponse() method!' ) {
146+ throw new Error ( `Expected specific content in body, got '${ body . choices [ 0 ] . message . content } '` ) ;
84147 }
85148 } ) ;
86149
0 commit comments