Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

13.6 KB
Raw Blame
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join, sep } from 'path';
import { spawnSync } from 'child_process';

// The worker binds the port in ~/.claude-mem/settings.json (getWorkerPort loads
// the file; an env var of the same name overrides it). doctor, uninstall and the
// OpenClaw installer read the port through SettingsDefaultsManager.get(), which
// sees only the env var and the built-in default, so a port set in the file was
// probed, stopped and registered at the wrong address.

// Sandbox every path these commands touch BEFORE any src module loads:
// shared/paths.ts freezes the data dir on first import, and the OpenClaw
// installer resolves its marketplace root from CLAUDE_CONFIG_DIR at import time.
// Keep that frozen path in a separate directory for the entire test process:
// other test files may use it after this file's afterAll removes its fixtures.
// Bun on Linux reads the
// home dir once at startup, so HOME set here does not move os.homedir(): the
// OpenClaw install, which writes under it, runs in a child process (below).
const sandbox = mkdtempSync(join(tmpdir(), 'claude-mem-cli-port-'));
const savedEnv: Record<string, string | undefined> = {};
for (const key of [
  'HOME', 'USERPROFILE', 'CLAUDE_CONFIG_DIR', 'CLAUDE_MEM_DATA_DIR',
  'CLAUDE_MEM_WORKER_PORT', 'CLAUDE_MEM_WORKER_HOST',
]) {
  savedEnv[key] = process.env[key];
}
process.env.HOME = sandbox;
process.env.USERPROFILE = sandbox;
process.env.CLAUDE_CONFIG_DIR = join(sandbox, '.claude');
const sharedPathsSandbox = mkdtempSync(join(tmpdir(), 'claude-mem-shared-paths-'));
process.env.CLAUDE_MEM_DATA_DIR = sharedPathsSandbox;
await import('../../src/shared/paths.js');
process.once('exit', () => rmSync(sharedPathsSandbox, { recursive: true, force: true }));
process.env.CLAUDE_MEM_DATA_DIR = join(sandbox, 'data');
mkdirSync(process.env.CLAUDE_MEM_DATA_DIR, { recursive: true });

const realShutdownHelperSnapshot = { ...(await import('../../src/services/install/shutdown-helper.js')) };
const shutdownPorts: Array<number | string> = [];
let shutdownCalled: () => void = () => {};

// The uninstall test stops at this call: the promise never settles, so nothing
// after the worker shutdown (file removal) ever runs.
mock.module('../../src/services/install/shutdown-helper.js', () => ({
  shutdownWorkerAndWait: (port: number | string) => {
    shutdownPorts.push(port);
    shutdownCalled();
    return new Promise(() => {});
  },
}));

const { SettingsDefaultsManager } = await import('../../src/shared/SettingsDefaultsManager.js');
const { clearPortCache } = await import('../../src/shared/worker-utils.js');

const FILE_PORT = '38888';
const ENV_PORT = '39999';

/**
 * The settings file getWorkerPort() reads (CLAUDE_MEM_DATA_DIR/settings.json,
 * resolved at call time), refused unless it is inside the sandbox.
 */
function sandboxSettingsPath(): string {
  const settingsPath = join(SettingsDefaultsManager.get('CLAUDE_MEM_DATA_DIR'), 'settings.json');
  if (!settingsPath.startsWith(sandbox + sep)) {
    throw new Error(`refusing to touch a settings file outside the test sandbox: ${settingsPath}`);
  }
  return settingsPath;
}

function writeSettings(settings: Record<string, string>): void {
  writeFileSync(sandboxSettingsPath(), JSON.stringify(settings));
}

beforeEach(() => {
  delete process.env.CLAUDE_MEM_WORKER_PORT;
  delete process.env.CLAUDE_MEM_WORKER_HOST;
  clearPortCache();
});

afterEach(() => {
  delete process.env.CLAUDE_MEM_WORKER_PORT;
  delete process.env.CLAUDE_MEM_WORKER_HOST;
  clearPortCache();
});

afterAll(() => {
  mock.module('../../src/services/install/shutdown-helper.js', () => realShutdownHelperSnapshot);
  for (const [key, value] of Object.entries(savedEnv)) {
    if (value === undefined) delete process.env[key];
    else process.env[key] = value;
  }
  clearPortCache();
  rmSync(sandbox, { recursive: true, force: true });
});

