|
| 1 | +// Memory Host SDK tests cover embedding worker path resolution. |
| 2 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 3 | +import type { NoParamCallback } from "node:fs"; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// resolveHomebrewStablePath tests — accessSync-mocked |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +// We must mock before importing the module under test. |
| 10 | +const mockAccessSync = vi.fn<NoParamCallback>(); |
| 11 | + |
| 12 | +vi.mock("node:fs", async () => { |
| 13 | + const actual = await vi.importActual<typeof import("node:fs")>("node:fs"); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + accessSync: mockAccessSync, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +const { resolveHomebrewStablePath } = await import("./embeddings-worker.js"); |
| 21 | + |
| 22 | +describe("resolveHomebrewStablePath", () => { |
| 23 | + beforeEach(() => { |
| 24 | + mockAccessSync.mockReset(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("returns the original path for non-Cellar paths", () => { |
| 28 | + const path = "/usr/local/bin/node"; |
| 29 | + expect(resolveHomebrewStablePath(path)).toBe(path); |
| 30 | + expect(mockAccessSync).not.toHaveBeenCalled(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("returns the original path for non-Node Cellar paths", () => { |
| 34 | + const path = "/opt/homebrew/Cellar/python/3.12/bin/python3"; |
| 35 | + expect(resolveHomebrewStablePath(path)).toBe(path); |
| 36 | + expect(mockAccessSync).not.toHaveBeenCalled(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns opt symlink when Cellar path and opt path exists", () => { |
| 40 | + const cellarPath = "/opt/homebrew/Cellar/node/25.7.0/bin/node"; |
| 41 | + const optPath = "/opt/homebrew/opt/node/bin/node"; |
| 42 | + mockAccessSync.mockImplementation((p: string) => { |
| 43 | + if (p === optPath) return; |
| 44 | + throw Object.assign(new Error("ENOENT"), { code: "ENOENT" }); |
| 45 | + }); |
| 46 | + |
| 47 | + expect(resolveHomebrewStablePath(cellarPath)).toBe(optPath); |
| 48 | + }); |
| 49 | + |
| 50 | + it("returns bin symlink for default node formula when only bin path exists", () => { |
| 51 | + const cellarPath = "/opt/homebrew/Cellar/node/25.7.0/bin/node"; |
| 52 | + const binPath = "/opt/homebrew/bin/node"; |
| 53 | + mockAccessSync.mockImplementation((p: string) => { |
| 54 | + if (p === binPath) return; |
| 55 | + throw Object.assign(new Error("ENOENT"), { code: "ENOENT" }); |
| 56 | + }); |
| 57 | + |
| 58 | + expect(resolveHomebrewStablePath(cellarPath)).toBe(binPath); |
| 59 | + }); |
| 60 | + |
| 61 | + it("returns original path when Cellar path and no stable symlink exists", () => { |
| 62 | + const cellarPath = "/opt/homebrew/Cellar/node/25.7.0/bin/node"; |
| 63 | + mockAccessSync.mockImplementation(() => { |
| 64 | + throw Object.assign(new Error("ENOENT"), { code: "ENOENT" }); |
| 65 | + }); |
| 66 | + |
| 67 | + expect(resolveHomebrewStablePath(cellarPath)).toBe(cellarPath); |
| 68 | + }); |
| 69 | + |
| 70 | + it("handles versioned formula (node@22) via opt path", () => { |
| 71 | + const cellarPath = "/opt/homebrew/Cellar/node@22/22.14.0/bin/node"; |
| 72 | + const optPath = "/opt/homebrew/opt/node@22/bin/node"; |
| 73 | + mockAccessSync.mockImplementation((p: string) => { |
| 74 | + if (p === optPath) return; |
| 75 | + throw Object.assign(new Error("ENOENT"), { code: "ENOENT" }); |
| 76 | + }); |
| 77 | + |
| 78 | + expect(resolveHomebrewStablePath(cellarPath)).toBe(optPath); |
| 79 | + }); |
| 80 | + |
| 81 | + it("handles Linuxbrew paths under /home/linuxbrew", () => { |
| 82 | + const cellarPath = "/home/linuxbrew/.linuxbrew/Cellar/node/25.7.0/bin/node"; |
| 83 | + const optPath = "/home/linuxbrew/.linuxbrew/opt/node/bin/node"; |
| 84 | + mockAccessSync.mockImplementation((p: string) => { |
| 85 | + if (p === optPath) return; |
| 86 | + throw Object.assign(new Error("ENOENT"), { code: "ENOENT" }); |
| 87 | + }); |
| 88 | + |
| 89 | + expect(resolveHomebrewStablePath(cellarPath)).toBe(optPath); |
| 90 | + }); |
| 91 | + |
| 92 | + it("prefers opt path over bin path when both exist", () => { |
| 93 | + const cellarPath = "/opt/homebrew/Cellar/node/25.7.0/bin/node"; |
| 94 | + const optPath = "/opt/homebrew/opt/node/bin/node"; |
| 95 | + const binPath = "/opt/homebrew/bin/node"; |
| 96 | + mockAccessSync.mockImplementation((p: string) => { |
| 97 | + if (p === optPath || p === binPath) return; |
| 98 | + throw Object.assign(new Error("ENOENT"), { code: "ENOENT" }); |
| 99 | + }); |
| 100 | + |
| 101 | + expect(resolveHomebrewStablePath(cellarPath)).toBe(optPath); |
| 102 | + }); |
| 103 | +}); |
0 commit comments