mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-02-02 08:33:36 +00:00
Reduce auto-mode-service.ts from 1308 to 516 lines (60% reduction) by extracting reusable functionality into shared packages and services: - Add feature prompt builders to @automaker/prompts (buildFeaturePrompt, buildFollowUpPrompt, buildContinuationPrompt, extractTitleFromDescription) - Add planning prompts and task parsing to @automaker/prompts - Add stream processor utilities to @automaker/utils (sleep, processStream) - Add git commit utilities to @automaker/git-utils (commitAll, hasUncommittedChanges) - Create ProjectAnalyzer service for project analysis - Create FeatureVerificationService for verify/commit operations - Extend FeatureLoader with updateStatus, updatePlanSpec, getPending methods - Expand FeatureStatus type to include all used statuses - Add PlanSpec and ParsedTask types to @automaker/types 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
294 lines
10 KiB
TypeScript
294 lines
10 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import { AutoModeService } from '@/services/auto-mode-service.js';
|
|
import {
|
|
getPlanningPromptPrefix,
|
|
parseTasksFromSpec,
|
|
parseTaskLine,
|
|
buildFeaturePrompt,
|
|
extractTitleFromDescription,
|
|
} from '@automaker/prompts';
|
|
|
|
describe('auto-mode-service.ts - Planning Mode', () => {
|
|
let service: AutoModeService;
|
|
const mockEvents = {
|
|
subscribe: vi.fn(),
|
|
emit: vi.fn(),
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
service = new AutoModeService(mockEvents as any);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
// Clean up any running processes
|
|
await service.stopAutoLoop().catch(() => {});
|
|
});
|
|
|
|
describe('getPlanningPromptPrefix (from @automaker/prompts)', () => {
|
|
it('should return empty string for skip mode', () => {
|
|
const result = getPlanningPromptPrefix('skip');
|
|
expect(result).toBe('');
|
|
});
|
|
|
|
it('should return lite prompt for lite mode without approval', () => {
|
|
const result = getPlanningPromptPrefix('lite', false);
|
|
expect(result).toContain('Planning Phase (Lite Mode)');
|
|
expect(result).toContain('[PLAN_GENERATED]');
|
|
expect(result).toContain('Feature Request');
|
|
});
|
|
|
|
it('should return lite_with_approval prompt for lite mode with approval', () => {
|
|
const result = getPlanningPromptPrefix('lite', true);
|
|
expect(result).toContain('Planning Phase (Lite Mode)');
|
|
expect(result).toContain('[SPEC_GENERATED]');
|
|
expect(result).toContain('DO NOT proceed with implementation');
|
|
});
|
|
|
|
it('should return spec prompt for spec mode', () => {
|
|
const result = getPlanningPromptPrefix('spec');
|
|
expect(result).toContain('Specification Phase (Spec Mode)');
|
|
expect(result).toContain('```tasks');
|
|
expect(result).toContain('T001');
|
|
expect(result).toContain('[TASK_START]');
|
|
expect(result).toContain('[TASK_COMPLETE]');
|
|
});
|
|
|
|
it('should return full prompt for full mode', () => {
|
|
const result = getPlanningPromptPrefix('full');
|
|
expect(result).toContain('Full Specification Phase (Full SDD Mode)');
|
|
expect(result).toContain('Phase 1: Foundation');
|
|
expect(result).toContain('Phase 2: Core Implementation');
|
|
expect(result).toContain('Phase 3: Integration & Testing');
|
|
});
|
|
|
|
it('should include the separator and Feature Request header', () => {
|
|
const result = getPlanningPromptPrefix('spec');
|
|
expect(result).toContain('---');
|
|
expect(result).toContain('## Feature Request');
|
|
});
|
|
|
|
it('should instruct agent to NOT output exploration text', () => {
|
|
const modes = ['lite', 'spec', 'full'] as const;
|
|
for (const mode of modes) {
|
|
const result = getPlanningPromptPrefix(mode);
|
|
expect(result).toContain('Do NOT output exploration text');
|
|
expect(result).toContain('Start DIRECTLY');
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('parseTasksFromSpec (via module)', () => {
|
|
// We need to test the module-level function
|
|
// Import it directly for testing
|
|
it('should parse tasks from a valid tasks block', async () => {
|
|
// This tests the internal logic through integration
|
|
// The function is module-level, so we verify behavior through the service
|
|
const specContent = `
|
|
## Specification
|
|
|
|
\`\`\`tasks
|
|
- [ ] T001: Create user model | File: src/models/user.ts
|
|
- [ ] T002: Add API endpoint | File: src/routes/users.ts
|
|
- [ ] T003: Write unit tests | File: tests/user.test.ts
|
|
\`\`\`
|
|
`;
|
|
// Since parseTasksFromSpec is a module-level function,
|
|
// we verify its behavior indirectly through plan parsing
|
|
expect(specContent).toContain('T001');
|
|
expect(specContent).toContain('T002');
|
|
expect(specContent).toContain('T003');
|
|
});
|
|
|
|
it('should handle tasks block with phases', () => {
|
|
const specContent = `
|
|
\`\`\`tasks
|
|
## Phase 1: Setup
|
|
- [ ] T001: Initialize project | File: package.json
|
|
- [ ] T002: Configure TypeScript | File: tsconfig.json
|
|
|
|
## Phase 2: Implementation
|
|
- [ ] T003: Create main module | File: src/index.ts
|
|
\`\`\`
|
|
`;
|
|
expect(specContent).toContain('Phase 1');
|
|
expect(specContent).toContain('Phase 2');
|
|
expect(specContent).toContain('T001');
|
|
expect(specContent).toContain('T003');
|
|
});
|
|
});
|
|
|
|
describe('plan approval flow', () => {
|
|
it('should track pending approvals correctly', () => {
|
|
expect(service.hasPendingApproval('test-feature')).toBe(false);
|
|
});
|
|
|
|
it('should allow cancelling non-existent approval without error', () => {
|
|
expect(() => service.cancelPlanApproval('non-existent')).not.toThrow();
|
|
});
|
|
|
|
it('should return running features count after stop', async () => {
|
|
const count = await service.stopAutoLoop();
|
|
expect(count).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('resolvePlanApproval', () => {
|
|
it('should return error when no pending approval exists', async () => {
|
|
const result = await service.resolvePlanApproval(
|
|
'non-existent-feature',
|
|
true,
|
|
undefined,
|
|
undefined,
|
|
undefined
|
|
);
|
|
expect(result.success).toBe(false);
|
|
expect(result.error).toContain('No pending approval');
|
|
});
|
|
|
|
it('should handle approval with edited plan', async () => {
|
|
// Without a pending approval, this should fail gracefully
|
|
const result = await service.resolvePlanApproval(
|
|
'test-feature',
|
|
true,
|
|
'Edited plan content',
|
|
undefined,
|
|
undefined
|
|
);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should handle rejection with feedback', async () => {
|
|
const result = await service.resolvePlanApproval(
|
|
'test-feature',
|
|
false,
|
|
undefined,
|
|
'Please add more details',
|
|
undefined
|
|
);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('buildFeaturePrompt (from @automaker/prompts)', () => {
|
|
it('should include feature ID and description', () => {
|
|
const feature = {
|
|
id: 'feat-123',
|
|
category: 'Test',
|
|
description: 'Add user authentication',
|
|
};
|
|
const result = buildFeaturePrompt(feature);
|
|
expect(result).toContain('feat-123');
|
|
expect(result).toContain('Add user authentication');
|
|
});
|
|
|
|
it('should include specification when present', () => {
|
|
const feature = {
|
|
id: 'feat-123',
|
|
category: 'Test',
|
|
description: 'Test feature',
|
|
spec: 'Detailed specification here',
|
|
};
|
|
const result = buildFeaturePrompt(feature);
|
|
expect(result).toContain('Specification:');
|
|
expect(result).toContain('Detailed specification here');
|
|
});
|
|
|
|
it('should include image paths when present', () => {
|
|
const feature = {
|
|
id: 'feat-123',
|
|
category: 'Test',
|
|
description: 'Test feature',
|
|
imagePaths: [
|
|
{ path: '/tmp/image1.png', filename: 'image1.png', mimeType: 'image/png' },
|
|
'/tmp/image2.jpg',
|
|
],
|
|
};
|
|
const result = buildFeaturePrompt(feature);
|
|
expect(result).toContain('Context Images Attached');
|
|
expect(result).toContain('image1.png');
|
|
expect(result).toContain('/tmp/image2.jpg');
|
|
});
|
|
|
|
it('should include summary tags instruction', () => {
|
|
const feature = {
|
|
id: 'feat-123',
|
|
category: 'Test',
|
|
description: 'Test feature',
|
|
};
|
|
const result = buildFeaturePrompt(feature);
|
|
expect(result).toContain('<summary>');
|
|
expect(result).toContain('summary');
|
|
});
|
|
});
|
|
|
|
describe('extractTitleFromDescription (from @automaker/prompts)', () => {
|
|
it("should return 'Untitled Feature' for empty description", () => {
|
|
expect(extractTitleFromDescription('')).toBe('Untitled Feature');
|
|
expect(extractTitleFromDescription(' ')).toBe('Untitled Feature');
|
|
});
|
|
|
|
it('should return first line if under 60 characters', () => {
|
|
const description = 'Add user login\nWith email validation';
|
|
expect(extractTitleFromDescription(description)).toBe('Add user login');
|
|
});
|
|
|
|
it('should truncate long first lines to 60 characters', () => {
|
|
const description =
|
|
'This is a very long feature description that exceeds the sixty character limit significantly';
|
|
const result = extractTitleFromDescription(description);
|
|
expect(result.length).toBe(60);
|
|
expect(result).toContain('...');
|
|
});
|
|
});
|
|
|
|
describe('PLANNING_PROMPTS structure (from @automaker/prompts)', () => {
|
|
it('should have all required planning modes', () => {
|
|
const modes = ['lite', 'spec', 'full'] as const;
|
|
for (const mode of modes) {
|
|
const result = getPlanningPromptPrefix(mode);
|
|
expect(result.length).toBeGreaterThan(100);
|
|
}
|
|
});
|
|
|
|
it('lite prompt should include correct structure', () => {
|
|
const result = getPlanningPromptPrefix('lite');
|
|
expect(result).toContain('Goal');
|
|
expect(result).toContain('Approach');
|
|
expect(result).toContain('Files to Touch');
|
|
expect(result).toContain('Tasks');
|
|
expect(result).toContain('Risks');
|
|
});
|
|
|
|
it('spec prompt should include task format instructions', () => {
|
|
const result = getPlanningPromptPrefix('spec');
|
|
expect(result).toContain('Problem');
|
|
expect(result).toContain('Solution');
|
|
expect(result).toContain('Acceptance Criteria');
|
|
expect(result).toContain('GIVEN-WHEN-THEN');
|
|
expect(result).toContain('Implementation Tasks');
|
|
expect(result).toContain('Verification');
|
|
});
|
|
|
|
it('full prompt should include phases', () => {
|
|
const result = getPlanningPromptPrefix('full');
|
|
expect(result).toContain('Problem Statement');
|
|
expect(result).toContain('User Story');
|
|
expect(result).toContain('Technical Context');
|
|
expect(result).toContain('Non-Goals');
|
|
expect(result).toContain('Phase 1');
|
|
expect(result).toContain('Phase 2');
|
|
expect(result).toContain('Phase 3');
|
|
});
|
|
});
|
|
|
|
describe('status management', () => {
|
|
it('should report correct status', () => {
|
|
const status = service.getStatus();
|
|
expect(status.runningFeatures).toEqual([]);
|
|
expect(status.isRunning).toBe(false);
|
|
expect(status.runningCount).toBe(0);
|
|
});
|
|
});
|
|
});
|