All files parser.ts

100% Statements 23/23
100% Branches 12/12
100% Functions 3/3
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 652x 2x           2x 8x 8x 7x   7x 1x         6x 1x     5x 7x 1x     6x 1x     5x                     3x           5x             2x 3x 1x     2x 1x      
import * as yaml from "js-yaml";
import * as fs from "fs";
import { TestSuite, TestCase } from "./types";
 
/**
 * Parse YAML test file into TestSuite
 */
export function parseTestFile(filePath: string): TestSuite {
  try {
    const fileContents = fs.readFileSync(filePath, "utf8");
    const parsed = yaml.load(fileContents) as any;
 
    if (!parsed.suite || typeof parsed.suite !== "string") {
      throw new Error(
        'YAML file must have a "suite" field with the suite name'
      );
    }
 
    if (!parsed.tests || !Array.isArray(parsed.tests)) {
      throw new Error('YAML file must have a "tests" array');
    }
 
    const tests: TestCase[] = parsed.tests.map((test: any, index: number) => {
      if (!test.name || typeof test.name !== "string") {
        throw new Error(`Test at index ${index} must have a "name" field`);
      }
 
      if (test.input === undefined) {
        throw new Error(`Test "${test.name}" must have an "input" field`);
      }
 
      return {
        name: test.name,
        description: test.description,
        input: test.input,
        expectedBehavior: test.expectedBehavior,
        exampleResponses: test.exampleResponses,
        timeout: test.timeout,
        metadata: test.metadata,
      };
    });
 
    return {
      suite: parsed.suite,
      description: parsed.description,
      tests,
    };
  } catch (error: any) {
    throw new Error(`Failed to parse test file ${filePath}: ${error.message}`);
  }
}
 
/**
 * Validate a test case
 */
export function validateTestCase(testCase: TestCase): void {
  if (!testCase.name) {
    throw new Error("Test case must have a name");
  }
 
  if (testCase.input === undefined) {
    throw new Error(`Test case "${testCase.name}" must have an input`);
  }
}