openai-node
65 строк · 1.6 Кб
1import { multipartFormRequestOptions, createForm } from 'openai/core';
2import { Blob } from 'openai/_shims/index';
3import { toFile } from 'openai';
4
5describe('form data validation', () => {
6test('valid values do not error', async () => {
7await multipartFormRequestOptions({
8body: {
9foo: 'foo',
10string: 1,
11bool: true,
12file: await toFile(Buffer.from('some-content')),
13blob: new Blob(['Some content'], { type: 'text/plain' }),
14},
15});
16});
17
18test('null', async () => {
19await expect(() =>
20multipartFormRequestOptions({
21body: {
22null: null,
23},
24}),
25).rejects.toThrow(TypeError);
26});
27
28test('undefined is stripped', async () => {
29const form = await createForm({
30foo: undefined,
31bar: 'baz',
32});
33expect(form.has('foo')).toBe(false);
34expect(form.get('bar')).toBe('baz');
35});
36
37test('nested undefined property is stripped', async () => {
38const form = await createForm({
39bar: {
40baz: undefined,
41},
42});
43expect(Array.from(form.entries())).toEqual([]);
44
45const form2 = await createForm({
46bar: {
47foo: 'string',
48baz: undefined,
49},
50});
51expect(Array.from(form2.entries())).toEqual([['bar[foo]', 'string']]);
52});
53
54test('nested undefined array item is stripped', async () => {
55const form = await createForm({
56bar: [undefined, undefined],
57});
58expect(Array.from(form.entries())).toEqual([]);
59
60const form2 = await createForm({
61bar: [undefined, 'foo'],
62});
63expect(Array.from(form2.entries())).toEqual([['bar[]', 'foo']]);
64});
65});
66