import fs from 'fs';
import path from 'path';
import { ComponentInfo } from '../crawler/components';
import { enrichComponentTests } from '../ai/enrichComponentTests';
export interface TestGeneratorOptions {
outputDir: string;
aiAgent?: 'none' | 'copilot' | 'openai' | 'custom';
dryRun?: boolean;
}
export async function generateComponentTests(
components: ComponentInfo[],
options: TestGeneratorOptions,
) {
if (!fs.existsSync(options.outputDir)) {
fs.mkdirSync(options.outputDir, { recursive: true });
}
for (const comp of components) {
const testFile = path.join(options.outputDir, `${comp.name}.test.tsx`);
const aiExtra = await enrichComponentTests(comp, options.aiAgent || 'none');
const testContent = `
import React from "react";
import { render, screen } from "@testing-library/react";
import ${comp.name} from "${comp.filePath.replace(/\\/g, '/')}";
describe("${comp.name}", () => {
it("renders without crashing", () => {
render(<${comp.name} />);
expect(screen.getByTestId("${comp.name.toLowerCase()}")).toBeInTheDocument();
});
${
comp.props.length > 0
? `it("accepts props correctly", () => {
render(<${comp.name} ${comp.props.map((p) => `${p}: "test"`).join(' ')} />);
});`
: ''
}
${aiExtra}
});
`;
if (options.dryRun) {
console.log(`📝 [DryRun] Would generate test: ${testFile}`);
} else {
fs.writeFileSync(testFile, testContent, 'utf-8');
console.log(`âś… Generated test: ${testFile}`);
}
}
}