-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploads.test.ts
65 lines (56 loc) · 2.13 KB
/
uploads.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
import fs from 'fs';
import { toFile, type ResponseLike } from '@maestro-org/maestro-arch-rpc-node-sdk/uploads';
import { File } from '@maestro-org/maestro-arch-rpc-node-sdk/_shims/index';
class MyClass {
name: string = 'foo';
}
function mockResponse({ url, content }: { url: string; content?: Blob }): ResponseLike {
return {
url,
blob: async () => content as any,
};
}
describe('toFile', () => {
it('throws a helpful error for mismatched types', async () => {
await expect(
// @ts-expect-error intentionally mismatched type
toFile({ foo: 'string' }),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unexpected data type: object; constructor: Object; props: ["foo"]"`,
);
await expect(
// @ts-expect-error intentionally mismatched type
toFile(new MyClass()),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unexpected data type: object; constructor: MyClass; props: ["name"]"`,
);
});
it('disallows string at the type-level', async () => {
// @ts-expect-error we intentionally do not type support for `string`
// to help people avoid passing a file path
const file = await toFile('contents');
expect(file.text()).resolves.toEqual('contents');
});
it('extracts a file name from a Response', async () => {
const response = mockResponse({ url: 'https://example.com/my/audio.mp3' });
const file = await toFile(response);
expect(file.name).toEqual('audio.mp3');
});
it('extracts a file name from a File', async () => {
const input = new File(['foo'], 'input.jsonl');
const file = await toFile(input);
expect(file.name).toEqual('input.jsonl');
});
it('extracts a file name from a ReadStream', async () => {
const input = fs.createReadStream('tests/uploads.test.ts');
const file = await toFile(input);
expect(file.name).toEqual('uploads.test.ts');
});
it('does not copy File objects', async () => {
const input = new File(['foo'], 'input.jsonl', { type: 'jsonl' });
const file = await toFile(input);
expect(file).toBe(input);
expect(file.name).toEqual('input.jsonl');
expect(file.type).toBe('jsonl');
});
});