|
| 1 | +/// <reference types="jest" /> |
| 2 | + |
| 3 | +/** |
| 4 | + * @jest-environment node |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect } from '@jest/globals'; |
| 8 | +import { z } from 'zod'; |
| 9 | + |
| 10 | +describe('Boolean string transform', () => { |
| 11 | + // Test the boolean transform that's used in the tool registrations |
| 12 | + const booleanStringTransform = (val: string) => val === 'true'; |
| 13 | + |
| 14 | + // Create a schema that matches the one in index.ts |
| 15 | + const booleanSchema = z |
| 16 | + .union([z.boolean(), z.string().transform(booleanStringTransform)]) |
| 17 | + .nullable() |
| 18 | + .optional(); |
| 19 | + |
| 20 | + describe('direct transform function', () => { |
| 21 | + it('should transform "true" to true', () => { |
| 22 | + expect(booleanStringTransform('true')).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should transform anything else to false', () => { |
| 26 | + expect(booleanStringTransform('false')).toBe(false); |
| 27 | + expect(booleanStringTransform('True')).toBe(false); |
| 28 | + expect(booleanStringTransform('1')).toBe(false); |
| 29 | + expect(booleanStringTransform('')).toBe(false); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('zod schema with boolean transform', () => { |
| 34 | + it('should accept and pass through boolean values', () => { |
| 35 | + expect(booleanSchema.parse(true)).toBe(true); |
| 36 | + expect(booleanSchema.parse(false)).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should transform string "true" to boolean true', () => { |
| 40 | + expect(booleanSchema.parse('true')).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should transform other string values to boolean false', () => { |
| 44 | + expect(booleanSchema.parse('false')).toBe(false); |
| 45 | + expect(booleanSchema.parse('1')).toBe(false); |
| 46 | + expect(booleanSchema.parse('')).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should pass through null and undefined', () => { |
| 50 | + expect(booleanSchema.parse(null)).toBeNull(); |
| 51 | + expect(booleanSchema.parse(undefined)).toBeUndefined(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + // Test multiple boolean schema transformations in the same schema |
| 56 | + describe('multiple boolean transforms in schema', () => { |
| 57 | + // Create a schema with multiple boolean transforms |
| 58 | + const complexSchema = z.object({ |
| 59 | + resolved: z |
| 60 | + .union([z.boolean(), z.string().transform(booleanStringTransform)]) |
| 61 | + .nullable() |
| 62 | + .optional(), |
| 63 | + on_component_only: z |
| 64 | + .union([z.boolean(), z.string().transform(booleanStringTransform)]) |
| 65 | + .nullable() |
| 66 | + .optional(), |
| 67 | + since_leak_period: z |
| 68 | + .union([z.boolean(), z.string().transform(booleanStringTransform)]) |
| 69 | + .nullable() |
| 70 | + .optional(), |
| 71 | + in_new_code_period: z |
| 72 | + .union([z.boolean(), z.string().transform(booleanStringTransform)]) |
| 73 | + .nullable() |
| 74 | + .optional(), |
| 75 | + }); |
| 76 | + |
| 77 | + it('should transform multiple boolean string values', () => { |
| 78 | + const result = complexSchema.parse({ |
| 79 | + resolved: 'true', |
| 80 | + on_component_only: 'false', |
| 81 | + since_leak_period: true, |
| 82 | + in_new_code_period: 'true', |
| 83 | + }); |
| 84 | + |
| 85 | + expect(result).toEqual({ |
| 86 | + resolved: true, |
| 87 | + on_component_only: false, |
| 88 | + since_leak_period: true, |
| 89 | + in_new_code_period: true, |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should handle mix of boolean, string, null and undefined values', () => { |
| 94 | + const result = complexSchema.parse({ |
| 95 | + resolved: true, |
| 96 | + on_component_only: 'true', |
| 97 | + since_leak_period: null, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(result).toEqual({ |
| 101 | + resolved: true, |
| 102 | + on_component_only: true, |
| 103 | + since_leak_period: null, |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments