refactor: overhaul home UI and audio playback reliability
Redesign the home page and voice stream panel into a compact dock layout to improve the chat experience. Implement a flush mechanism in the PCM playback worklet to handle the resampler tail, preventing premature session termination due to phantom outstanding playback. Fix document filter visibility in settings to ensure controls remain accessible when no results match, and refactor transcript insertion to maintain correct user/assistant ordering.
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { provideZonelessChangeDetection } from '@angular/core';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
import { provideRouter } from '@angular/router';
|
||||
import { of, Subject, type Observable } from 'rxjs';
|
||||
import { vi } from 'vitest';
|
||||
import { SettingsPage } from './settings.page';
|
||||
import { DocumentsService } from '../../services/documents/documents.service';
|
||||
import {
|
||||
DocumentListItem,
|
||||
DocumentListResponse,
|
||||
} from '../../services/documents/documents.types';
|
||||
import { AuthService } from '../../services/auth/auth.service';
|
||||
import { HttpService } from '../../services/http-request/http.service';
|
||||
import { ThemeService } from '../../services/theme/theme.service';
|
||||
import { ToastrService } from 'ngx-toastr';
|
||||
|
||||
function readyDoc(): DocumentListItem {
|
||||
return {
|
||||
id: 'doc-1',
|
||||
title: 'سند آزمون',
|
||||
filename: 'guide.pdf',
|
||||
status: 'ready',
|
||||
is_active: true,
|
||||
searchable: true,
|
||||
chunk_count: 12,
|
||||
created_at: '2024-01-01T10:00:00Z',
|
||||
job: { id: 'job-1', status: 'ready', stage: null, attempt_count: 0, retry_generation: 0, can_retry: false, error: null },
|
||||
review_required: false,
|
||||
};
|
||||
}
|
||||
|
||||
const filterSelect = (el: HTMLElement): HTMLSelectElement | null => el.querySelector('.documents-filters select');
|
||||
|
||||
describe('SettingsPage — document filters', () => {
|
||||
let getDocuments: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Server-side filtering: `all` returns one ready doc; every other status matches none.
|
||||
getDocuments = vi.fn((params: { status?: string } = {}): Observable<DocumentListResponse> => {
|
||||
const items = params.status ? [] : [readyDoc()];
|
||||
return of({ items, next_cursor: null, knowledge_revision: 0 });
|
||||
});
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [SettingsPage],
|
||||
providers: [
|
||||
provideZonelessChangeDetection(),
|
||||
provideHttpClient(),
|
||||
provideRouter([]),
|
||||
{
|
||||
provide: DocumentsService,
|
||||
useValue: {
|
||||
getConfiguration: () => of({}),
|
||||
getDocuments,
|
||||
getKnowledgeSelection: () => of({ revision: 0, document_ids: ['doc-1'] }),
|
||||
},
|
||||
},
|
||||
{ provide: AuthService, useValue: { getCurrentUserValue: () => ({ role: 'admin' }) } },
|
||||
{ provide: HttpService, useValue: { apiUrl: '/api/v1' } },
|
||||
{ provide: ThemeService, useValue: { isDarkMode: () => false, toggleDarkMode: () => {} } },
|
||||
{ provide: ToastrService, useValue: { success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() } },
|
||||
],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
it('renders the filter select and one document row initially', () => {
|
||||
const fixture = TestBed.createComponent(SettingsPage);
|
||||
fixture.detectChanges(); // ngOnInit → load all documents
|
||||
|
||||
expect(filterSelect(fixture.nativeElement as HTMLElement)).not.toBeNull();
|
||||
expect((fixture.nativeElement as HTMLElement).querySelectorAll('.documents-table tbody tr').length).toBe(1);
|
||||
});
|
||||
|
||||
it('keeps the filter select and table shell when a filter matches no rows', () => {
|
||||
const fixture = TestBed.createComponent(SettingsPage);
|
||||
fixture.detectChanges();
|
||||
const el = fixture.nativeElement as HTMLElement;
|
||||
|
||||
// Select a status that has zero documents.
|
||||
const component = fixture.componentInstance;
|
||||
component.activeFilter.set('failed');
|
||||
component.applyDocumentFilters();
|
||||
fixture.detectChanges();
|
||||
|
||||
// Regression: the whole panel used to vanish; the select + table must stay so the user
|
||||
// can change the filter back, and an in-table "no match" message is shown instead.
|
||||
expect(filterSelect(el)).not.toBeNull();
|
||||
expect(el.querySelector('.documents-table tbody tr')).not.toBeNull();
|
||||
expect(el.textContent).toContain('هیچ سندی با فیلتر انتخابشده مطابقت ندارد.');
|
||||
expect(el.textContent).not.toContain('هنوز سندی آپلود نشده است.');
|
||||
// The "N سند ثبت شده" footer must not claim 0 documents.
|
||||
expect(el.textContent).not.toContain('سند ثبت شده');
|
||||
});
|
||||
|
||||
it('restores the rows when the filter is reset back to all', () => {
|
||||
const fixture = TestBed.createComponent(SettingsPage);
|
||||
fixture.detectChanges();
|
||||
const component = fixture.componentInstance;
|
||||
|
||||
component.activeFilter.set('processing'); // empty
|
||||
component.applyDocumentFilters();
|
||||
fixture.detectChanges();
|
||||
expect((fixture.nativeElement as HTMLElement).querySelectorAll('.documents-table tbody tr').length).toBe(1); // just the empty row
|
||||
|
||||
component.activeFilter.set('all'); // back to everything
|
||||
component.applyDocumentFilters();
|
||||
fixture.detectChanges();
|
||||
const rows = (fixture.nativeElement as HTMLElement).querySelectorAll('.documents-table tbody tr');
|
||||
expect(rows.length).toBe(1);
|
||||
expect(rows[0].textContent).toContain('guide.pdf');
|
||||
});
|
||||
|
||||
it('hasActiveFilters reflects the active status/active-only filters', () => {
|
||||
const component = TestBed.createComponent(SettingsPage).componentInstance;
|
||||
expect(component.hasActiveFilters()).toBe(false);
|
||||
component.activeFilter.set('failed');
|
||||
expect(component.hasActiveFilters()).toBe(true);
|
||||
component.activeFilter.set('all');
|
||||
component.activeOnly.set(true);
|
||||
expect(component.hasActiveFilters()).toBe(true);
|
||||
});
|
||||
|
||||
it('shows a loading spinner on the بهروزرسانی button while documents are being fetched', () => {
|
||||
// The initial load resolves immediately; the refresh load stays pending so we can observe
|
||||
// the in-flight state (previously the button only disabled with no visible feedback).
|
||||
let call = 0;
|
||||
let pending!: Subject<DocumentListResponse>;
|
||||
getDocuments.mockReset();
|
||||
getDocuments.mockImplementation((): Observable<DocumentListResponse> => {
|
||||
call += 1;
|
||||
if (call === 1) return of({ items: [readyDoc()], next_cursor: null, knowledge_revision: 0 });
|
||||
pending = new Subject<DocumentListResponse>();
|
||||
return pending;
|
||||
});
|
||||
|
||||
const fixture = TestBed.createComponent(SettingsPage);
|
||||
fixture.detectChanges(); // initial load
|
||||
const el = fixture.nativeElement as HTMLElement;
|
||||
const refresh = el.querySelector('.documents-filters button') as HTMLButtonElement;
|
||||
expect(refresh.textContent).toContain('بهروزرسانی');
|
||||
expect(refresh.querySelector('.animate-spin')).toBeNull();
|
||||
|
||||
refresh.click(); // start refresh → stays pending
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(refresh.disabled).toBe(true);
|
||||
expect(refresh.getAttribute('aria-busy')).toBe('true');
|
||||
expect(refresh.textContent).toContain('در حال بهروزرسانی');
|
||||
expect(refresh.querySelector('.animate-spin')).not.toBeNull();
|
||||
|
||||
pending.next({ items: [readyDoc()], next_cursor: null, knowledge_revision: 0 });
|
||||
fixture.detectChanges();
|
||||
expect(refresh.disabled).toBe(false);
|
||||
expect(refresh.textContent).toContain('بهروزرسانی');
|
||||
expect(refresh.querySelector('.animate-spin')).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user