fix(connection): retry connect ping before unreachable (#1135)

This commit is contained in:
cucadmuh
2026-06-20 17:05:57 +03:00
committed by GitHub
parent d8e5d4eed4
commit b9b4f76c11
4 changed files with 106 additions and 19 deletions
+17 -2
View File
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { renderHook, act, waitFor } from '@testing-library/react';
import { resetAuthStore } from '@/test/helpers/storeReset';
import { useAuthStore } from '@/store/authStore';
@@ -27,6 +27,10 @@ beforeEach(() => {
vi.mocked(pingWithCredentials).mockReset();
});
afterEach(() => {
vi.useRealTimers();
});
function seedDualAddressServer(): string {
const id = useAuthStore.getState().addServer({
name: 'Home',
@@ -57,6 +61,7 @@ describe('useConnectionStatus.isLan', () => {
});
it('falls back to public when only the public address answers', async () => {
vi.useFakeTimers();
seedDualAddressServer();
vi.mocked(pingWithCredentials).mockImplementation(async url =>
url === 'https://music.example.com'
@@ -65,7 +70,12 @@ describe('useConnectionStatus.isLan', () => {
);
const { result } = renderHook(() => useConnectionStatus());
await waitFor(() => expect(result.current.status).toBe('connected'));
await act(async () => {
for (let i = 0; i < 2; i++) {
await vi.advanceTimersByTimeAsync(2000);
}
});
expect(result.current.status).toBe('connected');
// primary url is `https://music.example.com` — public. isLanUrl alone
// would have said `false` for the wrong reason (because the primary
// happens to be public); the test is meaningful because the LAN side
@@ -126,9 +136,14 @@ describe('useConnectionStatus online event', () => {
: { ok: false },
);
vi.useFakeTimers();
await act(async () => {
window.dispatchEvent(new Event('online'));
for (let i = 0; i < 2; i++) {
await vi.advanceTimersByTimeAsync(2000);
}
});
vi.useRealTimers();
await waitFor(() => expect(result.current.isLan).toBe(false));
// Both endpoints were probed (LAN refused, public answered).