From d13f4b852b2117a0757ffccdffb06f75a1267800 Mon Sep 17 00:00:00 2001 From: Aaravkhanal Date: Fri, 6 Nov 2026 09:57:10 +0530 Subject: [PATCH] fix(eval): delete current eval cases independently of tab mounting --- .../components/chat/chat.component.spec.ts | 46 +++++++++++++++++++ src/app/components/chat/chat.component.ts | 25 ++++++++-- .../eval-tab/eval-tab.component.spec.ts | 32 ++++++++++++- .../components/eval-tab/eval-tab.component.ts | 14 ++++-- .../side-panel/side-panel.component.spec.ts | 28 +++++++++++ .../side-panel/side-panel.component.ts | 7 +++ 6 files changed, 142 insertions(+), 10 deletions(-) diff --git a/src/app/components/chat/chat.component.spec.ts b/src/app/components/chat/chat.component.spec.ts index 7e8c4f88..50034a9b 100644 --- a/src/app/components/chat/chat.component.spec.ts +++ b/src/app/components/chat/chat.component.spec.ts @@ -331,6 +331,52 @@ describe('ChatComponent', () => { await fixture.whenStable(); }); + describe('deleting an eval case', () => { + beforeEach(() => { + component.appName = TEST_APP_1_NAME; + component.evalSetId = 'eval-set'; + component.evalCase = {evalId: 'case-1'} as any; + mockDialog.open.and.returnValue({afterClosed: () => of(true)} as any); + }); + + it('deletes without a directly mounted eval tab and waits for success', () => { + const notify = spyOn(component as any, 'openSnackBar'); + const refresh = spyOn(component.sidePanel(), 'refreshAfterEvalCaseDeleted'); + (component as any).deleteEvalCase(); + expect(refresh).not.toHaveBeenCalled(); + expect(mockEvalService.deleteEvalCase).toHaveBeenCalledWith( + TEST_APP_1_NAME, 'eval-set', 'case-1'); + expect(notify).not.toHaveBeenCalled(); + mockEvalService.deleteEvalCaseResponse.next({}); + expect(notify).toHaveBeenCalledWith('Eval case deleted', 'OK'); + expect(component.evalCase).toBeNull(); + expect(refresh).toHaveBeenCalledWith(TEST_APP_1_NAME, 'eval-set', 'case-1'); + }); + + it('does not replace a case selected while deletion is pending', () => { + (component as any).deleteEvalCase(); + component.evalCase = {evalId: 'case-2'} as any; + mockEvalService.deleteEvalCaseResponse.next({}); + expect(component.evalCase?.evalId).toBe('case-2'); + }); + + it('does not delete when confirmation is cancelled', () => { + mockDialog.open.and.returnValue({afterClosed: () => of(false)} as any); + (component as any).deleteEvalCase(); + expect(mockEvalService.deleteEvalCase).not.toHaveBeenCalled(); + expect(component.evalCase?.evalId).toBe('case-1'); + }); + + it('keeps the case and reports a failed request', () => { + const notify = spyOn(component as any, 'openSnackBar'); + (component as any).deleteEvalCase(); + mockEvalService.deleteEvalCaseResponse.error(new Error('delete failed')); + expect(notify).toHaveBeenCalledWith('Failed to delete eval case', 'OK'); + expect(notify).not.toHaveBeenCalledWith('Eval case deleted', 'OK'); + expect(component.evalCase?.evalId).toBe('case-1'); + }); + }); + describe('Component Initialization', () => { it('should create', () => { expect(component).toBeTruthy(); diff --git a/src/app/components/chat/chat.component.ts b/src/app/components/chat/chat.component.ts index 3f3505aa..fae6f431 100644 --- a/src/app/components/chat/chat.component.ts +++ b/src/app/components/chat/chat.component.ts @@ -3518,9 +3518,12 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy { } protected deleteEvalCase() { + const appName = this.appName; + const evalSetId = this.evalSetId; + const evalCaseId = this.evalCase!.evalId; const dialogData: DeleteSessionDialogData = { title: 'Confirm delete', - message: `Are you sure you want to delete ${this.evalCase!.evalId}?`, + message: `Are you sure you want to delete ${evalCaseId}?`, confirmButtonText: 'Delete', cancelButtonText: 'Cancel', }; @@ -3531,10 +3534,22 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy { }); dialogRef.afterClosed().subscribe((result: boolean) => { - if (result) { - this.evalTab()?.deleteEvalCase(this.evalCase!.evalId); - this.openSnackBar('Eval case deleted', 'OK'); - } + if (!result) return; + // The eval tab belongs to SidePanelComponent and may not be mounted. + this.evalService.deleteEvalCase(appName, evalSetId, evalCaseId).subscribe({ + next: () => { + if (this.appName === appName && this.evalSetId === evalSetId && + this.evalCase?.evalId === evalCaseId) { + this.evalCase = null; + this.resetEditEvalCaseVars(); + this.handleReturnToSession(true); + } + this.sidePanel().refreshAfterEvalCaseDeleted( + appName, evalSetId, evalCaseId); + this.openSnackBar('Eval case deleted', 'OK'); + }, + error: () => this.openSnackBar('Failed to delete eval case', 'OK'), + }); }); } diff --git a/src/app/components/eval-tab/eval-tab.component.spec.ts b/src/app/components/eval-tab/eval-tab.component.spec.ts index 685a338c..42936f12 100644 --- a/src/app/components/eval-tab/eval-tab.component.spec.ts +++ b/src/app/components/eval-tab/eval-tab.component.spec.ts @@ -20,7 +20,8 @@ import { MatDialog, MatDialogModule } from '@angular/material/dialog'; import { EvalTabComponent } from './eval-tab.component'; import { EvalService } from '../../core/services/eval.service'; import { SessionService } from '../../core/services/session.service'; -import { of, throwError } from 'rxjs'; +import { of, Subject, throwError } from 'rxjs'; +import {initTestBed} from '../../testing/utils'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { ActivatedRoute } from '@angular/router'; import { @@ -76,6 +77,7 @@ describe('EvalTabComponent', () => { featureFlagService.isA2ACardEnabled.and.returnValue(of(false)); featureFlagService.isEvalV2Enabled.and.returnValue(of(false)); + initTestBed(); // required for 1p compatibility await TestBed.configureTestingModule({ imports: [MatDialogModule, EvalTabComponent, NoopAnimationsModule], providers: [ @@ -99,6 +101,34 @@ describe('EvalTabComponent', () => { fixture.detectChanges(); }); + it('updates the table when the post-deletion list arrives', () => { + const response = new Subject(); + const service = TestBed.inject(EVAL_SERVICE) as jasmine.SpyObj; + service.listEvalCases.and.returnValue(response); + component.selectedEvalSet.set('set'); + component.evalCases = ['case-1', 'case-2']; + component.selectedEvalCase.set({evalId: 'case-1'} as any); + const getEvalCase = spyOn(component as any, 'getEvalCase'); + + component.refreshAfterEvalCaseDeleted('case-1'); + expect(component.selectedEvalCase()).toBeNull(); + response.next(['case-2']); + expect(component.evalCases).toEqual(['case-2']); + expect(component.dataSource.data).toEqual(['case-2']); + expect(getEvalCase).toHaveBeenCalledWith('case-2'); + }); + + it('preserves a different selected case during the refresh', () => { + const response = new Subject(); + const service = TestBed.inject(EVAL_SERVICE) as jasmine.SpyObj; + service.listEvalCases.and.returnValue(response); + component.evalCases = ['case-1', 'case-2']; + component.selectedEvalCase.set({evalId: 'case-2'} as any); + component.refreshAfterEvalCaseDeleted('case-1'); + response.next(['case-2']); + expect(component.selectedEvalCase()?.evalId).toBe('case-2'); + }); + it('should create', () => { expect(component).toBeTruthy(); }); diff --git a/src/app/components/eval-tab/eval-tab.component.ts b/src/app/components/eval-tab/eval-tab.component.ts index e59ccb7d..bb2419eb 100644 --- a/src/app/components/eval-tab/eval-tab.component.ts +++ b/src/app/components/eval-tab/eval-tab.component.ts @@ -763,13 +763,19 @@ export class EvalTabComponent implements OnInit, OnChanges { this.evalService .deleteEvalCase(this.appName(), this.selectedEvalSet(), evalCaseId) .subscribe((res) => { - this.deletedEvalCaseIndex = this.evalCases.indexOf(evalCaseId); - this.selectedEvalCase.set(null); - this.listEvalCases(); - this.changeDetectorRef.detectChanges(); + this.refreshAfterEvalCaseDeleted(evalCaseId); }); } + refreshAfterEvalCaseDeleted(evalCaseId: string) { + const selectedCase = this.selectedEvalCase(); + const replaceSelection = !selectedCase || selectedCase.evalId === evalCaseId; + this.deletedEvalCaseIndex = replaceSelection ? this.evalCases.indexOf(evalCaseId) : -1; + if (replaceSelection) this.selectedEvalCase.set(null); + // listEvalCases updates the view after the refreshed list arrives. + this.listEvalCases(); + } + confirmDeleteEvalSet(event: Event, evalSetId: string) { event.stopPropagation(); const dialogData: DeleteSessionDialogData = { diff --git a/src/app/components/side-panel/side-panel.component.spec.ts b/src/app/components/side-panel/side-panel.component.spec.ts index 74e86597..bd242f7b 100644 --- a/src/app/components/side-panel/side-panel.component.spec.ts +++ b/src/app/components/side-panel/side-panel.component.spec.ts @@ -248,6 +248,34 @@ describe('SidePanelComponent', () => { expect(component).toBeTruthy(); }); + describe('refreshing a deleted eval case', () => { + it('refreshes the dynamically created tab for the matching eval set', () => { + const refresh = jasmine.createSpy('refreshAfterEvalCaseDeleted'); + (component as any).evalTabRef.set({instance: { + appName: () => 'app', selectedEvalSet: () => 'set', + refreshAfterEvalCaseDeleted: refresh, + }}); + component.refreshAfterEvalCaseDeleted('app', 'set', 'case'); + expect(refresh).toHaveBeenCalledWith('case'); + }); + + it('does not refresh a different eval set', () => { + const refresh = jasmine.createSpy('refreshAfterEvalCaseDeleted'); + (component as any).evalTabRef.set({instance: { + appName: () => 'app', selectedEvalSet: () => 'other-set', + refreshAfterEvalCaseDeleted: refresh, + }}); + component.refreshAfterEvalCaseDeleted('app', 'set', 'case'); + expect(refresh).not.toHaveBeenCalled(); + }); + + it('handles an unmounted eval tab', () => { + (component as any).evalTabRef.set(null); + expect(() => component.refreshAfterEvalCaseDeleted('app', 'set', 'case')) + .not.toThrow(); + }); + }); + describe('Tab hiding', () => { it('should hide Trace tab when isTraceEnabled is false', () => { mockFeatureFlagService.isTraceEnabledResponse.next(false); diff --git a/src/app/components/side-panel/side-panel.component.ts b/src/app/components/side-panel/side-panel.component.ts index d90ab8c0..70730f8d 100644 --- a/src/app/components/side-panel/side-panel.component.ts +++ b/src/app/components/side-panel/side-panel.component.ts @@ -219,6 +219,13 @@ export class SidePanelComponent implements AfterViewInit, OnInit { }); } + refreshAfterEvalCaseDeleted(appName: string, evalSetId: string, evalCaseId: string) { + const tab = this.evalTabRef()?.instance ?? this.evalTabComponent(); + if (tab?.appName() === appName && tab.selectedEvalSet() === evalSetId) { + tab.refreshAfterEvalCaseDeleted(evalCaseId); + } + } + selectEvalCase(evalSetId: string, evalCase: EvalCase) { const tab = this.evalTabComponent(); if (tab) {