|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'bun:test'; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +import { validateFiles } from '../../../src/commands/validate/validate-files.js'; |
| 7 | + |
| 8 | +describe('validateFiles TypeScript eval configs', () => { |
| 9 | + let tempDir: string; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + tempDir = mkdtempSync(path.join(tmpdir(), 'agentv-validate-ts-config-')); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + rmSync(tempDir, { recursive: true, force: true }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('validates *.eval.ts through the core loader', async () => { |
| 20 | + const configFile = path.join(tempDir, 'suite.eval.ts'); |
| 21 | + writeFileSync( |
| 22 | + configFile, |
| 23 | + `export default { |
| 24 | + prompts: ['{{ input }}'], |
| 25 | + tests: [ |
| 26 | + { |
| 27 | + id: 'hello', |
| 28 | + vars: { input: 'Say hello' }, |
| 29 | + assert: [{ type: 'contains', value: 'hello' }], |
| 30 | + }, |
| 31 | + ], |
| 32 | +}; |
| 33 | +`, |
| 34 | + ); |
| 35 | + |
| 36 | + const summary = await validateFiles([configFile]); |
| 37 | + |
| 38 | + expect(summary.totalFiles).toBe(1); |
| 39 | + expect(summary.validFiles).toBe(1); |
| 40 | + expect(summary.results[0].filePath).toBe(path.normalize(configFile)); |
| 41 | + }); |
| 42 | + |
| 43 | + it('reports missing default export errors for TypeScript configs', async () => { |
| 44 | + const configFile = path.join(tempDir, 'suite.eval.ts'); |
| 45 | + writeFileSync(configFile, 'export const config = { tests: [] };\n'); |
| 46 | + |
| 47 | + const summary = await validateFiles([configFile]); |
| 48 | + |
| 49 | + expect(summary.invalidFiles).toBe(1); |
| 50 | + expect(summary.results[0].errors[0].message).toContain('default export'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('reports invalid TypeScript eval contract errors', async () => { |
| 54 | + const configFile = path.join(tempDir, 'suite.eval.mts'); |
| 55 | + writeFileSync( |
| 56 | + configFile, |
| 57 | + `export default { |
| 58 | + prompts: ['{{ input }}'], |
| 59 | + providers: ['openai:gpt-5'], |
| 60 | + tests: [{ id: 'hello', vars: { input: 'Say hello' } }], |
| 61 | +}; |
| 62 | +`, |
| 63 | + ); |
| 64 | + |
| 65 | + const summary = await validateFiles([configFile]); |
| 66 | + |
| 67 | + expect(summary.invalidFiles).toBe(1); |
| 68 | + expect(summary.results[0].errors[0].message).toContain("top-level 'providers'"); |
| 69 | + }); |
| 70 | + |
| 71 | + it('expands directories without treating custom assertion files as eval configs', async () => { |
| 72 | + const assertionsDir = path.join(tempDir, '.agentv', 'assertions'); |
| 73 | + mkdirSync(assertionsDir, { recursive: true }); |
| 74 | + writeFileSync(path.join(assertionsDir, 'custom-check.ts'), 'export default () => true;\n'); |
| 75 | + const configFile = path.join(tempDir, 'suite.eval.ts'); |
| 76 | + writeFileSync( |
| 77 | + configFile, |
| 78 | + `export default { |
| 79 | + prompts: ['{{ input }}'], |
| 80 | + tests: [ |
| 81 | + { |
| 82 | + id: 'hello', |
| 83 | + vars: { input: 'Say hello' }, |
| 84 | + assert: [{ type: 'contains', value: 'hello' }], |
| 85 | + }, |
| 86 | + ], |
| 87 | +}; |
| 88 | +`, |
| 89 | + ); |
| 90 | + |
| 91 | + const summary = await validateFiles([tempDir]); |
| 92 | + |
| 93 | + expect(summary.totalFiles).toBe(1); |
| 94 | + expect(summary.results[0].filePath).toBe(path.normalize(configFile)); |
| 95 | + }); |
| 96 | + |
| 97 | + it('does not validate promptfooconfig.ts as an eval config', async () => { |
| 98 | + const configFile = path.join(tempDir, 'promptfooconfig.ts'); |
| 99 | + writeFileSync(configFile, 'export default { prompts: ["{{ input }}"], tests: [] };\n'); |
| 100 | + |
| 101 | + const summary = await validateFiles([tempDir]); |
| 102 | + |
| 103 | + expect(summary.totalFiles).toBe(0); |
| 104 | + }); |
| 105 | + |
| 106 | + it('does not validate agentvconfig.ts as an eval config', async () => { |
| 107 | + const configFile = path.join(tempDir, 'agentvconfig.ts'); |
| 108 | + writeFileSync(configFile, 'export default { prompts: ["{{ input }}"], tests: [] };\n'); |
| 109 | + |
| 110 | + const summary = await validateFiles([tempDir]); |
| 111 | + |
| 112 | + expect(summary.totalFiles).toBe(0); |
| 113 | + }); |
| 114 | +}); |
0 commit comments