| 1 | import { spawn, execFileSync, type ChildProcess } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { |
| 6 | afterAll, |
| 7 | afterEach, |
| 8 | beforeAll, |
| 9 | describe, |
| 10 | expect, |
| 11 | it, |
| 12 | vi, |
| 13 | } from 'vitest'; |
| 14 | |
| 15 | import { |
| 16 | CodeSearchUnavailableError, |
| 17 | InvalidSearchQueryError, |
| 18 | } from './code-search.errors.js'; |
| 19 | import { indexRepository, searchIndex |
| 20 | |
| 21 | const hasZoekt = ( |
| 22 | try { |
| 23 | execFileSync('zoekt-webserver', |
| 24 | execFileSync('zoekt-git-index', |
| 25 | return true; |
| 26 | } catch { |
| 27 | return false; |
| 28 | } |
| 29 | })(); |
| 30 | |
| 31 | const b64 = ( |
| 32 | |
| 33 | describe('searchIndex', () |
| 34 | afterEach(() => |
| 35 | |
| 36 | const replyWith = |
| 37 | vi.stubGlobal( |
| 38 | 'fetch', |
| 39 | vi.fn().mockResolvedValue |
| 40 | new Response(typeof |
| 41 | status, |
| 42 | }), |
| 43 | ), |
| 44 | ); |
| 45 | const search = |
| 46 | searchIndex({ url: |
| 47 | |
| 48 | it('returns no files when zoekt reports none', |
| 49 | replyWith({ Result: |
| 50 | |
| 51 | await expect(search |
| 52 | }); |
| 53 | |
| 54 | it('decodes lines and turns byte offsets into string offsets', |
| 55 | const line = |
| 56 | replyWith({ |
| 57 | Result: { |
| 58 | Files: [ |
| 59 | { |
| 60 | FileName: 'a.ts', |
| 61 | Repository: 'repo_a', |
| 62 | Version: 'c1', |
| 63 | Language: 'TypeScript', |
| 64 | LineMatches: [ |
| 65 | { |
| 66 | Line: b64('a.ts |
| 67 | LineNumber: 0, |
| 68 | FileName: true, |
| 69 | LineFragments: [{ LineOffset: |
| 70 | }, |
| 71 | { |
| 72 | Line: b64(`${ |
| 73 | LineNumber: 3, |
| 74 | FileName: false, |
| 75 | LineFragments: [ |
| 76 | { |
| 77 | LineOffset: Buffer.byteLength |
| 78 | MatchLength: 6, |
| 79 | }, |
| 80 | ], |
| 81 | }, |
| 82 | ], |
| 83 | }, |
| 84 | { |
| 85 | FileName: 'b.ts', |
| 86 | Repository: 'repo_a', |
| 87 | Version: 'c1', |
| 88 | Language: 'TypeScript', |
| 89 | }, |
| 90 | ], |
| 91 | }, |
| 92 | }); |
| 93 | |
| 94 | await expect(search |
| 95 | { |
| 96 | repositoryId: 'repo_a', |
| 97 | commit: 'c1', |
| 98 | path: 'a.ts', |
| 99 | language: 'TypeScript', |
| 100 | lines: [ |
| 101 | { |
| 102 | lineNumber: 3, |
| 103 | line, |
| 104 | ranges: [{ start: |
| 105 | }, |
| 106 | ], |
| 107 | }, |
| 108 | { |
| 109 | repositoryId: 'repo_a', |
| 110 | commit: 'c1', |
| 111 | path: 'b.ts', |
| 112 | language: 'TypeScript', |
| 113 | lines: [], |
| 114 | }, |
| 115 | ]); |
| 116 | }); |
| 117 | |
| 118 | it('rejects a query zoekt cannot parse', |
| 119 | replyWith({ Error: |
| 120 | |
| 121 | await expect(search |
| 122 | }); |
| 123 | |
| 124 | it('is unavailable when zoekt fails', |
| 125 | replyWith('oops', |
| 126 | |
| 127 | await expect(search |
| 128 | constructor: CodeSearchUnavailableError, |
| 129 | cause: new Error |
| 130 | }); |
| 131 | }); |
| 132 | |
| 133 | it('is unavailable when zoekt cannot be reached', |
| 134 | const refused = |
| 135 | vi.stubGlobal('fetch |
| 136 | |
| 137 | await expect(search |
| 138 | constructor: CodeSearchUnavailableError, |
| 139 | cause: refused, |
| 140 | }); |
| 141 | }); |
| 142 | }); |
| 143 | |
| 144 | describe.skipIf(!hasZoekt |
| 145 | let root: |
| 146 | let webserver: |
| 147 | let shards: |
| 148 | const url = |
| 149 | |
| 150 | const repository = |
| 151 | const source = |
| 152 | const git = |
| 153 | execFileSync('git', |
| 154 | |
| 155 | execFileSync('git', |
| 156 | git('config', |
| 157 | git('config', |
| 158 | writeFileSync(path.join |
| 159 | git('add', |
| 160 | git('commit', |
| 161 | execFileSync('git', |
| 162 | 'clone', |
| 163 | '-q', |
| 164 | '--bare', |
| 165 | source, |
| 166 | path.join(root |
| 167 | ]); |
| 168 | return path.join |
| 169 | }; |
| 170 | |
| 171 | beforeAll(async () |
| 172 | root = mkdtempSync( |
| 173 | const indexDir = |
| 174 | |
| 175 | shards = await |
| 176 | indexDir, |
| 177 | repoDirectory: repository('repo_public |
| 178 | name: 'repo_public', |
| 179 | isPublic: true, |
| 180 | }); |
| 181 | await indexRepository({ |
| 182 | indexDir, |
| 183 | repoDirectory: repository('repo_private |
| 184 | name: 'repo_private', |
| 185 | isPublic: false, |
| 186 | }); |
| 187 | |
| 188 | webserver = spawn( |
| 189 | '-index', |
| 190 | indexDir, |
| 191 | '-listen', |
| 192 | '127.0.0.1:16070', |
| 193 | '-rpc', |
| 194 | '-html=false', |
| 195 | ]); |
| 196 | await vi.waitFor |
| 197 | async () => |
| 198 | { timeout: 10_000, interval |
| 199 | ); |
| 200 | }); |
| 201 | |
| 202 | afterAll(() => |
| 203 | webserver?.kill(); |
| 204 | rmSync(root, { |
| 205 | }); |
| 206 | |
| 207 | const searchUntil = |
| 208 | vi.waitFor( |
| 209 | async () => |
| 210 | const found = |
| 211 | expect(found).toHaveLength |
| 212 | return found; |
| 213 | }, |
| 214 | { timeout: 10_000, interval |
| 215 | ); |
| 216 | |
| 217 | it('names the shards after the repository', |
| 218 | expect(shards).toEqual |
| 219 | }); |
| 220 | |
| 221 | it('finds a line in every indexed shard', |
| 222 | const hits = |
| 223 | |
| 224 | expect(hits.map |
| 225 | 'repo_private', |
| 226 | 'repo_public', |
| 227 | ]); |
| 228 | }); |
| 229 | |
| 230 | it('narrows to shards flagged public', |
| 231 | const [hit] |
| 232 | |
| 233 | expect(hit).toEqual |
| 234 | repositoryId: 'repo_public', |
| 235 | commit: expect.stringMatching |
| 236 | path: 'a.ts', |
| 237 | language: 'TypeScript', |
| 238 | lines: [ |
| 239 | { |
| 240 | lineNumber: 1, |
| 241 | line: 'const café = findMe();', |
| 242 | ranges: [{ start: |
| 243 | }, |
| 244 | ], |
| 245 | }); |
| 246 | }); |
| 247 | }); |