test(resource): cover search filtering and lifecycle (#607)

This commit is contained in:
InfinityPacer
2026-07-31 10:59:35 +08:00
committed by GitHub
parent b85d412308
commit a1802f7f93
8 changed files with 1507 additions and 26 deletions
+37
View File
@@ -39,4 +39,41 @@ describe('SearchReplaceBatchCollector', () => {
expect(isSearchReplaceBatchEvent(malformedEvent)).toBe(false)
expect(() => new SearchReplaceBatchCollector<number>().append(malformedEvent)).toThrow('搜索结果批次格式无效')
})
it.each([
['negative batch index', { batch_index: -1 }],
['batch index outside the declared count', { batch_index: 3 }],
['non-integer total', { total_items: 1.5 }],
['missing items', { items: undefined }],
])('rejects %s metadata', (_case, overrides) => {
expect(isSearchReplaceBatchEvent({ ...createBatch(0, [1, 2]), ...overrides })).toBe(false)
})
it('rejects metadata drift and resets before accepting another sequence', () => {
const collector = new SearchReplaceBatchCollector<number>()
expect(collector.append(createBatch(0, [1, 2]))).toBeNull()
expect(() => collector.append({ ...createBatch(1, [3, 4]), total_items: 6 })).toThrow('搜索结果批次顺序不连续')
expect(collector.append(createBatch(0, [1, 2]))).toBeNull()
expect(collector.append(createBatch(1, [3, 4]))).toBeNull()
expect(collector.append(createBatch(2, [5]))).toEqual([1, 2, 3, 4, 5])
})
it('rejects a completed sequence whose item count differs from the declared total', () => {
const collector = new SearchReplaceBatchCollector<number>()
expect(collector.append(createBatch(0, [1, 2]))).toBeNull()
expect(collector.append(createBatch(1, [3, 4]))).toBeNull()
expect(() => collector.append(createBatch(2, []))).toThrow('搜索结果批次数量不完整')
})
it('starts a fresh sequence when another first batch arrives', () => {
const collector = new SearchReplaceBatchCollector<number>()
expect(collector.append(createBatch(0, [99, 100]))).toBeNull()
expect(collector.append(createBatch(0, [1, 2]))).toBeNull()
expect(collector.append(createBatch(1, [3, 4]))).toBeNull()
expect(collector.append(createBatch(2, [5]))).toEqual([1, 2, 3, 4, 5])
})
})