|
| 1 | +import { APIPromise } from 'openai/api-promise'; |
| 2 | +import OpenAI from 'openai/index'; |
| 3 | +import { compareType } from './utils/typing'; |
| 4 | + |
| 5 | +const client = new OpenAI({ apiKey: 'example-api-key' }); |
| 6 | + |
| 7 | +describe('request id', () => { |
| 8 | + test('types', () => { |
| 9 | + compareType<Awaited<APIPromise<string>>, string>(true); |
| 10 | + compareType<Awaited<APIPromise<number>>, number>(true); |
| 11 | + compareType<Awaited<APIPromise<null>>, null>(true); |
| 12 | + compareType<Awaited<APIPromise<void>>, void>(true); |
| 13 | + compareType<Awaited<APIPromise<Response>>, Response>(true); |
| 14 | + compareType<Awaited<APIPromise<Response>>, Response>(true); |
| 15 | + compareType<Awaited<APIPromise<{ foo: string }>>, { foo: string } & { _request_id?: string | null }>( |
| 16 | + true, |
| 17 | + ); |
| 18 | + compareType<Awaited<APIPromise<Array<{ foo: string }>>>, Array<{ foo: string }>>(true); |
| 19 | + }); |
| 20 | + |
| 21 | + test('withResponse', async () => { |
| 22 | + const client = new OpenAI({ |
| 23 | + apiKey: 'dummy', |
| 24 | + fetch: async () => |
| 25 | + new Response(JSON.stringify({ id: 'bar' }), { |
| 26 | + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, |
| 27 | + }), |
| 28 | + }); |
| 29 | + |
| 30 | + const { |
| 31 | + data: completion, |
| 32 | + response, |
| 33 | + request_id, |
| 34 | + } = await client.chat.completions.create({ messages: [], model: 'gpt-4' }).withResponse(); |
| 35 | + |
| 36 | + expect(request_id).toBe('req_id_xxx'); |
| 37 | + expect(response.headers.get('x-request-id')).toBe('req_id_xxx'); |
| 38 | + expect(completion.id).toBe('bar'); |
| 39 | + expect(JSON.stringify(completion)).toBe('{"id":"bar"}'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('object response', async () => { |
| 43 | + const client = new OpenAI({ |
| 44 | + apiKey: 'dummy', |
| 45 | + fetch: async () => |
| 46 | + new Response(JSON.stringify({ id: 'bar' }), { |
| 47 | + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, |
| 48 | + }), |
| 49 | + }); |
| 50 | + |
| 51 | + const rsp = await client.chat.completions.create({ messages: [], model: 'gpt-4' }); |
| 52 | + expect(rsp.id).toBe('bar'); |
| 53 | + expect(rsp._request_id).toBe('req_id_xxx'); |
| 54 | + expect(JSON.stringify(rsp)).toBe('{"id":"bar"}'); |
| 55 | + }); |
| 56 | + |
| 57 | + test('envelope response', async () => { |
| 58 | + const promise = new APIPromise<{ data: { foo: string } }>( |
| 59 | + client, |
| 60 | + (async () => { |
| 61 | + return { |
| 62 | + response: new Response(JSON.stringify({ data: { foo: 'bar' } }), { |
| 63 | + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, |
| 64 | + }), |
| 65 | + controller: {} as any, |
| 66 | + options: {} as any, |
| 67 | + requestLogID: 'log_...', |
| 68 | + retryOfRequestLogID: undefined, |
| 69 | + startTime: Date.now(), |
| 70 | + }; |
| 71 | + })(), |
| 72 | + )._thenUnwrap((d) => d.data); |
| 73 | + |
| 74 | + const rsp = await promise; |
| 75 | + expect(rsp.foo).toBe('bar'); |
| 76 | + expect(rsp._request_id).toBe('req_id_xxx'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('page response', async () => { |
| 80 | + const client = new OpenAI({ |
| 81 | + apiKey: 'dummy', |
| 82 | + fetch: async () => |
| 83 | + new Response(JSON.stringify({ data: [{ foo: 'bar' }] }), { |
| 84 | + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, |
| 85 | + }), |
| 86 | + }); |
| 87 | + |
| 88 | + const page = await client.fineTuning.jobs.list(); |
| 89 | + expect(page.data).toMatchObject([{ foo: 'bar' }]); |
| 90 | + expect((page as any)._request_id).toBeUndefined(); |
| 91 | + }); |
| 92 | + |
| 93 | + test('array response', async () => { |
| 94 | + const promise = new APIPromise<Array<{ foo: string }>>( |
| 95 | + client, |
| 96 | + (async () => { |
| 97 | + return { |
| 98 | + response: new Response(JSON.stringify([{ foo: 'bar' }]), { |
| 99 | + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' }, |
| 100 | + }), |
| 101 | + controller: {} as any, |
| 102 | + options: {} as any, |
| 103 | + requestLogID: 'log_...', |
| 104 | + retryOfRequestLogID: undefined, |
| 105 | + startTime: Date.now(), |
| 106 | + }; |
| 107 | + })(), |
| 108 | + ); |
| 109 | + |
| 110 | + const rsp = await promise; |
| 111 | + expect(rsp.length).toBe(1); |
| 112 | + expect(rsp[0]).toMatchObject({ foo: 'bar' }); |
| 113 | + expect((rsp as any)._request_id).toBeUndefined(); |
| 114 | + }); |
| 115 | + |
| 116 | + test('string response', async () => { |
| 117 | + const promise = new APIPromise<string>( |
| 118 | + client, |
| 119 | + (async () => { |
| 120 | + return { |
| 121 | + response: new Response('hello world', { |
| 122 | + headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/text' }, |
| 123 | + }), |
| 124 | + controller: {} as any, |
| 125 | + options: {} as any, |
| 126 | + requestLogID: 'log_...', |
| 127 | + retryOfRequestLogID: undefined, |
| 128 | + startTime: Date.now(), |
| 129 | + }; |
| 130 | + })(), |
| 131 | + ); |
| 132 | + |
| 133 | + const result = await promise; |
| 134 | + expect(result).toBe('hello world'); |
| 135 | + expect((result as any)._request_id).toBeUndefined(); |
| 136 | + }); |
| 137 | +}); |
0 commit comments