|
| 1 | +/** @vitest-environment node */ |
| 2 | +import { createHash } from 'node:crypto' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | +import { |
| 5 | + FILE_SEARCH_CHUNK_BYTES, |
| 6 | + FILE_SEARCH_INSERT_BATCH_BYTES, |
| 7 | + FILE_SEARCH_INSERT_BATCH_ROWS, |
| 8 | + FILE_SEARCH_INSERT_BATCH_TRIGRAM_KEYS, |
| 9 | +} from '@/lib/workspace-files/search/constants' |
| 10 | +import { iterateFileSearchBatches } from '@/lib/workspace-files/search/index-batches' |
| 11 | +import { |
| 12 | + estimateTrigramKeys, |
| 13 | + type FileSearchChunk, |
| 14 | + iterateFileSearchChunks, |
| 15 | + planFileSearchIndex, |
| 16 | +} from '@/lib/workspace-files/search/index-plan' |
| 17 | + |
| 18 | +const signal = new AbortController().signal |
| 19 | +const chunk = (content: string, ordinal = 0): FileSearchChunk => ({ |
| 20 | + content, |
| 21 | + ordinal, |
| 22 | + lineStart: ordinal + 1, |
| 23 | + fragment: false, |
| 24 | + overlap: 0, |
| 25 | +}) |
| 26 | + |
| 27 | +/** A de Bruijn prefix has no repeated three-letter windows, exercising the padding overhead. */ |
| 28 | +function uniqueTrigrams(): string { |
| 29 | + const alphabet = 'abcdefghijklmnopqrstuvwxyz' |
| 30 | + const positions = Array<number>(4).fill(0) |
| 31 | + let text = '' |
| 32 | + const visit = (depth: number, period: number) => { |
| 33 | + if (depth > 3) { |
| 34 | + if (3 % period === 0) for (let i = 1; i <= period; i++) text += alphabet[positions[i]] |
| 35 | + return |
| 36 | + } |
| 37 | + positions[depth] = positions[depth - period] |
| 38 | + visit(depth + 1, period) |
| 39 | + for (let i = positions[depth - period] + 1; i < alphabet.length; i++) { |
| 40 | + positions[depth] = i |
| 41 | + visit(depth + 1, depth) |
| 42 | + } |
| 43 | + } |
| 44 | + visit(1, 1) |
| 45 | + return text.slice(0, FILE_SEARCH_CHUNK_BYTES) |
| 46 | +} |
| 47 | + |
| 48 | +describe('file search insert batches', () => { |
| 49 | + it('counts GIN work per row, preserving every dense lockfile line and ordinal', () => { |
| 50 | + const text = Array.from( |
| 51 | + { length: 1200 }, |
| 52 | + (_, i) => |
| 53 | + `dependency-${i}: sha512-${createHash('sha512').update(String(i)).digest('base64')}\n` |
| 54 | + ).join('') |
| 55 | + const chunks = [ |
| 56 | + ...iterateFileSearchChunks(planFileSearchIndex({ text, partial: false }, signal), signal), |
| 57 | + ] |
| 58 | + const batches = [...iterateFileSearchBatches(chunks, signal)] |
| 59 | + expect(batches.length).toBeGreaterThan( |
| 60 | + Math.ceil(Buffer.byteLength(text) / FILE_SEARCH_INSERT_BATCH_BYTES) |
| 61 | + ) |
| 62 | + expect(batches.flat()).toEqual(chunks) |
| 63 | + expect( |
| 64 | + batches |
| 65 | + .flat() |
| 66 | + .map((c) => c.content) |
| 67 | + .join('') |
| 68 | + ).toBe(text) |
| 69 | + for (const batch of batches) { |
| 70 | + expect(batch.reduce((sum, c) => sum + estimateTrigramKeys(c.content), 0)).toBeLessThanOrEqual( |
| 71 | + FILE_SEARCH_INSERT_BATCH_TRIGRAM_KEYS |
| 72 | + ) |
| 73 | + } |
| 74 | + const repeated = chunk(chunks[0].content) |
| 75 | + const sameContent = [...iterateFileSearchBatches(Array(16).fill(repeated), signal)] |
| 76 | + expect(sameContent.length).toBeGreaterThan(1) |
| 77 | + }) |
| 78 | + |
| 79 | + it('retains byte batching for low-key text and the row bound for tiny chunks', () => { |
| 80 | + const full = chunk('a'.repeat(FILE_SEARCH_CHUNK_BYTES)) |
| 81 | + const byBytes = [...iterateFileSearchBatches(Array(17).fill(full), signal)] |
| 82 | + expect(byBytes.map((batch) => batch.length)).toEqual([16, 1]) |
| 83 | + const byRows = [ |
| 84 | + ...iterateFileSearchBatches(Array(FILE_SEARCH_INSERT_BATCH_ROWS + 1).fill(chunk('')), signal), |
| 85 | + ] |
| 86 | + expect(byRows.map((batch) => batch.length)).toEqual([FILE_SEARCH_INSERT_BATCH_ROWS, 1]) |
| 87 | + }) |
| 88 | + |
| 89 | + it('writes a maximum-key chunk alone without splitting or dropping it', () => { |
| 90 | + const dense = chunk(uniqueTrigrams(), 1) |
| 91 | + expect(estimateTrigramKeys(dense.content)).toBeGreaterThan( |
| 92 | + FILE_SEARCH_INSERT_BATCH_TRIGRAM_KEYS |
| 93 | + ) |
| 94 | + const first = chunk('first') |
| 95 | + const last = chunk('last', 2) |
| 96 | + expect([...iterateFileSearchBatches([first, dense, last], signal)]).toEqual([ |
| 97 | + [first], |
| 98 | + [dense], |
| 99 | + [last], |
| 100 | + ]) |
| 101 | + }) |
| 102 | + |
| 103 | + it('rejects oversized chunks and emits no empty batch', () => { |
| 104 | + expect([...iterateFileSearchBatches([], signal)]).toEqual([]) |
| 105 | + expect(() => [ |
| 106 | + ...iterateFileSearchBatches([chunk('a'.repeat(FILE_SEARCH_CHUNK_BYTES + 1))], signal), |
| 107 | + ]).toThrow('chunk exceeds its budget') |
| 108 | + }) |
| 109 | + |
| 110 | + it('consumes only one batch plus lookahead and honors cancellation between writes', () => { |
| 111 | + let consumed = 0 |
| 112 | + function* source() { |
| 113 | + for (let i = 0; i < 100; i++) { |
| 114 | + consumed++ |
| 115 | + yield chunk('a'.repeat(FILE_SEARCH_CHUNK_BYTES), i) |
| 116 | + } |
| 117 | + } |
| 118 | + const controller = new AbortController() |
| 119 | + const batches = iterateFileSearchBatches(source(), controller.signal) |
| 120 | + expect(batches.next().value).toHaveLength(16) |
| 121 | + expect(consumed).toBe(17) |
| 122 | + controller.abort(new Error('canceled')) |
| 123 | + expect(() => batches.next()).toThrow('canceled') |
| 124 | + expect(consumed).toBe(17) |
| 125 | + }) |
| 126 | +}) |
0 commit comments