UNPKG

@craftapit/tester

Version:

A focused, LLM-powered testing framework for natural language test scenarios

169 lines (168 loc) 7.63 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = init; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const inquirer_1 = __importDefault(require("inquirer")); const chalk_1 = __importDefault(require("chalk")); const ora_1 = __importDefault(require("ora")); const logger_1 = require("../../utils/logger"); const templates_1 = require("../utils/templates"); async function init(options) { const logger = new logger_1.Logger('CLI'); try { // Ask for project details if not provided const answers = await inquirer_1.default.prompt([ { type: 'input', name: 'directory', message: 'Where would you like to initialize the project?', default: options.directory || '.' }, { type: 'list', name: 'testType', message: 'What type of tests are you primarily going to write?', choices: ['API', 'UI', 'Database', 'Mixed'], default: 'API' }, { type: 'list', name: 'llmProvider', message: 'Which LLM provider would you like to use?', choices: ['openai', 'anthropic', 'custom'], default: 'openai' }, { type: 'input', name: 'apiKey', message: 'Enter your API key (or leave blank to use environment variables):', default: '' }, { type: 'confirm', name: 'createExamples', message: 'Would you like to create example test scenarios?', default: true }, { type: 'confirm', name: 'integrateCraftACoder', message: 'Would you like to integrate with CraftACoder for code generation?', default: true } ]); const projectDir = path.resolve(answers.directory); const spinner = (0, ora_1.default)(`Initializing project in ${projectDir}...`).start(); // Create directory structure await fs.mkdir(path.join(projectDir, 'tests'), { recursive: true }); await fs.mkdir(path.join(projectDir, 'tests', 'scenarios'), { recursive: true }); await fs.mkdir(path.join(projectDir, 'tests', 'results'), { recursive: true }); // Create config file const configContent = JSON.stringify({ browser: answers.testType === 'UI' || answers.testType === 'Mixed' ? { headless: false, slowMo: 50 } : undefined, api: answers.testType === 'API' || answers.testType === 'Mixed' ? { baseUrl: 'https://api.example.com' } : undefined, database: answers.testType === 'Database' || answers.testType === 'Mixed' ? { client: 'sqlite', connection: { filename: ':memory:' } } : undefined, llm: { provider: answers.llmProvider, model: answers.llmProvider === 'openai' ? 'gpt-4' : 'claude-2', apiKey: answers.apiKey || undefined }, logging: { level: 'info', screenshots: answers.testType === 'UI' || answers.testType === 'Mixed', screenshotsPath: './tests/results/screenshots' }, integrations: { craftacoder: answers.integrateCraftACoder ? { enabled: true, cliPath: 'craftacoder' } : undefined } }, null, 2); await fs.writeFile(path.join(projectDir, 'craft-a-tester.json'), configContent); // Create example scenarios if requested if (answers.createExamples) { spinner.text = 'Creating example scenarios...'; if (answers.testType === 'UI' || answers.testType === 'Mixed') { const uiExample = await (0, templates_1.getTemplateContent)('ui-example'); await fs.writeFile(path.join(projectDir, 'tests', 'scenarios', 'login-test.md'), uiExample); } if (answers.testType === 'API' || answers.testType === 'Mixed') { const apiExample = await (0, templates_1.getTemplateContent)('api-example'); await fs.writeFile(path.join(projectDir, 'tests', 'scenarios', 'api-test.md'), apiExample); } if (answers.testType === 'Database' || answers.testType === 'Mixed') { const dbExample = await (0, templates_1.getTemplateContent)('db-example'); await fs.writeFile(path.join(projectDir, 'tests', 'scenarios', 'db-test.md'), dbExample); } } spinner.succeed('Project initialized successfully!'); // Print next steps console.log('\n' + chalk_1.default.bold('Next steps:')); console.log('1. ' + chalk_1.default.cyan(`cd ${options.directory !== '.' ? options.directory : ''}`)); if (answers.testType === 'API' || answers.testType === 'Mixed') { console.log('2. ' + chalk_1.default.cyan('craft-a-tester run ./tests/scenarios/api-test.md')); } else { console.log('2. ' + chalk_1.default.cyan('craft-a-tester run ./tests/scenarios/login-test.md')); } if (answers.integrateCraftACoder) { console.log('\nTo use CraftACoder for code generation:'); console.log(chalk_1.default.cyan('craft-a-tester run ./tests/scenarios/api-test.md --generate-code')); } console.log('\nFor more information, see the documentation:'); console.log(chalk_1.default.blue('https://github.com/craftapit/craft-a-tester')); } catch (error) { logger.error('Error initializing project:', error); console.error(chalk_1.default.red('Error initializing project:'), error); process.exit(1); } }