Fix PWA offline fallback blocking localhost access when internet is disconnected (#2669) (#2784)

This commit is contained in:
ACX
2026-02-17 16:17:07 -07:00
committed by GitHub
parent 4062f1f83f
commit 43094bb8ef
4 changed files with 179 additions and 8 deletions
@@ -0,0 +1,60 @@
import {beforeEach, describe, expect, it, vi} from 'vitest';
import {TestBed} from '@angular/core/testing';
import {initializeAuthFactory} from './auth-initializer';
import {AuthInitializationService} from './auth-initialization-service';
import {OAuthService} from 'angular-oauth2-oidc';
import {AuthService} from '../../shared/service/auth.service';
import {AppSettingsService, PublicAppSettings} from '../../shared/service/app-settings.service';
import {BehaviorSubject} from 'rxjs';
describe('initializeAuthFactory', () => {
let authInitService: AuthInitializationService;
let publicSettingsSubject: BehaviorSubject<PublicAppSettings | null>;
beforeEach(() => {
publicSettingsSubject = new BehaviorSubject<PublicAppSettings | null>(null);
TestBed.configureTestingModule({
providers: [
{provide: OAuthService, useValue: {configure: vi.fn(), loadDiscoveryDocumentAndTryLogin: vi.fn()}},
{provide: AuthService, useValue: {tokenSubject: {next: vi.fn()}}},
{provide: AppSettingsService, useValue: {publicAppSettings$: publicSettingsSubject.asObservable()}},
AuthInitializationService,
]
});
authInitService = TestBed.inject(AuthInitializationService);
});
it('should proceed with auth initialization when navigator.onLine is false', async () => {
const markSpy = vi.spyOn(authInitService, 'markAsInitialized');
Object.defineProperty(navigator, 'onLine', {value: false, configurable: true});
const factory = TestBed.runInInjectionContext(() => initializeAuthFactory());
const initPromise = TestBed.runInInjectionContext(() => factory());
publicSettingsSubject.next({oidcEnabled: false, remoteAuthEnabled: false, oidcProviderDetails: null!});
await initPromise;
expect(markSpy).toHaveBeenCalled();
Object.defineProperty(navigator, 'onLine', {value: true, configurable: true});
});
it('should initialize normally when navigator.onLine is true', async () => {
const markSpy = vi.spyOn(authInitService, 'markAsInitialized');
Object.defineProperty(navigator, 'onLine', {value: true, configurable: true});
const factory = TestBed.runInInjectionContext(() => initializeAuthFactory());
const initPromise = TestBed.runInInjectionContext(() => factory());
publicSettingsSubject.next({oidcEnabled: false, remoteAuthEnabled: false, oidcProviderDetails: null!});
await initPromise;
expect(markSpy).toHaveBeenCalled();
});
});
@@ -27,12 +27,6 @@ export function initializeAuthFactory() {
const authInitService = inject(AuthInitializationService);
return new Promise<void>((resolve) => {
if (!navigator.onLine) {
console.warn('[Auth] App is offline, skipping auth initialization');
authInitService.markAsInitialized();
resolve();
return;
}
const settingsTimeout = setTimeout(() => {
console.warn('[Auth] Public settings fetch timed out, falling back to local auth');