Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ describe('AppComponent', () => {
natsLiveMessage$.next({data: {}} as NatsMessage);
await fixture.whenStable();
expect(mockToastService.showToast).not.toHaveBeenCalled();

// Suppression window should short-circuit before validation and toast rendering.
component['suppressLiveToastUntil'] = Date.now() + 5000;
mockNatsService.isValidMessage.and.returnValue(true);
natsLiveMessage$.next({data: {}} as NatsMessage);
await fixture.whenStable();
expect(mockToastService.showToast).not.toHaveBeenCalled();

component['suppressLiveToastUntil'] = 0;
mockNatsService.isValidMessage.and.returnValue(true);
natsLiveMessage$.next({data: {}} as NatsMessage);
await fixture.whenStable();
Expand Down
12 changes: 11 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class AppComponent implements OnInit, OnDestroy {
private readonly natsUrl: string | undefined;
private unreadMessagesCountSubscription: Subscription | undefined;
private liveMessageSubscription: Subscription | undefined;
private hasShownInitialNotificationToast = false;
private suppressLiveToastUntil = 0;
headerVariant: string = AppShellConfig.headerVariant;
applicationLogo: string = AppShellConfig.applicationLogo;
applicationName: string = AppShellConfig.applicationName;
Expand Down Expand Up @@ -334,16 +336,20 @@ export class AppComponent implements OnInit, OnDestroy {
// validBucketRe = regexp.MustCompile(^[a-zA-Z0-9_-]+$)
// validKeyRe = regexp.MustCompile(^[-/_=.a-zA-Z0-9]+$)
const natsUser = user.username.split('@')[0].replaceAll(/[^a-zA-Z0-9_-]/g, '_')
this.hasShownInitialNotificationToast = false;
this.suppressLiveToastUntil = 0;
this.natsService.initializeUser(natsUser, user.projects).then(() => {
setTimeout(() => {
if(this.appShellNotificationsCount > 0) {
if(this.appShellNotificationsCount > 0 && !this.hasShownInitialNotificationToast) {
const notification = {
id: Date.now().toString() + '-logged',
title: `You have ${this.appShellNotificationsCount} unread notifications`,
read: false,
subject: 'only-toast'
} as AppShellNotification;
this.toastService.showToast(notification, 8000);
this.hasShownInitialNotificationToast = true;
this.suppressLiveToastUntil = Date.now() + 5000;
}
}, 1000);
});
Expand All @@ -358,6 +364,9 @@ export class AppComponent implements OnInit, OnDestroy {
if (!message?.data) {
return;
}
if (Date.now() < this.suppressLiveToastUntil) {
return;
}
try {
if (this.natsService.isValidMessage(message.data)) {
console.log('Received valid message:', message);
Expand All @@ -371,6 +380,7 @@ export class AppComponent implements OnInit, OnDestroy {
};
// If you want to show the actual notification, you can show message.data instead of notification
this.toastService.showToast(notification, 8000);
this.hasShownInitialNotificationToast = true;
} else {
console.log('Invalid message format:', message);
}
Expand Down
12 changes: 9 additions & 3 deletions src/app/services/catalog-resolver.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('CatalogResolver', () => {
let catalogServiceSpy: jasmine.SpyObj<CatalogService>;

beforeEach(() => {
catalogServiceSpy = jasmine.createSpyObj('CatalogService', ['getCatalogDescriptors', 'retrieveCatalogDescriptors']);
catalogServiceSpy = jasmine.createSpyObj('CatalogService', ['getCatalogDescriptors', 'retrieveCatalogDescriptors', 'setCatalogDescriptors']);

TestBed.configureTestingModule({
providers: [
Expand All @@ -27,16 +27,21 @@ describe('CatalogResolver', () => {
});

it('should resolve catalog descriptors calling the service if not present in memory', () => {
const descriptors = [{} as CatalogDescriptor];
catalogServiceSpy.getCatalogDescriptors.and.returnValue([]);
catalogServiceSpy.retrieveCatalogDescriptors.and.returnValue(of([{} as CatalogDescriptor]));
service.resolve();
catalogServiceSpy.retrieveCatalogDescriptors.and.returnValue(of(descriptors));

service.resolve().subscribe();

expect(catalogServiceSpy.retrieveCatalogDescriptors).toHaveBeenCalled();
expect(catalogServiceSpy.setCatalogDescriptors).toHaveBeenCalledWith(descriptors);
});

it('should resolve catalog descriptors without calling the service if present in memory', () => {
catalogServiceSpy.getCatalogDescriptors.and.returnValue([{} as CatalogDescriptor]);
service.resolve();
expect(catalogServiceSpy.retrieveCatalogDescriptors).not.toHaveBeenCalled();
expect(catalogServiceSpy.setCatalogDescriptors).not.toHaveBeenCalled();
});

it('should handle errors from retrieveCatalogDescriptors and return empty array', (done) => {
Expand All @@ -52,6 +57,7 @@ describe('CatalogResolver', () => {
service.resolve().subscribe(result => {
expect(result).toEqual([]);
expect(console.error).toHaveBeenCalledWith('Error retrieving catalog descriptors', error);
expect(catalogServiceSpy.setCatalogDescriptors).toHaveBeenCalledWith([]);
done();
});
});
Expand Down
8 changes: 6 additions & 2 deletions src/app/services/catalog-resolver.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
import { catchError, Observable, of } from 'rxjs';
import { catchError, Observable, of, tap } from 'rxjs';
import { CatalogService } from './catalog.service';
import { CatalogDescriptor } from '../openapi/component-catalog';

Expand All @@ -14,10 +14,14 @@ export class CatalogResolver implements Resolve<CatalogDescriptor[]> {
const catalogDescriptors = this.catalogService.getCatalogDescriptors();
if (catalogDescriptors.length > 0) {
return of(catalogDescriptors);
}
}
return this.catalogService.retrieveCatalogDescriptors().pipe(
tap((descriptors) => {
this.catalogService.setCatalogDescriptors(descriptors);
}),
catchError(error => {
console.error('Error retrieving catalog descriptors', error);
this.catalogService.setCatalogDescriptors([]);
return of([]);
})
);
Expand Down
Loading