scai
Version:
> AI-powered CLI tools for smart commit messages, auto generated comments, and readme files — all powered by local models.
46 lines (43 loc) • 1.67 kB
JavaScript
import fs from 'fs/promises';
import path from 'path';
import { ModelConfig } from '../../config/ModelConfig.js';
export const generateTestsModule = {
name: 'generateTests',
description: 'Generate a Jest test file for the class/module',
async run({ code, filepath }) {
const model = ModelConfig.getModel();
const lang = ModelConfig.getLanguage();
const prompt = `
You are a senior ${lang.toUpperCase()} engineer. Given the following class or module, generate a Jest test file.
Guidelines:
- Use the 'jest' test framework
- Cover public methods and one edge case
- Name the file <original>.test.ts
- Only return valid TypeScript code
--- CODE START ---
${code}
--- CODE END ---
`.trim();
const res = await fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model,
prompt,
stream: false
})
});
const data = await res.json();
const testCode = data.response?.trim();
if (!testCode)
throw new Error('⚠️ No test code returned from model');
// Determine test file path next to refactored file
if (!filepath)
throw new Error('Missing filepath in pipeline context');
const { dir, name } = path.parse(filepath);
const testPath = path.join(dir, `${name}.test.ts`);
await fs.writeFile(testPath, testCode, 'utf-8');
console.log(`✅ Test file saved to: ${testPath}`);
return { code, filepath }; // unchanged input
}
};