openai-node
57 строк · 1.8 Кб
1import fs from 'fs';
2import { toFile, type ResponseLike } from 'openai/uploads';
3import { File } from 'openai/_shims/index';
4
5class MyClass {
6name: string = 'foo';
7}
8
9function mockResponse({ url, content }: { url: string; content?: Blob }): ResponseLike {
10return {
11url,
12blob: async () => content as any,
13};
14}
15
16describe('toFile', () => {
17it('throws a helpful error for mismatched types', async () => {
18await expect(
19// @ts-expect-error intentionally mismatched type
20toFile({ foo: 'string' }),
21).rejects.toThrowErrorMatchingInlineSnapshot(
22`"Unexpected data type: object; constructor: Object; props: ["foo"]"`,
23);
24
25await expect(
26// @ts-expect-error intentionally mismatched type
27toFile(new MyClass()),
28).rejects.toThrowErrorMatchingInlineSnapshot(
29`"Unexpected data type: object; constructor: MyClass; props: ["name"]"`,
30);
31});
32
33it('disallows string at the type-level', async () => {
34// @ts-expect-error we intentionally do not type support for `string`
35// to help people avoid passing a file path
36const file = await toFile('contents');
37expect(file.text()).resolves.toEqual('contents');
38});
39
40it('extracts a file name from a Response', async () => {
41const response = mockResponse({ url: 'https://example.com/my/audio.mp3' });
42const file = await toFile(response);
43expect(file.name).toEqual('audio.mp3');
44});
45
46it('extracts a file name from a File', async () => {
47const input = new File(['foo'], 'input.jsonl');
48const file = await toFile(input);
49expect(file.name).toEqual('input.jsonl');
50});
51
52it('extracts a file name from a ReadStream', async () => {
53const input = fs.createReadStream('tests/uploads.test.ts');
54const file = await toFile(input);
55expect(file.name).toEqual('uploads.test.ts');
56});
57});
58