import { provideHttpClient } from '@angular/common/http'; import { provideHttpClientTesting } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; import { provideRouter } from '@angular/router'; import { AgentStreamComponent } from './agent-stream.component'; describe('action card text selection', () => { let component: AgentStreamComponent; let header: HTMLDivElement; let outside: HTMLDivElement; beforeEach(() => { TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting(), provideRouter([])] }); component = TestBed.createComponent(AgentStreamComponent).componentInstance; header = document.createElement('div'); header.textContent = '点击元素 Projects'; header.addEventListener('click', event => component.toggleActionExpanded('action-1', event)); outside = document.createElement('div'); outside.textContent = '其他步骤'; document.body.append(header, outside); window.getSelection()?.removeAllRanges(); }); afterEach(() => { window.getSelection()?.removeAllRanges(); header.remove(); outside.remove(); }); function selectText(element: HTMLElement): void { const range = document.createRange(); range.selectNodeContents(element); window.getSelection()?.addRange(range); } it('keeps selected header text and the expanded card in place for copying', () => { component.toggleActionExpanded('action-1'); selectText(header); header.click(); expect(component.isActionExpanded('action-1')).toBeTrue(); expect(window.getSelection()?.toString()).toBe('点击元素 Projects'); }); it('expands and collapses normally without selected text', () => { header.click(); expect(component.isActionExpanded('action-1')).toBeTrue(); header.click(); expect(component.isActionExpanded('action-1')).toBeFalse(); }); it('allows another card to open while unrelated text is selected', () => { selectText(outside); header.click(); expect(component.isActionExpanded('action-1')).toBeTrue(); }); });