|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { isDriverNotInstalled, loadConnectors } from "../utils/module-loader.js"; |
| 3 | + |
| 4 | +describe("isDriverNotInstalled", () => { |
| 5 | + it("should return true when the driver package is missing", () => { |
| 6 | + const err = new Error( |
| 7 | + "Cannot find package 'pg' imported from /fake/path" |
| 8 | + ); |
| 9 | + (err as NodeJS.ErrnoException).code = "ERR_MODULE_NOT_FOUND"; |
| 10 | + |
| 11 | + expect(isDriverNotInstalled(err, "pg")).toBe(true); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should return false when a different driver is missing", () => { |
| 15 | + const err = new Error( |
| 16 | + "Cannot find package 'pg' imported from /fake/path" |
| 17 | + ); |
| 18 | + (err as NodeJS.ErrnoException).code = "ERR_MODULE_NOT_FOUND"; |
| 19 | + |
| 20 | + expect(isDriverNotInstalled(err, "mysql2")).toBe(false); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should return true for driver subpath imports", () => { |
| 24 | + const err = new Error( |
| 25 | + "Cannot find package 'mysql2/promise' imported from /fake/path" |
| 26 | + ); |
| 27 | + (err as NodeJS.ErrnoException).code = "ERR_MODULE_NOT_FOUND"; |
| 28 | + |
| 29 | + expect(isDriverNotInstalled(err, "mysql2")).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should return false when missing module name only contains driver as a substring", () => { |
| 33 | + const err = new Error( |
| 34 | + "Cannot find package 'pg-connection-string' imported from /fake/path" |
| 35 | + ); |
| 36 | + (err as NodeJS.ErrnoException).code = "ERR_MODULE_NOT_FOUND"; |
| 37 | + |
| 38 | + expect(isDriverNotInstalled(err, "pg")).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should return false for unrelated ERR_MODULE_NOT_FOUND errors", () => { |
| 42 | + const err = new Error( |
| 43 | + "Cannot find package 'some-internal-dep' imported from /fake/path" |
| 44 | + ); |
| 45 | + (err as NodeJS.ErrnoException).code = "ERR_MODULE_NOT_FOUND"; |
| 46 | + |
| 47 | + expect(isDriverNotInstalled(err, "pg")).toBe(false); |
| 48 | + expect(isDriverNotInstalled(err, "mysql2")).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should return false for non-module errors", () => { |
| 52 | + const err = new Error("Some other error"); |
| 53 | + expect(isDriverNotInstalled(err, "pg")).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should return false for non-Error values", () => { |
| 57 | + expect(isDriverNotInstalled("string error", "pg")).toBe(false); |
| 58 | + expect(isDriverNotInstalled(null, "pg")).toBe(false); |
| 59 | + expect(isDriverNotInstalled(undefined, "pg")).toBe(false); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("loadConnectors", () => { |
| 64 | + it("should log and continue when a driver is missing", async () => { |
| 65 | + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 66 | + |
| 67 | + const driverErr = new Error("Cannot find package 'pg' imported from /fake/path"); |
| 68 | + (driverErr as NodeJS.ErrnoException).code = "ERR_MODULE_NOT_FOUND"; |
| 69 | + |
| 70 | + await loadConnectors([ |
| 71 | + { load: () => Promise.reject(driverErr), name: "PostgreSQL", driver: "pg" }, |
| 72 | + ]); |
| 73 | + |
| 74 | + expect(errorSpy).toHaveBeenCalledWith( |
| 75 | + 'Skipping PostgreSQL connector: driver package "pg" not installed.' |
| 76 | + ); |
| 77 | + errorSpy.mockRestore(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should rethrow non-driver errors", async () => { |
| 81 | + const runtimeErr = new Error("Unexpected syntax error"); |
| 82 | + |
| 83 | + await expect( |
| 84 | + loadConnectors([ |
| 85 | + { load: () => Promise.reject(runtimeErr), name: "PostgreSQL", driver: "pg" }, |
| 86 | + ]) |
| 87 | + ).rejects.toThrow("Unexpected syntax error"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should load all connectors when drivers are available", async () => { |
| 91 | + let loaded = 0; |
| 92 | + await loadConnectors([ |
| 93 | + { load: async () => { loaded++; }, name: "TestDB", driver: "test-driver" }, |
| 94 | + ]); |
| 95 | + expect(loaded).toBe(1); |
| 96 | + }); |
| 97 | +}); |
0 commit comments