Add a confirmation workflow for removing documents and refactor the documents table to support responsive layouts. The table now uses a fixed layout with defined column widths and handles overflow on mobile devices. The redundant filename column has been removed.
249 lines
11 KiB
TypeScript
249 lines
11 KiB
TypeScript
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 toastr = { success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() };
|
|
|
|
const filterSelect = (el: HTMLElement): HTMLSelectElement | null => el.querySelector('.documents-filters select');
|
|
|
|
describe('SettingsPage — document filters', () => {
|
|
let getDocuments: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(async () => {
|
|
toastr.success.mockClear();
|
|
toastr.error.mockClear();
|
|
toastr.warning.mockClear();
|
|
toastr.info.mockClear();
|
|
// 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: toastr },
|
|
],
|
|
}).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);
|
|
// نام فایل column was dropped (redundant with عنوان سند); the row shows the title.
|
|
expect(rows[0].textContent).toContain('سند آزمون');
|
|
expect(rows[0].textContent).not.toContain('guide.pdf');
|
|
});
|
|
|
|
it('drops the redundant نام فایل column and renders exactly 6 columns', () => {
|
|
const fixture = TestBed.createComponent(SettingsPage);
|
|
fixture.detectChanges();
|
|
const el = fixture.nativeElement as HTMLElement;
|
|
|
|
const headers = Array.from(el.querySelectorAll('.documents-table thead th')).map((t) =>
|
|
t.textContent?.trim(),
|
|
);
|
|
expect(headers).toEqual([
|
|
'عنوان سند',
|
|
'وضعیت پردازش',
|
|
'تعداد بخشها',
|
|
'تاریخ آپلود',
|
|
'فعالسازی',
|
|
'عملیات',
|
|
]);
|
|
expect(headers).not.toContain('نام فایل');
|
|
// A data row has exactly 6 cells matching the 6 columns.
|
|
expect(el.querySelector('.documents-table tbody tr')?.children.length).toBe(6);
|
|
});
|
|
|
|
it('confirms before removing: the bin button opens a modal, cancel keeps the doc, confirm shows the MVP notice', () => {
|
|
const fixture = TestBed.createComponent(SettingsPage);
|
|
fixture.detectChanges();
|
|
const el = fixture.nativeElement as HTMLElement;
|
|
|
|
const openRemoveModal = () => {
|
|
const row = el.querySelector('.documents-table tbody tr') as HTMLElement;
|
|
const remove = Array.from(row.querySelectorAll('button')).find((b) =>
|
|
(b.getAttribute('aria-label') ?? '').includes('حذف سند'),
|
|
) as HTMLButtonElement;
|
|
remove.click();
|
|
fixture.detectChanges();
|
|
};
|
|
|
|
openRemoveModal();
|
|
const modal = el.querySelector('[role="alertdialog"]') as HTMLElement;
|
|
expect(modal).toBeTruthy();
|
|
expect(modal.textContent).toContain('سند آزمون');
|
|
expect(modal.textContent).toContain('مطمئن هستید');
|
|
expect(toastr.info).not.toHaveBeenCalled(); // nothing happens until confirmed
|
|
|
|
// Cancel closes the modal and keeps the document, with no notice.
|
|
(modal.querySelector('.btn--outline') as HTMLButtonElement).click();
|
|
fixture.detectChanges();
|
|
expect(el.querySelector('[role="alertdialog"]')).toBeNull();
|
|
expect(el.querySelectorAll('.documents-table tbody tr').length).toBe(1);
|
|
expect(toastr.info).not.toHaveBeenCalled();
|
|
|
|
// Confirm closes the modal and surfaces the MVP notice (deletion is not yet implemented);
|
|
// the row is left in place and no backend delete call is made.
|
|
openRemoveModal();
|
|
(el.querySelector('[role="alertdialog"] .btn--primary') as HTMLButtonElement).click();
|
|
fixture.detectChanges();
|
|
expect(toastr.info).toHaveBeenCalledTimes(1);
|
|
expect(el.querySelector('[role="alertdialog"]')).toBeNull();
|
|
expect(el.querySelectorAll('.documents-table tbody tr').length).toBe(1);
|
|
});
|
|
|
|
it('renders OCR and FAQ admin-approval toggles in the app settings and toggles FAQ', () => {
|
|
const fixture = TestBed.createComponent(SettingsPage);
|
|
fixture.detectChanges();
|
|
const component = fixture.componentInstance;
|
|
component.selectTab('app');
|
|
fixture.detectChanges();
|
|
const el = fixture.nativeElement as HTMLElement;
|
|
|
|
const labels = Array.from(el.querySelectorAll('.setting-info label')).map((l) =>
|
|
l.textContent?.trim(),
|
|
);
|
|
expect(labels).toContain('نیاز به تأیید ادمین برای اسناد OCR شده');
|
|
expect(labels).toContain('نیاز به تأیید ادمین برای اسناد FAQ');
|
|
|
|
// Toggles in order: dark mode, OCR, FAQ. The FAQ toggle flips its own signal.
|
|
const switches = el.querySelectorAll('.setting-control .toggle-switch');
|
|
expect(switches.length).toBe(3);
|
|
expect(component.faqAdminApproval()).toBe(false);
|
|
(switches[2].querySelector('input') as HTMLInputElement).click();
|
|
fixture.detectChanges();
|
|
expect(component.faqAdminApproval()).toBe(true);
|
|
});
|
|
|
|
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();
|
|
});
|
|
});
|