-
Notifications
You must be signed in to change notification settings - Fork 906
/
responses.test.ts
148 lines (132 loc) · 4.9 KB
/
responses.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { APIPromise, createResponseHeaders } from 'openai/core';
import OpenAI from 'openai/index';
import { Headers } from 'openai/_shims/index';
import { Response } from 'node-fetch';
import { compareType } from './utils/typing';
describe('response parsing', () => {
// TODO: test unicode characters
test('headers are case agnostic', async () => {
const headers = createResponseHeaders(new Headers({ 'Content-Type': 'foo', Accept: 'text/plain' }));
expect(headers['content-type']).toEqual('foo');
expect(headers['Content-type']).toEqual('foo');
expect(headers['Content-Type']).toEqual('foo');
expect(headers['accept']).toEqual('text/plain');
expect(headers['Accept']).toEqual('text/plain');
expect(headers['Hello-World']).toBeUndefined();
});
test('duplicate headers are concatenated', () => {
const headers = createResponseHeaders(
new Headers([
['Content-Type', 'text/xml'],
['Content-Type', 'application/json'],
]),
);
expect(headers['content-type']).toBe('text/xml, application/json');
});
});
describe('request id', () => {
test('types', () => {
compareType<Awaited<APIPromise<string>>, string>(true);
compareType<Awaited<APIPromise<number>>, number>(true);
compareType<Awaited<APIPromise<null>>, null>(true);
compareType<Awaited<APIPromise<void>>, void>(true);
compareType<Awaited<APIPromise<Response>>, Response>(true);
compareType<Awaited<APIPromise<Response>>, Response>(true);
compareType<Awaited<APIPromise<{ foo: string }>>, { foo: string } & { _request_id?: string | null }>(
true,
);
compareType<Awaited<APIPromise<Array<{ foo: string }>>>, Array<{ foo: string }>>(true);
});
test('withResponse', async () => {
const client = new OpenAI({
apiKey: 'dummy',
fetch: async () =>
new Response(JSON.stringify({ id: 'bar' }), {
headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' },
}),
});
const {
data: completion,
response,
request_id,
} = await client.chat.completions.create({ messages: [], model: 'gpt-4' }).withResponse();
expect(request_id).toBe('req_id_xxx');
expect(response.headers.get('x-request-id')).toBe('req_id_xxx');
expect(completion.id).toBe('bar');
expect(JSON.stringify(completion)).toBe('{"id":"bar"}');
});
test('object response', async () => {
const client = new OpenAI({
apiKey: 'dummy',
fetch: async () =>
new Response(JSON.stringify({ id: 'bar' }), {
headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' },
}),
});
const rsp = await client.chat.completions.create({ messages: [], model: 'gpt-4' });
expect(rsp.id).toBe('bar');
expect(rsp._request_id).toBe('req_id_xxx');
expect(JSON.stringify(rsp)).toBe('{"id":"bar"}');
});
test('envelope response', async () => {
const promise = new APIPromise<{ data: { foo: string } }>(
(async () => {
return {
response: new Response(JSON.stringify({ data: { foo: 'bar' } }), {
headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' },
}),
controller: {} as any,
options: {} as any,
};
})(),
)._thenUnwrap((d) => d.data);
const rsp = await promise;
expect(rsp.foo).toBe('bar');
expect(rsp._request_id).toBe('req_id_xxx');
});
test('page response', async () => {
const client = new OpenAI({
apiKey: 'dummy',
fetch: async () =>
new Response(JSON.stringify({ data: [{ foo: 'bar' }] }), {
headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' },
}),
});
const page = await client.fineTuning.jobs.list();
expect(page.data).toMatchObject([{ foo: 'bar' }]);
expect((page as any)._request_id).toBeUndefined();
});
test('array response', async () => {
const promise = new APIPromise<Array<{ foo: string }>>(
(async () => {
return {
response: new Response(JSON.stringify([{ foo: 'bar' }]), {
headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/json' },
}),
controller: {} as any,
options: {} as any,
};
})(),
);
const rsp = await promise;
expect(rsp.length).toBe(1);
expect(rsp[0]).toMatchObject({ foo: 'bar' });
expect((rsp as any)._request_id).toBeUndefined();
});
test('string response', async () => {
const promise = new APIPromise<string>(
(async () => {
return {
response: new Response('hello world', {
headers: { 'x-request-id': 'req_id_xxx', 'content-type': 'application/text' },
}),
controller: {} as any,
options: {} as any,
};
})(),
);
const result = await promise;
expect(result).toBe('hello world');
expect((result as any)._request_id).toBeUndefined();
});
});