import os import requests import json import logging import sys # Configure to run from root context sys.path.append(os.path.join(os.getcwd(), "connector-superoffice")) # Mock Config if needed, or use real one try: from config import settings except ImportError: print("Could not import settings. Ensure you are in project root.") sys.exit(1) # FORCE CE URL for internal Docker comms if running inside container # If running outside, this might need localhost. # settings.COMPANY_EXPLORER_URL is used. API_USER = os.getenv("API_USER", "admin") API_PASS = os.getenv("API_PASSWORD", "gemini") def test_dynamic_role_change(): print("🧪 STARTING TEST: Dynamic Role Change & Content Generation\n") # Define Scenarios scenarios = [ { "name": "Scenario A (CEO)", "job_title": "Geschäftsführer", "expect_keywords": ["Kostenreduktion", "Effizienz", "Amortisation"] }, { "name": "Scenario B (Warehouse Mgr)", "job_title": "Lagerleiter", "expect_keywords": ["Stress", "Sauberkeit", "Entlastung"] } ] results = {} for s in scenarios: print(f"--- Running {s['name']} ---") print(f"Role Trigger: '{s['job_title']}'") payload = { "so_contact_id": 2, # RoboPlanet Test "so_person_id": 2, "crm_name": "RoboPlanet GmbH-SOD", "crm_website": "www.roboplanet.de", # Ensure we match the industry (Logistics) "job_title": s['job_title'] } try: url = f"{settings.COMPANY_EXPLORER_URL}/api/provision/superoffice-contact" print(f"POST {url}") resp = requests.post(url, json=payload, auth=(API_USER, API_PASS)) resp.raise_for_status() data = resp.json() # Validation texts = data.get("texts", {}) subject = texts.get("subject", "") intro = texts.get("intro", "") print(f"Received Role: {data.get('role_name')}") print(f"Received Subject: {subject}") # Check Keywords full_text = (subject + " " + intro).lower() matches = [k for k in s['expect_keywords'] if k.lower() in full_text] if len(matches) > 0: print(f"✅ Content Match! Found keywords: {matches}") results[s['name']] = "PASS" else: print(f"❌ Content Mismatch. Expected {s['expect_keywords']}, got text: {subject}...") results[s['name']] = "FAIL" results[f"{s['name']}_Subject"] = subject # Store for comparison later except Exception as e: print(f"❌ API Error: {e}") results[s['name']] = "ERROR" print("") # Final Comparison print("--- Final Result Analysis ---") if results["Scenario A (CEO)"] == "PASS" and results["Scenario B (Warehouse Mgr)"] == "PASS": if results["Scenario A (CEO)_Subject"] != results["Scenario B (Warehouse Mgr)_Subject"]: print("✅ SUCCESS: Different roles generated different, targeted content.") else: print("⚠️ WARNING: Content matched keywords but Subjects are identical! Check Matrix.") else: print("❌ TEST FAILED. See individual steps.") if __name__ == "__main__": test_dynamic_role_change()