|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createLanceDbRuntimeLoader, type LanceDbRuntimeLogger } from "./lancedb-runtime.js"; |
| 3 | + |
| 4 | +const TEST_RUNTIME_MANIFEST = { |
| 5 | + name: "openclaw-memory-lancedb-runtime", |
| 6 | + private: true as const, |
| 7 | + type: "module" as const, |
| 8 | + dependencies: { |
| 9 | + "@lancedb/lancedb": "^0.27.1", |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +type LanceDbModule = typeof import("@lancedb/lancedb"); |
| 14 | +type RuntimeManifest = { |
| 15 | + name: string; |
| 16 | + private: true; |
| 17 | + type: "module"; |
| 18 | + dependencies: Record<string, string>; |
| 19 | +}; |
| 20 | + |
| 21 | +function createMockModule(): LanceDbModule { |
| 22 | + return { |
| 23 | + connect: vi.fn(), |
| 24 | + } as unknown as LanceDbModule; |
| 25 | +} |
| 26 | + |
| 27 | +function createLoader( |
| 28 | + overrides: { |
| 29 | + env?: NodeJS.ProcessEnv; |
| 30 | + importBundled?: () => Promise<LanceDbModule>; |
| 31 | + importResolved?: (resolvedPath: string) => Promise<LanceDbModule>; |
| 32 | + resolveRuntimeEntry?: (params: { |
| 33 | + runtimeDir: string; |
| 34 | + manifest: RuntimeManifest; |
| 35 | + }) => string | null; |
| 36 | + installRuntime?: (params: { |
| 37 | + runtimeDir: string; |
| 38 | + manifest: RuntimeManifest; |
| 39 | + env: NodeJS.ProcessEnv; |
| 40 | + logger?: LanceDbRuntimeLogger; |
| 41 | + }) => Promise<string>; |
| 42 | + } = {}, |
| 43 | +) { |
| 44 | + return createLanceDbRuntimeLoader({ |
| 45 | + env: overrides.env ?? ({} as NodeJS.ProcessEnv), |
| 46 | + resolveStateDir: () => "/tmp/openclaw-state", |
| 47 | + runtimeManifest: TEST_RUNTIME_MANIFEST, |
| 48 | + importBundled: |
| 49 | + overrides.importBundled ?? |
| 50 | + (async () => { |
| 51 | + throw new Error("Cannot find package '@lancedb/lancedb'"); |
| 52 | + }), |
| 53 | + importResolved: overrides.importResolved ?? (async () => createMockModule()), |
| 54 | + resolveRuntimeEntry: overrides.resolveRuntimeEntry ?? (() => null), |
| 55 | + installRuntime: |
| 56 | + overrides.installRuntime ?? |
| 57 | + (async ({ runtimeDir }: { runtimeDir: string }) => |
| 58 | + `${runtimeDir}/node_modules/@lancedb/lancedb/index.js`), |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +describe("lancedb runtime loader", () => { |
| 63 | + it("uses the bundled module when it is already available", async () => { |
| 64 | + const bundledModule = createMockModule(); |
| 65 | + const importBundled = vi.fn(async () => bundledModule); |
| 66 | + const importResolved = vi.fn(async () => createMockModule()); |
| 67 | + const resolveRuntimeEntry = vi.fn(() => null); |
| 68 | + const installRuntime = vi.fn(async () => "/tmp/openclaw-state/plugin-runtimes/lancedb.js"); |
| 69 | + const loader = createLoader({ |
| 70 | + importBundled, |
| 71 | + importResolved, |
| 72 | + resolveRuntimeEntry, |
| 73 | + installRuntime, |
| 74 | + }); |
| 75 | + |
| 76 | + await expect(loader.load()).resolves.toBe(bundledModule); |
| 77 | + |
| 78 | + expect(resolveRuntimeEntry).not.toHaveBeenCalled(); |
| 79 | + expect(installRuntime).not.toHaveBeenCalled(); |
| 80 | + expect(importResolved).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("reuses an existing user runtime install before attempting a reinstall", async () => { |
| 84 | + const runtimeModule = createMockModule(); |
| 85 | + const importResolved = vi.fn(async () => runtimeModule); |
| 86 | + const resolveRuntimeEntry = vi.fn( |
| 87 | + () => "/tmp/openclaw-state/plugin-runtimes/memory-lancedb/runtime-entry.js", |
| 88 | + ); |
| 89 | + const installRuntime = vi.fn( |
| 90 | + async () => "/tmp/openclaw-state/plugin-runtimes/memory-lancedb/runtime-entry.js", |
| 91 | + ); |
| 92 | + const loader = createLoader({ |
| 93 | + importResolved, |
| 94 | + resolveRuntimeEntry, |
| 95 | + installRuntime, |
| 96 | + }); |
| 97 | + |
| 98 | + await expect(loader.load()).resolves.toBe(runtimeModule); |
| 99 | + |
| 100 | + expect(resolveRuntimeEntry).toHaveBeenCalledWith( |
| 101 | + expect.objectContaining({ |
| 102 | + runtimeDir: "/tmp/openclaw-state/plugin-runtimes/memory-lancedb/lancedb", |
| 103 | + }), |
| 104 | + ); |
| 105 | + expect(installRuntime).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it("installs LanceDB into user state when the bundled runtime is unavailable", async () => { |
| 109 | + const runtimeModule = createMockModule(); |
| 110 | + const logger: LanceDbRuntimeLogger = { |
| 111 | + warn: vi.fn(), |
| 112 | + info: vi.fn(), |
| 113 | + }; |
| 114 | + const importResolved = vi.fn(async () => runtimeModule); |
| 115 | + const resolveRuntimeEntry = vi.fn(() => null); |
| 116 | + const installRuntime = vi.fn( |
| 117 | + async ({ runtimeDir }: { runtimeDir: string }) => |
| 118 | + `${runtimeDir}/node_modules/@lancedb/lancedb/index.js`, |
| 119 | + ); |
| 120 | + const loader = createLoader({ |
| 121 | + importResolved, |
| 122 | + resolveRuntimeEntry, |
| 123 | + installRuntime, |
| 124 | + }); |
| 125 | + |
| 126 | + await expect(loader.load(logger)).resolves.toBe(runtimeModule); |
| 127 | + |
| 128 | + expect(installRuntime).toHaveBeenCalledWith( |
| 129 | + expect.objectContaining({ |
| 130 | + runtimeDir: "/tmp/openclaw-state/plugin-runtimes/memory-lancedb/lancedb", |
| 131 | + manifest: TEST_RUNTIME_MANIFEST, |
| 132 | + }), |
| 133 | + ); |
| 134 | + expect(logger.warn).toHaveBeenCalledWith( |
| 135 | + expect.stringContaining( |
| 136 | + "installing runtime deps under /tmp/openclaw-state/plugin-runtimes/memory-lancedb/lancedb", |
| 137 | + ), |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it("fails fast in nix mode instead of attempting auto-install", async () => { |
| 142 | + const installRuntime = vi.fn( |
| 143 | + async ({ runtimeDir }: { runtimeDir: string }) => |
| 144 | + `${runtimeDir}/node_modules/@lancedb/lancedb/index.js`, |
| 145 | + ); |
| 146 | + const loader = createLoader({ |
| 147 | + env: { OPENCLAW_NIX_MODE: "1" } as NodeJS.ProcessEnv, |
| 148 | + installRuntime, |
| 149 | + }); |
| 150 | + |
| 151 | + await expect(loader.load()).rejects.toThrow( |
| 152 | + "memory-lancedb: failed to load LanceDB and Nix mode disables auto-install.", |
| 153 | + ); |
| 154 | + expect(installRuntime).not.toHaveBeenCalled(); |
| 155 | + }); |
| 156 | + |
| 157 | + it("clears the cached failure so later calls can retry the install", async () => { |
| 158 | + const runtimeModule = createMockModule(); |
| 159 | + const installRuntime = vi |
| 160 | + .fn() |
| 161 | + .mockRejectedValueOnce(new Error("network down")) |
| 162 | + .mockResolvedValueOnce( |
| 163 | + "/tmp/openclaw-state/plugin-runtimes/memory-lancedb/lancedb/node_modules/@lancedb/lancedb/index.js", |
| 164 | + ); |
| 165 | + const importResolved = vi.fn(async () => runtimeModule); |
| 166 | + const loader = createLoader({ |
| 167 | + installRuntime, |
| 168 | + importResolved, |
| 169 | + }); |
| 170 | + |
| 171 | + await expect(loader.load()).rejects.toThrow("network down"); |
| 172 | + await expect(loader.load()).resolves.toBe(runtimeModule); |
| 173 | + |
| 174 | + expect(installRuntime).toHaveBeenCalledTimes(2); |
| 175 | + }); |
| 176 | +}); |
0 commit comments