contract-clause-analyzer / tests /test_risk_analysis.py
satomitheito's picture
hf streamlit
908ff10
"""Test the risk analysis agent with sample classified clauses."""
import json
import os
from agents.risk_analysis_agent import analyze_risk, risk_analysis_node
OUTPUT_DIR = os.path.join(os.path.dirname(__file__), "..", "outputs")
SAMPLE_CLASSIFIED_CLAUSES = [
{
"id": 1,
"text": "This Agreement shall be governed by and construed in accordance with the laws of the State of Delaware.",
"section": "General",
"clause_type": "Governing Law",
"confidence": 0.99,
},
{
"id": 2,
"text": "During the term of this Agreement and for a period of two years thereafter, the Employee shall not directly or indirectly engage in any business that competes with the Company.",
"section": "Restrictive Covenants",
"clause_type": "Non-Compete",
"confidence": 0.95,
},
{
"id": 3,
"text": "Either party may terminate this Agreement at any time upon thirty (30) days prior written notice to the other party.",
"section": "Termination",
"clause_type": "Termination For Convenience",
"confidence": 0.95,
},
{
"id": 4,
"text": "The Company shall indemnify and hold harmless the Contractor from any claims, damages, or expenses arising from the Company's breach of this Agreement.",
"section": "Liability",
"clause_type": "Indemnification",
"confidence": 0.95,
},
{
"id": 5,
"text": "The Licensor grants to the Licensee a non-exclusive, non-transferable license to use the Software solely for internal business purposes.",
"section": "License",
"clause_type": "Non-Transferable License",
"confidence": 0.95,
},
]
def test_single_risk():
"""Test risk analysis for a single clause."""
clause = SAMPLE_CLASSIFIED_CLAUSES[1] # Non-compete clause
result = analyze_risk(clause["text"], clause["clause_type"])
print(f"Clause: {clause['text'][:80]}...")
print(f"Type: {clause['clause_type']}")
print(f"Risk: {result['risk_score']}")
print(f"Factors: {result['risk_factors']}")
print(f"Why: {result['reasoning']}")
print()
def test_risk_analysis_node():
"""Test the full LangGraph node with multiple clauses."""
state = {"classified_clauses": SAMPLE_CLASSIFIED_CLAUSES}
result = risk_analysis_node(state)
print(f"{'ID':<4} {'Type':<30} {'Risk':<8} {'Factors'}")
print("-" * 90)
for clause in result["risk_scores"]:
factors = "; ".join(clause.get("risk_factors", []))
print(f"{clause['id']:<4} {clause['clause_type']:<30} {clause['risk_score']:<8.2f} {factors[:50]}")
output_path = os.path.join(OUTPUT_DIR, "risk_analysis_results.json")
with open(output_path, "w") as f:
json.dump(result["risk_scores"], f, indent=2)
print(f"\nResults saved to {output_path}")
if __name__ == "__main__":
print("Single Clause Risk Test\n")
test_single_risk()
print("Full Node Test\n")
test_risk_analysis_node()