Files
common_agent/frontend/src/pages/__tests__/RagDocumentsPage.spec.ts

126 lines
3.7 KiB
TypeScript

import { flushPromises, mount } from '@vue/test-utils';
import ElementPlus from 'element-plus';
import { describe, expect, it, vi } from 'vitest';
import RagDocumentsPage from '../RagDocumentsPage.vue';
import { getRagDocumentById, listRagDocuments, queryRagDocuments } from '@/api/ragDocuments';
import { queryRagStores } from '@/api/ragStores';
vi.mock('@/api/ragStores', () => ({
queryRagStores: vi.fn(() =>
Promise.resolve({
resultcode: '0',
message: null,
data: [
{ id: '1', storeCode: 'PROD_DOC', storeName: '产品制度库', status: '启用' },
{ id: '2', storeCode: 'FAQ', storeName: 'FAQ知识库', status: '停用' },
],
}),
),
}));
vi.mock('@/api/ragDocuments', () => ({
SOURCE_TYPE_RAG: 'RAG',
listRagDocuments: vi.fn(() =>
Promise.resolve({
resultcode: '0',
message: null,
data: [
{
id: '11',
storeId: '1',
attachmentId: '101',
documentTitle: '产品制度总则',
documentSummary: '制度摘要',
parseStatus: 'UPLOADED',
indexStatus: 'PENDING',
enabled: true,
remark: '制度文档',
createTime: '2026-05-21 10:00:00',
},
{
id: '22',
storeId: '2',
attachmentId: '202',
documentTitle: 'FAQ 手册',
documentSummary: 'FAQ 摘要',
parseStatus: 'PARSED',
indexStatus: 'INDEXED',
enabled: false,
remark: '常见问题',
createTime: '2026-05-21 11:00:00',
},
],
}),
),
queryRagDocuments: vi.fn(),
getRagDocumentById: vi.fn((id: string) =>
Promise.resolve({
resultcode: '0',
message: null,
data: {
id,
storeId: '2',
attachmentId: '202',
documentTitle: 'FAQ 手册',
documentSummary: 'FAQ 摘要',
enabled: false,
remark: '常见问题',
},
}),
),
saveRagDocument: vi.fn(() => Promise.resolve({ resultcode: '0', message: null, data: true })),
deleteRagDocument: vi.fn(() => Promise.resolve({ resultcode: '0', message: null, data: true })),
batchUploadRagDocuments: vi.fn(() => Promise.resolve({ resultcode: '0', message: null, data: [] })),
}));
describe('RagDocumentsPage', () => {
it('loads documents from list api instead of broken query api', async () => {
const wrapper = mount(RagDocumentsPage, {
global: {
plugins: [ElementPlus],
},
});
await flushPromises();
expect(queryRagStores).toHaveBeenCalled();
expect(listRagDocuments).toHaveBeenCalled();
expect(queryRagDocuments).not.toHaveBeenCalled();
expect(wrapper.text()).toContain('产品制度总则');
expect(wrapper.text()).toContain('FAQ 手册');
});
it('filters rows locally and still avoids query api on search', async () => {
const wrapper = mount(RagDocumentsPage, {
global: {
plugins: [ElementPlus],
},
});
await flushPromises();
await wrapper.get('[data-test="doc-keyword-input"]').setValue('FAQ');
await wrapper.get('[data-test="doc-search"]').trigger('click');
await flushPromises();
expect(listRagDocuments).toHaveBeenCalled();
expect(queryRagDocuments).not.toHaveBeenCalled();
expect(wrapper.text()).toContain('FAQ 手册');
expect(wrapper.text()).not.toContain('产品制度总则');
});
it('loads backend detail when editing a row', async () => {
const wrapper = mount(RagDocumentsPage, {
global: {
plugins: [ElementPlus],
},
});
await flushPromises();
await wrapper.get('[data-test="doc-edit-22"]').trigger('click');
await flushPromises();
expect(getRagDocumentById).toHaveBeenCalledWith('22');
});
});