mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-21 01:34:22 +01:00
chore: generate sdk into packages/sdk
This commit is contained in:
85
packages/sdk/tests/form.test.ts
Normal file
85
packages/sdk/tests/form.test.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { multipartFormRequestOptions, createForm } from '@opencode-ai/sdk/internal/uploads';
|
||||
import { toFile } from '@opencode-ai/sdk/core/uploads';
|
||||
|
||||
describe('form data validation', () => {
|
||||
test('valid values do not error', async () => {
|
||||
await multipartFormRequestOptions(
|
||||
{
|
||||
body: {
|
||||
foo: 'foo',
|
||||
string: 1,
|
||||
bool: true,
|
||||
file: await toFile(Buffer.from('some-content')),
|
||||
blob: new Blob(['Some content'], { type: 'text/plain' }),
|
||||
},
|
||||
},
|
||||
fetch,
|
||||
);
|
||||
});
|
||||
|
||||
test('null', async () => {
|
||||
await expect(() =>
|
||||
multipartFormRequestOptions(
|
||||
{
|
||||
body: {
|
||||
null: null,
|
||||
},
|
||||
},
|
||||
fetch,
|
||||
),
|
||||
).rejects.toThrow(TypeError);
|
||||
});
|
||||
|
||||
test('undefined is stripped', async () => {
|
||||
const form = await createForm(
|
||||
{
|
||||
foo: undefined,
|
||||
bar: 'baz',
|
||||
},
|
||||
fetch,
|
||||
);
|
||||
expect(form.has('foo')).toBe(false);
|
||||
expect(form.get('bar')).toBe('baz');
|
||||
});
|
||||
|
||||
test('nested undefined property is stripped', async () => {
|
||||
const form = await createForm(
|
||||
{
|
||||
bar: {
|
||||
baz: undefined,
|
||||
},
|
||||
},
|
||||
fetch,
|
||||
);
|
||||
expect(Array.from(form.entries())).toEqual([]);
|
||||
|
||||
const form2 = await createForm(
|
||||
{
|
||||
bar: {
|
||||
foo: 'string',
|
||||
baz: undefined,
|
||||
},
|
||||
},
|
||||
fetch,
|
||||
);
|
||||
expect(Array.from(form2.entries())).toEqual([['bar[foo]', 'string']]);
|
||||
});
|
||||
|
||||
test('nested undefined array item is stripped', async () => {
|
||||
const form = await createForm(
|
||||
{
|
||||
bar: [undefined, undefined],
|
||||
},
|
||||
fetch,
|
||||
);
|
||||
expect(Array.from(form.entries())).toEqual([]);
|
||||
|
||||
const form2 = await createForm(
|
||||
{
|
||||
bar: [undefined, 'foo'],
|
||||
},
|
||||
fetch,
|
||||
);
|
||||
expect(Array.from(form2.entries())).toEqual([['bar[]', 'foo']]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user