fix(frontend): fixes library health pinging even when the user is not authenticated (#385)

* fix(frontend): library health pings even not authenticated

* test

* authInitialized guard

* subscription
This commit is contained in:
NamLe029
2026-04-22 17:27:05 -04:00
committed by Zach Yale
parent 91c19739dc
commit d502c18948
5 changed files with 57 additions and 30 deletions
@@ -31,16 +31,16 @@ describe('LibraryHealthService', () => {
service = TestBed.inject(LibraryHealthService);
});
async function initializeService(): Promise<void> {
async function fetchHealth(): Promise<void> {
const fetchQuerySpy = vi.spyOn(queryClient, 'fetchQuery');
service.initialize();
service.fetchHealth();
await fetchQuerySpy.mock.results[0]?.value;
}
it('fetches initial health on initialize', async () => {
it('fetches health', async () => {
httpGetSpy.mockReturnValue(of({1: true, 2: false}));
await initializeService();
await fetchHealth();
expect(httpGetSpy).toHaveBeenCalledOnce();
expect(service.health()).toEqual({1: true, 2: false});
@@ -49,7 +49,7 @@ describe('LibraryHealthService', () => {
it('reports unhealthy for a library with false health', async () => {
httpGetSpy.mockReturnValue(of({1: true, 2: false}));
await initializeService();
await fetchHealth();
expect(service.isUnhealthy(2)).toBe(true);
});
@@ -57,7 +57,7 @@ describe('LibraryHealthService', () => {
it('reports healthy for a library with true health', async () => {
httpGetSpy.mockReturnValue(of({1: true}));
await initializeService();
await fetchHealth();
expect(service.isUnhealthy(1)).toBe(false);
});
@@ -65,15 +65,13 @@ describe('LibraryHealthService', () => {
it('reports false for an unknown library', async () => {
httpGetSpy.mockReturnValue(of({}));
await initializeService();
await fetchHealth();
expect(service.isUnhealthy(99)).toBe(false);
});
it('updates state from websocket messages', async () => {
httpGetSpy.mockReturnValue(of({1: true}));
await initializeService();
service.initWebsocket();
wsSubject.next({body: JSON.stringify({libraryHealth: {1: false}})} as IMessage);
expect(service.isUnhealthy(1)).toBe(true);
@@ -1,4 +1,5 @@
import {computed, inject, Injectable} from '@angular/core';
import {computed, inject, Injectable, DestroyRef} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {HttpClient} from '@angular/common/http';
import {lastValueFrom} from 'rxjs';
import {RxStompService} from '../../../shared/websocket/rx-stomp.service';
@@ -13,6 +14,9 @@ export class LibraryHealthService {
private http = inject(HttpClient);
private rxStompService = inject(RxStompService);
private queryClient = inject(QueryClient);
private destroyRef = inject(DestroyRef);
private socketInitialized = false;
private healthQuery = injectQuery(() => ({
...this.getHealthQueryOptions(),
@@ -28,13 +32,20 @@ export class LibraryHealthService {
});
}
initialize(): void {
fetchHealth(): void {
void this.queryClient.fetchQuery(this.getHealthQueryOptions());
}
this.rxStompService.watch('/topic/library-health').subscribe(msg => {
const payload = JSON.parse(msg.body);
this.queryClient.setQueryData(LIBRARY_HEALTH_QUERY_KEY, payload.libraryHealth);
});
initWebsocket(): void {
if (this.socketInitialized) return;
this.rxStompService.watch('/topic/library-health')
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(msg => {
const payload = JSON.parse(msg.body);
this.queryClient.setQueryData(LIBRARY_HEALTH_QUERY_KEY, payload.libraryHealth);
});
this.socketInitialized = true;
}
isUnhealthy(libraryId: number): boolean {