|
| 1 | +import { describe, expect, it } from 'bun:test' |
| 2 | +import { z } from 'zod' |
| 3 | +import { createJSONSchema, createResponseFormat } from './responseFormat' |
| 4 | + |
| 5 | +describe('createJSONSchema', () => { |
| 6 | + // it('should convert a basic object schema to JSON schema', () => { |
| 7 | + // const schema = z.object({ |
| 8 | + // age: z.number(), |
| 9 | + // name: z.string() |
| 10 | + // }) |
| 11 | + |
| 12 | + // const jsonSchema = createJSONSchema({ schema }) |
| 13 | + |
| 14 | + // expect(jsonSchema.type).toBe('object') |
| 15 | + // expect(jsonSchema.properties).toEqual({ |
| 16 | + // age: { type: 'number' }, |
| 17 | + // name: { type: 'string' } |
| 18 | + // }) |
| 19 | + // expect(jsonSchema.additionalProperties).toBe(false) |
| 20 | + // }) |
| 21 | + |
| 22 | + it('should handle custom schemas with params.type and params.token', () => { |
| 23 | + const customSchema = z.custom<string>(() => true, { |
| 24 | + params: { token: 'my_custom_token', type: 'custom' } |
| 25 | + }) |
| 26 | + |
| 27 | + const schema = z.object({ |
| 28 | + customField: customSchema |
| 29 | + }) |
| 30 | + |
| 31 | + const jsonSchema = createJSONSchema({ schema }) |
| 32 | + |
| 33 | + expect(jsonSchema.type).toBe('object') |
| 34 | + expect((jsonSchema.properties as Record<string, unknown>).customField).toEqual({ |
| 35 | + const: 'my_custom_token', |
| 36 | + type: 'string' |
| 37 | + }) |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('createResponseFormat', () => { |
| 42 | + it('should create a response format with correct structure', () => { |
| 43 | + const schema = z.object({ |
| 44 | + age: z.number(), |
| 45 | + name: z.string() |
| 46 | + }) |
| 47 | + |
| 48 | + const responseFormat = createResponseFormat({ |
| 49 | + name: 'test_response', |
| 50 | + schema |
| 51 | + }) |
| 52 | + |
| 53 | + expect(responseFormat.name).toBe('test_response') |
| 54 | + expect(responseFormat.type).toBe('json_schema') |
| 55 | + expect(responseFormat.strict).toBe(true) |
| 56 | + expect(responseFormat.$brand).toBe('auto-parseable-response-format') |
| 57 | + expect(responseFormat.schema.properties).toEqual({ |
| 58 | + age: { type: 'number' }, |
| 59 | + name: { type: 'string' } |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should parse raw JSON correctly', () => { |
| 64 | + const schema = z.object({ |
| 65 | + value: z.number() |
| 66 | + }) |
| 67 | + |
| 68 | + const responseFormat = createResponseFormat({ |
| 69 | + name: 'number_response', |
| 70 | + schema |
| 71 | + }) |
| 72 | + |
| 73 | + const parsed = responseFormat.$parseRaw('{"value": 42}') |
| 74 | + |
| 75 | + expect(parsed).toEqual({ value: 42 }) |
| 76 | + }) |
| 77 | +}) |
0 commit comments