UNPKG

cmte

Version:

Design by Committee™ except it's just you and LLMs

147 lines (126 loc) 4.56 kB
#!/usr/bin/env node import path from 'path'; import { fileURLToPath } from 'url'; import { createContext, TemplateRenderer } from "./index.js"; import { writeFile, ensureDir } from "../utils/fs.js"; import logger from "../utils/logger.js"; // Get the directory name const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const rootDir = path.resolve(__dirname, '../../'); // Sample template for testing const SAMPLE_TEMPLATE = ` # Test Template ## Context Variables - Directive Name: {{directiveName}} - Service Name: {{serviceName}} ## File Collections Here are code files: {{code}} Here are document files: {{documents}} ## Filtered Collections Here are TypeScript files: {{code:*.ts}} `; // Sample LLM response with mustache tags const SAMPLE_LLM_RESPONSE = ` # Generated Content Here's a variable: {{item.name}} And another: {{item.description}} `; // Make sure paths are absolute for testing const makeAbsolute = filePath => { return path.isAbsolute(filePath) ? filePath : path.join(rootDir, filePath); }; // Sample context const sampleContext = { directiveName: 'TestDirective', serviceName: 'TestService' }; /** * Runs a template engine test */ async function runTest() { try { logger.info('Starting template engine test'); // Create test output directory const outputDir = path.join(rootDir, '_dev/test'); await ensureDir(outputDir); // Create test template file const templatePath = path.join(outputDir, 'test-template.md'); await writeFile(templatePath, SAMPLE_TEMPLATE); logger.info(`Created test template at: ${templatePath}`); // Create mock source files for collections if they don't exist const srcDir = path.join(outputDir, 'src'); await ensureDir(path.join(srcDir, 'utils')); await ensureDir(path.join(srcDir, 'templates')); const files = [{ path: path.join(srcDir, 'index.ts'), content: 'export * from "./utils/helpers.js";\n' }, { path: path.join(srcDir, 'utils/helpers.js'), content: 'export function helper() { return true; }\n' }, { path: path.join(srcDir, 'templates/renderer.ts'), content: 'export class Renderer {}\n' }, { path: path.join(outputDir, 'README.md'), content: '# Test Project\n' }, { path: path.join(outputDir, 'docs/usage.md'), content: '# Usage Guide\n' }]; await ensureDir(path.join(outputDir, 'docs')); for (const file of files) { await writeFile(file.path, file.content); logger.debug(`Created mock file: ${file.path}`); } // Define collections directly with absolute paths const collections = { code: [path.join(srcDir, 'index.ts'), path.join(srcDir, 'utils/helpers.js'), path.join(srcDir, 'templates/renderer.ts')], documents: [path.join(outputDir, 'README.md'), path.join(outputDir, 'docs/usage.md')] }; // Create context const context = createContext(); Object.assign(context, sampleContext); // Create template renderer with the output directory as the template dir const renderer = new TemplateRenderer(context, outputDir); // Register collections directly renderer.registerCollections(collections); logger.info('Registered collections', { collectionNames: Object.keys(collections), codeFiles: collections.code, docFiles: collections.documents }); // Test normal template rendering const renderedTemplate = await renderer.renderTemplate('test-template.md'); logger.info('Rendered normal template'); // Test LLM response rendering const renderedLLMResponse = await renderer.render(SAMPLE_LLM_RESPONSE, { isLLMResponse: true }); logger.info('Rendered LLM response'); // Save rendered outputs const outputPath = path.join(outputDir, 'test-output.md'); const llmOutputPath = path.join(outputDir, 'test-llm-output.md'); await writeFile(outputPath, renderedTemplate); await writeFile(llmOutputPath, renderedLLMResponse); logger.info('Template test completed successfully', { outputPath, llmOutputPath, contextKeys: Object.keys(context), collections: Object.keys(collections) }); console.log(`Test completed. Output saved to: ${outputPath} and ${llmOutputPath}`); } catch (error) { logger.error('Template test failed', { error }); console.error('Test failed:', error.message); } } // Run the test if this file is executed directly if (process.argv[1] === fileURLToPath(import.meta.url)) { runTest(); }