describe('npx claude-mem doctor probes the worker port from settings.json', () => {
  const originalFetch = globalThis.fetch;
  let requested: string[] = [];
  let doctorOutput = '';

  beforeEach(() => {
    requested = [];
    doctorOutput = '';
    globalThis.fetch = (async (input: RequestInfo | URL) => {
      requested.push(String(input));
      return new Response('{}', { status: 503 });
    }) as typeof fetch;
  });

  afterEach(() => {
    globalThis.fetch = originalFetch;
  });

  // Doctor reports its result through process.exitCode (process.exit() trips
  // a libuv assertion on Windows, nodejs/node#56645). Keep the code it sets
  // from becoming this test process's exit status.
  let doctorExitCode: typeof process.exitCode;

  async function runDoctor(): Promise<void> {
    const { runDoctorCommand } = await import('../../src/npx-cli/commands/doctor.js');
    // Run from source, there is no npm package root to read a version from.
    const pathsModule = await import('../../src/npx-cli/utils/paths.js');
    const version = spyOn(pathsModule, 'readPluginVersion').mockReturnValue('0.0.0-test');
    const exit = spyOn(process, 'exit').mockImplementation((() => {
      throw new Error('doctor must set process.exitCode, not call process.exit()');
    }) as never);
    const log = spyOn(console, 'log').mockImplementation(() => {});
    const previousExitCode = process.exitCode;
    try {
      await runDoctorCommand();
    } finally {
      doctorExitCode = process.exitCode;
      // Bun keeps a set exit code when it is assigned undefined, so an unset
      // code is restored as 0 (a failing test still makes bun test exit 1).
      process.exitCode = previousExitCode ?? 0;
      doctorOutput = log.mock.calls.flat().join('\n');
      version.mockRestore();
      exit.mockRestore();
      log.mockRestore();
    }
  }

  it('uses CLAUDE_MEM_WORKER_URL over host and port', async () => {
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT, CLAUDE_MEM_WORKER_URL: 'https://worker.example/mem/' });

    await runDoctor();

    expect(requested).toContain('https://worker.example/mem/api/health');
    expect(requested).not.toContain(`http://127.0.0.1:${FILE_PORT}/api/health`);
    // The exit status must match the verdict doctor printed.
    expect(doctorExitCode).toBe(doctorOutput.includes('All required checks passed.') ? 0 : 1);
  }, 30_000);

  it('reports an invalid CLAUDE_MEM_WORKER_URL instead of probing host and port', async () => {
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT, CLAUDE_MEM_WORKER_URL: 'http://worker.example' });

    await runDoctor();

    expect(requested.filter(url => url.endsWith('/api/health'))).toEqual([]);
    expect(doctorOutput).toContain('CLAUDE_MEM_WORKER_URL must use https');
  }, 30_000);

  it('lets the environment variable override settings.json', async () => {
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT });
    process.env.CLAUDE_MEM_WORKER_PORT = ENV_PORT;

    await runDoctor();

    expect(requested).toContain(`http://127.0.0.1:${ENV_PORT}/api/health`);
  }, 30_000);

  it('does not create settings.json during a diagnostic', async () => {
    rmSync(sandboxSettingsPath(), { force: true });

    await runDoctor();

    expect(existsSync(sandboxSettingsPath())).toBe(false);
  }, 30_000);

  it('does not rewrite legacy settings or add migration markers', async () => {
    const raw = JSON.stringify({
      CLAUDE_MEM_WORKER_PORT: FILE_PORT,
      CLAUDE_MEM_OPENROUTER_MODEL: 'xiaomi/mimo-v2-flash:free',
      CLAUDE_MEM_LLM_TIMEOUT_MS: '30000',
    });
    writeFileSync(sandboxSettingsPath(), raw);
    const filesBefore = readdirSync(join(sandbox, 'data')).sort();

    await runDoctor();

    expect(readFileSync(sandboxSettingsPath(), 'utf-8')).toBe(raw);
    expect(readdirSync(join(sandbox, 'data')).sort()).toEqual(filesBefore);
    expect(requested).toContain(`http://127.0.0.1:${FILE_PORT}/api/health`);
  }, 30_000);
});

describe('npx claude-mem backup reaches the worker at CLAUDE_MEM_WORKER_URL', () => {
  const originalFetch = globalThis.fetch;

  afterEach(() => {
    globalThis.fetch = originalFetch;
  });

  it('uses the URL over host and port', async () => {
    const requested: string[] = [];
    globalThis.fetch = (async (input: RequestInfo | URL) => {
      requested.push(String(input));
      return new Response('{}');
    }) as typeof fetch;
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT, CLAUDE_MEM_WORKER_URL: 'https://worker.example' });

    const { fetchWorkerBackupApi } = await import('../../src/npx-cli/commands/backup.js');
    await fetchWorkerBackupApi('/api/backup/status');

    expect(requested).toEqual(['https://worker.example/api/backup/status']);
  });
});

