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

2.2 KB
Raw Blame
import { expect, it } from 'bun:test';
import { existsSync, mkdtempSync, realpathSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';

// A typo such as `--dryrun` must be a usage error, not the real action:
// these commands change stored memory when `--dry-run` is absent.
const workerService = join(import.meta.dir, '..', '..', 'src', 'services', 'worker-service.ts');

function freePort(): number {
  const server = Bun.serve({ port: 0, fetch: () => new Response() });
  const port = server.port;
  server.stop(true);
  return port;
}

const cases: Array<{ args: string[]; unknown: string; usage: string }> = [
  { args: ['project', 'merge', 'alpha', 'beta', '--dryrun'], unknown: '--dryrun', usage: 'Usage: project merge' },
  { args: ['project', 'merge', 'alpha', 'beta', '--dry-run=true'], unknown: '--dry-run=true', usage: 'Usage: project merge' },
  { args: ['cleanup', '--dryrun'], unknown: '--dryrun', usage: 'Usage: cleanup' },
  { args: ['adopt', '--dryrun'], unknown: '--dryrun', usage: 'Usage: adopt' },
  { args: ['adopt', '--dry-run', '--branch=feat', '--brnach', 'feat'], unknown: '--brnach', usage: 'Usage: adopt' },
];

for (const { args, unknown, usage } of cases) {
  it(`rejects ${args.join(' ')} before changing any data`, () => {
    const root = realpathSync(mkdtempSync(join(tmpdir(), 'cmem-unknown-flag-')));
    const dataDir = join(root, 'data');
    try {
      const run = Bun.spawnSync([process.execPath, workerService, ...args], {
        env: {
          ...process.env, HOME: root, USERPROFILE: root, CLAUDE_MEM_DATA_DIR: dataDir, CLAUDE_CONFIG_DIR: join(root, 'config'),
          CLAUDE_MEM_TELEMETRY: '0', CLAUDE_MEM_WORKER_HOST: '127.0.0.1', CLAUDE_MEM_WORKER_PORT: String(freePort()),
        },
        cwd: root,
        stdout: 'pipe', stderr: 'pipe',
      });
      const stderr = new TextDecoder().decode(run.stderr);
      expect(run.exitCode, stderr).toBe(1);
      expect(stderr).toContain(`Unknown argument: ${unknown}`);
      expect(stderr).toContain(usage);
      expect(existsSync(join(dataDir, 'claude-mem.db'))).toBe(false);
      expect(existsSync(join(dataDir, '.cleanup-v12.4.3-applied'))).toBe(false);
    } finally {
      rmSync(root, { recursive: true, force: true });
    }
  }, 30_000);
}