|
| 1 | +/** |
| 2 | + * SkillLinter Test Suite |
| 3 | + * |
| 4 | + * Tests the main linter orchestrator that coordinates validators and collects results. |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 8 | +import { SkillLinter } from '../../src/core/linter.js'; |
| 9 | +import { createMockConfig, createMockSkill } from '../helpers/test-fixtures.js'; |
| 10 | +import type { LintConfig } from '../../src/types/index.js'; |
| 11 | + |
| 12 | +describe('SkillLinter', () => { |
| 13 | + let mockConfig: LintConfig; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + mockConfig = createMockConfig({ |
| 17 | + scenarios: { |
| 18 | + structure: true, |
| 19 | + size: true, |
| 20 | + references: true, |
| 21 | + links: { enabled: true, checkExternal: false }, |
| 22 | + keywords: false, |
| 23 | + harness: false, |
| 24 | + } |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('Constructor', () => { |
| 29 | + it('should create linter with all enabled validators', () => { |
| 30 | + const linter = new SkillLinter(mockConfig); |
| 31 | + expect(linter).toBeDefined(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should create linter with no validators when all disabled', () => { |
| 35 | + const config = createMockConfig({ |
| 36 | + scenarios: { |
| 37 | + structure: false, |
| 38 | + size: false, |
| 39 | + references: false, |
| 40 | + links: { enabled: false, checkExternal: false }, |
| 41 | + keywords: false, |
| 42 | + harness: false, |
| 43 | + } |
| 44 | + }); |
| 45 | + const linter = new SkillLinter(config); |
| 46 | + expect(linter).toBeDefined(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should create linter with only structure validator', () => { |
| 50 | + const config = createMockConfig({ |
| 51 | + scenarios: { |
| 52 | + structure: true, |
| 53 | + size: false, |
| 54 | + references: false, |
| 55 | + links: { enabled: false, checkExternal: false }, |
| 56 | + keywords: false, |
| 57 | + harness: false, |
| 58 | + } |
| 59 | + }); |
| 60 | + const linter = new SkillLinter(config); |
| 61 | + expect(linter).toBeDefined(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('lintSkill', () => { |
| 66 | + it('should lint a valid skill successfully', async () => { |
| 67 | + const linter = new SkillLinter(mockConfig); |
| 68 | + const skill = createMockSkill({ |
| 69 | + content: '# Test Skill\n\nValid content here.', |
| 70 | + }); |
| 71 | + |
| 72 | + const result = await linter.lintSkill(skill, mockConfig); |
| 73 | + |
| 74 | + expect(result).toBeDefined(); |
| 75 | + expect(result.skill).toBe('test-skill'); |
| 76 | + expect(result.skillPath).toBe(skill.path); |
| 77 | + expect(result.results).toBeDefined(); |
| 78 | + expect(Array.isArray(result.results)).toBe(true); |
| 79 | + expect(result.duration).toBeGreaterThan(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should validate input parameters', async () => { |
| 83 | + const linter = new SkillLinter(mockConfig); |
| 84 | + |
| 85 | + // Invalid skill |
| 86 | + await expect( |
| 87 | + linter.lintSkill(null as any, mockConfig) |
| 88 | + ).rejects.toThrow('Invalid skill'); |
| 89 | + |
| 90 | + // Missing path |
| 91 | + await expect( |
| 92 | + linter.lintSkill({ content: 'test' } as any, mockConfig) |
| 93 | + ).rejects.toThrow('Invalid skill: missing or invalid path property'); |
| 94 | + |
| 95 | + // Missing content |
| 96 | + await expect( |
| 97 | + linter.lintSkill({ path: '/test' } as any, mockConfig) |
| 98 | + ).rejects.toThrow('Invalid skill: missing or invalid content property'); |
| 99 | + |
| 100 | + // Invalid config |
| 101 | + await expect( |
| 102 | + linter.lintSkill(createMockSkill(), null as any) |
| 103 | + ).rejects.toThrow('Invalid configuration'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle empty skill content', async () => { |
| 107 | + const linter = new SkillLinter(mockConfig); |
| 108 | + const skill = createMockSkill({ content: ' ' }); // Single space to pass validation |
| 109 | + |
| 110 | + const result = await linter.lintSkill(skill, mockConfig); |
| 111 | + |
| 112 | + expect(result).toBeDefined(); |
| 113 | + // Result may pass or fail depending on validators |
| 114 | + }); |
| 115 | + |
| 116 | + it('should collect results from all enabled validators', async () => { |
| 117 | + const config = createMockConfig({ |
| 118 | + scenarios: { |
| 119 | + structure: true, |
| 120 | + size: true, |
| 121 | + references: true, |
| 122 | + links: { enabled: true, checkExternal: false }, |
| 123 | + keywords: false, |
| 124 | + harness: false, |
| 125 | + } |
| 126 | + }); |
| 127 | + const linter = new SkillLinter(config); |
| 128 | + const skill = createMockSkill({ |
| 129 | + content: '# Test\n\nSome content.', |
| 130 | + }); |
| 131 | + |
| 132 | + const result = await linter.lintSkill(skill, config); |
| 133 | + |
| 134 | + // Should have results from structure, size, references, links |
| 135 | + expect(result.results.length).toBeGreaterThanOrEqual(4); |
| 136 | + const validatorNames = result.results.map(r => r.validator); |
| 137 | + expect(validatorNames).toContain('structure'); |
| 138 | + expect(validatorNames).toContain('size'); |
| 139 | + expect(validatorNames).toContain('references'); |
| 140 | + expect(validatorNames).toContain('links'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should collect no results when all validators disabled', async () => { |
| 144 | + const config = createMockConfig({ |
| 145 | + scenarios: { |
| 146 | + structure: false, |
| 147 | + size: false, |
| 148 | + references: false, |
| 149 | + links: { enabled: false, checkExternal: false }, |
| 150 | + keywords: false, |
| 151 | + harness: false, |
| 152 | + } |
| 153 | + }); |
| 154 | + const linter = new SkillLinter(config); |
| 155 | + const skill = createMockSkill(); |
| 156 | + |
| 157 | + const result = await linter.lintSkill(skill, config); |
| 158 | + |
| 159 | + expect(result.results).toHaveLength(0); |
| 160 | + expect(result.passed).toBe(true); // No validators = no failures |
| 161 | + }); |
| 162 | + |
| 163 | + it('should report pass when no errors found', async () => { |
| 164 | + const config = createMockConfig({ |
| 165 | + scenarios: { |
| 166 | + structure: false, |
| 167 | + size: true, |
| 168 | + references: false, |
| 169 | + links: { enabled: false, checkExternal: false }, |
| 170 | + keywords: false, |
| 171 | + harness: false, |
| 172 | + } |
| 173 | + }); |
| 174 | + const linter = new SkillLinter(config); |
| 175 | + const skill = createMockSkill({ |
| 176 | + content: '# Test\n\n' + 'x'.repeat(100), // Small, valid content |
| 177 | + }); |
| 178 | + |
| 179 | + const result = await linter.lintSkill(skill, config); |
| 180 | + |
| 181 | + expect(result.passed).toBe(true); |
| 182 | + expect(result.summary.errors).toBe(0); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should report failure when errors found', async () => { |
| 186 | + const config = createMockConfig({ |
| 187 | + scenarios: { |
| 188 | + structure: false, |
| 189 | + size: true, |
| 190 | + references: false, |
| 191 | + links: { enabled: false, checkExternal: false }, |
| 192 | + keywords: false, |
| 193 | + harness: false, |
| 194 | + }, |
| 195 | + thresholds: { |
| 196 | + size: { |
| 197 | + maxLines: 1, |
| 198 | + maxTokens: 10, |
| 199 | + } |
| 200 | + } |
| 201 | + }); |
| 202 | + const linter = new SkillLinter(config); |
| 203 | + const skill = createMockSkill({ |
| 204 | + content: 'x'.repeat(1000), // Way too large |
| 205 | + }); |
| 206 | + |
| 207 | + const result = await linter.lintSkill(skill, config); |
| 208 | + |
| 209 | + expect(result.passed).toBe(false); |
| 210 | + expect(result.summary.errors).toBeGreaterThan(0); |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | + describe('Error Handling', () => { |
| 215 | + it('should handle validator errors gracefully', async () => { |
| 216 | + const linter = new SkillLinter(mockConfig); |
| 217 | + const skill = createMockSkill({ |
| 218 | + content: '# Test\n\nValid content.', |
| 219 | + }); |
| 220 | + |
| 221 | + // Should not throw even if a validator has issues |
| 222 | + const result = await linter.lintSkill(skill, mockConfig); |
| 223 | + expect(result).toBeDefined(); |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + describe('Parallel Execution', () => { |
| 228 | + it('should run validators in parallel when configured', async () => { |
| 229 | + const config = createMockConfig({ |
| 230 | + scenarios: { |
| 231 | + structure: true, |
| 232 | + size: true, |
| 233 | + references: true, |
| 234 | + links: { enabled: false, checkExternal: false }, |
| 235 | + keywords: false, |
| 236 | + harness: false, |
| 237 | + }, |
| 238 | + execution: { |
| 239 | + timeout: 60000, |
| 240 | + maxRetries: 2, |
| 241 | + parallel: true, |
| 242 | + maxConcurrency: 2, |
| 243 | + } |
| 244 | + }); |
| 245 | + const linter = new SkillLinter(config); |
| 246 | + const skill = createMockSkill({ |
| 247 | + content: '# Test\n\nSome content.', |
| 248 | + }); |
| 249 | + |
| 250 | + const result = await linter.lintSkill(skill, config); |
| 251 | + |
| 252 | + expect(result).toBeDefined(); |
| 253 | + expect(result.passed).toBeDefined(); |
| 254 | + expect(result.results.length).toBeGreaterThan(0); |
| 255 | + }); |
| 256 | + |
| 257 | + it('should run validators sequentially when parallel is false', async () => { |
| 258 | + const config = createMockConfig({ |
| 259 | + scenarios: { |
| 260 | + structure: true, |
| 261 | + size: true, |
| 262 | + references: false, |
| 263 | + links: { enabled: false, checkExternal: false }, |
| 264 | + keywords: false, |
| 265 | + harness: false, |
| 266 | + }, |
| 267 | + execution: { |
| 268 | + timeout: 60000, |
| 269 | + maxRetries: 2, |
| 270 | + parallel: false, |
| 271 | + } |
| 272 | + }); |
| 273 | + const linter = new SkillLinter(config); |
| 274 | + const skill = createMockSkill({ |
| 275 | + content: '# Test\n\nSome content.', |
| 276 | + }); |
| 277 | + |
| 278 | + const result = await linter.lintSkill(skill, config); |
| 279 | + |
| 280 | + expect(result).toBeDefined(); |
| 281 | + expect(result.passed).toBeDefined(); |
| 282 | + }); |
| 283 | + }); |
| 284 | + |
| 285 | + describe('lint() method', () => { |
| 286 | + it('should validate skill path parameter', async () => { |
| 287 | + const linter = new SkillLinter(mockConfig); |
| 288 | + |
| 289 | + await expect( |
| 290 | + linter.lint('', mockConfig) |
| 291 | + ).rejects.toThrow('Invalid skill path'); |
| 292 | + |
| 293 | + await expect( |
| 294 | + linter.lint(null as any, mockConfig) |
| 295 | + ).rejects.toThrow('Invalid skill path'); |
| 296 | + }); |
| 297 | + |
| 298 | + it('should validate config parameter', async () => { |
| 299 | + const linter = new SkillLinter(mockConfig); |
| 300 | + |
| 301 | + await expect( |
| 302 | + linter.lint('/test/path', null as any) |
| 303 | + ).rejects.toThrow('Invalid configuration'); |
| 304 | + |
| 305 | + await expect( |
| 306 | + linter.lint('/test/path', {} as any) |
| 307 | + ).rejects.toThrow('Invalid configuration: missing scenarios object'); |
| 308 | + }); |
| 309 | + }); |
| 310 | +}); |
0 commit comments