| 1 | import type { ConfigService } from '@nestjs/config'; |
| 2 | import { execFileSync } |
| 3 | import { existsSync, mkdtempSync |
| 4 | import { tmpdir } |
| 5 | import path from 'node:path |
| 6 | import type { Readable |
| 7 | import { text } |
| 8 | import { afterEach, beforeEach |
| 9 | |
| 10 | import { CodeSearchUnavailableError } |
| 11 | import { |
| 12 | indexRepository, |
| 13 | listMatchingFiles, |
| 14 | searchFiles, |
| 15 | type CodeSearchFile, |
| 16 | } from '../../../lib/git/code-search/zoekt.js'; |
| 17 | import type { S3Service |
| 18 | import { CodeSearchService } |
| 19 | |
| 20 | vi.mock('../../../lib/git/code-search/zoekt.js' |
| 21 | ...(await importOriginal |
| 22 | typeof import('../../../lib/git/code-search/zoekt.js') |
| 23 | >()), |
| 24 | indexRepository: vi.fn |
| 25 | listMatchingFiles: vi.fn |
| 26 | searchFiles: vi.fn |
| 27 | })); |
| 28 | |
| 29 | const URL = ' |
| 30 | const MARKER = ' |
| 31 | const stamp = ( |
| 32 | |
| 33 | function fakeS3() { |
| 34 | const objects = |
| 35 | const calls: |
| 36 | return { |
| 37 | objects, |
| 38 | calls, |
| 39 | bucket: 'ghost', |
| 40 | getObject: vi.fn |
| 41 | const body = |
| 42 | if (body === |
| 43 | throw Object.assign( |
| 44 | } |
| 45 | return { Body: |
| 46 | }), |
| 47 | putObject: vi.fn |
| 48 | async ({ Key |
| 49 | calls.push(`put |
| 50 | objects.set(Key |
| 51 | }, |
| 52 | ), |
| 53 | deleteUnder: vi.fn |
| 54 | calls.push(`delete |
| 55 | for (const key |
| 56 | if (key.startsWith |
| 57 | } |
| 58 | }), |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | const file = ( |
| 63 | repositoryId, |
| 64 | path, |
| 65 | commit: 'c1', |
| 66 | language: 'TypeScript', |
| 67 | }); |
| 68 | const hit = ( |
| 69 | ...file(repositoryId, |
| 70 | matchCount: 0, |
| 71 | lines: [], |
| 72 | }); |
| 73 | |
| 74 | describe('CodeSearchService', () |
| 75 | let root: |
| 76 | let repo: |
| 77 | let s3: |
| 78 | |
| 79 | const git = |
| 80 | execFileSync('git', |
| 81 | .toString() |
| 82 | .trim(); |
| 83 | const commit = |
| 84 | writeFileSync(path.join |
| 85 | git('add', |
| 86 | git('commit', |
| 87 | git('push', |
| 88 | return git('rev-parse |
| 89 | }; |
| 90 | const service = |
| 91 | new CodeSearchService( |
| 92 | { get: (key |
| 93 | s3 as unknown |
| 94 | ); |
| 95 | const target = |
| 96 | repositoryId: 'repo_a', |
| 97 | isPublic: true, |
| 98 | repoDirectory: repo, |
| 99 | }); |
| 100 | |
| 101 | beforeEach(() => |
| 102 | root = mkdtempSync( |
| 103 | repo = path. |
| 104 | execFileSync('git', |
| 105 | execFileSync('git', |
| 106 | git('config', |
| 107 | git('config', |
| 108 | |
| 109 | s3 = fakeS3() |
| 110 | vi.mocked(indexRepository |
| 111 | .mockReset() |
| 112 | .mockImplementation(async ( |
| 113 | writeFileSync(path.join |
| 114 | return ['repo_a_v16.00000.zoekt'] |
| 115 | }); |
| 116 | vi.mocked(listMatchingFiles |
| 117 | vi.mocked(searchFiles |
| 118 | .mockReset() |
| 119 | .mockImplementation(async ( |
| 120 | files.map((page |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | afterEach(() => |
| 125 | |
| 126 | describe('index', |
| 127 | it('does nothing when code search is off', |
| 128 | commit('one'); |
| 129 | |
| 130 | await service({}) |
| 131 | |
| 132 | expect(indexRepository).not |
| 133 | expect(s3.getObject |
| 134 | }); |
| 135 | |
| 136 | it('does nothing for an empty repository', |
| 137 | await service().index |
| 138 | |
| 139 | expect(indexRepository).not |
| 140 | }); |
| 141 | |
| 142 | it('publishes the shards, drops stale ones, then moves the marker', |
| 143 | const head = |
| 144 | s3.objects.set |
| 145 | |
| 146 | await service().index |
| 147 | |
| 148 | expect(indexRepository).toHaveBeenCalledWith |
| 149 | expect.objectContaining({ |
| 150 | repoDirectory: repo, |
| 151 | name: 'repo_a', |
| 152 | isPublic: false, |
| 153 | }), |
| 154 | ); |
| 155 | expect(s3.calls |
| 156 | 'put zoekt/repo_a_v16.00000.zoekt', |
| 157 | 'delete zoekt/repo_a_v', |
| 158 | `put ${MARKER}`, |
| 159 | ]); |
| 160 | expect(Object.fromEntries |
| 161 | 'zoekt/repo_a_v16.00000.zoekt': 'shard', |
| 162 | [MARKER]: stamp |
| 163 | }); |
| 164 | }); |
| 165 | |
| 166 | it('skips a HEAD that is already published', |
| 167 | s3.objects.set |
| 168 | |
| 169 | await service().index |
| 170 | |
| 171 | expect(indexRepository).not |
| 172 | }); |
| 173 | |
| 174 | it('republishes when only the visibility changed', |
| 175 | const head = |
| 176 | s3.objects.set |
| 177 | |
| 178 | await service().index |
| 179 | |
| 180 | expect(indexRepository).toHaveBeenCalledWith |
| 181 | expect.objectContaining({ |
| 182 | ); |
| 183 | expect(s3.objects |
| 184 | }); |
| 185 | |
| 186 | it('remembers a published HEAD instead of asking S3 again', |
| 187 | commit('one'); |
| 188 | const search = |
| 189 | |
| 190 | await search.index |
| 191 | await search.index |
| 192 | |
| 193 | expect(indexRepository).toHaveBeenCalledTimes |
| 194 | expect(s3.getObject |
| 195 | }); |
| 196 | |
| 197 | it('remembers a HEAD S3 confirmed as published', |
| 198 | s3.objects.set |
| 199 | const search = |
| 200 | |
| 201 | await search.index |
| 202 | await search.index |
| 203 | |
| 204 | expect(s3.getObject |
| 205 | }); |
| 206 | |
| 207 | it('queues one rerun for the calls made while a run is in flight', |
| 208 | commit('one'); |
| 209 | let second = |
| 210 | const search = |
| 211 | vi.mocked(indexRepository |
| 212 | async ({ indexDir |
| 213 | second = commit( |
| 214 | void search.index( |
| 215 | void search.index( |
| 216 | writeFileSync(path.join |
| 217 | return ['repo_a_v16.00000.zoekt'] |
| 218 | }, |
| 219 | ); |
| 220 | |
| 221 | await search.index |
| 222 | |
| 223 | expect(indexRepository).toHaveBeenCalledTimes |
| 224 | expect(s3.objects |
| 225 | }); |
| 226 | |
| 227 | it('fails when the marker cannot be read', |
| 228 | commit('one'); |
| 229 | s3.getObject.mockRejectedValueOnce |
| 230 | |
| 231 | await expect(service |
| 232 | }); |
| 233 | |
| 234 | it('removes its scratch directory when indexing fails', |
| 235 | commit('one'); |
| 236 | let indexDir = |
| 237 | vi.mocked(indexRepository |
| 238 | indexDir = options. |
| 239 | throw new Error( |
| 240 | }); |
| 241 | |
| 242 | await expect(service |
| 243 | |
| 244 | expect(existsSync(indexDir |
| 245 | expect(s3.objects |
| 246 | }); |
| 247 | }); |
| 248 | |
| 249 | describe('indexInBackground', |
| 250 | it('swallows a failed index', |
| 251 | commit('one'); |
| 252 | vi.mocked(indexRepository |
| 253 | |
| 254 | service().indexInBackground(target |
| 255 | |
| 256 | await vi.waitFor |
| 257 | }); |
| 258 | }); |
| 259 | |
| 260 | describe('searchRepository', |
| 261 | const search = |
| 262 | on.searchRepository({ |
| 263 | |
| 264 | it('is unavailable when code search is off', |
| 265 | await expect(search |
| 266 | CodeSearchUnavailableError, |
| 267 | ); |
| 268 | }); |
| 269 | |
| 270 | it('reports and starts indexing when HEAD is not published', |
| 271 | const head = |
| 272 | |
| 273 | const { indexing |
| 274 | |
| 275 | expect(indexing).toBe |
| 276 | await vi.waitFor |
| 277 | }); |
| 278 | |
| 279 | it('reports indexing while a run is in flight', |
| 280 | commit('one'); |
| 281 | let finish!: |
| 282 | vi.mocked(indexRepository |
| 283 | () => new |
| 284 | ); |
| 285 | const on = |
| 286 | const run = |
| 287 | await vi.waitFor |
| 288 | |
| 289 | const { indexing |
| 290 | finish(); |
| 291 | await run; |
| 292 | |
| 293 | expect(indexing).toBe |
| 294 | }); |
| 295 | |
| 296 | it('does not index a HEAD that is already published', |
| 297 | s3.objects.set |
| 298 | |
| 299 | const { indexing |
| 300 | |
| 301 | expect(indexing).toBe |
| 302 | expect(indexRepository).not |
| 303 | expect(listMatchingFiles).toHaveBeenCalledWith |
| 304 | url: URL, |
| 305 | query: 'r:^repo_a$ (x)', |
| 306 | }); |
| 307 | }); |
| 308 | |
| 309 | it('drops files from other repositories before cutting the page', |
| 310 | s3.objects.set |
| 311 | vi.mocked(listMatchingFiles |
| 312 | file('repo_b', |
| 313 | file('repo_a', |
| 314 | file('repo_b', |
| 315 | file('repo_a', |
| 316 | ]); |
| 317 | |
| 318 | const { files, |
| 319 | |
| 320 | expect(searchFiles).toHaveBeenCalledWith |
| 321 | url: URL, |
| 322 | query: 'r:^repo_a$ (x) or (r:repo_b)', |
| 323 | files: [file(' |
| 324 | }); |
| 325 | expect(files).toEqual |
| 326 | expect(nextCursor).toBeNull |
| 327 | }); |
| 328 | |
| 329 | it('pages through the files with an offset cursor', |
| 330 | s3.objects.set |
| 331 | vi.mocked(listMatchingFiles |
| 332 | ['a.ts', 'b.ts' |
| 333 | ); |
| 334 | const page = |
| 335 | service().searchRepository({ |
| 336 | ...target(), |
| 337 | query: 'x', |
| 338 | cursor, |
| 339 | limit: 2, |
| 340 | }); |
| 341 | |
| 342 | const first = |
| 343 | expect(first.files |
| 344 | hit('repo_a', |
| 345 | hit('repo_a', |
| 346 | ]); |
| 347 | expect(first.nextCursor |
| 348 | |
| 349 | const second = |
| 350 | expect(second.files |
| 351 | expect(second.nextCursor |
| 352 | }); |
| 353 | }); |
| 354 | |
| 355 | describe('searchPublic', |
| 356 | const visible = |
| 357 | |
| 358 | it('is unavailable when code search is off', |
| 359 | await expect( |
| 360 | service({}).searchPublic |
| 361 | ).rejects.toBeInstanceOf( |
| 362 | }); |
| 363 | |
| 364 | it('narrows the query to public shards and keeps only what the caller sees', |
| 365 | vi.mocked(listMatchingFiles |
| 366 | file('repo_a'), |
| 367 | file('repo_private'), |
| 368 | ]); |
| 369 | const sees = |
| 370 | |
| 371 | await expect( |
| 372 | service().searchPublic({ |
| 373 | query: 'x', |
| 374 | limit: 10, |
| 375 | repositoryIds: ['repo_a'], |
| 376 | visible: sees, |
| 377 | }), |
| 378 | ).resolves.toEqual( |
| 379 | expect(listMatchingFiles).toHaveBeenCalledWith |
| 380 | url: URL, |
| 381 | query: 'public:yes r:^(repo_a)$ (x)', |
| 382 | }); |
| 383 | expect(sees).toHaveBeenCalledWith |
| 384 | }); |
| 385 | }); |
| 386 | }); |