describe('npx claude-mem uninstall stops the worker on the port from settings.json', () => {
  // uninstall asks "Are you sure?" on an interactive stdin before it stops the
  // worker; pin non-TTY so the test reaches shutdown when run from a terminal.
  const originalStdinIsTTY = Object.getOwnPropertyDescriptor(process.stdin, 'isTTY');

  beforeAll(() => {
    Object.defineProperty(process.stdin, 'isTTY', { configurable: true, value: false });
  });

  afterAll(() => {
    if (originalStdinIsTTY) {
      Object.defineProperty(process.stdin, 'isTTY', originalStdinIsTTY);
    } else {
      delete (process.stdin as { isTTY?: boolean }).isTTY;
    }
  });

  beforeEach(() => {
    shutdownPorts.length = 0;
  });

  async function portUninstallStops(): Promise<string> {
    const pathsModule = await import('../../src/npx-cli/utils/paths.js');
    const installed = spyOn(pathsModule, 'isPluginInstalled').mockReturnValue(true);
    const called = new Promise<void>(resolve => { shutdownCalled = resolve; });
    try {
      const { runUninstallCommand } = await import('../../src/npx-cli/commands/uninstall.js');
      void runUninstallCommand();
      await called;
    } finally {
      installed.mockRestore();
    }
    return String(shutdownPorts[0]);
  }

  it('uses the port written in settings.json', async () => {
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT });

    expect(await portUninstallStops()).toBe(FILE_PORT);
  });

  it('lets the environment variable override settings.json', async () => {
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT });
    process.env.CLAUDE_MEM_WORKER_PORT = ENV_PORT;

    expect(await portUninstallStops()).toBe(ENV_PORT);
  });
});

describe('OpenClaw install registers the worker port from settings.json', () => {
  const openclawConfig = join(sandbox, '.openclaw', 'openclaw.json');
  const installerPath = join(import.meta.dir, '..', '..', 'src', 'services', 'integrations', 'OpenClawInstaller.ts');
  const childScript = join(sandbox, 'install-openclaw.mjs');

  beforeAll(() => {
    // A pre-built plugin bundle where the installer looks for one.
    const dist = join(sandbox, '.claude', 'plugins', 'marketplaces', 'thedotmack', 'openclaw', 'dist');
    mkdirSync(dist, { recursive: true });
    writeFileSync(join(dist, 'index.js'), '');

    // The installer writes under os.homedir(), and Bun on Linux reads the home
    // dir once at startup, so changing HOME in this process does not move it.
    // Run the install in a child whose HOME is the sandbox from the start, and
    // have the child refuse to install anywhere else.
    writeFileSync(childScript, [
      "import { homedir } from 'node:os';",
      "import { pathToFileURL } from 'node:url';",
      'if (homedir() !== process.env.CLAUDE_MEM_TEST_SANDBOX_HOME) {',
      "  console.error(`refusing to install: home dir ${homedir()} is not the test sandbox`);",
      '  process.exit(3);',
      '}',
      'const { installOpenClawPlugin } = await import(pathToFileURL(process.env.CLAUDE_MEM_TEST_INSTALLER).href);',
      'process.exit(installOpenClawPlugin());',
    ].join('\n'));
  });

  beforeEach(() => {
    rmSync(join(sandbox, '.openclaw'), { recursive: true, force: true });
  });

  function install(): { workerPort?: number } {
    const env: Record<string, string> = {};
    for (const [key, value] of Object.entries(process.env)) {
      if (value !== undefined) env[key] = value;
    }
    Object.assign(env, {
      HOME: sandbox,
      USERPROFILE: sandbox,
      CLAUDE_CONFIG_DIR: join(sandbox, '.claude'),
      CLAUDE_MEM_DATA_DIR: join(sandbox, 'data'),
      CLAUDE_MEM_TEST_SANDBOX_HOME: sandbox,
      CLAUDE_MEM_TEST_INSTALLER: installerPath,
      // The child has no tests/preload.ts mocks; nothing it might send leaves the machine.
      CLAUDE_MEM_TELEMETRY_HOST: 'http://127.0.0.1:9',
    });
    // cwd is one of the installer's marketplace roots; keep it in the sandbox too.
    const child = spawnSync(process.execPath, [childScript], { cwd: sandbox, env, encoding: 'utf-8' });
    expect({ status: child.status, stderr: child.status === 0 ? '' : child.stderr }).toEqual({ status: 0, stderr: '' });
    return JSON.parse(readFileSync(openclawConfig, 'utf-8')).plugins.entries['claude-mem'].config;
  }

  it('uses the port written in settings.json', () => {
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT });

    expect(install().workerPort).toBe(Number(FILE_PORT));
  });

  it('lets the environment variable override settings.json', () => {
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT });
    process.env.CLAUDE_MEM_WORKER_PORT = ENV_PORT;

    expect(install().workerPort).toBe(Number(ENV_PORT));
  });

  it('keeps a workerPort the user already set in openclaw.json', () => {
    writeSettings({ CLAUDE_MEM_WORKER_PORT: FILE_PORT });
    mkdirSync(join(sandbox, '.openclaw'), { recursive: true });
    writeFileSync(openclawConfig, JSON.stringify({
      plugins: { entries: { 'claude-mem': { enabled: true, config: { workerPort: 40001 } } } },
    }));

    expect(install().workerPort).toBe(40001);
  });
});