/**
* Tests for pattern detection service
*/
import { describe, it, expect } from 'vitest';
import { detectPattern } from '../patternDetector.js';
import {
simpleNote,
bugFixNote,
refactoringNote,
documentationNote,
configNote,
testingNote,
complexNote,
} from '../../__fixtures__/sessionNotes.js';
describe('detectPattern', () => {
describe('bug-fix detection', () => {
it('detects bug-fix pattern from summary keywords', () => {
const result = detectPattern({
summary: 'Fixed critical bug in login flow',
timestamp: '2024-01-01T00:00:00Z',
});
expect(result.pattern).toBe('bug-fix');
expect(result.confidence).toBeGreaterThan(0);
});
it('detects bug-fix from fixture', () => {
const result = detectPattern(bugFixNote);
expect(result.pattern).toBe('bug-fix');
});
});
describe('new-feature detection', () => {
it('detects new-feature pattern from created files', () => {
const result = detectPattern({
summary: 'Added new user dashboard',
timestamp: '2024-01-01T00:00:00Z',
fileChanges: [
{ path: 'src/Dashboard.tsx', type: 'created' },
{ path: 'src/api/dashboard.ts', type: 'created' },
],
});
expect(result.pattern).toBe('new-feature');
});
it('detects new-feature from fixture', () => {
const result = detectPattern(complexNote);
// complexNote has multiple patterns (created files = new-feature, but also config changes)
// The pattern detector may return 'new-feature' or 'mixed' depending on weighting
expect(['new-feature', 'mixed']).toContain(result.pattern);
});
});
describe('refactoring detection', () => {
it('detects refactoring pattern from summary', () => {
const result = detectPattern({
summary: 'Refactored authentication module for better maintainability',
timestamp: '2024-01-01T00:00:00Z',
});
expect(result.pattern).toBe('refactoring');
});
it('detects refactoring from fixture', () => {
const result = detectPattern(refactoringNote);
expect(result.pattern).toBe('refactoring');
});
});
describe('documentation detection', () => {
it('detects documentation pattern from file changes', () => {
const result = detectPattern({
summary: 'Updated API documentation',
timestamp: '2024-01-01T00:00:00Z',
fileChanges: [
{ path: 'README.md', type: 'modified' },
{ path: 'docs/API.md', type: 'created' },
],
});
expect(result.pattern).toBe('documentation');
});
it('detects documentation from fixture', () => {
const result = detectPattern(documentationNote);
expect(result.pattern).toBe('documentation');
});
});
describe('configuration detection', () => {
it('detects configuration pattern from config files', () => {
const result = detectPattern({
summary: 'Updated build configuration',
timestamp: '2024-01-01T00:00:00Z',
fileChanges: [
{ path: 'webpack.config.js', type: 'modified' },
{ path: '.eslintrc.json', type: 'modified' },
],
});
expect(result.pattern).toBe('configuration');
});
it('detects configuration from fixture', () => {
const result = detectPattern(configNote);
expect(result.pattern).toBe('configuration');
});
});
describe('testing detection', () => {
it('detects testing pattern from test files', () => {
const result = detectPattern({
summary: 'Added unit tests for auth module',
timestamp: '2024-01-01T00:00:00Z',
fileChanges: [
{ path: 'src/__tests__/auth.test.ts', type: 'created' },
{ path: 'src/__tests__/user.test.ts', type: 'created' },
],
});
expect(result.pattern).toBe('testing');
});
it('detects testing from fixture', () => {
const result = detectPattern(testingNote);
expect(result.pattern).toBe('testing');
});
});
describe('confidence scores', () => {
it('returns higher confidence for multiple indicators', () => {
const strongMatch = detectPattern({
summary: 'Fixed critical bug causing crash',
timestamp: '2024-01-01T00:00:00Z',
tags: ['bug', 'hotfix', 'fix'],
});
const weakMatch = detectPattern({
summary: 'Updated code',
timestamp: '2024-01-01T00:00:00Z',
});
expect(strongMatch.confidence).toBeGreaterThan(weakMatch.confidence);
});
it('returns confidence between 0 and 100', () => {
const result = detectPattern(complexNote);
expect(result.confidence).toBeGreaterThanOrEqual(0);
expect(result.confidence).toBeLessThanOrEqual(100);
});
});
describe('edge cases', () => {
it('handles minimal note', () => {
const result = detectPattern(simpleNote);
expect(result.pattern).toBeDefined();
expect(typeof result.confidence).toBe('number');
});
it('handles empty commands and file changes', () => {
const result = detectPattern({
summary: 'Some work',
timestamp: '2024-01-01T00:00:00Z',
commands: [],
fileChanges: [],
});
expect(result.pattern).toBeDefined();
});
});
});