igeng
This commit is contained in:
@@ -1,23 +1,67 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import type { KnowledgeItem } from '#/api/knowledge';
|
||||
import { actionProblem, runKnowledgeBatch } from './batch';
|
||||
import { actionProblem, prepareKnowledgeBatch, runKnowledgeBatch } from './batch';
|
||||
|
||||
const item = (id: string, status = 'draft') => ({ id, title: id, revision: 4, status, conditions: '通用预约流程' } as KnowledgeItem);
|
||||
const settle = async () => { for (let index = 0; index < 8; index++) await Promise.resolve(); };
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
const promise = new Promise<T>((done) => { resolve = done; });
|
||||
return { promise, resolve };
|
||||
}
|
||||
describe('knowledge batch workflow', () => {
|
||||
it('preserves revisions, reports partial failures and continues to other items', async () => {
|
||||
const seen: number[] = [];
|
||||
const result = await runKnowledgeBatch([item('a'), item('b'), item('c')], async (entry) => {
|
||||
seen.push(entry.revision);
|
||||
it('preserves revisions, reports partial failures and never retries ambiguous mutations', async () => {
|
||||
const execute = vi.fn(async (entry: KnowledgeItem) => {
|
||||
if (entry.id === 'b') throw { response: { data: { detail: '知识版本已变化,请刷新' } } };
|
||||
}, () => {}, () => false);
|
||||
expect(seen).toEqual([4, 4, 4]);
|
||||
});
|
||||
const result = await runKnowledgeBatch([item('a'), item('b'), item('c')], execute, () => {}, () => false);
|
||||
expect(execute.mock.calls.map(([entry]) => entry.revision)).toEqual([4, 4, 4]);
|
||||
expect(execute).toHaveBeenCalledTimes(3);
|
||||
expect(result.map((row) => row.status)).toEqual(['success', 'failed', 'success']);
|
||||
expect(result[1]?.error).toContain('版本已变化');
|
||||
});
|
||||
it('stops only subsequent requests after an in-flight mutation completes', async () => {
|
||||
let stop = false;
|
||||
const result = await runKnowledgeBatch([item('a'), item('b')], async () => { stop = true; }, () => {}, () => stop);
|
||||
expect(result.map((row) => row.status)).toEqual(['success', 'pending']);
|
||||
it('limits mutations to three in flight and stops unscheduled rows while finishing the in-flight requests', async () => {
|
||||
const gates = Array.from({ length: 6 }, () => deferred<void>());
|
||||
let stop = false; let active = 0; let maximum = 0;
|
||||
const seen: string[] = [];
|
||||
const pending = runKnowledgeBatch(Array.from({ length: 6 }, (_, index) => item(String(index))), async (entry) => {
|
||||
seen.push(entry.id); active++; maximum = Math.max(maximum, active);
|
||||
await gates[Number(entry.id)]!.promise; active--;
|
||||
}, () => {}, () => stop);
|
||||
await settle(); expect(seen).toEqual(['0', '1', '2']);
|
||||
gates[0]!.resolve(); await settle(); expect(seen).toEqual(['0', '1', '2', '3']);
|
||||
stop = true; gates[1]!.resolve(); gates[2]!.resolve(); gates[3]!.resolve();
|
||||
const result = await pending;
|
||||
expect(maximum).toBe(3); expect(active).toBe(0);
|
||||
expect(result.map((row) => row.status)).toEqual(['success', 'success', 'success', 'success', 'pending', 'pending']);
|
||||
expect(seen).toEqual(['0', '1', '2', '3']);
|
||||
});
|
||||
it('fetches at most four details at once, preserves selection order, and blocks newer or invalid revisions', async () => {
|
||||
const entries = Array.from({ length: 7 }, (_, index) => item(String(index)));
|
||||
const gates = entries.map(() => deferred<KnowledgeItem>());
|
||||
let active = 0; let maximum = 0;
|
||||
const fetch = vi.fn(async (entry: KnowledgeItem) => {
|
||||
active++; maximum = Math.max(maximum, active);
|
||||
const value = await gates[Number(entry.id)]!.promise; active--; return value;
|
||||
});
|
||||
const pending = prepareKnowledgeBatch(entries, 'approve_publish', fetch, () => false);
|
||||
expect(fetch).toHaveBeenCalledTimes(4);
|
||||
gates[2]!.resolve({ ...entries[2]!, revision: 5 }); await settle(); expect(fetch).toHaveBeenCalledTimes(5);
|
||||
gates[0]!.resolve(entries[0]!); gates[1]!.resolve({ ...entries[1]!, conditions: '' });
|
||||
gates[3]!.resolve(entries[3]!); await settle();
|
||||
gates[4]!.resolve(entries[4]!); gates[5]!.resolve(entries[5]!); gates[6]!.resolve(entries[6]!);
|
||||
const result = await pending;
|
||||
expect(maximum).toBe(4); expect(result.details.map((entry) => entry.id)).toEqual(entries.map((entry) => entry.id));
|
||||
expect(result.problems['2']).toContain('版本已更新'); expect(result.problems['1']).toContain('适用条件');
|
||||
expect(entries[2]!.revision).toBe(4);
|
||||
});
|
||||
it('does not schedule more detail requests after its scope is invalidated', async () => {
|
||||
let stopped = false;
|
||||
const gates = Array.from({ length: 4 }, () => deferred<KnowledgeItem>());
|
||||
const fetch = vi.fn((entry: KnowledgeItem) => gates[Number(entry.id)]!.promise);
|
||||
const pending = prepareKnowledgeBatch(Array.from({ length: 8 }, (_, index) => item(String(index))), 'approve', fetch, () => stopped);
|
||||
stopped = true; gates.forEach((gate, index) => gate.resolve(item(String(index)))); await pending;
|
||||
expect(fetch).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
it('only permits deletion of rejected knowledge', () => {
|
||||
expect(actionProblem(item('a', 'rejected'), 'delete')).toBe('');
|
||||
@@ -25,10 +69,25 @@ describe('knowledge batch workflow', () => {
|
||||
expect(actionProblem(item('a', status), 'delete')).toBeTruthy();
|
||||
}
|
||||
});
|
||||
it('does not approve missing conditions or publish drafts', () => {
|
||||
expect(actionProblem({ ...item('a'), conditions: ' ' }, 'approve')).toContain('适用条件');
|
||||
it('requires draft status and conditions for either review action and never directly publishes drafts', () => {
|
||||
for (const action of ['approve', 'approve_publish'] as const) {
|
||||
expect(actionProblem({ ...item('a'), conditions: ' ' }, action)).toContain('适用条件');
|
||||
expect(actionProblem(item('a', 'stale'), action)).toBeTruthy();
|
||||
expect(actionProblem(item('a'), action)).toBe('');
|
||||
}
|
||||
expect(actionProblem(item('a'), 'publish')).toBeTruthy();
|
||||
expect(actionProblem(item('a', 'approved'), 'publish')).toBe('');
|
||||
expect(actionProblem(item('a', 'stale'), 'approve')).toBeTruthy();
|
||||
});
|
||||
it('deduplicates IDs before detail preparation and mutation while retaining the first selected revision', async () => {
|
||||
const first = item('same'); const duplicate = { ...first, revision: 9 };
|
||||
const fetch = vi.fn(async (entry: KnowledgeItem) => ({ ...entry }));
|
||||
const prepared = await prepareKnowledgeBatch([first, duplicate, item('other')], 'approve', fetch, () => false);
|
||||
expect(fetch.mock.calls.map(([entry]) => [entry.id, entry.revision])).toEqual([['same', 4], ['other', 4]]);
|
||||
expect(prepared.details).toHaveLength(2); expect(prepared.problems).toEqual({});
|
||||
const execute = vi.fn(async () => {});
|
||||
const result = await runKnowledgeBatch([first, duplicate, item('other')], execute, () => {}, () => false);
|
||||
expect(execute.mock.calls).toHaveLength(2);
|
||||
expect(result.map((row) => row.id)).toEqual(['same', 'other']);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user