Files
faq_chatbot_frontend/src/app/shared/ui/voice-transcript/voice-transcript.component.spec.ts
T
PeymanHonarjoo 9e8dd33927 feat: implement answer-limited state handling in voice stream service
- Introduced handling for 'answer_limited' events from the server, allowing the UI to reflect when an assistant's answer has reached the output-token limit.
- Updated the VoiceStreamService to manage limited and interrupted turns with bounded memory.
- Enhanced the transcript component to display appropriate messages for limited answers, including a fallback for unavailable text.
- Added styles to visually indicate limited answers in the UI.
- Created unit tests for the new functionality, ensuring correct behavior for limited answers and their presentation.
2026-10-12 16:51:04 +03:30

235 lines
9.5 KiB
TypeScript

import { provideZonelessChangeDetection } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { VoiceTranscriptComponent } from './voice-transcript.component';
import { VoiceMessage, VoiceSource } from '../../../services/voice-stream/voice-stream.types';
const LIMITED_LABEL = 'پاسخ محدود شده';
const LIMITED_HELP = 'برای توضیح بیشتر بگویید «ادامه بده».';
const UNAVAILABLE_TEXT = 'متن این پاسخ در دسترس نیست';
function message(overrides: Partial<VoiceMessage>): VoiceMessage {
return {
id: 'turn-1:assistant',
turnRef: 'turn-1',
role: 'assistant',
text: 'پاسخ',
final: true,
...overrides,
};
}
async function render(messages: VoiceMessage[]): Promise<HTMLElement> {
const fixture = TestBed.createComponent(VoiceTranscriptComponent);
fixture.componentRef.setInput('messages', messages);
fixture.detectChanges();
await fixture.whenStable();
return fixture.nativeElement as HTMLElement;
}
describe('VoiceTranscriptComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [VoiceTranscriptComponent],
providers: [provideZonelessChangeDetection()],
}).compileComponents();
});
it('shows the limited indication with the continuation help next to the transcript text', async () => {
const element = await render([message({ limited: true })]);
expect(element.textContent).toContain('پاسخ'); // the available transcript stays visible
expect(element.textContent).toContain(LIMITED_LABEL);
expect(element.textContent).toContain(LIMITED_HELP);
// The help text is a separate indication, never appended to the provider transcript.
const text = element.querySelector('.bubble-text')?.textContent ?? '';
expect(text).toBe('پاسخ');
expect(text).not.toContain(LIMITED_HELP);
});
it('shows the unavailable-text presentation when a limited answer has no transcript text', async () => {
const element = await render([message({ text: '', limited: true })]);
expect(element.textContent).toContain(UNAVAILABLE_TEXT);
expect(element.textContent).toContain(LIMITED_LABEL);
expect(element.textContent).toContain(LIMITED_HELP);
});
it('keeps the sources of a limited answer visible', async () => {
const element = await render([
message({
limited: true,
sources: [
{
source_ref: 'S1',
document_id: 'doc-1',
chunk_id: 'chunk-1',
title: 'سند آزمون',
excerpt: 'بانک سؤالات و پاسخ‌های سامانه هوش مصنوعی تلفنی',
location: { page: null, section: 'docx/paragraph/1 | docx/table/1/row/1/cell/1' },
},
],
}),
]);
expect(element.querySelectorAll('.source-chip')).toHaveLength(1);
expect(element.textContent).toContain(LIMITED_LABEL);
});
it('shows the excerpt instead of the document section path', async () => {
const source: VoiceSource = {
source_ref: 'S1',
document_id: 'doc-1',
chunk_id: 'chunk-1',
title: 'document.docx',
excerpt: 'ساعت کاری شرکت از شنبه تا چهارشنبه است',
location: { page: null, section: 'docx/paragraph/1 | docx/table/1/row/1/cell/1' },
};
const element = await render([message({ sources: [source] })]);
const label = element.querySelector('.source-label')?.textContent ?? '';
expect(label).toContain('ساعت کاری شرکت از شنبه تا چهارشنبه است');
expect(label).not.toContain('docx/table/1/row/1/cell/1');
expect(label).not.toContain('document.docx'); // the filename is not the chip label
});
it('keeps the full excerpt and the document details in the hover tooltip', async () => {
const source: VoiceSource = {
source_ref: 'S1',
document_id: 'doc-1',
chunk_id: 'chunk-1',
title: 'document.docx',
excerpt: 'سؤال: ساعت کاری؟\nپاسخ: ۸ تا ۱۳',
location: { page: 3, section: 'docx/paragraph/1' },
};
const element = await render([message({ sources: [source] })]);
const tooltip = element.querySelector('.source-chip')?.getAttribute('title') ?? '';
expect(tooltip).toContain('document.docx');
expect(tooltip).toContain('docx/paragraph/1');
expect(tooltip).toContain('صفحه 3');
expect(tooltip).toContain('سؤال: ساعت کاری؟\nپاسخ: ۸ تا ۱۳');
});
it('shows a single-paragraph excerpt completely with its line breaks', async () => {
const excerpt =
'بانک سؤالات و پاسخ‌های سامانه هوش مصنوعی تلفنی: ساعت کاری شرکت از شنبه تا چهارشنبه است';
const source: VoiceSource = {
source_ref: 'S1',
document_id: 'doc-1',
chunk_id: 'chunk-1',
title: 'document.docx',
excerpt,
};
const element = await render([message({ sources: [source] })]);
const label = element.querySelector<HTMLElement>('.source-label');
// a single paragraph is not summarized: the full text is rendered…
expect(label?.textContent).toBe(excerpt);
// …and wraps instead of being clipped to one line
const style = getComputedStyle(label!);
expect(style.whiteSpace).toBe('pre-wrap');
expect(style.textOverflow).not.toBe('ellipsis');
});
it('summarizes a multi-paragraph excerpt to its first paragraph with an ellipsis', async () => {
const source: VoiceSource = {
source_ref: 'S1',
document_id: 'doc-1',
chunk_id: 'chunk-1',
title: 'document.docx',
excerpt:
'بانک سؤالات و پاسخ‌های سامانه هوش مصنوعی تلفنی\nسؤال: ساعت کاری شرکت چنده؟\nپاسخ: ۸ تا ۱۳',
};
const element = await render([message({ sources: [source] })]);
const label = element.querySelector('.source-label')?.textContent ?? '';
expect(label).toBe('بانک سؤالات و پاسخ‌های سامانه هوش مصنوعی تلفنی …');
expect(label).not.toContain('سؤال: ساعت کاری');
expect(label).not.toContain('پاسخ: ۸ تا ۱۳');
// the complete excerpt stays available in the hover tooltip
expect(element.querySelector('.source-chip')?.getAttribute('title')).toContain('پاسخ: ۸ تا ۱۳');
});
it('treats blank-line-separated paragraphs as multiple paragraphs', async () => {
const source: VoiceSource = {
source_ref: 'S1',
document_id: 'doc-1',
chunk_id: 'chunk-1',
title: 'document.docx',
excerpt: 'پاراگراف اول\n\nپاراگراف دوم',
};
const element = await render([message({ sources: [source] })]);
const label = element.querySelector('.source-label')?.textContent ?? '';
expect(label).toBe('پاراگراف اول …');
expect(label).not.toContain('پاراگراف دوم');
});
it('trims the excerpt so trailing wire newlines do not add empty lines', async () => {
const source: VoiceSource = {
source_ref: 'S1',
document_id: 'doc-1',
chunk_id: 'chunk-1',
title: 'document.docx',
excerpt: 'پاسخ: ۸ تا ۱۳\n',
};
const element = await render([message({ sources: [source] })]);
expect(element.querySelector('.source-label')?.textContent).toBe('پاسخ: ۸ تا ۱۳');
});
it('falls back to the document title for sources the server marks unavailable', async () => {
const element = await render([
message({
sources: [
{
source_ref: 'S1',
document_id: 'doc-1',
chunk_id: 'chunk-1',
title: 'document.docx',
excerpt: 'متن منقضی‌شده',
available: false,
},
],
}),
]);
const chip = element.querySelector('.source-chip');
expect(chip?.classList.contains('is-unavailable')).toBe(true);
expect(chip?.querySelector('.source-label')?.textContent).toBe('document.docx');
expect(chip?.querySelector('.source-label')?.textContent).not.toContain('متن منقضی‌شده');
// The revoked excerpt stays hidden in the tooltip too.
expect(chip?.getAttribute('title')).not.toContain('متن منقضی‌شده');
expect(chip?.getAttribute('title')).toContain('document.docx');
});
it('presents an interrupted answer as interrupted, not as limited', async () => {
const element = await render([
message({ limited: true, interrupted: true, text: 'پاسخ قطع‌شده' }),
]);
expect(element.textContent).toContain('(قطع شد)');
expect(element.textContent).not.toContain(LIMITED_LABEL);
expect(element.textContent).not.toContain(LIMITED_HELP);
});
it('never applies the limited indication to user rows or ordinary answers', async () => {
const element = await render([
message({ id: 'turn-1:user', turnRef: 'turn-1', role: 'user', text: 'ادامه بده' }),
message({ id: 'turn-2:assistant', turnRef: 'turn-2', text: 'پاسخ عادی' }),
]);
expect(element.textContent).not.toContain(LIMITED_LABEL);
expect(element.textContent).toContain('ادامه بده');
expect(element.textContent).toContain('پاسخ عادی');
});
it('renders the limited indication in an RTL bubble', async () => {
const element = await render([message({ limited: true })]);
const bubble = element.querySelector('.bubble');
expect(bubble?.getAttribute('dir')).toBe('rtl');
expect(bubble?.classList.contains('is-limited')).toBe(true);
});
});