File size: 6,338 Bytes
3998131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from fastapi.testclient import TestClient
from api.main import app
import os

client = TestClient(app)

def test_api_endpoints():
    print("=== Testing API Endpoints ===")
    
    # 1. Test Root
    response = client.get("/")
    assert response.status_code == 200
    print("[PASS] Root Endpoint")

    # 2. Test Law Explanation (Module A)
    # Mocking or assuming Module A works. 
    # If Vector DB is empty for Module A, it might return empty sources but should not crash.
    print("\n[Testing] /api/v1/explain")
    payload = {"query": "How to get citizenship?"}
    try:
        response = client.post("/api/v1/explain", json=payload)
        if response.status_code == 200:
            data = response.json()
            print(f"[PASS] Law Explanation: {data.get('summary', 'No summary')[:50]}...")
        else:
            print(f"[FAIL] Law Explanation: {response.status_code} - {response.text}")
    except Exception as e:
        print(f"[ERROR] Law Explanation: {e}")

    # 3. Test Letter Generation (Module C)
    print("\n[Testing] /api/v1/generate-letter")
    payload = {
        "description": "I need a citizenship certificate for my son",
        "additional_data": {"Date": "2081-01-01", "District": "Kathmandu"}
    }
    
    # Check for API Key
    if not os.getenv("MISTRAL_API_KEY"):
        print("[WARN] MISTRAL_API_KEY not set. Skipping generation test to avoid failure.")
    else:
        try:
            response = client.post("/api/v1/generate-letter", json=payload)
            if response.status_code == 200:
                data = response.json()
                if data['success']:
                    print(f"[PASS] Letter Generation: {data.get('template_used')}")
                else:
                    print(f"[FAIL] Letter Generation: {data.get('error')}")
            else:
                print(f"[FAIL] Letter Generation: {response.status_code} - {response.text}")
        except Exception as e:
            print(f"[ERROR] Letter Generation: {e}")

    # 4. Test Analyze Requirements (Module C)
    print("\n[Testing] /api/v1/analyze-requirements")
    payload = {"description": "I need a citizenship certificate"}
    try:
        response = client.post("/api/v1/analyze-requirements", json=payload)
        if response.status_code == 200:
            data = response.json()
            if data['success']:
                print(f"[PASS] Analysis: Detected {len(data.get('detected_placeholders', []))} placeholders")
                print(f"      Missing {data.get('missing_fields')}")
                
                # 5. Test Generate with Specific Template (New Flow)
                template_name = data.get('template_used')
                if template_name:
                    print(f"\n[Testing] /api/v1/generate-letter (Direct Template: {template_name})")
                    payload_direct = {
                        "description": "I need a citizenship certificate",
                        "template_name": template_name,
                        "additional_data": {"Date": "2081-01-01", "District": "Kathmandu"}
                    }
                    if os.getenv("MISTRAL_API_KEY"):
                        resp_direct = client.post("/api/v1/generate-letter", json=payload_direct)
                        if resp_direct.status_code == 200:
                            data_direct = resp_direct.json()
                            if data_direct['success'] and data_direct['template_used'] == template_name:
                                print(f"[PASS] Direct Generation Successful")
                            else:
                                print(f"[FAIL] Direct Generation: {data_direct.get('error')}")
            else:
                print(f"[FAIL] Analysis: {data.get('error')}")
        else:
            print(f"[FAIL] Analysis: {response.status_code} - {response.text}")
    except Exception as e:
        print(f"[ERROR] Analysis: {e}")

    # 6. Test Granular APIs (3-Step Flow)
    print("\n[Testing] Granular API Flow")
    
    # Step 1: Search
    print("  1. Search Template")
    search_payload = {"query": "I need a citizenship certificate"}
    template_name = None
    try:
        resp = client.post("/api/v1/search-template", json=search_payload)
        if resp.status_code == 200:
            data = resp.json()
            if data['success']:
                template_name = data['template_name']
                print(f"     [PASS] Found: {template_name}")
            else:
                print(f"     [FAIL] Search: {data.get('error')}")
        else:
            print(f"     [FAIL] Search Status: {resp.status_code}")
    except Exception as e:
        print(f"     [ERROR] Search: {e}")

    if template_name:
        # Step 2: Get Details
        print("  2. Get Template Details")
        details_payload = {"template_name": template_name}
        placeholders = []
        try:
            resp = client.post("/api/v1/get-template-details", json=details_payload)
            if resp.status_code == 200:
                data = resp.json()
                if data['success']:
                    placeholders = data['placeholders']
                    print(f"     [PASS] Placeholders: {placeholders}")
                else:
                    print(f"     [FAIL] Details: {data.get('error')}")
            else:
                print(f"     [FAIL] Details Status: {resp.status_code}")
        except Exception as e:
            print(f"     [ERROR] Details: {e}")

        # Step 3: Fill Template
        print("  3. Fill Template")
        # Create dummy data for all placeholders
        fill_data = {p: "TEST_VALUE" for p in placeholders}
        fill_payload = {"template_name": template_name, "placeholders": fill_data}
        try:
            resp = client.post("/api/v1/fill-template", json=fill_payload)
            if resp.status_code == 200:
                data = resp.json()
                if data['success']:
                    print(f"     [PASS] Letter Generated (Length: {len(data['letter'])})")
                else:
                    print(f"     [FAIL] Fill: {data.get('error')}")
            else:
                print(f"     [FAIL] Fill Status: {resp.status_code}")
        except Exception as e:
            print(f"     [ERROR] Fill: {e}")

if __name__ == "__main__":
    test_api_endpoints()