Files
zellyy-finance/src/lib/supabase/tests/index.ts
gpt-engineer-app[bot] 971a1d29e5 Refactor SupabaseConnectionTest component
The SupabaseConnectionTest component was refactored into smaller, more manageable components to improve readability and maintainability.
2025-03-15 12:45:12 +00:00

93 lines
3.1 KiB
TypeScript

import { testAuth } from './authTests';
import { testRestApi } from './apiTests';
import { testDatabaseConnection } from './databaseTests';
import { TestResults, TestDebugInfo } from './types';
import { supabase, isValidUrl } from '../client';
import { getSupabaseUrl, getSupabaseKey, isCorsProxyEnabled, getProxyType } from '../config';
export const testSupabaseConnection = async (): Promise<TestResults> => {
// 기본 결과 객체 초기화
const results: TestResults = {
url: getSupabaseUrl(),
proxyUrl: '', // 빈 문자열로 초기화
usingProxy: isCorsProxyEnabled(),
proxyType: getProxyType(),
client: true,
restApi: false,
auth: false,
database: false,
errors: [],
debugInfo: {
originalUrl: getSupabaseUrl(),
proxyUrl: '',
usingProxy: isCorsProxyEnabled(),
proxyType: getProxyType(),
keyLength: getSupabaseKey().length,
browserInfo: navigator.userAgent,
timestamp: new Date().toISOString(),
backupProxySuccess: false,
lastErrorDetails: ''
}
};
try {
// 클라이언트 유효성 체크
if (!supabase) {
results.client = false;
results.errors.push('Supabase 클라이언트 초기화 실패');
return results;
}
// CORS 프록시 URL 설정
if (results.usingProxy) {
const baseUrl = getSupabaseUrl();
const proxyType = getProxyType();
if (proxyType === 'corsproxy.io') {
results.proxyUrl = `https://corsproxy.io/?${encodeURIComponent(baseUrl)}`;
} else if (proxyType === 'cors-anywhere') {
results.proxyUrl = `https://cors-anywhere.herokuapp.com/${baseUrl}`;
} else {
results.proxyUrl = baseUrl; // 기본값
}
// debugInfo에도 proxyUrl 설정
results.debugInfo.proxyUrl = results.proxyUrl;
} else {
results.proxyUrl = results.url; // 프록시 사용 안 함
results.debugInfo.proxyUrl = results.url;
}
// 테스트 실행
const authResults = await testAuth(supabase);
const apiResults = await testRestApi(supabase);
const dbResults = await testDatabaseConnection(supabase);
// 결과 업데이트
results.auth = authResults.success;
results.restApi = apiResults.success;
results.database = dbResults.success;
// 오류 수집
if (!authResults.success && authResults.error) {
results.errors.push(`인증 테스트 실패: ${authResults.error}`);
results.debugInfo.lastErrorDetails += `인증: ${authResults.error}; `;
}
if (!apiResults.success && apiResults.error) {
results.errors.push(`REST API 테스트 실패: ${apiResults.error}`);
results.debugInfo.lastErrorDetails += `API: ${apiResults.error}; `;
}
if (!dbResults.success && dbResults.error) {
results.errors.push(`DB 테스트 실패: ${dbResults.error}`);
results.debugInfo.lastErrorDetails += `DB: ${dbResults.error}; `;
}
} catch (error: any) {
const errorMsg = `테스트 실행 오류: ${error.message || '알 수 없는 오류'}`;
results.errors.push(errorMsg);
results.debugInfo.lastErrorDetails = errorMsg;
}
return results;
};