Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(knowledge): bound batch polling and ignore stale responses (#4648)

Arthur2309f63c5d3c
diff --git a/frontend/src/views/knowledge/KnowledgeBase.test.ts b/frontend/src/views/knowledge/KnowledgeBase.test.ts
index d914781d6..f114eda45 100644
--- a/frontend/src/views/knowledge/KnowledgeBase.test.ts
+++ b/frontend/src/views/knowledge/KnowledgeBase.test.ts
@@ -13,7 +13,7 @@ async function setup(t: TestContext, translate = (key: string, _params?: unknown
getCardDetails: t.mock.fn(), getfDetails() {}, onVisibleChange() {},
}
const listKnowledgeFolders = t.mock.fn(async (_id: string) => ({ data: { folders: [] } }))
- const batchQueryKnowledge = t.mock.fn(async (query: string) => {
+ const batchQueryKnowledge = t.mock.fn(async (query: string, _kbId?: string) => {
const ids = new URLSearchParams(query).getAll('ids')
return { success: true, data: hook.cardList.value.filter(card => ids.includes(card.id)).map(card => ({ ...card })) }
})
@@ -181,7 +181,8 @@ test('batch reparse preserves loaded pages and updates statuses without reloadin
t.mock.timers.tick(1500)
await flush()
assert.equal(f.vm.pendingReparseAck.has('doc-69'), false)
- assert.equal(f.batchQueryKnowledge.mock.calls[0].arguments[0], 'ids=doc-69&')
+ assert.equal(f.batchQueryKnowledge.mock.calls[0].arguments[0], 'ids=doc-69')
+ assert.equal(f.batchQueryKnowledge.mock.calls[0].arguments[1], 'kb')
assert.equal(f.hook.getKnowled.mock.callCount(), 0)
assert.equal(f.hook.cardList.value, cards)
assert.equal(cards.length, 70)
@@ -190,16 +191,28 @@ test('batch reparse preserves loaded pages and updates statuses without reloadin
test('an older batch acknowledgement timeout does not clear a newer batch', async t => {
const f = await setup(t)
- f.vm.pendingReparseAck = new Set(['older'])
- const older = f.vm.awaitBatchReparseReflection(['older'])
- for (let i = 0; i < 29; i++) {
- await flush()
+ f.hook.cardList.value = [
+ { id: 'older', parse_status: 'completed' },
+ { id: 'newer', parse_status: 'completed' },
+ ]
+ f.batchQueryKnowledge.mock.mockImplementation(async (query: string) => ({
+ success: true,
+ data: new URLSearchParams(query).getAll('ids').map(id => ({ id, parse_status: 'completed' })),
+ }))
+ f.vm.selectedIds = new Set(['older'])
+ await f.vm.confirmBatchReparse()
+ await flush()
+ for (let i = 0; i < 15; i++) {
t.mock.timers.tick(400)
+ await flush()
}
- f.vm.pendingReparseAck.add('newer')
+ f.vm.selectedIds = new Set(['newer'])
+ await f.vm.confirmBatchReparse()
await flush()
- t.mock.timers.tick(400)
- await older
+ for (let i = 0; i < 15; i++) {
+ t.mock.timers.tick(400)
+ await flush()
+ }
assert.equal(f.vm.pendingReparseAck.has('older'), false)
assert.equal(f.vm.pendingReparseAck.has('newer'), true)
assert.equal(f.hook.getKnowled.mock.callCount(), 0)
@@ -207,16 +220,29 @@ test('an older batch acknowledgement timeout does not clear a newer batch', asyn
test('batch acknowledgement waits stop once their own IDs are acknowledged', async t => {
const f = await setup(t)
- f.vm.pendingReparseAck = new Set(['older', 'newer'])
- let finished = false
- const older = f.vm.awaitBatchReparseReflection(['older']).then(() => { finished = true })
+ f.hook.cardList.value = [
+ { id: 'older', parse_status: 'completed' },
+ { id: 'newer', parse_status: 'completed' },
+ ]
+ f.batchQueryKnowledge.mock.mockImplementation(async () => ({
+ success: true,
+ data: [
+ { id: 'older', parse_status: 'processing' },
+ { id: 'newer', parse_status: 'completed' },
+ ],
+ }))
+ f.vm.selectedIds = new Set(['older', 'newer'])
+ await f.vm.confirmBatchReparse()
await flush()
- f.vm.pendingReparseAck.delete('older')
- t.mock.timers.tick(400)
+ assert.equal(f.vm.pendingReparseAck.has('older'), true)
+ assert.equal(f.vm.pendingReparseAck.has('newer'), true)
+ t.mock.timers.tick(1500)
await flush()
- assert.equal(finished, true)
- await older
+ assert.equal(f.vm.pendingReparseAck.has('older'), false)
assert.equal(f.vm.pendingReparseAck.has('newer'), true)
+ assert.equal(f.hook.cardList.value[0].parse_status, 'processing')
+ assert.equal(f.hook.cardList.value[1].parse_status, 'pending')
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
})
test('accepts a rebuild completed between polls after the bounded acknowledgement window', async t => {
diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue
index 0cc6c487b..f475a83f2 100644
--- a/frontend/src/views/knowledge/KnowledgeBase.vue
+++ b/frontend/src/views/knowledge/KnowledgeBase.vue
@@ -452,7 +452,13 @@ useAssistantPageContext(() => {
knowledgeTitle: docOpen ? details.title : undefined,
};
});
+const STATUS_POLL_INTERVAL_MS = 1500;
+// UUID 放在 GET 查询串中,限制单次长度,避免大批量重建触发代理的请求行上限。
+const STATUS_QUERY_BATCH_SIZE = 100;
let timeout: ReturnType<typeof setTimeout> | null = null;
+let statusPollGeneration = 0;
+let statusPollRequest: object | null = null;
+let statusPollingDisposed = false;
let knowledgeScroll = ref()
let page = 1;
let pageSize = 35;
@@ -579,14 +585,31 @@ const batchTagPreSelectedIds = computed(() => {
}
return Array.from(firstTagIds);
});
-// IDs submitted for async batch reparse; hold optimistic pending until the worker updates DB.
-const pendingReparseAck = ref<Set<string>>(new Set());
+// 批量接口成功只表示任务已入队,后台可能还没更新状态。
+// 暂时保留 pending;若重建很快、轮询没看到处理中状态,也要在期限后恢复真实状态。
+const BATCH_REPARSE_ACK_TIMEOUT_MS = 12000;
+const pendingReparseAck = new Map<string, ReturnType<typeof setTimeout>>();
+// 等待结束后仍保留重建版本,避免迟到的旧查询再次被当成当前结果。
+const reparseVersionById = new Map<string, number>();
+
+const clearReparseAck = (id: string) => {
+ const timer = pendingReparseAck.get(id);
+ if (timer !== undefined) clearTimeout(timer);
+ pendingReparseAck.delete(id);
+};
const applyOptimisticBatchReparse = (ids: string[]) => {
const idSet = new Set(ids);
for (const card of cardList.value) {
if (!idSet.has(card.id)) continue;
- pendingReparseAck.value.add(card.id);
+ clearReparseAck(card.id);
+ reparseVersionById.set(card.id, (reparseVersionById.get(card.id) ?? 0) + 1);
+ const timer = setTimeout(() => {
+ if (pendingReparseAck.get(card.id) !== timer) return;
+ pendingReparseAck.delete(card.id);
+ updateStatus(cardList.value.filter(needsStatusPolling), 0);
+ }, BATCH_REPARSE_ACK_TIMEOUT_MS);
+ pendingReparseAck.set(card.id, timer);
card.parse_status = 'pending';
card.summary_status = undefined;
card.description = '';
@@ -595,17 +618,16 @@ const applyOptimisticBatchReparse = (ids: string[]) => {
}
};
-const awaitBatchReparseReflection = async (ids: string[]) => {
- // updateStatus already polls in-flight rows and acknowledges the worker.
- // Only bound the optimistic window here: reloading page 1 discards loaded
- // pages, flashes the list spinner and can refresh a different folder/KB.
- const maxPolls = 30;
- const delayMs = 400;
- for (let i = 0; i < maxPolls && ids.some(id => pendingReparseAck.value.has(id)); i++) {
- await new Promise<void>((r) => setTimeout(r, delayMs));
- }
- // Another batch may have been submitted while this one was waiting.
- ids.forEach(id => pendingReparseAck.value.delete(id));
+// 切换知识库、目录或离开页面时,让定时任务和未返回的请求一起失效。
+// 新页面的轮询不必等待旧请求结束。
+const resetStatusPolling = () => {
+ statusPollGeneration++;
+ if (timeout !== null) clearTimeout(timeout);
+ timeout = null;
+ statusPollRequest = null;
+ for (const id of pendingReparseAck.keys()) clearReparseAck(id);
+ reparseVersionById.clear();
+ batchReparsing.value = false;
};
const confirmBatchReparse = async () => {
@@ -623,23 +645,27 @@ const confirmBatchReparse = async () => {
if (skipped > 0) {
MessagePlugin.warning(t('knowledgeBase.batchReparseSkippedInFlight', { count: skipped }));
}
+ const submissionGeneration = statusPollGeneration;
+ const targetKbId = kbId.value;
batchReparsing.value = true;
try {
- const res: any = await batchReparseKnowledge(kbId.value, ids);
+ const res: any = await batchReparseKnowledge(targetKbId, ids);
+ if (statusPollingDisposed || submissionGeneration !== statusPollGeneration) return;
if (res?.success) {
MessagePlugin.success(t('knowledgeBase.batchReparseSuccess', { count: ids.length }));
applyOptimisticBatchReparse(ids);
clearSelection();
batchMode.value = false;
scheduleWikiStatusProbes();
- void awaitBatchReparseReflection(ids);
} else {
MessagePlugin.error(res?.message || t('knowledgeBase.batchReparseFailed'));
}
} catch (e: any) {
- MessagePlugin.error(e?.message || t('knowledgeBase.batchReparseFailed'));
+ if (!statusPollingDisposed && submissionGeneration === statusPollGeneration) {
+ MessagePlugin.error(e?.message || t('knowledgeBase.batchReparseFailed'));
+ }
} finally {
- batchReparsing.value = false;
+ if (submissionGeneration === statusPollGeneration) batchReparsing.value = false;
}
};
@@ -804,6 +830,7 @@ const folderTreeLoading = ref(false);
// The folder being browsed; ROOT_FOLDER_PATH ('') is the knowledge base top
// level, a real node of the tree rather than a separate mode.
const selectedFolderPath = ref<string>(ROOT_FOLDER_PATH);
+watch([kbId, selectedFolderPath], resetStatusPolling, { flush: 'sync' });
const folderTreeCollapsed = ref(readStoredFlag(FOLDER_TREE_COLLAPSED_KEY));
const hasFolders = computed(() => (folderTree.value?.folders?.length ?? 0) > 0);
// The folder column only earns its space once the knowledge base actually has
@@ -1402,13 +1429,20 @@ onUnmounted(() => {
window.removeEventListener('weknora:knowledge-file-drop', handleKnowledgeFileDrop as EventListener);
window.removeEventListener('weknora:open-knowledge', handleOpenKnowledgeEvent as EventListener);
stopMovePoll();
- if (timeout !== null) {
- clearTimeout(timeout);
- timeout = null;
- }
+ statusPollingDisposed = true;
+ resetStatusPolling();
});
watch(() => cardList.value, (newValue) => {
if (isFAQ.value) return;
+ // 排序、筛选或上传后的列表刷新,可能读到后台尚未更新的旧状态。
+ // 保留已提交文档的 pending,但不延长原来的等待期限。
+ for (const card of newValue) {
+ if (pendingReparseAck.has(card.id) && !isParseInFlight(card.parse_status)) {
+ card.parse_status = 'pending';
+ card.summary_status = undefined;
+ card.description = '';
+ }
+ }
docListLoading.value = false;
// Auto-open document if navigated with ?knowledge_id=xxx.
@@ -1467,31 +1501,58 @@ const needsStatusPolling = (item: KnowledgeCard) => {
const pollDelayFor = (items: KnowledgeCard[]) =>
items.length > 0 && items.every(item => (item.stalled_minutes ?? 0) > 0)
? STALLED_POLL_INTERVAL_MS
- : 1500;
+ : STATUS_POLL_INTERVAL_MS;
-const updateStatus = (analyzeList: KnowledgeCard[], delay = 1500) => {
+const updateStatus = (analyzeList: KnowledgeCard[], delay = STATUS_POLL_INTERVAL_MS) => {
if (timeout !== null) {
clearTimeout(timeout);
timeout = null;
}
- if (!analyzeList.length) return;
+ if (statusPollingDisposed || statusPollRequest || !analyzeList.length) return;
- let query = ``;
- for (let i = 0; i < analyzeList.length; i++) {
- query += `ids=${analyzeList[i].id}&`;
- }
- timeout = setTimeout(() => {
- batchQueryKnowledge(query).then((result: any) => {
+ const generation = statusPollGeneration;
+ const targetKbId = kbId.value;
+ timeout = setTimeout(async () => {
+ timeout = null;
+ if (statusPollingDisposed || generation !== statusPollGeneration) return;
+ // 定时器等待期间可能又提交了一批;发请求时以当前列表为准。
+ const requestedIds = cardList.value.filter(needsStatusPolling).map(item => item.id);
+ if (!requestedIds.length) return;
+ const request = {};
+ statusPollRequest = request;
+ const isCurrentRequest = () => !statusPollingDisposed
+ && generation === statusPollGeneration && statusPollRequest === request;
+
+ try {
let shouldRefreshWikiStatus = false;
- if (result.success && result.data) {
+ // 整轮串行查询共用一个请求锁,避免监听器或确认定时器插入重叠轮询。
+ for (let offset = 0; offset < requestedIds.length; offset += STATUS_QUERY_BATCH_SIZE) {
+ if (!isCurrentRequest()) return;
+ const batchIds = requestedIds.slice(offset, offset + STATUS_QUERY_BATCH_SIZE);
+ const batchIdSet = new Set(batchIds);
+ const query = batchIds.map(id => `ids=${encodeURIComponent(id)}`).join('&');
+ // 每个子请求各自记录版本,先前发出的查询不能覆盖其后提交的新一轮重建。
+ const reparseVersions = new Map(batchIds.map(id => [id, reparseVersionById.get(id)]));
+ const hasSameReparse = (id: string) => reparseVersionById.get(id) === reparseVersions.get(id);
+ let result: any;
+ try {
+ result = await batchQueryKnowledge(query, targetKbId);
+ } catch {
+ if (!isCurrentRequest()) return;
+ // 失败子批次保留待查状态;继续查询后续文档,下一轮再重试失败的部分。
+ continue;
+ }
+ if (!isCurrentRequest()) return;
+ if (!result?.success || !Array.isArray(result.data)) continue;
+
(result.data as KnowledgeCard[]).forEach((item: KnowledgeCard) => {
const index = cardList.value.findIndex(card => card.id == item.id);
- if (index == -1) return;
+ if (!batchIdSet.has(item.id) || index == -1 || !hasSameReparse(item.id)) return;
let parseStatus = item.parse_status;
- if (pendingReparseAck.value.has(item.id)) {
+ if (pendingReparseAck.has(item.id)) {
if (isParseInFlight(item.parse_status)) {
- pendingReparseAck.value.delete(item.id);
+ clearReparseAck(item.id);
} else {
parseStatus = 'pending';
}
@@ -1502,49 +1563,37 @@ const updateStatus = (analyzeList: KnowledgeCard[], delay = 1500) => {
card.stall_state = item.stall_state;
card.stalled_minutes = stalledMinutes({ parse_status: parseStatus, last_activity_at: item.last_activity_at });
- if (cardList.value[index].parse_status !== parseStatus ||
- cardList.value[index].summary_status !== item.summary_status ||
- cardList.value[index].description !== item.description) {
+ if (card.parse_status !== parseStatus ||
+ card.summary_status !== item.summary_status ||
+ card.description !== item.description) {
shouldRefreshWikiStatus ||= shouldRefreshWikiStatusAfterKnowledgePoll(
- cardList.value[index],
+ card,
{ ...item, parse_status: parseStatus },
);
-
- // Always update the card data
- cardList.value[index].parse_status = parseStatus;
- cardList.value[index].summary_status = item.summary_status;
- cardList.value[index].description = item.description;
+ card.parse_status = parseStatus;
+ card.summary_status = item.summary_status;
+ card.description = item.description;
delete traceAvailableById[item.id];
- }
+ }
});
- // A requested row the batch no longer returns is gone (deleted
- // elsewhere, replaced by a data-source sync). Left in the list it
- // kept its in-flight status, so its spinner and this poll never ended.
+ // 只有成功子批次能判定缺失,未查询、查询失败或重新提交的文档都要保留。
const returnedIds = new Set((result.data as KnowledgeCard[]).map(item => item.id));
- const goneIds = new Set(analyzeList.map(item => item.id).filter(id => !returnedIds.has(id)));
+ const goneIds = new Set(batchIds.filter(id => !returnedIds.has(id) && hasSameReparse(id)));
if (goneIds.size > 0) {
const before = cardList.value.length;
cardList.value = cardList.value.filter(card => !goneIds.has(card.id));
total.value = Math.max(0, total.value - (before - cardList.value.length));
+ goneIds.forEach(clearReparseAck);
}
}
- if (shouldRefreshWikiStatus) {
- void fetchWikiStatusOnce();
- }
- // If there are no changes, the watch won't trigger, so we must manually poll again
- // Even if there are changes, we can manually poll again just to be safe.
- // The watch will clear this timeout if it triggers.
- const stillPending = cardList.value.filter(needsStatusPolling);
- if (stillPending.length > 0) {
- updateStatus(stillPending, pollDelayFor(stillPending));
+ if (shouldRefreshWikiStatus) void fetchWikiStatusOnce();
+ } finally {
+ if (isCurrentRequest()) {
+ statusPollRequest = null;
+ const stillPending = cardList.value.filter(needsStatusPolling);
+ if (stillPending.length > 0) updateStatus(stillPending, pollDelayFor(stillPending));
}
- }).catch((_err) => {
- // 错误处理
- const stillPending = cardList.value.filter(needsStatusPolling);
- if (stillPending.length > 0) {
- updateStatus(stillPending, pollDelayFor(stillPending));
- }
- });
+ }
}, delay);
};
diff --git a/frontend/src/views/knowledge/KnowledgeBaseBatchReparse.test.ts b/frontend/src/views/knowledge/KnowledgeBaseBatchReparse.test.ts
new file mode 100644
index 000000000..9b3a1abbc
--- /dev/null
+++ b/frontend/src/views/knowledge/KnowledgeBaseBatchReparse.test.ts
@@ -0,0 +1,675 @@
+import assert from 'node:assert/strict'
+import test, { type TestContext } from 'node:test'
+import * as vue from 'vue'
+import * as stall from '../../utils/knowledgeProcessingStall.ts'
+import { fixture, flush } from '../../test-utils/vueFixture.ts'
+
+type Row = {
+ id: string
+ parse_status: string
+ summary_status?: string
+ description?: string
+}
+type QueryResult = { success: boolean; data: Row[] }
+type ListQuery = { page: number; page_size: number; folder_path?: string }
+
+function deferred<T>() {
+ let resolve!: (value: T) => void
+ let reject!: (reason: Error) => void
+ const promise = new Promise<T>((accept, fail) => { resolve = accept; reject = fail })
+ return { promise, resolve, reject }
+}
+
+function row(id: string, parse_status = 'completed', description = 'original'): Row {
+ return { id, parse_status, summary_status: 'completed', description }
+}
+
+// 每推进一小段时间,就处理 Vue 监听器和 Promise 回调,
+// 避免一次性触发所有定时器,制造真实运行中不会出现的执行顺序。
+async function advance(t: TestContext, milliseconds: number) {
+ for (let remaining = milliseconds; remaining > 0; remaining -= 100) {
+ t.mock.timers.tick(Math.min(100, remaining))
+ await flush()
+ }
+}
+
+async function setup(t: TestContext, initialRows: Row[] = [row('doc')]) {
+ const f = fixture(t)
+ const route = vue.reactive({ params: { kbId: 'kb' }, query: {} })
+ const scopes = new Map<string, Row[]>([['kb/', initialRows.map(item => ({ ...item }))]])
+ const cards = vue.ref<Row[]>([])
+ const total = vue.ref(0)
+ const getKnowled = t.mock.fn(async (query: ListQuery, kb: string) => {
+ const source = scopes.get(kb + '/' + (query.folder_path || '')) || []
+ const start = (query.page - 1) * query.page_size
+ const next = source.slice(start, start + query.page_size).map(item => ({ ...item }))
+ if (query.page === 1) cards.value = next
+ else cards.value.push(...next)
+ total.value = source.length
+ })
+ const hook = {
+ cardList: cards, total, moreIndex: vue.ref(-1), details: vue.reactive({ id: '' }),
+ getKnowled, getCardDetails() {}, getfDetails() {}, onVisibleChange() {},
+ }
+ const queries: Array<{ ids: string[]; result: ReturnType<typeof deferred<QueryResult>> }> = []
+ let autoReply = false
+ const batchQueryKnowledge = t.mock.fn((query: string) => {
+ const ids = new URLSearchParams(query).getAll('ids')
+ const result = deferred<QueryResult>()
+ queries.push({ ids, result })
+ if (autoReply) {
+ const serverRows = scopes.get(route.params.kbId + '/') || []
+ result.resolve({
+ success: true,
+ data: serverRows.filter(item => ids.includes(item.id)).map(item => ({ ...item })),
+ })
+ }
+ return result.promise
+ })
+ let holdSubmission = false
+ const submissions: Array<{ kb: string; ids: string[]; result: ReturnType<typeof deferred<{ success: boolean }>> }> = []
+ const batchReparseKnowledge = t.mock.fn((kb: string, ids: string[]) => {
+ const result = deferred<{ success: boolean }>()
+ submissions.push({ kb, ids: [...ids], result })
+ if (!holdSubmission) result.resolve({ success: true })
+ return result.promise
+ })
+ const messages = { success: t.mock.fn(), error: t.mock.fn(), warning: t.mock.fn(), info: t.mock.fn() }
+ const page = f.component(new URL('./KnowledgeBase.vue', import.meta.url), {
+ 'vue-router': { useRoute: () => route, useRouter: () => ({ replace() {} }) },
+ 'vue-i18n': { useI18n: () => ({ t: (key: string) => key }) },
+ 'tdesign-vue-next': { MessagePlugin: messages },
+ '@/hooks/useKnowledgeBase': () => hook,
+ '@/stores/ui': { useUIStore: () => ({ clearSelectedTagIds() {} }) },
+ '@/stores/organization': { useOrganizationStore: () => ({ sharedKnowledgeBases: [], getKBPermission: () => 'owner' }) },
+ '@/stores/auth': { useAuthStore: () => ({ hasRole: () => true }) },
+ '@/stores/chatResources': { useChatResourcesStore: () => ({
+ fetchKnowledgeBaseById: async (id: string) => ({ id, type: 'document' }),
+ rawKnowledgeBases: [], async ensureKnowledgeBases() {},
+ }) },
+ '@/stores/editorResources': { useEditorResourcesStore: () => ({ parserEngines: [], async ensureParserEngines() {} }) },
+ '@/stores/uploadConfirm': { useUploadConfirmStore: () => ({}) },
+ '@/stores/uploadTasks': { useUploadTasksStore: () => ({}) },
+ '@/hooks/useMarqueeSelect': { useMarqueeSelect: () => ({}) },
+ '@/composables/useAssistantPageContext': { useAssistantPageContext() {} },
+ '@/utils/kbPermission': { permissionCanEditKB: () => true, permissionCanManageKB: () => true },
+ '@/utils/knowledgeDeletion': {}, '@/utils/knowledgeTrace': {},
+ '@/utils/knowledgeProcessingStall': stall, '@/api/wiki': {}, '@/api/knowledge-base': {},
+ '@/api/knowledge-base/index': {
+ batchQueryKnowledge, batchReparseKnowledge,
+ listKnowledgeFolders: async () => ({ data: { folders: [{ path: 'other', name: 'other' }] } }),
+ listKnowledgeTags: async () => ({ data: { data: [], total: 0 } }),
+ },
+ })
+ await flush()
+ // 用已加载多页的状态开始测试,覆盖滚动加载后的列表。
+ cards.value = initialRows.map(item => ({ ...item }))
+ total.value = initialRows.length
+ await flush()
+ getKnowled.mock.resetCalls()
+ const submit = async (ids: string[]) => {
+ page.vm.selectedIds = new Set(ids)
+ page.vm.batchMode = true
+ await page.vm.confirmBatchReparse()
+ await flush()
+ }
+ const respond = async (index: number, data: Row[]) => {
+ assert.ok(queries[index], '应存在序号为 ' + index + ' 的状态查询')
+ queries[index].result.resolve({ success: true, data })
+ await flush()
+ }
+ return {
+ ...f, ...page, route, hook, queries, submissions, messages, batchQueryKnowledge, batchReparseKnowledge,
+ submit, respond,
+ setScope(kb: string, folder: string, data: Row[]) { scopes.set(kb + '/' + folder, data) },
+ autoReply() { autoReply = true },
+ holdSubmission() { holdSubmission = true },
+ }
+}
+
+test('批量重建保留已加载的文档、页码、总数和加载状态,不重载列表', async t => {
+ const f = await setup(t, Array.from({ length: 100 }, (_, index) => row('doc-' + index)))
+ f.vm.page = 3
+ f.vm.knowledgeScroll = { scrollTop: 2400 }
+ const originalList = f.hook.cardList.value
+ const loading: boolean[] = []
+ const stop = vue.watch(() => f.vm.docListLoading, value => loading.push(value), { flush: 'sync' })
+ t.after(stop)
+
+ await f.submit(['doc-0', 'doc-99'])
+
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0, '批量重建不应重新请求第一页')
+ assert.equal(f.hook.cardList.value, originalList)
+ assert.equal(f.hook.cardList.value.length, 100)
+ assert.equal(f.hook.total.value, 100)
+ assert.equal(f.vm.page, 3)
+ assert.equal(f.vm.knowledgeScroll.scrollTop, 2400)
+ assert.equal(f.vm.docListLoading, false)
+ assert.ok(!loading.includes(true), '后台状态轮询不应隐藏文档计数')
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending')
+ assert.equal(f.hook.cardList.value[99].parse_status, 'pending')
+ assert.equal(f.hook.cardList.value[1].parse_status, 'completed')
+ await advance(t, 1500)
+ assert.deepEqual(f.queries[0].ids.sort(), ['doc-0', 'doc-99'])
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
+})
+
+test('状态请求未返回时,文档属性变化不会再发起一次查询', async t => {
+ const f = await setup(t, [row('doc', 'processing')])
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 1)
+ f.hook.cardList.value[0].description = 'changed while the server is slow'
+ await flush()
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 1, '同一视图最多只能有一个尚未返回的状态请求')
+ await f.respond(0, [row('doc', 'processing')])
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 2, '当前请求结束后应继续正常轮询')
+})
+
+test('新增批次加入现有轮询,不与尚未返回的状态请求重叠', async t => {
+ const f = await setup(t, [row('first', 'processing'), row('second')])
+ await advance(t, 1500)
+ assert.deepEqual(f.queries[0].ids, ['first'])
+ await f.submit(['second'])
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 1)
+ await f.respond(0, [row('first', 'processing')])
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 2)
+ assert.deepEqual(f.queries[1].ids.sort(), ['first', 'second'])
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
+})
+
+for (const destination of ['folder', 'knowledge base'] as const) {
+ test('切换' + (destination === 'folder' ? '目录' : '知识库') + '后,旧状态响应不能覆盖新视图或干扰其轮询', async t => {
+ const f = await setup(t, [row('doc', 'processing', 'old view')])
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 1)
+ if (destination === 'folder') {
+ f.setScope('kb', 'other', [row('doc', 'processing', 'new view')])
+ f.vm.selectedFolderPath = 'other'
+ } else {
+ f.setScope('other-kb', '', [row('doc', 'processing', 'new view')])
+ f.route.params.kbId = 'other-kb'
+ }
+ await flush()
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 2, '新视图应能独立发起状态查询')
+ await f.respond(0, [row('doc', 'completed', 'stale response')])
+ assert.equal(f.hook.cardList.value[0].description, 'new view')
+ assert.equal(f.hook.cardList.value[0].parse_status, 'processing')
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 2, '旧请求返回后不能替换新视图中尚未结束的请求')
+ await f.respond(1, [row('doc', 'completed', 'current response')])
+ assert.equal(f.hook.cardList.value[0].description, 'current response')
+ })
+}
+
+for (const settlesAs of ['success', 'failure'] as const) {
+ test('卸载后忽略旧请求的' + (settlesAs === 'success' ? '成功响应' : '失败响应') + ',不再继续轮询', async t => {
+ const f = await setup(t, [row('doc', 'processing', 'before unmount')])
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 1)
+ f.close()
+ if (settlesAs === 'success') await f.respond(0, [row('doc', 'completed', 'late completion')])
+ else {
+ f.queries[0].result.reject(new Error('late network failure'))
+ await flush()
+ }
+ assert.equal(f.hook.cardList.value[0].parse_status, 'processing')
+ assert.equal(f.hook.cardList.value[0].description, 'before unmount')
+ await advance(t, 15000)
+ assert.equal(f.queries.length, 1)
+ })
+}
+
+for (const destination of ['folder', 'knowledge base', 'unmount'] as const) {
+ test(({ folder: '切换目录', 'knowledge base': '切换知识库', unmount: '卸载组件' }[destination]) + '后,旧批量提交响应不能修改当前列表', async t => {
+ const f = await setup(t)
+ f.holdSubmission()
+ const submission = f.submit(['doc'])
+ await flush()
+ assert.equal(f.submissions.length, 1)
+ if (destination === 'folder') {
+ f.setScope('kb', 'other', [row('doc', 'completed', 'new view')])
+ f.vm.selectedFolderPath = 'other'
+ } else if (destination === 'knowledge base') {
+ f.setScope('other-kb', '', [row('doc', 'completed', 'new view')])
+ f.route.params.kbId = 'other-kb'
+ } else {
+ f.close()
+ }
+ await flush()
+ const before = f.hook.cardList.value.map(item => ({ ...item }))
+ f.hook.getKnowled.mock.resetCalls()
+ f.submissions[0].result.resolve({ success: true })
+ await submission
+ assert.deepEqual(f.hook.cardList.value.map(item => ({ ...item })), before)
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
+ await advance(t, 15000)
+ assert.equal(f.queries.length, 0)
+ })
+}
+
+test('服务器确认开始处理后解除临时待处理状态,完成后停止轮询', async t => {
+ const f = await setup(t)
+ await f.submit(['doc'])
+ await advance(t, 1500)
+ await f.respond(0, [row('doc', 'processing')])
+ assert.equal(f.hook.cardList.value[0].parse_status, 'processing')
+ await advance(t, 1500)
+ await f.respond(1, [row('doc', 'completed', 'rebuilt')])
+ assert.equal(f.hook.cardList.value[0].parse_status, 'completed')
+ assert.equal(f.hook.cardList.value[0].description, 'rebuilt')
+ const count = f.queries.length
+ await advance(t, 15000)
+ assert.equal(f.queries.length, count, '已完成文档的确认定时器到期后,不应重新启动轮询')
+})
+
+test('持续返回旧完成状态时,先保留待处理提示,等待到期后恢复服务器状态', async t => {
+ const f = await setup(t)
+ f.autoReply()
+ await f.submit(['doc'])
+ await advance(t, 6000)
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending')
+ await advance(t, 9000)
+ assert.equal(f.hook.cardList.value[0].parse_status, 'completed')
+ const count = f.queries.length
+ await advance(t, 6000)
+ assert.equal(f.queries.length, count, '确认等待结束后不应无限轮询')
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
+})
+
+test('第一批等待到期不会提前结束第二批的确认等待', async t => {
+ const f = await setup(t, [row('first'), row('second')])
+ f.autoReply()
+ await f.submit(['first'])
+ await advance(t, 6000)
+ await f.submit(['second'])
+ await advance(t, 8000)
+ assert.equal(f.hook.cardList.value.find(item => item.id === 'first')?.parse_status, 'completed')
+ assert.equal(f.hook.cardList.value.find(item => item.id === 'second')?.parse_status, 'pending')
+ await advance(t, 7000)
+ assert.equal(f.hook.cardList.value.find(item => item.id === 'second')?.parse_status, 'completed')
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
+})
+
+test('状态查询失败时保留文档,通过现有轮询重试', async t => {
+ const f = await setup(t, [row('doc', 'processing')])
+ await advance(t, 1500)
+ f.queries[0].result.reject(new Error('temporary network failure'))
+ await flush()
+ assert.equal(f.hook.cardList.value.length, 1)
+ assert.equal(f.hook.cardList.value[0].parse_status, 'processing')
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 2)
+ await f.respond(1, [row('doc', 'completed')])
+ const count = f.queries.length
+ await advance(t, 3000)
+ assert.equal(f.queries.length, count)
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
+})
+
+test('移除状态响应中已消失的文档,但保留未参与查询的已完成文档', async t => {
+ const f = await setup(t, [row('gone', 'processing'), row('active', 'processing'), row('untouched')])
+ await advance(t, 1500)
+ assert.deepEqual(f.queries[0].ids.sort(), ['active', 'gone'])
+ await f.respond(0, [row('active', 'processing')])
+ assert.deepEqual(f.hook.cardList.value.map(item => item.id), ['active', 'untouched'])
+ assert.equal(f.hook.total.value, 2)
+ await advance(t, 1500)
+ assert.deepEqual(f.queries[1].ids, ['active'])
+ await f.respond(1, [row('active', 'completed')])
+ const count = f.queries.length
+ await advance(t, 3000)
+ assert.equal(f.queries.length, count)
+})
+
+
+for (const oldResponse of ['processing', 'missing'] as const) {
+ test('重建前发出的查询不能确认新批次或删除其文档:' + (oldResponse === 'processing' ? '旧响应为处理中' : '旧响应不含文档'), async t => {
+ // 文档已解析完成但摘要仍待处理时,会继续轮询,也允许重新构建。
+ // 先挂起这次查询,再提交重建,确保旧响应不能被当作新一轮的处理结果。
+ const f = await setup(t, [{ ...row('doc'), summary_status: 'pending' }])
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 1)
+ await f.submit(['doc'])
+ await f.respond(0, oldResponse === 'processing' ? [row('doc', 'processing', 'previous parse')] : [])
+ assert.equal(f.hook.cardList.value.length, 1, '重建前的快照不能删除刚提交重建的文档')
+ assert.equal(f.hook.total.value, 1)
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending')
+ assert.equal(f.hook.cardList.value[0].description, '')
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 2)
+ await f.respond(1, [row('doc', 'completed')])
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending',
+ '旧查询中的处理中状态不能结束新批次的确认等待')
+ })
+}
+
+test('切目录后允许新提交,旧请求的 finally 不能解除新提交的忙碌状态', async t => {
+ const f = await setup(t)
+ f.holdSubmission()
+ const oldSubmission = f.submit(['doc'])
+ await flush()
+ assert.equal(f.submissions.length, 1)
+ f.setScope('kb', 'other', [row('new-doc')])
+ f.vm.selectedFolderPath = 'other'
+ await flush()
+ const newSubmission = f.submit(['new-doc'])
+ await flush()
+ assert.equal(f.submissions.length, 2, '新目录的提交不应被旧目录尚未返回的 POST 阻塞')
+ assert.equal(f.vm.batchReparsing, true)
+
+ f.submissions[0].result.resolve({ success: true })
+ await oldSubmission
+ assert.equal(f.vm.batchReparsing, true, '旧请求的 finally 不能清除新 POST 的忙碌状态')
+ assert.equal(f.hook.cardList.value[0].id, 'new-doc')
+ assert.equal(f.hook.cardList.value[0].parse_status, 'completed')
+ await f.submit(['new-doc'])
+ assert.equal(f.submissions.length, 2, '当前 POST 尚未结束时,应继续阻止第三次提交')
+
+ f.submissions[1].result.resolve({ success: true })
+ await newSubmission
+ assert.equal(f.vm.batchReparsing, false)
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending')
+})
+
+
+for (const oldResponse of ['completed', 'missing'] as const) {
+ test('确认等待到期后,重建前的查询仍然无效:' + (oldResponse === 'completed' ? '旧响应为已完成' : '旧响应不含文档'), async t => {
+ const f = await setup(t, [{ ...row('doc'), summary_status: 'pending' }])
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 1)
+ await f.submit(['doc'])
+ // 让重建前的查询挂起到新批次的确认等待结束之后。
+ // 等待到期只能解除临时提示,不能让旧响应重新变成有效结果。
+ await advance(t, 15000)
+ assert.equal(f.queries.length, 1, '旧请求挂起期间不应发起重叠查询')
+ await f.respond(0, oldResponse === 'completed' ? [row('doc', 'completed', 'previous parse')] : [])
+ assert.equal(f.hook.cardList.value.length, 1, '旧响应未包含文档时,也不能删除新一轮重建的文档')
+ assert.equal(f.hook.total.value, 1)
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending')
+ assert.equal(f.hook.cardList.value[0].description, '')
+
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 2, '丢弃旧响应后仍应继续轮询')
+ await f.respond(1, [row('doc', 'completed', 'rebuilt')])
+ assert.equal(f.hook.cardList.value[0].parse_status, 'completed')
+ assert.equal(f.hook.cardList.value[0].description, 'rebuilt')
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 2, '当前有效请求返回已完成后,应停止轮询')
+ })
+}
+
+
+test('同视图刷新列表后继续状态轮询,并保留原确认等待期限', async t => {
+ const f = await setup(t)
+ f.autoReply()
+ await f.submit(['doc'])
+ await advance(t, 6000)
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending')
+ const beforeRefresh = f.hook.cardList.value
+ const queriesBeforeRefresh = f.queries.length
+
+ // 排序、筛选或上传都可能正常刷新列表。此时任务尚未开始,
+ // 新数组里仍是旧的已完成状态,不能因此停止等待,也不能重新计时。
+ await f.vm.loadKnowledgeFiles('kb')
+ await flush()
+ assert.notEqual(f.hook.cardList.value, beforeRefresh)
+ assert.equal(f.hook.getKnowled.mock.callCount(), 1, '这里只允许本次主动刷新列表的请求')
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending')
+ await advance(t, 5900)
+ assert.ok(f.queries.length > queriesBeforeRefresh, '替换列表快照后不应停止状态轮询')
+ assert.equal(f.hook.cardList.value[0].parse_status, 'pending')
+
+ f.setScope('kb', '', [row('doc', 'completed', 'fresh server result')])
+ const queriesBeforeExpiry = f.queries.length
+ await advance(t, 3100)
+ assert.ok(f.queries.length > queriesBeforeExpiry, '原等待期限到达后,应发起新的状态查询')
+ assert.equal(f.hook.cardList.value[0].parse_status, 'completed',
+ '第 6 秒刷新列表,不应把 12 秒确认等待从头计时')
+ assert.equal(f.hook.cardList.value[0].description, 'fresh server result')
+ assert.equal(f.hook.getKnowled.mock.callCount(), 1, '后台等待确认不应额外重载列表')
+ const completedQueries = f.queries.length
+ await advance(t, 3000)
+ assert.equal(f.queries.length, completedQueries)
+})
+
+
+// 固定 UUID 便于复现,同时保留真实请求中每个 ID 的 36 字符长度。
+const uuid = (index: number) => `00000000-0000-4000-8000-${(index + 1).toString(16).padStart(12, '0')}`
+const uuidRows = (count: number, status = 'completed') =>
+ Array.from({ length: count }, (_, index) => row(uuid(index), status))
+
+function assertBoundedQueries(queries: Array<{ ids: string[] }>) {
+ for (const { ids } of queries) {
+ assert.ok(ids.length > 0 && ids.length <= 100, '每个状态查询必须包含 1 至 100 个文档 ID')
+ assert.ok(ids.every(id => /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-8[0-9a-f]{3}-[0-9a-f]{12}$/.test(id)),
+ '边界用例必须使用真实长度的 UUID')
+ const query = new URLSearchParams([['kb_id', uuid(999)], ...ids.map(id => ['ids', id])])
+ assert.ok(Buffer.byteLength(`GET /api/v1/knowledge/batch?${query} HTTP/1.1\r\n`) < 8192,
+ '包含知识库 UUID 的状态请求行应小于 8 KB')
+ }
+}
+
+test('200 个 UUID 分成两个串行查询,重建完成后保留列表和滚动位置', async t => {
+ const initial = uuidRows(200)
+ const f = await setup(t, initial)
+ f.vm.page = 6
+ f.vm.knowledgeScroll = { scrollTop: 8400 }
+ await f.submit(initial.map(item => item.id))
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 1)
+ assert.deepEqual(f.queries[0].ids, initial.slice(0, 100).map(item => item.id))
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 1, '第一批返回前不能发送第二批或另一轮轮询')
+ await f.respond(0, initial.slice(0, 100).map(item => row(item.id, 'processing')))
+ assert.equal(f.queries.length, 2)
+ assert.equal(f.hook.cardList.value.length, 200, '第一批响应不能把第二批文档判为缺失')
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 2, '第二批未返回时,整轮请求仍应保持占用')
+ await f.respond(1, initial.slice(100).map(item => row(item.id, 'processing')))
+ assert.deepEqual(f.queries.flatMap(query => query.ids), initial.map(item => item.id))
+ await advance(t, 1500)
+ await f.respond(2, initial.slice(0, 100))
+ await f.respond(3, initial.slice(100))
+ await advance(t, 15000)
+ assert.equal(f.queries.length, 4, '所有文档完成后应停止轮询')
+ assertBoundedQueries(f.queries)
+ assert.ok(f.hook.cardList.value.every(item => item.parse_status === 'completed'))
+ assert.equal(f.hook.cardList.value.length, 200)
+ assert.equal(f.hook.total.value, 200)
+ assert.equal(f.vm.page, 6)
+ assert.equal(f.vm.knowledgeScroll.scrollTop, 8400)
+ assert.equal(f.vm.docListLoading, false)
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0, '整个重建过程不能回到第一页整表刷新')
+})
+
+for (const failure of ['reject', 'unsuccessful'] as const) {
+ for (const failedBatch of [0, 1]) {
+ test(`第 ${failedBatch + 1} 个子批次${failure === 'reject' ? '网络失败' : '返回失败'}后保留文档,其他子批次正常处理并可恢复`, async t => {
+ const initial = uuidRows(200, 'processing')
+ const f = await setup(t, initial)
+ await advance(t, 1500)
+ for (let batch = 0; batch < 2; batch++) {
+ assert.equal(f.queries.length, batch + 1)
+ assert.equal(f.queries[batch].ids.length, 100)
+ if (batch === failedBatch) {
+ if (failure === 'reject') f.queries[batch].result.reject(new Error('临时网络故障'))
+ else f.queries[batch].result.resolve({ success: false, data: [] })
+ await flush()
+ } else {
+ // 成功子批次少返回一行:只允许删除这一行,失败子批次必须完整保留。
+ await f.respond(batch, initial.slice(batch * 100 + 1, (batch + 1) * 100).map(item => row(item.id)))
+ }
+ }
+ const failedIds = initial.slice(failedBatch * 100, (failedBatch + 1) * 100).map(item => item.id)
+ assert.equal(f.hook.cardList.value.length, 199)
+ assert.equal(f.hook.total.value, 199)
+ assert.deepEqual(f.hook.cardList.value.filter(item => item.parse_status === 'processing').map(item => item.id), failedIds)
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 3)
+ assert.deepEqual(f.queries[2].ids, failedIds, '下一轮只重试仍待处理的文档')
+ await f.respond(2, failedIds.map(id => row(id)))
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 3)
+ assertBoundedQueries(f.queries)
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
+ })
+ }
+}
+
+test('连续提交三批重建时,新文档加入下一轮分块查询,不重叠也不丢失', async t => {
+ const initial = uuidRows(300)
+ const f = await setup(t, initial)
+ await f.submit(initial.slice(0, 100).map(item => item.id))
+ await advance(t, 1500)
+ await f.submit(initial.slice(100, 200).map(item => item.id))
+ await f.submit(initial.slice(200).map(item => item.id))
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 1)
+ await f.respond(0, initial.slice(0, 100).map(item => row(item.id, 'processing')))
+ assert.equal(f.hook.cardList.value.length, 300)
+ assert.ok(f.hook.cardList.value.slice(100).every(item => item.parse_status === 'pending'))
+ await advance(t, 1500)
+ for (let batch = 0; batch < 3; batch++) {
+ assert.equal(f.queries.length, batch + 2)
+ assert.deepEqual(f.queries[batch + 1].ids, initial.slice(batch * 100, (batch + 1) * 100).map(item => item.id))
+ await f.respond(batch + 1, initial.slice(batch * 100, (batch + 1) * 100).map(item => row(item.id, 'processing')))
+ }
+ assertBoundedQueries(f.queries)
+ assert.equal(f.hook.cardList.value.length, 300)
+ assert.ok(f.hook.cardList.value.every(item => item.parse_status === 'processing'))
+ assert.equal(f.hook.getKnowled.mock.callCount(), 0)
+})
+
+for (const destination of ['folder', 'knowledge base', 'unmount'] as const) {
+ for (const settlesAs of ['success', 'failure'] as const) {
+ test(`分块查询中${{ folder: '切目录', 'knowledge base': '切知识库', unmount: '卸载' }[destination]}后,忽略旧批次${settlesAs === 'success' ? '成功' : '失败'}并停止后续请求`, async t => {
+ const initial = uuidRows(200, 'processing')
+ const f = await setup(t, initial)
+ await advance(t, 1500)
+ assert.equal(f.queries[0].ids.length, 100)
+ if (destination === 'folder') {
+ f.setScope('kb', 'other', [row(uuid(0), 'processing', '新目录')])
+ f.vm.selectedFolderPath = 'other'
+ } else if (destination === 'knowledge base') {
+ f.setScope('other-kb', '', [row(uuid(0), 'processing', '新知识库')])
+ f.route.params.kbId = 'other-kb'
+ } else f.close()
+ await flush()
+ const currentRows = f.hook.cardList.value.map(item => ({ ...item }))
+ await advance(t, 1500)
+ const count = destination === 'unmount' ? 1 : 2
+ assert.equal(f.queries.length, count)
+ if (settlesAs === 'success') await f.respond(0, [])
+ else {
+ f.queries[0].result.reject(new Error('旧视图请求失败'))
+ await flush()
+ }
+ assert.deepEqual(f.hook.cardList.value.map(item => ({ ...item })), currentRows)
+ await advance(t, 3000)
+ assert.equal(f.queries.length, count, '旧请求不能继续第二子批次,也不能解除新视图的请求锁')
+ if (destination !== 'unmount') {
+ await f.respond(1, [row(uuid(0), 'completed', '当前视图')])
+ assert.equal(f.hook.cardList.value[0].description, '当前视图')
+ }
+ assertBoundedQueries(f.queries)
+ })
+ }
+}
+
+for (const oldResponse of ['completed', 'missing'] as const) {
+ test('第二子批次挂起时重新构建,确认等待到期后仍丢弃旧' + (oldResponse === 'completed' ? '完成状态' : '缺失状态'), async t => {
+ const initial = uuidRows(200).map(item => ({ ...item, summary_status: 'pending' }))
+ const f = await setup(t, initial)
+ await advance(t, 1500)
+ await f.respond(0, initial.slice(0, 100).map(item => row(item.id)))
+ assert.equal(f.queries.length, 2)
+ const rebuiltId = uuid(100)
+ await f.submit([rebuiltId])
+ await advance(t, 15000)
+ assert.equal(f.queries.length, 2)
+ await f.respond(1, initial.slice(oldResponse === 'completed' ? 100 : 101).map(item => row(item.id)))
+ assert.equal(f.hook.cardList.value.length, 200)
+ assert.equal(f.hook.total.value, 200)
+ assert.equal(f.hook.cardList.value.find(item => item.id === rebuiltId)?.parse_status, 'pending')
+ assert.equal(f.hook.cardList.value.find(item => item.id === rebuiltId)?.description, '')
+ await advance(t, 1500)
+ assert.deepEqual(f.queries[2].ids, [rebuiltId])
+ await f.respond(2, [row(rebuiltId, 'completed', '新一轮处理结果')])
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 3)
+ assertBoundedQueries(f.queries)
+ assert.equal(f.hook.cardList.value.find(item => item.id === rebuiltId)?.description, '新一轮处理结果')
+ })
+}
+
+
+test('201 个文档保留不足 100 个的尾批,响应不能修改其他子批次的文档', async t => {
+ const initial = uuidRows(201, 'processing')
+ const f = await setup(t, initial)
+ await advance(t, 1500)
+ await f.respond(0, [
+ ...initial.slice(0, 100).map(item => row(item.id)),
+ row(uuid(200), 'completed', '不属于当前子请求的结果'),
+ ])
+ assert.equal(f.hook.cardList.value[200].parse_status, 'processing')
+ assert.equal(f.hook.cardList.value[200].description, 'original')
+ assert.equal(f.queries[1].ids.length, 100)
+ await f.respond(1, initial.slice(100, 200).map(item => row(item.id)))
+ assert.deepEqual(f.queries[2].ids, [uuid(200)])
+ await f.respond(2, [row(uuid(200))])
+ assert.deepEqual(f.queries.flatMap(query => query.ids), initial.map(item => item.id))
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 3)
+ assert.equal(f.hook.total.value, 201)
+ assertBoundedQueries(f.queries)
+})
+
+test('第二子请求发出前已提交的重建,应使用该子请求开始时的版本确认状态', async t => {
+ const initial = uuidRows(200).map(item => ({ ...item, summary_status: 'pending' }))
+ const f = await setup(t, initial)
+ await advance(t, 1500)
+ await f.submit([uuid(100)])
+ await f.respond(0, initial.slice(0, 100).map(item => row(item.id)))
+ assert.equal(f.queries.length, 2)
+ await f.respond(1, initial.slice(100).map(item => row(item.id, item.id === uuid(100) ? 'processing' : 'completed')))
+ assert.equal(f.hook.cardList.value[100].parse_status, 'processing', '此响应来自重建提交后的查询,应接受处理状态')
+ await advance(t, 1500)
+ assert.deepEqual(f.queries[2].ids, [uuid(100)])
+ await f.respond(2, [row(uuid(100))])
+ assert.equal(f.hook.cardList.value[100].parse_status, 'completed', '已经确认处理状态,不应再等临时提示到期')
+ assertBoundedQueries(f.queries)
+})
+
+for (const destination of ['folder', 'knowledge base'] as const) {
+ test('第二子批次返回前切换' + (destination === 'folder' ? '目录' : '知识库') + ',不会回写旧状态或继续第三批', async t => {
+ const initial = uuidRows(201, 'processing')
+ const f = await setup(t, initial)
+ await advance(t, 1500)
+ await f.respond(0, initial.slice(0, 100).map(item => row(item.id)))
+ assert.equal(f.queries.length, 2)
+ const newRow = row(uuid(100), 'processing', '新视图')
+ if (destination === 'folder') {
+ f.setScope('kb', 'other', [newRow])
+ f.vm.selectedFolderPath = 'other'
+ } else {
+ f.setScope('other-kb', '', [newRow])
+ f.route.params.kbId = 'other-kb'
+ }
+ await flush()
+ await advance(t, 1500)
+ assert.equal(f.queries.length, 3)
+ await f.respond(1, [row(uuid(100), 'completed', '旧视图')])
+ assert.equal(f.hook.cardList.value[0].description, '新视图')
+ assert.equal(f.hook.cardList.value[0].parse_status, 'processing')
+ await advance(t, 3000)
+ assert.equal(f.queries.length, 3, '不能继续旧视图的尾批,也不能解开新视图的请求锁')
+ await f.respond(2, [row(uuid(100), 'completed', '新视图已完成')])
+ assert.equal(f.hook.cardList.value[0].description, '新视图已完成')
+ assertBoundedQueries(f.queries)
+ })
+}