#!/usr/bin/env python """Comprehensive test of AI and billing system at commit #10""" import django import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings') django.setup() print('='*70) print('FINAL COMPREHENSIVE TEST - COMMIT #10 STATE') print('='*70) # Test 1: Credit Cost Config Save print('\n1. Testing CreditCostConfig Save:') try: from igny8_core.business.billing.models import CreditCostConfig obj = CreditCostConfig.objects.get(operation_type='clustering') original_cost = obj.credits_cost obj.credits_cost = 5 obj.save() print(f' ✓ Save successful: clustering cost changed to {obj.credits_cost}') obj.credits_cost = original_cost obj.save() print(f' ✓ Reverted to original: {obj.credits_cost}') except Exception as e: print(f' ✗ ERROR: {e}') # Test 2: Credit Check print('\n2. Testing Credit Check:') try: from igny8_core.business.billing.services.credit_service import CreditService from igny8_core.auth.models import Account acc = Account.objects.first() print(f' Account: {acc.name} with {acc.credits} credits') CreditService.check_credits(acc, 'clustering') print(f' ✓ Credit check passed for clustering') CreditService.check_credits(acc, 'idea_generation') print(f' ✓ Credit check passed for idea_generation') CreditService.check_credits(acc, 'content_generation', 1000) print(f' ✓ Credit check passed for content_generation (1000 words)') except Exception as e: print(f' ✗ ERROR: {e}') # Test 3: AI Core print('\n3. Testing AICore Initialization:') try: from igny8_core.ai.ai_core import AICore from igny8_core.auth.models import Account acc = Account.objects.first() ai_core = AICore(account=acc) print(f' ✓ AICore initialized for account: {acc.name}') has_key = "SET" if ai_core._openai_api_key else "NOT SET" print(f' - OpenAI key: {has_key}') except Exception as e: print(f' ✗ ERROR: {e}') # Test 4: AI Engine print('\n4. Testing AIEngine:') try: from igny8_core.ai.engine import AIEngine from igny8_core.auth.models import Account acc = Account.objects.first() engine = AIEngine(account=acc) print(f' ✓ AIEngine initialized') # Test operation type mapping op_type = engine._get_operation_type('auto_cluster') print(f' ✓ Operation mapping: auto_cluster → {op_type}') except Exception as e: print(f' ✗ ERROR: {e}') # Test 5: Credit Deduction print('\n5. Testing Credit Deduction:') try: from igny8_core.business.billing.services.credit_service import CreditService from igny8_core.auth.models import Account from django.db import transaction acc = Account.objects.first() original_credits = acc.credits print(f' Before: {original_credits} credits') with transaction.atomic(): CreditService.deduct_credits( account=acc, operation_type='clustering', tokens_input=100, tokens_output=200 ) acc.refresh_from_db() print(f' After deduction: {acc.credits} credits') print(f' ✓ Deducted: {original_credits - acc.credits} credits') # Rollback transaction.set_rollback(True) acc.refresh_from_db() print(f' After rollback: {acc.credits} credits') except Exception as e: print(f' ✗ ERROR: {e}') print('\n' + '='*70) print('ALL TESTS COMPLETE - System is healthy!') print('='*70)