|
| 1 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { PluginConfigValidator } from './plugin-config-validator.js'; |
| 4 | +import { createLogger } from '../logger.js'; |
| 5 | +import type { PluginMetadata } from '../plugin-loader.js'; |
| 6 | + |
| 7 | +describe('PluginConfigValidator', () => { |
| 8 | + let validator: PluginConfigValidator; |
| 9 | + let logger: ReturnType<typeof createLogger>; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + logger = createLogger({ level: 'error' }); |
| 13 | + validator = new PluginConfigValidator(logger); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('validatePluginConfig', () => { |
| 17 | + it('should validate valid configuration', () => { |
| 18 | + const configSchema = z.object({ |
| 19 | + port: z.number().min(1000).max(65535), |
| 20 | + host: z.string(), |
| 21 | + debug: z.boolean().default(false), |
| 22 | + }); |
| 23 | + |
| 24 | + const plugin: PluginMetadata = { |
| 25 | + name: 'com.test.plugin', |
| 26 | + version: '1.0.0', |
| 27 | + configSchema, |
| 28 | + init: async () => {}, |
| 29 | + }; |
| 30 | + |
| 31 | + const config = { |
| 32 | + port: 3000, |
| 33 | + host: 'localhost', |
| 34 | + debug: true, |
| 35 | + }; |
| 36 | + |
| 37 | + const validatedConfig = validator.validatePluginConfig(plugin, config); |
| 38 | + |
| 39 | + expect(validatedConfig).toEqual(config); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should apply defaults for missing optional fields', () => { |
| 43 | + const configSchema = z.object({ |
| 44 | + port: z.number().default(3000), |
| 45 | + host: z.string().default('localhost'), |
| 46 | + debug: z.boolean().default(false), |
| 47 | + }); |
| 48 | + |
| 49 | + const plugin: PluginMetadata = { |
| 50 | + name: 'com.test.plugin', |
| 51 | + version: '1.0.0', |
| 52 | + configSchema, |
| 53 | + init: async () => {}, |
| 54 | + }; |
| 55 | + |
| 56 | + const config = { |
| 57 | + port: 8080, |
| 58 | + }; |
| 59 | + |
| 60 | + const validatedConfig = validator.validatePluginConfig(plugin, config); |
| 61 | + |
| 62 | + expect(validatedConfig).toEqual({ |
| 63 | + port: 8080, |
| 64 | + host: 'localhost', |
| 65 | + debug: false, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should throw error for invalid configuration', () => { |
| 70 | + const configSchema = z.object({ |
| 71 | + port: z.number().min(1000).max(65535), |
| 72 | + host: z.string(), |
| 73 | + }); |
| 74 | + |
| 75 | + const plugin: PluginMetadata = { |
| 76 | + name: 'com.test.plugin', |
| 77 | + version: '1.0.0', |
| 78 | + configSchema, |
| 79 | + init: async () => {}, |
| 80 | + }; |
| 81 | + |
| 82 | + const config = { |
| 83 | + port: 100, // Invalid: < 1000 |
| 84 | + host: 'localhost', |
| 85 | + }; |
| 86 | + |
| 87 | + expect(() => validator.validatePluginConfig(plugin, config)).toThrow(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should provide detailed error messages', () => { |
| 91 | + const configSchema = z.object({ |
| 92 | + port: z.number().min(1000), |
| 93 | + host: z.string().min(1), |
| 94 | + }); |
| 95 | + |
| 96 | + const plugin: PluginMetadata = { |
| 97 | + name: 'com.test.plugin', |
| 98 | + version: '1.0.0', |
| 99 | + configSchema, |
| 100 | + init: async () => {}, |
| 101 | + }; |
| 102 | + |
| 103 | + const config = { |
| 104 | + port: 100, |
| 105 | + host: '', |
| 106 | + }; |
| 107 | + |
| 108 | + try { |
| 109 | + validator.validatePluginConfig(plugin, config); |
| 110 | + expect.fail('Should have thrown validation error'); |
| 111 | + } catch (error) { |
| 112 | + const errorMessage = (error as Error).message; |
| 113 | + expect(errorMessage).toContain('com.test.plugin'); |
| 114 | + expect(errorMessage).toContain('port'); |
| 115 | + expect(errorMessage).toContain('host'); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + it('should skip validation when no schema is provided', () => { |
| 120 | + const plugin: PluginMetadata = { |
| 121 | + name: 'com.test.plugin', |
| 122 | + version: '1.0.0', |
| 123 | + init: async () => {}, |
| 124 | + }; |
| 125 | + |
| 126 | + const config = { anything: 'goes' }; |
| 127 | + |
| 128 | + const validatedConfig = validator.validatePluginConfig(plugin, config); |
| 129 | + |
| 130 | + expect(validatedConfig).toEqual(config); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('validatePartialConfig', () => { |
| 135 | + it('should validate partial configuration', () => { |
| 136 | + const configSchema = z.object({ |
| 137 | + port: z.number().min(1000), |
| 138 | + host: z.string(), |
| 139 | + debug: z.boolean(), |
| 140 | + }); |
| 141 | + |
| 142 | + const plugin: PluginMetadata = { |
| 143 | + name: 'com.test.plugin', |
| 144 | + version: '1.0.0', |
| 145 | + configSchema, |
| 146 | + init: async () => {}, |
| 147 | + }; |
| 148 | + |
| 149 | + const partialConfig = { |
| 150 | + port: 8080, |
| 151 | + }; |
| 152 | + |
| 153 | + const validatedConfig = validator.validatePartialConfig(plugin, partialConfig); |
| 154 | + |
| 155 | + expect(validatedConfig).toEqual({ port: 8080 }); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('getDefaultConfig', () => { |
| 160 | + it('should extract default configuration', () => { |
| 161 | + const configSchema = z.object({ |
| 162 | + port: z.number().default(3000), |
| 163 | + host: z.string().default('localhost'), |
| 164 | + debug: z.boolean().default(false), |
| 165 | + }); |
| 166 | + |
| 167 | + const plugin: PluginMetadata = { |
| 168 | + name: 'com.test.plugin', |
| 169 | + version: '1.0.0', |
| 170 | + configSchema, |
| 171 | + init: async () => {}, |
| 172 | + }; |
| 173 | + |
| 174 | + const defaults = validator.getDefaultConfig(plugin); |
| 175 | + |
| 176 | + expect(defaults).toEqual({ |
| 177 | + port: 3000, |
| 178 | + host: 'localhost', |
| 179 | + debug: false, |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should return undefined when schema requires fields', () => { |
| 184 | + const configSchema = z.object({ |
| 185 | + port: z.number(), |
| 186 | + host: z.string(), |
| 187 | + }); |
| 188 | + |
| 189 | + const plugin: PluginMetadata = { |
| 190 | + name: 'com.test.plugin', |
| 191 | + version: '1.0.0', |
| 192 | + configSchema, |
| 193 | + init: async () => {}, |
| 194 | + }; |
| 195 | + |
| 196 | + const defaults = validator.getDefaultConfig(plugin); |
| 197 | + |
| 198 | + expect(defaults).toBeUndefined(); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + describe('isConfigValid', () => { |
| 203 | + it('should return true for valid config', () => { |
| 204 | + const configSchema = z.object({ |
| 205 | + port: z.number(), |
| 206 | + }); |
| 207 | + |
| 208 | + const plugin: PluginMetadata = { |
| 209 | + name: 'com.test.plugin', |
| 210 | + version: '1.0.0', |
| 211 | + configSchema, |
| 212 | + init: async () => {}, |
| 213 | + }; |
| 214 | + |
| 215 | + const isValid = validator.isConfigValid(plugin, { port: 3000 }); |
| 216 | + |
| 217 | + expect(isValid).toBe(true); |
| 218 | + }); |
| 219 | + |
| 220 | + it('should return false for invalid config', () => { |
| 221 | + const configSchema = z.object({ |
| 222 | + port: z.number(), |
| 223 | + }); |
| 224 | + |
| 225 | + const plugin: PluginMetadata = { |
| 226 | + name: 'com.test.plugin', |
| 227 | + version: '1.0.0', |
| 228 | + configSchema, |
| 229 | + init: async () => {}, |
| 230 | + }; |
| 231 | + |
| 232 | + const isValid = validator.isConfigValid(plugin, { port: 'invalid' }); |
| 233 | + |
| 234 | + expect(isValid).toBe(false); |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + describe('getConfigErrors', () => { |
| 239 | + it('should return errors for invalid config', () => { |
| 240 | + const configSchema = z.object({ |
| 241 | + port: z.number().min(1000), |
| 242 | + host: z.string().min(1), |
| 243 | + }); |
| 244 | + |
| 245 | + const plugin: PluginMetadata = { |
| 246 | + name: 'com.test.plugin', |
| 247 | + version: '1.0.0', |
| 248 | + configSchema, |
| 249 | + init: async () => {}, |
| 250 | + }; |
| 251 | + |
| 252 | + const errors = validator.getConfigErrors(plugin, { port: 100, host: '' }); |
| 253 | + |
| 254 | + expect(errors).toHaveLength(2); |
| 255 | + expect(errors[0].path).toBe('port'); |
| 256 | + expect(errors[1].path).toBe('host'); |
| 257 | + }); |
| 258 | + |
| 259 | + it('should return empty array for valid config', () => { |
| 260 | + const configSchema = z.object({ |
| 261 | + port: z.number(), |
| 262 | + }); |
| 263 | + |
| 264 | + const plugin: PluginMetadata = { |
| 265 | + name: 'com.test.plugin', |
| 266 | + version: '1.0.0', |
| 267 | + configSchema, |
| 268 | + init: async () => {}, |
| 269 | + }; |
| 270 | + |
| 271 | + const errors = validator.getConfigErrors(plugin, { port: 3000 }); |
| 272 | + |
| 273 | + expect(errors).toEqual([]); |
| 274 | + }); |
| 275 | + }); |
| 276 | +}); |
0 commit comments