|
| 1 | +/** |
| 2 | + * Unit tests for the dbt-core version probe used by |
| 3 | + * `DBTPowerUserExtension.refreshVersionTelemetryAttributes` to populate the |
| 4 | + * `dbtCoreVersion` customAttribute on every telemetry event. |
| 5 | + * |
| 6 | + * Covers the four outcomes the probe needs to distinguish: success (exit 0 |
| 7 | + * with a non-empty version line on stdout), unrecognised dist (exit 0 with |
| 8 | + * empty stdout), spawn failure / non-zero exit, and timeout. Each maps to |
| 9 | + * either a returned version string or `undefined` (best-effort skip). |
| 10 | + * |
| 11 | + * Uses dependency injection on the spawn function (`SpawnFn` parameter) |
| 12 | + * rather than mocking `child_process` — `import * as childProcess` produces |
| 13 | + * ESM-style bindings that resist redefinition under ts-jest, so the test |
| 14 | + * passes its own spawner directly. |
| 15 | + */ |
| 16 | +import { describe, expect, it, jest } from "@jest/globals"; |
| 17 | +import { ChildProcess } from "child_process"; |
| 18 | +import { EventEmitter } from "events"; |
| 19 | + |
| 20 | +import { probeDbtCoreVersion, SpawnFn } from "../../telemetry/versionProbes"; |
| 21 | + |
| 22 | +interface FakeProcOptions { |
| 23 | + exitCode?: number | null; |
| 24 | + stdoutChunks?: string[]; |
| 25 | + emitError?: Error; |
| 26 | + closeDelayMs?: number; |
| 27 | +} |
| 28 | + |
| 29 | +function makeFakeProc(opts: FakeProcOptions = {}): ChildProcess { |
| 30 | + const proc = new EventEmitter() as unknown as ChildProcess; |
| 31 | + // Install null-stream stdout/stderr that the probe's optional-chained |
| 32 | + // listeners can attach to. |
| 33 | + (proc as any).stdout = new EventEmitter(); |
| 34 | + (proc as any).stderr = new EventEmitter(); |
| 35 | + |
| 36 | + const eventTimer = setTimeout(() => { |
| 37 | + if (opts.emitError) { |
| 38 | + proc.emit("error", opts.emitError); |
| 39 | + return; |
| 40 | + } |
| 41 | + for (const chunk of opts.stdoutChunks ?? []) { |
| 42 | + (proc as any).stdout.emit("data", Buffer.from(chunk, "utf8")); |
| 43 | + } |
| 44 | + proc.emit("close", opts.exitCode ?? 0); |
| 45 | + }, opts.closeDelayMs ?? 0); |
| 46 | + |
| 47 | + // The probe under test calls `proc.kill()` when it times out — make sure |
| 48 | + // the fake's inner timer is cleared too so jest doesn't hang waiting on |
| 49 | + // the still-scheduled `setTimeout(...60_000)` in the timeout-path test. |
| 50 | + (proc as any).kill = jest.fn(() => { |
| 51 | + clearTimeout(eventTimer); |
| 52 | + }); |
| 53 | + |
| 54 | + return proc; |
| 55 | +} |
| 56 | + |
| 57 | +describe("probeDbtCoreVersion", () => { |
| 58 | + it("returns the trimmed version string when Python prints it on exit 0", async () => { |
| 59 | + const spawnSpy = jest.fn(() => |
| 60 | + makeFakeProc({ exitCode: 0, stdoutChunks: ["1.10.20\n"] }), |
| 61 | + ) as unknown as SpawnFn; |
| 62 | + const result = await probeDbtCoreVersion("/usr/bin/python3", spawnSpy); |
| 63 | + expect(result).toBe("1.10.20"); |
| 64 | + // Argv check: probe script must use importlib.metadata so it works even |
| 65 | + // when dbt-core's import chain is broken (the bug we're targeting). |
| 66 | + expect(spawnSpy as unknown as jest.Mock).toHaveBeenCalledWith( |
| 67 | + "/usr/bin/python3", |
| 68 | + ["-c", expect.stringContaining("from importlib.metadata import version")], |
| 69 | + expect.any(Object), |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it("returns undefined when stdout is empty (PackageNotFoundError swallowed by the probe script)", async () => { |
| 74 | + const spawnSpy = jest.fn(() => |
| 75 | + makeFakeProc({ exitCode: 0, stdoutChunks: [] }), |
| 76 | + ) as unknown as SpawnFn; |
| 77 | + const result = await probeDbtCoreVersion("/usr/bin/python3", spawnSpy); |
| 78 | + expect(result).toBeUndefined(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("returns undefined on non-zero exit (probe script crashed)", async () => { |
| 82 | + const spawnSpy = jest.fn(() => |
| 83 | + makeFakeProc({ exitCode: 2, stdoutChunks: ["something\n"] }), |
| 84 | + ) as unknown as SpawnFn; |
| 85 | + const result = await probeDbtCoreVersion("/usr/bin/python3", spawnSpy); |
| 86 | + expect(result).toBeUndefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("returns undefined when spawn emits an error event (interpreter missing)", async () => { |
| 90 | + const spawnSpy = jest.fn(() => |
| 91 | + makeFakeProc({ emitError: new Error("ENOENT: no such file") }), |
| 92 | + ) as unknown as SpawnFn; |
| 93 | + const result = await probeDbtCoreVersion("/nonexistent/python", spawnSpy); |
| 94 | + expect(result).toBeUndefined(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("returns undefined when spawn synchronously throws", async () => { |
| 98 | + const spawnSpy = jest.fn(() => { |
| 99 | + throw new Error("EACCES"); |
| 100 | + }) as unknown as SpawnFn; |
| 101 | + const result = await probeDbtCoreVersion("/etc/passwd", spawnSpy); |
| 102 | + expect(result).toBeUndefined(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("returns undefined and kills the process if the probe runs longer than the timeout", async () => { |
| 106 | + // closeDelayMs is far past the 5s timeout; the timeout fires first. |
| 107 | + const proc = makeFakeProc({ |
| 108 | + exitCode: 0, |
| 109 | + stdoutChunks: ["never delivered\n"], |
| 110 | + closeDelayMs: 60_000, |
| 111 | + }); |
| 112 | + const spawnSpy = jest.fn(() => proc) as unknown as SpawnFn; |
| 113 | + const result = await probeDbtCoreVersion("/usr/bin/python3", spawnSpy); |
| 114 | + expect(result).toBeUndefined(); |
| 115 | + expect((proc as any).kill).toHaveBeenCalled(); |
| 116 | + }, 10_000); |
| 117 | +}); |
0 commit comments