(src)
| 2287 | }; |
| 2288 | |
| 2289 | const runSourceTest = async (src) => { |
| 2290 | const id = String(src.id || ''); |
| 2291 | if (!id) return null; |
| 2292 | const current = getTestStatus(id); |
| 2293 | if (current.state === 'testing') { |
| 2294 | return null; |
| 2295 | } |
| 2296 | setTestStatus(id, { state: 'testing', message: '', checks: [], capabilities: {}, limited: false }); |
| 2297 | const controller = new AbortController(); |
| 2298 | const timer = setTimeout(() => controller.abort(), 25000); |
| 2299 | try { |
| 2300 | const res = await fetch(withBase('/api/pro/sources/test.php'), { |
| 2301 | method: 'POST', |
| 2302 | credentials: 'include', |
| 2303 | headers: { |
| 2304 | 'Content-Type': 'application/json', |
| 2305 | 'X-CSRF-Token': getCsrf(), |
| 2306 | 'Accept': 'application/json', |
| 2307 | }, |
| 2308 | body: JSON.stringify({ id }), |
| 2309 | signal: controller.signal, |
| 2310 | }); |
| 2311 | const data = await safeJson(res); |
| 2312 | const checks = normalizeTestChecks(data && data.checks); |
| 2313 | const capabilities = normalizeCapabilities(data && data.capabilities); |
| 2314 | const limited = !!(data && data.limited); |
| 2315 | if (!data || data.ok !== true) { |
| 2316 | const msg = String((data && (data.error || data.message)) || tf('source_test_error', 'Test failed')).trim(); |
| 2317 | setTestStatus(id, { state: 'error', message: msg, checks, capabilities, limited: false }); |
| 2318 | showToast(msg, 5000); |
| 2319 | return false; |
| 2320 | } |
| 2321 | |
| 2322 | const msg = data && data.message ? String(data.message) : ''; |
| 2323 | setTestStatus(id, { |
| 2324 | state: limited ? 'warning' : 'ok', |
| 2325 | message: msg, |
| 2326 | checks, |
| 2327 | capabilities, |
| 2328 | limited, |
| 2329 | }); |
| 2330 | if (limited) { |
| 2331 | const warn = msg || tf('source_test_limited', 'Connected (limited)'); |
| 2332 | showToast(warn, 5000); |
| 2333 | } |
| 2334 | return true; |
| 2335 | } catch (err) { |
| 2336 | const timedOut = err && err.name === 'AbortError'; |
| 2337 | const msg = timedOut |
| 2338 | ? tf('source_test_timeout', 'Test timed out.') |
| 2339 | : (err?.message || tf('source_test_error', 'Test failed')); |
| 2340 | setTestStatus(id, { state: 'error', message: msg, checks: [], capabilities: {}, limited: false }); |
| 2341 | showToast(msg, 5000); |
| 2342 | return false; |
| 2343 | } finally { |
| 2344 | clearTimeout(timer); |
| 2345 | } |
| 2346 | }; |
no test coverage detected