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