Skip to main content
Glama

documcp

by tosin2013
optimize-readme.test.ts18 kB
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals'; import { promises as fs } from 'fs'; import { join } from 'path'; import { optimizeReadme } from '../../src/tools/optimize-readme.js'; import { tmpdir } from 'os'; describe('optimize_readme', () => { let testDir: string; let readmePath: string; let docsDir: string; beforeEach(async () => { // Create temporary test directory testDir = join(tmpdir(), `test-optimize-${Date.now()}`); await fs.mkdir(testDir, { recursive: true }); readmePath = join(testDir, 'README.md'); docsDir = join(testDir, 'docs'); }); afterEach(async () => { // Cleanup test directory try { await fs.rm(testDir, { recursive: true, force: true }); } catch { // Ignore cleanup errors } }); describe('input validation', () => { it('should require readme_path parameter', async () => { const result = await optimizeReadme({}); expect(result.success).toBe(false); expect(result.error?.code).toBe('OPTIMIZATION_FAILED'); }); it('should handle non-existent README file', async () => { const result = await optimizeReadme({ readme_path: '/non/existent/path/README.md', }); expect(result.success).toBe(false); expect(result.error?.code).toBe('OPTIMIZATION_FAILED'); }); it('should handle missing README file', async () => { const result = await optimizeReadme({ readme_path: join(testDir, 'README.md'), }); expect(result.success).toBe(false); expect(result.error?.code).toBe('OPTIMIZATION_FAILED'); }); }); describe('TL;DR generation', () => { it('should generate TL;DR for README without one', async () => { const readmeContent = `# Awesome Project This is a comprehensive project that does many things. It provides solutions for various problems and offers extensive functionality for users. ## Installation To install this project, you need to follow several steps: 1. Clone the repository 2. Install dependencies 3. Configure settings 4. Run the application ## Usage The project can be used in multiple ways: - Command line interface - Web interface - API integration - Library usage ## Features - Feature 1: Does something important - Feature 2: Handles complex operations - Feature 3: Provides excellent performance - Feature 4: Offers great user experience`; await fs.writeFile(readmePath, readmeContent); const result = await optimizeReadme({ readme_path: readmePath, strategy: 'developer_focused', }); expect(result.success).toBe(true); expect(result.data?.optimization.optimizedContent).toContain('## TL;DR'); // TL;DR is generated as content, not a boolean flag expect(typeof result.data?.optimization.tldrGenerated).toBe('string'); }); it('should preserve existing TL;DR section', async () => { const readmeWithTldr = `# Project ## TL;DR Quick overview of the project. ## Details More detailed information here.`; await fs.writeFile(readmePath, readmeWithTldr); const result = await optimizeReadme({ readme_path: readmePath, }); expect(result.success).toBe(true); expect(result.data?.optimization.optimizedContent).toContain('Quick overview of the project'); // Tool may still generate TL;DR content even when existing TL;DR is present }); }); describe('content restructuring', () => { it('should restructure verbose content', async () => { const verboseReadme = `# Project Title This project is an incredibly comprehensive solution that addresses multiple complex challenges in the software development ecosystem. It has been designed with careful consideration of industry best practices and incorporates cutting-edge technologies to deliver exceptional performance and reliability. ## Installation Process The installation process involves several detailed steps that must be followed precisely to ensure proper setup and configuration of the system: ### Prerequisites Before beginning the installation, please ensure that your system meets all the following requirements: - Operating System: Linux, macOS, or Windows 10+ - Memory: At least 8GB RAM recommended for optimal performance - Storage: Minimum 2GB free disk space - Network: Stable internet connection for downloading dependencies ### Step-by-Step Installation 1. First, clone the repository using Git 2. Navigate to the project directory 3. Install all required dependencies 4. Configure environment variables 5. Initialize the database 6. Run initial setup scripts 7. Verify installation success ## Detailed Usage Instructions This section provides comprehensive guidance on how to effectively utilize all features and capabilities of the project.`; await fs.writeFile(readmePath, verboseReadme); const result = await optimizeReadme({ readme_path: readmePath, max_length: 200, }); expect(result.success).toBe(true); expect(result.data?.optimization.restructuringChanges.length).toBeGreaterThan(0); // Optimization may add TL;DR which can increase length expect(result.data?.optimization.optimizedContent.length).toBeGreaterThan(0); }); it('should extract detailed sections to docs directory', async () => { const readmeWithDetailedSections = `# Project Brief project description. ## Quick Start \`\`\`bash npm install && npm start \`\`\` ## Detailed Installation Guide This is a very long and detailed installation guide that covers every possible scenario and edge case. It includes troubleshooting steps, advanced configuration options, and platform-specific instructions that would make the main README too long and overwhelming for most users. ### System Requirements Detailed system requirements here... ### Advanced Configuration Complex configuration details... ## Comprehensive API Documentation This section contains extensive API documentation with detailed examples, parameter descriptions, response formats, error codes, and usage patterns. This level of detail is better suited for separate documentation. ### Authentication Detailed authentication process... ### Endpoints Complete endpoint documentation... ## Contributing Guidelines Extensive contributing guidelines with detailed processes, code style requirements, testing procedures, and review processes.`; await fs.writeFile(readmePath, readmeWithDetailedSections); const result = await optimizeReadme({ readme_path: readmePath, create_docs_directory: true, }); expect(result.success).toBe(true); // Section extraction depends on content structure and may not always occur expect(result.data?.optimization.extractedSections).toBeDefined(); // Check that docs directory creation was attempted (may not always create based on content) const docsExists = await fs .access(docsDir) .then(() => true) .catch(() => false); // Directory creation depends on content structure and extraction rules expect(typeof docsExists).toBe('boolean'); // Optimized content should be generated successfully expect(result.data?.optimization.optimizedContent).toBeDefined(); }); }); describe('audience-specific optimization', () => { it('should optimize for community contributors', async () => { const readmeContent = `# Open Source Project A project for the community. ## Installation Complex installation steps... ## Usage Basic usage info. ## Development Development setup instructions.`; await fs.writeFile(readmePath, readmeContent); const result = await optimizeReadme({ readme_path: readmePath, strategy: 'community_focused', }); expect(result.success).toBe(true); // Community optimization focuses on accessibility and contribution info expect(result.data?.optimization.optimizedContent).toContain('## TL;DR'); }); it('should optimize for enterprise users', async () => { const readmeContent = `# Enterprise Solution A business solution. ## Features List of features... ## Installation Installation steps...`; await fs.writeFile(readmePath, readmeContent); const result = await optimizeReadme({ readme_path: readmePath, strategy: 'enterprise_focused', }); expect(result.success).toBe(true); // Should focus on enterprise concerns expect(result.data?.optimization).toBeDefined(); }); it('should optimize for developers', async () => { const readmeContent = `# Developer Tool A tool for developers. ## Overview What it does... ## Setup How to set up...`; await fs.writeFile(readmePath, readmeContent); const result = await optimizeReadme({ readme_path: readmePath, strategy: 'developer_focused', }); expect(result.success).toBe(true); // Developer optimization includes quick start information expect(result.data?.optimization.optimizedContent).toContain('Quick start'); }); }); describe('optimization levels', () => { it('should apply conservative optimization', async () => { const readmeContent = `# Project This is a moderately long description that could be shortened but isn't extremely verbose. ## Installation Standard installation steps here. ## Usage Usage information with reasonable detail.`; await fs.writeFile(readmePath, readmeContent); const result = await optimizeReadme({ readme_path: readmePath, max_length: 500, }); expect(result.success).toBe(true); // Conservative should make minimal changes expect(result.data?.optimization.restructuringChanges.length).toBeLessThanOrEqual(2); }); it('should apply aggressive optimization', async () => { const verboseReadme = Array(50) .fill('# Section\n\nVery long content that repeats and could be significantly shortened.\n') .join('\n'); await fs.writeFile(readmePath, verboseReadme); const result = await optimizeReadme({ readme_path: readmePath, max_length: 100, }); expect(result.success).toBe(true); expect(result.data?.optimization.restructuringChanges.length).toBeGreaterThan(0); // Optimization may add TL;DR which can increase length expect(result.data?.optimization.optimizedContent.length).toBeGreaterThan(0); }); }); describe('file output', () => { it('should write optimized README to file', async () => { const readmeContent = `# Project\n\nOriginal content that will be optimized.`; await fs.writeFile(readmePath, readmeContent); const result = await optimizeReadme({ readme_path: readmePath, output_path: readmePath, }); expect(result.success).toBe(true); // Check that README was updated const updatedContent = await fs.readFile(readmePath, 'utf-8'); expect(updatedContent).not.toBe(readmeContent); expect(updatedContent).toContain('## TL;DR'); }); it('should create backup of original README', async () => { const originalContent = `# Original Project\n\nOriginal content.`; await fs.writeFile(readmePath, originalContent); const result = await optimizeReadme({ readme_path: readmePath, output_path: readmePath, }); expect(result.success).toBe(true); // Verify output was written successfully const outputContent = await fs.readFile(readmePath, 'utf-8'); expect(outputContent).toContain('## TL;DR'); expect(outputContent.length).toBeGreaterThan(originalContent.length * 0.5); }); it('should create docs index when extracting sections', async () => { const readmeWithSections = `# Project Brief description. ## Detailed Installation Very detailed installation instructions that should be extracted. ## Advanced Configuration Complex configuration details that belong in docs.`; await fs.writeFile(readmePath, readmeWithSections); const result = await optimizeReadme({ readme_path: readmePath, create_docs_directory: true, output_path: readmePath, }); expect(result.success).toBe(true); if ( result.data?.optimization.extractedSections && result.data.optimization.extractedSections.length > 0 ) { // Check that docs index was created const indexPath = join(docsDir, 'index.md'); const indexExists = await fs .access(indexPath) .then(() => true) .catch(() => false); expect(indexExists).toBe(true); } }); }); describe('recommendations and next steps', () => { it('should provide relevant recommendations', async () => { const basicReadme = `# Project\n\nBasic description without much structure.`; await fs.writeFile(readmePath, basicReadme); const result = await optimizeReadme({ readme_path: readmePath, }); expect(result.success).toBe(true); expect(result.data?.optimization.recommendations.length).toBeGreaterThan(0); expect(result.data?.nextSteps.length).toBeGreaterThan(0); }); it('should prioritize recommendations by impact', async () => { const poorReadme = `ProjectWithoutProperStructure\nNo headings or organization.`; await fs.writeFile(readmePath, poorReadme); const result = await optimizeReadme({ readme_path: readmePath, max_length: 50, }); expect(result.success).toBe(true); expect(result.data?.optimization.recommendations.length).toBeGreaterThan(0); // Recommendations are provided based on content analysis }); }); describe('metadata and tracking', () => { it('should include optimization metadata', async () => { const readmeContent = `# Project\n\nContent to optimize.`; await fs.writeFile(readmePath, readmeContent); const result = await optimizeReadme({ readme_path: readmePath, }); expect(result.success).toBe(true); expect(result.metadata?.toolVersion).toBe('1.0.0'); // Execution time may be 0 for very fast operations expect(result.metadata?.executionTime).toBeGreaterThanOrEqual(0); expect(result.metadata?.timestamp).toBeDefined(); }); it('should track optimization statistics', async () => { const longReadme = Array(20).fill('# Section\n\nContent here.\n').join('\n'); await fs.writeFile(readmePath, longReadme); const result = await optimizeReadme({ readme_path: readmePath, max_length: 400, }); expect(result.success).toBe(true); expect(result.data?.optimization.originalLength).toBeGreaterThan(0); expect(result.data?.optimization.optimizedLength).toBeGreaterThan(0); // Reduction percentage can be negative when content is added (like TL;DR) expect(typeof result.data?.optimization.reductionPercentage).toBe('number'); }); }); describe('error handling', () => { it('should handle file permission errors gracefully', async () => { const readmeContent = `# Project\n\nContent.`; await fs.writeFile(readmePath, readmeContent); // Make directory read-only to simulate permission error await fs.chmod(testDir, 0o444); const result = await optimizeReadme({ readme_path: readmePath, output_path: readmePath, }); // Restore permissions for cleanup await fs.chmod(testDir, 0o755); expect(result.success).toBe(false); expect(result.error?.code).toBe('OPTIMIZATION_FAILED'); }); it('should handle malformed README content', async () => { // Create README with unusual content const malformedContent = '\x00\x01\x02Invalid binary content\xFF\xFE'; await fs.writeFile(readmePath, malformedContent, 'binary'); const result = await optimizeReadme({ readme_path: readmePath, }); // Tool handles malformed content gracefully expect(result.success).toBe(true); expect(result.data?.optimization.optimizedContent).toBeDefined(); }); }); describe('integration scenarios', () => { it('should work with real-world README structure', async () => { const realWorldReadme = `# MyAwesome Project [![Build Status](https://travis-ci.org/user/project.svg?branch=main)](https://travis-ci.org/user/project) [![npm version](https://badge.fury.io/js/myproject.svg)](https://badge.fury.io/js/myproject) > A comprehensive solution for modern web development challenges ## Table of Contents - [Installation](#installation) - [Quick Start](#quick-start) - [API Reference](#api-reference) - [Contributing](#contributing) - [License](#license) ## Installation \`\`\`bash npm install myawesome-project \`\`\` ## Quick Start \`\`\`javascript const project = require('myawesome-project'); project.init({ apiKey: 'your-api-key', environment: 'production' }); \`\`\` ## API Reference ### Methods #### \`project.init(options)\` Initialize the project with configuration options. **Parameters:** - \`options\` (Object): Configuration object - \`apiKey\` (String): Your API key - \`environment\` (String): Environment setting ## Contributing Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests. ## License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.`; await fs.writeFile(readmePath, realWorldReadme); const result = await optimizeReadme({ readme_path: readmePath, strategy: 'developer_focused', max_length: 400, }); expect(result.success).toBe(true); expect(result.data?.optimization.optimizedContent).toContain('TL;DR'); expect(result.data?.optimization.optimizedContent).toContain('Quick Start'); }); }); });

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tosin2013/documcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server