PerformanceSettingsForm.test.ts111 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { DatabaseFormSchema } from './PerformanceSettingsForm' |
| 4 | |
| 5 | describe('DatabaseFormSchema', () => { |
| 6 | describe('percent unit', () => { |
| 7 | it('accepts 1 (lower bound)', () => { |
| 8 | const result = DatabaseFormSchema.safeParse({ |
| 9 | DB_MAX_POOL_SIZE: 1, |
| 10 | DB_MAX_POOL_SIZE_UNIT: 'percent', |
| 11 | }) |
| 12 | expect(result.success).toBe(true) |
| 13 | }) |
| 14 | |
| 15 | it('accepts 100 (upper bound)', () => { |
| 16 | const result = DatabaseFormSchema.safeParse({ |
| 17 | DB_MAX_POOL_SIZE: 100, |
| 18 | DB_MAX_POOL_SIZE_UNIT: 'percent', |
| 19 | }) |
| 20 | expect(result.success).toBe(true) |
| 21 | }) |
| 22 | |
| 23 | it('accepts a mid-range value', () => { |
| 24 | const result = DatabaseFormSchema.safeParse({ |
| 25 | DB_MAX_POOL_SIZE: 50, |
| 26 | DB_MAX_POOL_SIZE_UNIT: 'percent', |
| 27 | }) |
| 28 | expect(result.success).toBe(true) |
| 29 | }) |
| 30 | |
| 31 | it('rejects 0', () => { |
| 32 | const result = DatabaseFormSchema.safeParse({ |
| 33 | DB_MAX_POOL_SIZE: 0, |
| 34 | DB_MAX_POOL_SIZE_UNIT: 'percent', |
| 35 | }) |
| 36 | expect(result.success).toBe(false) |
| 37 | }) |
| 38 | |
| 39 | it('rejects 101', () => { |
| 40 | const result = DatabaseFormSchema.safeParse({ |
| 41 | DB_MAX_POOL_SIZE: 101, |
| 42 | DB_MAX_POOL_SIZE_UNIT: 'percent', |
| 43 | }) |
| 44 | expect(result.success).toBe(false) |
| 45 | if (!result.success) { |
| 46 | const issue = result.error.issues.find((i) => i.path[0] === 'DB_MAX_POOL_SIZE') |
| 47 | expect(issue?.message).toBe('Percentage must be between 1 and 100') |
| 48 | } |
| 49 | }) |
| 50 | |
| 51 | it('rejects negative values', () => { |
| 52 | const result = DatabaseFormSchema.safeParse({ |
| 53 | DB_MAX_POOL_SIZE: -5, |
| 54 | DB_MAX_POOL_SIZE_UNIT: 'percent', |
| 55 | }) |
| 56 | expect(result.success).toBe(false) |
| 57 | }) |
| 58 | |
| 59 | it('coerces string input', () => { |
| 60 | const result = DatabaseFormSchema.safeParse({ |
| 61 | DB_MAX_POOL_SIZE: '75', |
| 62 | DB_MAX_POOL_SIZE_UNIT: 'percent', |
| 63 | }) |
| 64 | expect(result.success).toBe(true) |
| 65 | }) |
| 66 | |
| 67 | it('rejects out-of-range string input', () => { |
| 68 | const result = DatabaseFormSchema.safeParse({ |
| 69 | DB_MAX_POOL_SIZE: '150', |
| 70 | DB_MAX_POOL_SIZE_UNIT: 'percent', |
| 71 | }) |
| 72 | expect(result.success).toBe(false) |
| 73 | }) |
| 74 | }) |
| 75 | |
| 76 | describe('connections unit', () => { |
| 77 | it('accepts 1 (min)', () => { |
| 78 | const result = DatabaseFormSchema.safeParse({ |
| 79 | DB_MAX_POOL_SIZE: 1, |
| 80 | DB_MAX_POOL_SIZE_UNIT: 'connections', |
| 81 | }) |
| 82 | expect(result.success).toBe(true) |
| 83 | }) |
| 84 | |
| 85 | it('accepts values above 100 (no upper schema bound for absolute)', () => { |
| 86 | const result = DatabaseFormSchema.safeParse({ |
| 87 | DB_MAX_POOL_SIZE: 500, |
| 88 | DB_MAX_POOL_SIZE_UNIT: 'connections', |
| 89 | }) |
| 90 | expect(result.success).toBe(true) |
| 91 | }) |
| 92 | |
| 93 | it('rejects 0', () => { |
| 94 | const result = DatabaseFormSchema.safeParse({ |
| 95 | DB_MAX_POOL_SIZE: 0, |
| 96 | DB_MAX_POOL_SIZE_UNIT: 'connections', |
| 97 | }) |
| 98 | expect(result.success).toBe(false) |
| 99 | }) |
| 100 | }) |
| 101 | |
| 102 | describe('unit enum', () => { |
| 103 | it('rejects unknown unit values', () => { |
| 104 | const result = DatabaseFormSchema.safeParse({ |
| 105 | DB_MAX_POOL_SIZE: 10, |
| 106 | DB_MAX_POOL_SIZE_UNIT: 'bytes', |
| 107 | }) |
| 108 | expect(result.success).toBe(false) |
| 109 | }) |
| 110 | }) |
| 111 | }) |