contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
239 lines (237 loc) โข 12.2 kB
JavaScript
/**
* Phase 3 Test: State-Aware Tool Behavior
*
* This test verifies that:
* 1. CinematicWorkflowTool respects workflow state constraints
* 2. State transitions work correctly (confirm_outline, start_screenplay, enter_iteration)
* 3. Tool respects user preferences (auto_generate_sketches, sketch_style)
* 4. Tool blocks operations not allowed in current state
* 5. Tool allows operations when in correct state
*/
import { CinematicWorkflowTool } from '../services/tools/CinematicWorkflowTool.js';
import { StateManager } from '../services/stateManager.js';
import fs from 'fs/promises';
import path from 'path';
async function testPhase3() {
console.log('๐งช Phase 3 Test: State-Aware Tool Behavior\n');
const testDir = path.join(process.cwd(), 'test_phase3_output');
try {
// Create test directory
await fs.mkdir(testDir, { recursive: true });
console.log(`โ Created test directory: ${testDir}\n`);
const stateManager = new StateManager(testDir);
const cinematicTool = new CinematicWorkflowTool(testDir);
// Test 1: Create Story Project
console.log('Test 1: Create Story Project');
console.log('='.repeat(60));
await stateManager.createStoryProject('Test Detective Story', 'A detective investigates a mysterious case in a futuristic city.');
const currentState = await stateManager.getCurrentState();
console.log(` โ Story project created with state: ${currentState}`);
console.log(` โ State is OUTLINE_DRAFT: ${currentState === 'OUTLINE_DRAFT' ? 'โ
' : 'โ'}`);
// Test 2: Try to create scene in OUTLINE_DRAFT (should fail)
console.log('\nTest 2: Block Scene Creation in OUTLINE_DRAFT');
console.log('='.repeat(60));
const sceneContent = `## SCENE 1: Opening
### Scene Description
Detective enters the lab.
### Visual Sketch Description
- **WIDE SHOT**: Detective walking into futuristic lab
`;
const createResult1 = await cinematicTool.execute({
operation: 'create_scene',
scene_file_path: 'scenes/scene_01.md',
content: sceneContent
});
console.log(` โ Create scene blocked: ${!createResult1.success ? 'โ
' : 'โ'}`);
if (!createResult1.success) {
console.log(` โ Error message: ${createResult1.error}`);
}
// Test 3: Confirm Outline
console.log('\nTest 3: Confirm Outline (State Transition)');
console.log('='.repeat(60));
const confirmResult = await cinematicTool.execute({
operation: 'confirm_outline'
});
console.log(` โ Confirm outline success: ${confirmResult.success ? 'โ
' : 'โ'}`);
if (confirmResult.success) {
console.log(` โ Message: ${confirmResult.message}`);
console.log(` โ New state: ${confirmResult.data?.new_state}`);
}
const stateAfterConfirm = await stateManager.getCurrentState();
console.log(` โ State is OUTLINE_CONFIRMED: ${stateAfterConfirm === 'OUTLINE_CONFIRMED' ? 'โ
' : 'โ'}`);
// Test 4: Try to create scene in OUTLINE_CONFIRMED (should still fail)
console.log('\nTest 4: Block Scene Creation in OUTLINE_CONFIRMED');
console.log('='.repeat(60));
const createResult2 = await cinematicTool.execute({
operation: 'create_scene',
scene_file_path: 'scenes/scene_01.md',
content: sceneContent
});
console.log(` โ Create scene blocked: ${!createResult2.success ? 'โ
' : 'โ'}`);
if (!createResult2.success) {
console.log(` โ Error message: ${createResult2.error}`);
}
// Test 5: Start Screenplay
console.log('\nTest 5: Start Screenplay (State Transition)');
console.log('='.repeat(60));
const startResult = await cinematicTool.execute({
operation: 'start_screenplay'
});
console.log(` โ Start screenplay success: ${startResult.success ? 'โ
' : 'โ'}`);
if (startResult.success) {
console.log(` โ Message: ${startResult.message}`);
console.log(` โ New state: ${startResult.data?.new_state}`);
}
const stateAfterStart = await stateManager.getCurrentState();
console.log(` โ State is SCREENPLAY_WRITING: ${stateAfterStart === 'SCREENPLAY_WRITING' ? 'โ
' : 'โ'}`);
// Test 6: Create scene in SCREENPLAY_WRITING (should succeed)
console.log('\nTest 6: Allow Scene Creation in SCREENPLAY_WRITING');
console.log('='.repeat(60));
const createResult3 = await cinematicTool.execute({
operation: 'create_scene',
scene_file_path: 'scenes/scene_01.md',
content: sceneContent,
auto_generate_images: false // Disable for test speed
});
console.log(` โ Create scene success: ${createResult3.success ? 'โ
' : 'โ'}`);
if (createResult3.success) {
console.log(` โ File created: ${createResult3.data?.file_path}`);
console.log(` โ Lines written: ${createResult3.data?.lines_written}`);
}
// Test 7: Try to generate sketches in SCREENPLAY_WRITING (should fail)
console.log('\nTest 7: Block Sketch Generation in SCREENPLAY_WRITING');
console.log('='.repeat(60));
const sketchResult1 = await cinematicTool.execute({
operation: 'generate_scene_images',
scene_file_path: 'scenes/scene_01.md',
image_style: 'storyboard'
});
console.log(` โ Sketch generation blocked: ${!sketchResult1.success ? 'โ
' : 'โ'}`);
if (!sketchResult1.success) {
console.log(` โ Error message: ${sketchResult1.error}`);
}
// Test 8: Enter Iteration
console.log('\nTest 8: Enter Iteration (State Transition)');
console.log('='.repeat(60));
const iterationResult = await cinematicTool.execute({
operation: 'enter_iteration'
});
console.log(` โ Enter iteration success: ${iterationResult.success ? 'โ
' : 'โ'}`);
if (iterationResult.success) {
console.log(` โ Message: ${iterationResult.message}`);
console.log(` โ New state: ${iterationResult.data?.new_state}`);
console.log(` โ Auto-generate sketches: ${iterationResult.data?.auto_generate_sketches}`);
console.log(` โ Sketch style: ${iterationResult.data?.sketch_style}`);
}
const stateAfterIteration = await stateManager.getCurrentState();
console.log(` โ State is ITERATION: ${stateAfterIteration === 'ITERATION' ? 'โ
' : 'โ'}`);
// Test 9: Generate sketches in ITERATION (should succeed)
console.log('\nTest 9: Allow Sketch Generation in ITERATION');
console.log('='.repeat(60));
const sketchResult2 = await cinematicTool.execute({
operation: 'generate_scene_images',
scene_file_path: 'scenes/scene_01.md',
image_style: 'storyboard'
});
console.log(` โ Sketch generation allowed: ${sketchResult2.success ? 'โ
' : 'โ'}`);
if (sketchResult2.success) {
console.log(` โ Images generated: ${sketchResult2.data?.successful_generations || 0}`);
}
else {
// It's OK if it fails due to no visual descriptions or image generation issues
console.log(` โ Note: ${sketchResult2.error}`);
}
// Test 10: User Preference Respect
console.log('\nTest 10: Respect User Preferences');
console.log('='.repeat(60));
// Update preferences
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 preference set: ${autoSketch ? 'โ
' : 'โ'}`);
console.log(` โ Sketch style preference: ${sketchStyle}`);
console.log(` โ Style is cinematic: ${sketchStyle === 'cinematic' ? 'โ
' : 'โ'}`);
// Test 11: Invalid State Transitions
console.log('\nTest 11: Block Invalid State Transitions');
console.log('='.repeat(60));
// Try to confirm outline from ITERATION (should fail)
const invalidConfirm = await cinematicTool.execute({
operation: 'confirm_outline'
});
console.log(` โ Invalid confirm blocked: ${!invalidConfirm.success ? 'โ
' : 'โ'}`);
if (!invalidConfirm.success) {
console.log(` โ Error: ${invalidConfirm.error}`);
}
// Test 12: Rollback Always Allowed
console.log('\nTest 12: Rollback Always Allowed');
console.log('='.repeat(60));
// Update scene to create a backup
await cinematicTool.execute({
operation: 'update_scene',
scene_file_path: 'scenes/scene_01.md',
content: sceneContent + '\n\n## Updated content',
auto_generate_images: false
});
const rollbackResult = await cinematicTool.execute({
operation: 'rollback_scene',
scene_file_path: 'scenes/scene_01.md'
});
console.log(` โ Rollback allowed: ${rollbackResult.success ? 'โ
' : 'โ'}`);
if (rollbackResult.success) {
console.log(` โ Restored from: ${rollbackResult.data?.restored_from}`);
}
// Summary
console.log('\n' + '='.repeat(60));
console.log('๐ Phase 3 Test Summary');
console.log('='.repeat(60));
const allTests = [
currentState === 'OUTLINE_DRAFT',
!createResult1.success, // Blocked in OUTLINE_DRAFT
confirmResult.success && stateAfterConfirm === 'OUTLINE_CONFIRMED',
!createResult2.success, // Blocked in OUTLINE_CONFIRMED
startResult.success && stateAfterStart === 'SCREENPLAY_WRITING',
createResult3.success, // Allowed in SCREENPLAY_WRITING
!sketchResult1.success, // Blocked in SCREENPLAY_WRITING
iterationResult.success && stateAfterIteration === 'ITERATION',
sketchResult2.success || sketchResult2.error?.includes('No visual descriptions'), // Allowed in ITERATION
autoSketch === true && sketchStyle === 'cinematic',
!invalidConfirm.success, // Invalid transition blocked
rollbackResult.success // Rollback always allowed
];
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 3 tests passed! State-aware tool behavior is working correctly:');
console.log(' - Tool respects workflow state constraints');
console.log(' - State transitions work correctly');
console.log(' - Operations blocked in wrong states');
console.log(' - Operations allowed in correct states');
console.log(' - User preferences are respected');
console.log(' - Invalid transitions are blocked');
console.log(' - Rollback always allowed');
}
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
testPhase3().catch(console.error);