File size: 4,339 Bytes
3eedfc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import appwritePRSService from './services/appwritePrsService.js';

async function testValidMP() {
  console.log('\n=== Testing Valid MP Data ===');
  try {
    const result = await appwritePRSService.getMemberData('Narendra Modi', 'MP', 'Varanasi', 'Uttar Pradesh');
    console.log('Result:', JSON.stringify(result, null, 2));
    console.log('Found:', result.found);
    console.log('Party:', result.party);
    console.log('Constituency:', result.constituency);
  } catch (error) {
    console.error('Error:', error.message);
  }
}

async function testInvalidName() {
  console.log('\n=== Testing Invalid Name ===');
  try {
    const result = await appwritePRSService.getMemberData('Invalid Name XYZ', 'MP');
    console.log('Result:', JSON.stringify(result, null, 2));
    console.log('Found:', result.found);
  } catch (error) {
    console.error('Error:', error.message);
  }
}

async function testMLA() {
  console.log('\n=== Testing MLA Data ===');
  try {
    const result = await appwritePRSService.getMemberData('Yogi Adityanath', 'MLA', 'Gorakhpur', 'Uttar Pradesh');
    console.log('Result:', JSON.stringify(result, null, 2));
    console.log('Found:', result.found);
    console.log('Party:', result.party);
  } catch (error) {
    console.error('Error:', error.message);
  }
}

async function testCacheHit() {
  console.log('\n=== Testing Cache Hit ===');
  try {
    console.log('First call:');
    const result1 = await appwritePRSService.getMemberData('Narendra Modi', 'MP', 'Varanasi', 'Uttar Pradesh');
    console.log('Found:', result1.found, 'Party:', result1.party);

    console.log('Second call (should be from cache):');
    const result2 = await appwritePRSService.getMemberData('Narendra Modi', 'MP', 'Varanasi', 'Uttar Pradesh');
    console.log('Found:', result2.found, 'Party:', result2.party);

    console.log('Results match:', JSON.stringify(result1) === JSON.stringify(result2));
  } catch (error) {
    console.error('Error:', error.message);
  }
}

async function testHealthCheck() {
  console.log('\n=== Testing Health Check ===');
  try {
    const health = await appwritePRSService.healthCheck();
    console.log('Health Status:', JSON.stringify(health, null, 2));
  } catch (error) {
    console.error('Error:', error.message);
  }
}

async function testStats() {
  console.log('\n=== Testing Stats ===');
  try {
    const stats = appwritePRSService.getStats();
    console.log('Stats:', JSON.stringify(stats, null, 2));
  } catch (error) {
    console.error('Error:', error.message);
  }
}

async function testRawResponse() {
  console.log('\n=== Testing Raw Appwrite Response ===');
  try {
    const response = await fetch('https://cloud.appwrite.io/v1/functions/68ffcb25003df2ce3663/executions', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Appwrite-Project': process.env.APPWRITE_PROJECT_ID,
        'X-Appwrite-Key': process.env.APPWRITE_API_KEY
      },
      body: JSON.stringify({
        name: 'Narendra Modi',
        type: 'MP',
        constituency: 'Varanasi',
        state: 'Uttar Pradesh'
      })
    });

    const rawResult = await response.json();
    console.log('Raw Response:', JSON.stringify(rawResult, null, 2));
    console.log('Response Status:', response.status);
    console.log('Response OK:', response.ok);

    if (rawResult.responseBody) {
      try {
        const actualData = JSON.parse(rawResult.responseBody);
        console.log('Parsed Response Body:', JSON.stringify(actualData, null, 2));
      } catch (parseError) {
        console.log('Response Body (raw string):', rawResult.responseBody);
      }
    }
  } catch (error) {
    console.error('Error fetching raw response:', error.message);
  }
}

async function runTests() {
  console.log('Starting Appwrite Data Fetching Tests...\n');

  await testHealthCheck();
  await testStats();

  await testRawResponse();

  await testValidMP();
  await testInvalidName();
  await testMLA();
  await testCacheHit();

  console.log('\n=== Final Stats ===');
  const finalStats = appwritePRSService.getStats();
  console.log('Final Stats:', JSON.stringify(finalStats, null, 2));

  console.log('\nTests completed.');
}


runTests().catch(console.error);

export { runTests, testValidMP, testInvalidName, testMLA, testCacheHit, testHealthCheck, testStats };