File size: 3,308 Bytes
25f22bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import api from '../services/api';

// Test function to check API response structure
export const testApiStructure = async () => {
  try {
    console.log('🧪 [TEST] Starting API structure test...');
    
    // Test accounts endpoint
    console.log('🧪 [TEST] Testing /accounts endpoint...');
    const response = await api.get('/accounts');
    
    console.log('🧪 [TEST] Raw response:', response);
    console.log('🧪 [TEST] Response data:', response.data);
    console.log('🧪 [TEST] Response data type:', typeof response.data);
    console.log('🧪 [TEST] Response data is array:', Array.isArray(response.data));
    
    if (response.data) {
      console.log('🧪 [TEST] Response data keys:', Object.keys(response.data));
      
      if (response.data.success) {
        console.log('🧪 [TEST] Response is successful');
        if (response.data.accounts) {
          console.log('🧪 [TEST] Accounts found:', response.data.accounts.length);
          console.log('🧪 [TEST] Accounts type:', typeof response.data.accounts);
          console.log('🧪 [TEST] Accounts is array:', Array.isArray(response.data.accounts));
          
          // Filter LinkedIn accounts
          const linkedinAccounts = response.data.accounts.filter(
            account => account && account.social_network && account.social_network.toLowerCase() === 'linkedin'
          );
          console.log('🧪 [TEST] LinkedIn accounts found:', linkedinAccounts.length);
          console.log('🧪 [TEST] LinkedIn accounts:', linkedinAccounts);
        }
      } else {
        console.log('🧪 [TEST] Response not successful');
      }
    }
    
    return response;
  } catch (error) {
    console.error('🧪 [TEST] API test failed:', error);
    return error;
  }
};

// Test function to simulate the service behavior
export const testServiceBehavior = async () => {
  try {
    console.log('🧪 [TEST] Testing service behavior...');
    
    const response = await api.get('/accounts');
    
    // Simulate the service logic
    let accounts = [];
    
    if (response.data && response.data.success && response.data.accounts) {
      console.log('🧪 [TEST] Format 1: success + accounts structure');
      accounts = response.data.accounts;
    } else if (Array.isArray(response.data)) {
      console.log('🧪 [TEST] Format 2: Direct array');
      accounts = response.data;
    } else {
      console.error('🧪 [TEST] Unexpected response format:', response.data);
      throw new Error('Unexpected response format from API');
    }
    
    console.log('🧪 [TEST] All accounts:', accounts);
    
    // Filter for LinkedIn accounts only
    const linkedinAccounts = accounts.filter(
      account => account && account.social_network && account.social_network.toLowerCase() === 'linkedin'
    );
    
    console.log('🧪 [TEST] Filtered LinkedIn accounts:', linkedinAccounts);
    return linkedinAccounts;
  } catch (error) {
    console.error('🧪 [TEST] Service test failed:', error);
    return [];
  }
};

// Run tests
// if (typeof window !== 'undefined') {
//   // Browser environment - run tests on load
//   window.addEventListener('load', () => {
//     console.log('🧪 [TEST] Running API tests...');
//     testApiStructure();
//     testServiceBehavior();
//   });
// }