|
| 1 | +// npx vitest run src/services/ripgrep/__tests__/diagnostic.spec.ts |
| 2 | + |
| 3 | +import * as path from "path" |
| 4 | + |
| 5 | +import { vi, describe, it, expect, beforeEach } from "vitest" |
| 6 | + |
| 7 | +import { getRipgrepDiagnostic } from "../diagnostic" |
| 8 | + |
| 9 | +const ripgrepMock = vi.hoisted(() => ({ |
| 10 | + value: undefined as { rgPath?: string } | undefined, |
| 11 | +})) |
| 12 | + |
| 13 | +const fsMock = vi.hoisted(() => ({ |
| 14 | + existing: new Set<string>(), |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock("../internal/loadRipgrep", () => ({ |
| 18 | + loadRipgrep: () => ripgrepMock.value, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock("../../../utils/fs", () => ({ |
| 22 | + fileExistsAtPath: (p: string) => Promise.resolve(fsMock.existing.has(p)), |
| 23 | +})) |
| 24 | + |
| 25 | +const APP_ROOT = "/app" |
| 26 | + |
| 27 | +const binName = process.platform.startsWith("win") ? "rg.exe" : "rg" |
| 28 | +const universalRelBin = `bin/${process.platform}-${process.arch}/${binName}` |
| 29 | + |
| 30 | +const expectedCandidates = [ |
| 31 | + path.join(APP_ROOT, "node_modules", "@vscode", "ripgrep", "bin", binName), |
| 32 | + path.join(APP_ROOT, "node_modules", "vscode-ripgrep", "bin", binName), |
| 33 | + path.join(APP_ROOT, "node_modules.asar.unpacked", "vscode-ripgrep", "bin", binName), |
| 34 | + path.join(APP_ROOT, "node_modules.asar.unpacked", "@vscode", "ripgrep", "bin", binName), |
| 35 | + path.join(APP_ROOT, "node_modules", "@vscode", "ripgrep-universal", ...universalRelBin.split("/")), |
| 36 | + path.join(APP_ROOT, "node_modules.asar.unpacked", "@vscode", "ripgrep-universal", ...universalRelBin.split("/")), |
| 37 | +] |
| 38 | + |
| 39 | +describe("getRipgrepDiagnostic", () => { |
| 40 | + beforeEach(() => { |
| 41 | + ripgrepMock.value = undefined |
| 42 | + fsMock.existing = new Set<string>() |
| 43 | + }) |
| 44 | + |
| 45 | + it("includes rgPath and fileExistsAtPath: true when loadRipgrep returns an existing path", async () => { |
| 46 | + const rgPath = "/some/path" |
| 47 | + ripgrepMock.value = { rgPath } |
| 48 | + fsMock.existing = new Set([rgPath]) |
| 49 | + |
| 50 | + const report = await getRipgrepDiagnostic(APP_ROOT) |
| 51 | + |
| 52 | + expect(report).toContain("rgPath: /some/path") |
| 53 | + expect(report).toContain("fileExistsAtPath: true") |
| 54 | + expect(report).toContain("after .asar→.asar.unpacked: /some/path") |
| 55 | + }) |
| 56 | + |
| 57 | + it("rewrites node_modules.asar to node_modules.asar.unpacked in the report", async () => { |
| 58 | + const rgPath = "/app/node_modules.asar/foo/rg" |
| 59 | + const substituted = "/app/node_modules.asar.unpacked/foo/rg" |
| 60 | + ripgrepMock.value = { rgPath } |
| 61 | + fsMock.existing = new Set([substituted]) |
| 62 | + |
| 63 | + const report = await getRipgrepDiagnostic(APP_ROOT) |
| 64 | + |
| 65 | + expect(report).toContain(`after .asar→.asar.unpacked: ${substituted}`) |
| 66 | + expect(report).toContain("fileExistsAtPath: true") |
| 67 | + }) |
| 68 | + |
| 69 | + it("reports require failure when loadRipgrep returns undefined", async () => { |
| 70 | + ripgrepMock.value = undefined |
| 71 | + |
| 72 | + const report = await getRipgrepDiagnostic(APP_ROOT) |
| 73 | + |
| 74 | + expect(report).toContain("loadRipgrep() returned undefined (require threw)") |
| 75 | + }) |
| 76 | + |
| 77 | + it("reports rgPath: (undefined) when loadRipgrep returns an object without rgPath", async () => { |
| 78 | + ripgrepMock.value = {} |
| 79 | + |
| 80 | + const report = await getRipgrepDiagnostic(APP_ROOT) |
| 81 | + |
| 82 | + expect(report).toContain("rgPath: (undefined)") |
| 83 | + expect(report).not.toContain("after .asar→.asar.unpacked:") |
| 84 | + }) |
| 85 | + |
| 86 | + it("marks only the first probe candidate as found when only it exists", async () => { |
| 87 | + fsMock.existing = new Set([expectedCandidates[0]]) |
| 88 | + |
| 89 | + const report = await getRipgrepDiagnostic(APP_ROOT) |
| 90 | + |
| 91 | + const found = expectedCandidates.filter((c) => report.includes(`✓ ${c}`)) |
| 92 | + const missing = expectedCandidates.filter((c) => report.includes(`✗ ${c}`)) |
| 93 | + |
| 94 | + expect(found).toEqual([expectedCandidates[0]]) |
| 95 | + expect(missing).toEqual(expectedCandidates.slice(1)) |
| 96 | + }) |
| 97 | + |
| 98 | + it("marks all probe candidates as missing when none exist", async () => { |
| 99 | + fsMock.existing = new Set<string>() |
| 100 | + |
| 101 | + const report = await getRipgrepDiagnostic(APP_ROOT) |
| 102 | + |
| 103 | + for (const candidate of expectedCandidates) { |
| 104 | + expect(report).toContain(`✗ ${candidate}`) |
| 105 | + } |
| 106 | + expect(report).not.toContain("✓ ") |
| 107 | + }) |
| 108 | +}) |
0 commit comments