|
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 |
|
| 18 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 19 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
18 | 20 | import {ComponentFixture, TestBed} from '@angular/core/testing'; |
19 | | -import {MatDialogModule, MatDialogRef} from '@angular/material/dialog'; |
| 21 | +import {MatButtonToggleHarness} from '@angular/material/button-toggle/testing'; |
| 22 | +import {MatDialog} from '@angular/material/dialog'; |
| 23 | +import {MatListHarness} from '@angular/material/list/testing'; |
| 24 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
20 | 25 |
|
| 26 | +import {Span} from '../../core/models/Trace'; |
21 | 27 |
|
22 | 28 | import {EventTabComponent} from './event-tab.component'; |
| 29 | +import {TraceChartComponent} from './trace-chart/trace-chart.component'; |
| 30 | + |
| 31 | +const MOCK_TRACE_DATA: Span[] = [ |
| 32 | + { |
| 33 | + name: 'agent.act', |
| 34 | + start_time: 1733084700000000000, |
| 35 | + end_time: 1733084760000000000, |
| 36 | + span_id: 'span-1', |
| 37 | + trace_id: 'trace-1', |
| 38 | + attributes: { |
| 39 | + 'event_id': 1, |
| 40 | + 'gcp.vertex.agent.invocation_id': '21332-322222', |
| 41 | + 'gcp.vertex.agent.llm_request': |
| 42 | + '{"contents":[{"role":"user","parts":[{"text":"Hello"}]},{"role":"agent","parts":[{"text":"Hi. What can I help you with?"}]},{"role":"user","parts":[{"text":"I need help with my project."}]}]}', |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'tool.invoke', |
| 47 | + start_time: 1733084705000000000, |
| 48 | + end_time: 1733084755000000000, |
| 49 | + span_id: 'span-2', |
| 50 | + parent_span_id: 'span-1', |
| 51 | + trace_id: 'trace-1', |
| 52 | + attributes: { |
| 53 | + 'tool_name': 'project_helper', |
| 54 | + }, |
| 55 | + children: [ |
| 56 | + { |
| 57 | + name: 'sub-tool-1.invoke', |
| 58 | + start_time: 1733084710000000000, |
| 59 | + end_time: 1733084750000000000, |
| 60 | + span_id: 'span-3', |
| 61 | + parent_span_id: 'span-2', |
| 62 | + trace_id: 'trace-1', |
| 63 | + attributes: { |
| 64 | + 'sub_tool_name': 'sub_project_helper_1', |
| 65 | + }, |
| 66 | + children: [ |
| 67 | + { |
| 68 | + name: 'sub-tool-2.invoke', |
| 69 | + start_time: 1733084715000000000, |
| 70 | + end_time: 1733084745000000000, |
| 71 | + span_id: 'span-4', |
| 72 | + parent_span_id: 'span-3', |
| 73 | + trace_id: 'trace-1', |
| 74 | + attributes: { |
| 75 | + 'sub_tool_name': 'sub_project_helper_2', |
| 76 | + }, |
| 77 | + children: [ |
| 78 | + { |
| 79 | + name: 'sub-tool-3.invoke', |
| 80 | + start_time: 1733084720000000000, |
| 81 | + end_time: 1733084740000000000, |
| 82 | + span_id: 'span-5', |
| 83 | + parent_span_id: 'span-4', |
| 84 | + trace_id: 'trace-1', |
| 85 | + attributes: { |
| 86 | + 'sub_tool_name': 'sub_project_helper_3', |
| 87 | + }, |
| 88 | + children: [], |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + ], |
| 93 | + }, |
| 94 | + ], |
| 95 | + } |
| 96 | +] as Span[]; |
| 97 | + |
| 98 | +const MOCK_EVENTS_MAP = new Map<string, any>([ |
| 99 | + ['event1', {title: 'Event 1 Title'}], |
| 100 | + ['event2', {title: 'Event 2 Title'}], |
| 101 | +]); |
23 | 102 |
|
24 | 103 | describe('EventTabComponent', () => { |
25 | 104 | let component: EventTabComponent; |
26 | 105 | let fixture: ComponentFixture<EventTabComponent>; |
27 | | - const mockDialogRef = { |
28 | | - close: jasmine.createSpy('close'), |
29 | | - }; |
| 106 | + let loader: HarnessLoader; |
| 107 | + const matDialogSpy = jasmine.createSpyObj('MatDialog', ['open']); |
30 | 108 |
|
31 | 109 | beforeEach(async () => { |
32 | | - await TestBed.configureTestingModule({ |
33 | | - imports: [MatDialogModule, EventTabComponent], |
34 | | - providers: [{ provide: MatDialogRef, useValue: mockDialogRef }], |
35 | | -}).compileComponents(); |
| 110 | + await TestBed |
| 111 | + .configureTestingModule({ |
| 112 | + imports: [EventTabComponent, NoopAnimationsModule], |
| 113 | + providers: [{provide: MatDialog, useValue: matDialogSpy}], |
| 114 | + }) |
| 115 | + .compileComponents(); |
36 | 116 |
|
37 | 117 | fixture = TestBed.createComponent(EventTabComponent); |
38 | 118 | component = fixture.componentInstance; |
| 119 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 120 | + matDialogSpy.open.calls.reset(); |
39 | 121 | fixture.detectChanges(); |
40 | 122 | }); |
41 | 123 |
|
42 | 124 | it('should create', () => { |
43 | 125 | expect(component).toBeTruthy(); |
44 | 126 | }); |
| 127 | + |
| 128 | + it('should display "No conversations" if eventsMap is empty', () => { |
| 129 | + expect(fixture.nativeElement.textContent).toContain('No conversations'); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('with events', () => { |
| 133 | + beforeEach(async () => { |
| 134 | + fixture.componentRef.setInput('eventsMap', MOCK_EVENTS_MAP); |
| 135 | + fixture.detectChanges(); |
| 136 | + await fixture.whenStable(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should display events list by default', async () => { |
| 140 | + const list = await loader.getHarness(MatListHarness); |
| 141 | + const items = await list.getItems(); |
| 142 | + expect(items.length).toBe(2); |
| 143 | + expect(await items[0].getFullText()).toContain('Event 1 Title'); |
| 144 | + expect(await items[1].getFullText()).toContain('Event 2 Title'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should emit selectedEvent on event click', async () => { |
| 148 | + spyOn(component.selectedEvent, 'emit'); |
| 149 | + const list = await loader.getHarness(MatListHarness); |
| 150 | + const items = await list.getItems(); |
| 151 | + await (await items[0].host()).click(); |
| 152 | + expect(component.selectedEvent.emit).toHaveBeenCalledWith('event1'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should not show toggle if traceData is empty', async () => { |
| 156 | + const hasToggleGroup = fixture.nativeElement.querySelector( |
| 157 | + 'mat-button-toggle-group', |
| 158 | + ); |
| 159 | + expect(hasToggleGroup).toBeNull(); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('with trace data', () => { |
| 164 | + beforeEach(async () => { |
| 165 | + fixture.componentRef.setInput('eventsMap', MOCK_EVENTS_MAP); |
| 166 | + fixture.componentRef.setInput('traceData', MOCK_TRACE_DATA); |
| 167 | + fixture.detectChanges(); |
| 168 | + await fixture.whenStable(); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should show toggle buttons', async () => { |
| 172 | + const toggles = await loader.getAllHarnesses(MatButtonToggleHarness); |
| 173 | + expect(toggles.length).toBe(2); |
| 174 | + expect(await toggles[0].getText()).toBe('Events'); |
| 175 | + expect(await toggles[1].getText()).toBe('Trace'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should switch to trace view and display traces', async () => { |
| 179 | + const traceToggle = await loader.getHarness( |
| 180 | + MatButtonToggleHarness.with({text: 'Trace'}), |
| 181 | + ); |
| 182 | + await traceToggle.check(); |
| 183 | + fixture.detectChanges(); |
| 184 | + |
| 185 | + const list = await loader.getHarness(MatListHarness); |
| 186 | + const items = await list.getItems(); |
| 187 | + expect(items.length).toBe(1); |
| 188 | + expect(await items[0].getFullText()).toContain('Invocation 21332-322222'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should open dialog when trace item is clicked', async () => { |
| 192 | + const traceToggle = await loader.getHarness( |
| 193 | + MatButtonToggleHarness.with({text: 'Trace'}), |
| 194 | + ); |
| 195 | + await traceToggle.check(); |
| 196 | + fixture.detectChanges(); |
| 197 | + |
| 198 | + const list = await loader.getHarness(MatListHarness); |
| 199 | + const items = await list.getItems(); |
| 200 | + await (await items[0].host()).click(); |
| 201 | + |
| 202 | + expect(matDialogSpy.open).toHaveBeenCalledWith(TraceChartComponent, { |
| 203 | + width: 'auto', |
| 204 | + maxWidth: '90vw', |
| 205 | + data: { |
| 206 | + spans: component.spansByTraceId().get('trace-1'), |
| 207 | + invocId: '21332-322222', |
| 208 | + }, |
| 209 | + }); |
| 210 | + }); |
| 211 | + |
| 212 | + it('should display multiple traces if present', async () => { |
| 213 | + const MOCK_TRACE_DATA_WITH_MULTIPLE_TRACES: Span[] = [ |
| 214 | + ...MOCK_TRACE_DATA, |
| 215 | + { |
| 216 | + name: 'agent.act-2', |
| 217 | + start_time: 1733084700000000000, |
| 218 | + end_time: 1733084760000000000, |
| 219 | + span_id: 'span-10', |
| 220 | + trace_id: 'trace-2', |
| 221 | + attributes: { |
| 222 | + 'event_id': 10, |
| 223 | + 'gcp.vertex.agent.invocation_id': 'invoc-2', |
| 224 | + 'gcp.vertex.agent.llm_request': '{}', |
| 225 | + }, |
| 226 | + }, |
| 227 | + ]; |
| 228 | + fixture.componentRef.setInput( |
| 229 | + 'traceData', |
| 230 | + MOCK_TRACE_DATA_WITH_MULTIPLE_TRACES, |
| 231 | + ); |
| 232 | + fixture.detectChanges(); |
| 233 | + await fixture.whenStable(); |
| 234 | + |
| 235 | + const traceToggle = await loader.getHarness( |
| 236 | + MatButtonToggleHarness.with({text: 'Trace'}), |
| 237 | + ); |
| 238 | + await traceToggle.check(); |
| 239 | + fixture.detectChanges(); |
| 240 | + |
| 241 | + const list = await loader.getHarness(MatListHarness); |
| 242 | + const items = await list.getItems(); |
| 243 | + expect(items.length).toBe(2); |
| 244 | + expect(await items[0].getFullText()).toContain('Invocation 21332-322222'); |
| 245 | + expect(await items[1].getFullText()).toContain('Invocation invoc-2'); |
| 246 | + }); |
| 247 | + }); |
45 | 248 | }); |
0 commit comments