openai-node

Форк
0
/
form.test.ts 
65 строк · 1.6 Кб
1
import { multipartFormRequestOptions, createForm } from 'openai/core';
2
import { Blob } from 'openai/_shims/index';
3
import { toFile } from 'openai';
4

5
describe('form data validation', () => {
6
  test('valid values do not error', async () => {
7
    await multipartFormRequestOptions({
8
      body: {
9
        foo: 'foo',
10
        string: 1,
11
        bool: true,
12
        file: await toFile(Buffer.from('some-content')),
13
        blob: new Blob(['Some content'], { type: 'text/plain' }),
14
      },
15
    });
16
  });
17

18
  test('null', async () => {
19
    await expect(() =>
20
      multipartFormRequestOptions({
21
        body: {
22
          null: null,
23
        },
24
      }),
25
    ).rejects.toThrow(TypeError);
26
  });
27

28
  test('undefined is stripped', async () => {
29
    const form = await createForm({
30
      foo: undefined,
31
      bar: 'baz',
32
    });
33
    expect(form.has('foo')).toBe(false);
34
    expect(form.get('bar')).toBe('baz');
35
  });
36

37
  test('nested undefined property is stripped', async () => {
38
    const form = await createForm({
39
      bar: {
40
        baz: undefined,
41
      },
42
    });
43
    expect(Array.from(form.entries())).toEqual([]);
44

45
    const form2 = await createForm({
46
      bar: {
47
        foo: 'string',
48
        baz: undefined,
49
      },
50
    });
51
    expect(Array.from(form2.entries())).toEqual([['bar[foo]', 'string']]);
52
  });
53

54
  test('nested undefined array item is stripped', async () => {
55
    const form = await createForm({
56
      bar: [undefined, undefined],
57
    });
58
    expect(Array.from(form.entries())).toEqual([]);
59

60
    const form2 = await createForm({
61
      bar: [undefined, 'foo'],
62
    });
63
    expect(Array.from(form2.entries())).toEqual([['bar[]', 'foo']]);
64
  });
65
});
66

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.