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.3 KB
Raw Blame
import { describe, it, expect } from 'bun:test';
import { spawnSync } from 'child_process';
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';

// Both readers of Claude Code's plugin toggle — this runner and the compiled
// launcher — must reach the same answer as the worker's plugin-state reader,
// which strips a UTF-8 BOM. An editor that saves settings.json with a BOM
// (Notepad, PowerShell) used to make this runner treat a disabled plugin as
// enabled, so every hook started Bun and then the worker refused to start.
const runner = join(import.meta.dir, '..', 'plugin', 'scripts', 'bun-runner.js');

function runWithSettings(settings: string): { status: number | null; stdout: string } {
  const root = realpathSync(mkdtempSync(join(tmpdir(), 'cmem-bun-runner-settings-')));
  try {
    const home = join(root, 'home');
    const project = join(root, 'project');
    mkdirSync(join(home, '.claude'), { recursive: true });
    mkdirSync(project);
    writeFileSync(join(home, '.claude', 'settings.json'), settings);
    const stub = join(root, 'stub.js');
    writeFileSync(stub, 'process.stdout.write("ran\\n");\n');
    const result = spawnSync(process.execPath, [runner, stub], {
      cwd: project,
      input: '{}',
      env: { PATH: process.env.PATH ?? '', HOME: home, USERPROFILE: home, BUN_PATH: process.execPath },
      encoding: 'utf-8',
    });
    return { status: result.status, stdout: result.stdout ?? '' };
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
}

const disabled = JSON.stringify({ enabledPlugins: { 'claude-mem@thedotmack': false } });

describe('bun-runner.js and the Claude plugin toggle', () => {
  it('stops before the script when the plugin is disabled', () => {
    expect(runWithSettings(disabled).stdout).toBe('');
  });

  it('stops before the script when the disabled settings.json starts with a UTF-8 BOM', () => {
    const { status, stdout } = runWithSettings('\uFEFF' + disabled);
    expect(stdout).toBe('');
    expect(status).toBe(0);
  });

  it('runs the script for an enabled plugin whose settings.json starts with a UTF-8 BOM', () => {
    const enabled = JSON.stringify({ enabledPlugins: { 'claude-mem@thedotmack': true } });
    expect(runWithSettings('\uFEFF' + enabled).stdout).toBe('ran\n');
  });
}, 30_000);