form.test.ts 1.9 KB

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