UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

192 lines (191 loc) โ€ข 10.8 kB
/** * Phase 2 Test: State Storage & Detection * * This test verifies that: * 1. StateManager can create story projects with initial state * 2. StateManager can read and parse YAML frontmatter * 3. StateManager can update workflow state * 4. StateManager can update user preferences * 5. ChatService detects story projects and loads state * 6. ChatService appends state context to system prompt */ import { StateManager } from '../services/stateManager.js'; import { ChatService } from '../services/chatService.js'; import fs from 'fs/promises'; import path from 'path'; async function testPhase2() { console.log('๐Ÿงช Phase 2 Test: State Storage & Detection\n'); const testDir = path.join(process.cwd(), 'test_phase2_output'); try { // Create test directory await fs.mkdir(testDir, { recursive: true }); console.log(`โœ“ Created test directory: ${testDir}\n`); // Test 1: StateManager - Create Story Project console.log('Test 1: Create Story Project'); console.log('='.repeat(60)); const stateManager = new StateManager(testDir); await stateManager.createStoryProject('Test Detective Story', 'A detective investigates a mysterious case in a futuristic city.'); const hasProject = await stateManager.hasStoryProject(); console.log(` โœ“ Story project created: ${hasProject ? 'โœ…' : 'โŒ'}`); // Test 2: StateManager - Read State console.log('\nTest 2: Read State'); console.log('='.repeat(60)); const parsed = await stateManager.readState(); console.log(` โœ“ State read successfully: ${parsed ? 'โœ…' : 'โŒ'}`); if (parsed) { console.log(` โœ“ Workflow state: ${parsed.state.workflow_state}`); console.log(` โœ“ Auto-generate sketches: ${parsed.state.auto_generate_sketches}`); console.log(` โœ“ Sketch style: ${parsed.state.sketch_style_preference}`); console.log(` โœ“ Created at: ${parsed.state.created_at}`); console.log(` โœ“ Last updated: ${parsed.state.last_updated}`); console.log(` โœ“ Content length: ${parsed.content.length} chars`); } // Test 3: StateManager - Update Workflow State console.log('\nTest 3: Update Workflow State'); console.log('='.repeat(60)); await stateManager.updateWorkflowState('OUTLINE_CONFIRMED'); const updatedState = await stateManager.getCurrentState(); console.log(` โœ“ State updated to: ${updatedState}`); console.log(` โœ“ State is OUTLINE_CONFIRMED: ${updatedState === 'OUTLINE_CONFIRMED' ? 'โœ…' : 'โŒ'}`); // Test 4: StateManager - Update Preferences console.log('\nTest 4: Update Preferences'); console.log('='.repeat(60)); await stateManager.updatePreferences({ auto_generate_sketches: true, sketch_style_preference: 'cinematic' }); const autoSketch = await stateManager.isAutoSketchEnabled(); const sketchStyle = await stateManager.getSketchStyle(); console.log(` โœ“ Auto-sketch enabled: ${autoSketch ? 'โœ…' : 'โŒ'}`); console.log(` โœ“ Sketch style: ${sketchStyle}`); console.log(` โœ“ Sketch style is cinematic: ${sketchStyle === 'cinematic' ? 'โœ…' : 'โŒ'}`); // Test 5: StateManager - State Constraints console.log('\nTest 5: State Constraints'); console.log('='.repeat(60)); const states = ['OUTLINE_DRAFT', 'OUTLINE_CONFIRMED', 'SCREENPLAY_WRITING', 'ITERATION']; for (const state of states) { const constraints = stateManager.getStateConstraints(state); console.log(` โœ“ ${state}:`); console.log(` - Can generate screenplay: ${constraints.canGenerateScreenplay}`); console.log(` - Can generate sketches: ${constraints.canGenerateSketches}`); console.log(` - Can edit outline: ${constraints.canEditOutline}`); console.log(` - Can iterate: ${constraints.canIterate}`); } // Test 6: StateManager - State Transitions console.log('\nTest 6: State Transitions'); console.log('='.repeat(60)); const validTransitions = [ { from: 'OUTLINE_DRAFT', to: 'OUTLINE_CONFIRMED', expected: true }, { from: 'OUTLINE_DRAFT', to: 'SCREENPLAY_WRITING', expected: false }, { from: 'OUTLINE_CONFIRMED', to: 'SCREENPLAY_WRITING', expected: true }, { from: 'SCREENPLAY_WRITING', to: 'ITERATION', expected: true }, { from: 'ITERATION', to: 'OUTLINE_DRAFT', expected: true } ]; for (const transition of validTransitions) { const isValid = stateManager.validateStateTransition(transition.from, transition.to); const result = isValid === transition.expected ? 'โœ…' : 'โŒ'; console.log(` ${result} ${transition.from} โ†’ ${transition.to}: ${isValid ? 'valid' : 'invalid'}`); } // Test 7: ChatService - Detect Story Project console.log('\nTest 7: ChatService - Detect Story Project'); console.log('='.repeat(60)); const chatService = new ChatService(testDir); const session = await chatService.createSession(); console.log(` โœ“ Session created: ${session.id}`); console.log(` โœ“ Has story project: ${session.hasStoryProject ? 'โœ…' : 'โŒ'}`); console.log(` โœ“ Story project state: ${session.storyProjectState?.workflow_state || 'none'}`); // Test 8: ChatService - State Context in System Prompt console.log('\nTest 8: State Context in System Prompt'); console.log('='.repeat(60)); const systemMessage = session.messages.find(m => m.role === 'system'); if (systemMessage) { const hasStateContext = systemMessage.content.includes('Current Story Project Context'); const hasWorkflowState = systemMessage.content.includes('Current Workflow State'); const hasAutoSketch = systemMessage.content.includes('Auto-generate Sketches'); console.log(` โœ“ System message exists: โœ…`); console.log(` โœ“ Has state context: ${hasStateContext ? 'โœ…' : 'โŒ'}`); console.log(` โœ“ Has workflow state: ${hasWorkflowState ? 'โœ…' : 'โŒ'}`); console.log(` โœ“ Has auto-sketch info: ${hasAutoSketch ? 'โœ…' : 'โŒ'}`); if (hasStateContext) { console.log('\n ๐Ÿ“„ State Context Snippet:'); const contextStart = systemMessage.content.indexOf('Current Story Project Context'); const contextSnippet = systemMessage.content.substring(contextStart, contextStart + 300); console.log(` ${contextSnippet}...`); } } // Test 9: ChatService - Refresh State console.log('\nTest 9: ChatService - Refresh State'); console.log('='.repeat(60)); // Update state directly await stateManager.updateWorkflowState('SCREENPLAY_WRITING'); // Refresh state in session await chatService.refreshStoryProjectState(session.id); const refreshedSession = chatService.getSession(session.id); console.log(` โœ“ State refreshed: ${refreshedSession?.storyProjectState?.workflow_state === 'SCREENPLAY_WRITING' ? 'โœ…' : 'โŒ'}`); console.log(` โœ“ New state: ${refreshedSession?.storyProjectState?.workflow_state}`); // Test 10: ChatService - No Story Project console.log('\nTest 10: ChatService - No Story Project'); console.log('='.repeat(60)); const emptyDir = path.join(testDir, 'empty'); await fs.mkdir(emptyDir, { recursive: true }); const chatServiceEmpty = new ChatService(emptyDir); const emptySession = await chatServiceEmpty.createSession(); console.log(` โœ“ Session created: ${emptySession.id}`); console.log(` โœ“ Has story project: ${emptySession.hasStoryProject ? 'โŒ' : 'โœ…'}`); console.log(` โœ“ Story project state: ${emptySession.storyProjectState ? 'โŒ' : 'โœ… (none)'}`); const emptySystemMessage = emptySession.messages.find(m => m.role === 'system'); if (emptySystemMessage) { const hasStateContext = emptySystemMessage.content.includes('Current Story Project Context'); console.log(` โœ“ No state context in system prompt: ${!hasStateContext ? 'โœ…' : 'โŒ'}`); } // Summary console.log('\n' + '='.repeat(60)); console.log('๐Ÿ“Š Phase 2 Test Summary'); console.log('='.repeat(60)); const allTests = [ hasProject, parsed !== null, updatedState === 'OUTLINE_CONFIRMED', autoSketch === true, sketchStyle === 'cinematic', session.hasStoryProject === true, session.storyProjectState?.workflow_state === 'OUTLINE_CONFIRMED', systemMessage?.content.includes('Current Story Project Context') === true, refreshedSession?.storyProjectState?.workflow_state === 'SCREENPLAY_WRITING', emptySession.hasStoryProject === false ]; const passedTests = allTests.filter(t => t).length; const totalTests = allTests.length; const passRate = ((passedTests / totalTests) * 100).toFixed(1); console.log(`\nTests Passed: ${passedTests}/${totalTests} (${passRate}%)`); if (passedTests === totalTests) { console.log('\nโœ… All Phase 2 tests passed! State management is working correctly:'); console.log(' - Story projects can be created with initial state'); console.log(' - State can be read and parsed from YAML frontmatter'); console.log(' - Workflow state can be updated'); console.log(' - User preferences can be updated'); console.log(' - ChatService detects story projects'); console.log(' - ChatService loads state into sessions'); console.log(' - State context is appended to system prompts'); console.log(' - State can be refreshed during conversations'); } else { console.log('\nโš ๏ธ Some tests failed. Review the output above.'); } // Cleanup console.log(`\n๐Ÿงน Cleaning up test directory: ${testDir}`); await fs.rm(testDir, { recursive: true, force: true }); console.log('โœ“ Cleanup complete\n'); } catch (error) { console.error('โŒ Test failed with error:', error); // Cleanup on error try { await fs.rm(testDir, { recursive: true, force: true }); } catch { } throw error; } } // Run the test testPhase2().catch(console.error);