/** * Agentic QE v3 - Pattern Matching Service * Implements IPatternMatchingService for learning and applying test patterns * * Uses TypeScript Compiler API for real AST parsing and pattern extraction * Uses NomicEmbedder for semantic embeddings when available */ import { Result } from '../../../shared/types'; import { MemoryBackend } from '../../../kernel/interfaces'; import { Pattern, LearnPatternsRequest, LearnedPatterns } from '../interfaces'; import { IEmbeddingProvider } from '../../../shared/embeddings'; /** * Interface for the pattern matching service */ export interface IPatternMatchingService { findMatchingPatterns(context: PatternSearchContext): Promise>; applyPattern(patternId: string, targetCode: string): Promise>; recordPattern(pattern: PatternDefinition): Promise>; learnPatterns(request: LearnPatternsRequest): Promise>; getPattern(patternId: string): Promise; listPatterns(filter?: PatternFilter): Promise; } /** * Context for pattern search */ export interface PatternSearchContext { sourceCode?: string; fileType?: string; testType?: 'unit' | 'integration' | 'e2e' | 'property'; framework?: string; tags?: string[]; semanticQuery?: string; } /** * Pattern match result */ export interface PatternMatch { pattern: Pattern; score: number; matchReason: string; suggestedApplication: string; } /** * Applied pattern result */ export interface AppliedPattern { patternId: string; generatedCode: string; modifications: PatternModification[]; confidence: number; } /** * Pattern modification record */ export interface PatternModification { location: string; original?: string; replacement: string; reason: string; } /** * Pattern definition for recording */ export interface PatternDefinition { name: string; structure: string; description?: string; tags?: string[]; testType?: 'unit' | 'integration' | 'e2e' | 'property'; framework?: string; examples?: PatternExample[]; } /** * Pattern example */ export interface PatternExample { input: string; output: string; context?: string; } /** * Pattern filter */ export interface PatternFilter { testType?: 'unit' | 'integration' | 'e2e' | 'property'; framework?: string; tags?: string[]; minApplicability?: number; limit?: number; } /** * Configuration for the pattern matcher */ export interface PatternMatcherConfig { maxPatterns: number; minMatchScore: number; enableVectorSearch: boolean; embeddingDimension: number; patternNamespace: string; /** Optional embedder instance (defaults to NomicEmbedder with fallback) */ embedder?: IEmbeddingProvider; } /** * Pattern Matching Service Implementation * Manages test patterns with learning and semantic search capabilities */ export declare class PatternMatcherService implements IPatternMatchingService { private readonly memory; private readonly config; private readonly patternCache; private readonly tsParser; private readonly embedder; constructor(memory: MemoryBackend, config?: Partial); /** * Match testable patterns from a source file * Uses TypeScript AST to identify functions, classes, and methods that need tests */ matchTestablePatterns(filePath: string): Promise>; /** * Match testable patterns from source code string */ matchTestablePatternsFromCode(sourceCode: string, fileName?: string): Promise>; /** * Create a testable pattern for a function */ private createFunctionPattern; /** * Create a testable pattern for a class */ private createClassPattern; /** * Create a testable pattern for a class method */ private createMethodPattern; /** * Suggest tests for a function based on its signature and complexity */ private suggestTestsForFunction; /** * Suggest tests for a class */ private suggestTestsForClass; /** * Suggest tests for a class method */ private suggestTestsForMethod; /** * Generate a happy path test code snippet */ private generateHappyPathTest; /** * Generate a mock value for a parameter based on its type */ private generateMockValue; /** * Estimate number of branches from cyclomatic complexity */ private estimateBranches; /** * Find patterns matching the given context */ findMatchingPatterns(context: PatternSearchContext): Promise>; /** * Apply a pattern to generate code */ applyPattern(patternId: string, targetCode: string): Promise>; /** * Record a new pattern */ recordPattern(definition: PatternDefinition): Promise>; /** * Learn patterns from existing test files */ learnPatterns(request: LearnPatternsRequest): Promise>; /** * Get a pattern by ID */ getPattern(patternId: string): Promise; /** * List patterns with optional filtering */ listPatterns(filter?: PatternFilter): Promise; private semanticSearch; private matchByTags; private matchByMetadata; private matchByStructure; private mergeMatches; private analyzeCodeStructure; private extractImports; private extractIdentifiers; private estimateComplexity; private extractPlaceholders; private resolvePlaceholder; private calculateApplicationConfidence; private calculateStructureMatch; private recordPatternUsage; private extractPatternsFromFile; /** * Extract test block patterns (describe/it/test) */ private extractTestBlockPatterns; /** * Extract setup/teardown patterns (beforeEach, afterEach, etc.) */ private extractSetupTeardownPatterns; /** * Extract assertion patterns from test code */ private extractAssertionPatterns; /** * Extract mocking patterns from test code */ private extractMockingPatterns; /** * Extract a complete block of code starting from an opening brace */ private extractBlockContent; private deduplicatePatterns; private hashStructure; private calculateLearningConfidence; /** * Generate embedding for a pattern definition * Uses NomicEmbedder for semantic embeddings (falls back to pseudo-embeddings if Ollama unavailable) */ private generatePatternEmbedding; /** * Generate embedding for a search query * Uses the same embedder as patterns for consistent similarity matching */ private generateQueryEmbedding; /** * Format a pattern definition for embedding generation * Creates a semantic-rich text representation */ private formatPatternForEmbedding; } /** * Information about a parameter */ interface ParameterInfo { name: string; type: string | undefined; optional: boolean; defaultValue: string | undefined; } /** * Testable pattern extracted from code analysis */ export interface TestablePattern { type: 'function' | 'class' | 'method' | 'module'; name: string; complexity: number; lines: { start: number; end: number; }; branches: number; suggestedTests: SuggestedTest[]; context: { parameters?: ParameterInfo[]; returnType?: string; dependencies?: string[]; isAsync?: boolean; }; } /** * Suggested test for a pattern */ interface SuggestedTest { description: string; type: 'happy-path' | 'edge-case' | 'error-handling' | 'boundary'; priority: 'high' | 'medium' | 'low'; testCode?: string; } export {}; //# sourceMappingURL=pattern-matcher.d.ts.map