Files
mem0/integrations/pi-agent-plugin/tests/config.test.ts
kartik-mem0 71dc0cae07 fix(plugins): read the mem0 CLI key and ignore unexpanded host placeholders
The Python plugin core and the opencode, pi and deepseek plugins now fall back to the key `mem0 init` saves in ~/.mem0/config.json, so a configured CLI is enough to authenticate. The Python core also stops using or caching a literal ${api_key} left behind when a host (Cursor) does not expand its plugin variables, which is what kept asking for auth after the key was set.

Fixes #7346
2026-09-25 10:24:04 +05:30

70 lines
2.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import * as fs from "node:fs";
import * as path from "node:path";
import { loadConfig } from "../src/config/index.ts";
vi.mock("node:fs");
describe("loadConfig", () => {
const originalEnv = process.env;
beforeEach(() => {
process.env = { ...originalEnv, HOME: "/home/testuser" };
vi.mocked(fs.existsSync).mockReturnValue(false);
});
afterEach(() => {
process.env = originalEnv;
vi.restoreAllMocks();
});
it("reads config from env vars when no config file exists", () => {
process.env.MEM0_API_KEY = "m0-test-key";
process.env.MEM0_USER_ID = "env-user";
const config = loadConfig();
expect(config.apiKey).toBe("m0-test-key");
expect(config.userId).toBe("env-user");
expect(config.autoCapture).toBe(true);
expect(config.defaultScope).toBe("project");
});
it("returns empty apiKey when no key found anywhere", () => {
delete process.env.MEM0_API_KEY;
const config = loadConfig();
expect(config.apiKey).toBe("");
});
it("falls back to the key mem0 init saved", () => {
delete process.env.MEM0_API_KEY;
vi.mocked(fs.readFileSync).mockImplementation((file) => {
if (String(file).endsWith(path.join(".mem0", "config.json"))) {
return JSON.stringify({ platform: { api_key: "m0-cli-key" } });
}
throw new Error("ENOENT");
});
expect(loadConfig().apiKey).toBe("m0-cli-key");
});
it("reads config file and merges with defaults", () => {
delete process.env.MEM0_API_KEY;
delete process.env.MEM0_USER_ID;
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fs.readFileSync).mockReturnValue(
JSON.stringify({ apiKey: "m0-file-key", userId: "file-user" })
);
const config = loadConfig();
expect(config.apiKey).toBe("m0-file-key");
expect(config.userId).toBe("file-user");
});
it("env vars override config file", () => {
process.env.MEM0_API_KEY = "m0-env-key";
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fs.readFileSync).mockReturnValue(
JSON.stringify({ apiKey: "m0-file-key" })
);
const config = loadConfig();
expect(config.apiKey).toBe("m0-env-key");
});
});