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

4.2 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
import { execFileSync } from 'node:child_process';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';

const INSTALLER = path.resolve(import.meta.dir, '../../src/services/integrations/WindsurfHooksInstaller.ts');

// Windsurf's hooks.json is shared with other tools, so the installer has to
// read the file it inherits before it can merge into it. These cases pin what
// happens when it cannot: the file must be left exactly as it was and the
// install must fail, rather than reporting success with nothing registered.
describe('Windsurf install refuses a hooks.json it cannot merge', () => {
  let root: string;

  beforeEach(() => {
    root = mkdtempSync(path.join(tmpdir(), 'windsurf-shape-'));
  });

  afterEach(() => {
    rmSync(root, { recursive: true, force: true });
  });

  function install(raw: string): { code: number; file: string | null; said: string } {
    const home = path.join(root, 'home');
    const workspace = path.join(root, 'workspace');
    const hooksPath = path.join(home, '.codeium', 'windsurf', 'hooks.json');
    const pluginRoot = path.join(root, 'plugin');
    const bunRoot = path.join(root, 'runtime');
    for (const dir of [workspace, path.dirname(hooksPath), path.join(pluginRoot, 'scripts'), path.join(bunRoot, 'bin')]) {
      mkdirSync(dir, { recursive: true });
    }
    writeFileSync(hooksPath, raw);
    // Installation checks these paths; it never executes either fixture file.
    const bunPath = path.join(bunRoot, 'bin', process.platform === 'win32' ? 'bun.exe' : 'bun');
    writeFileSync(bunPath, 'fixture runtime');
    chmodSync(bunPath, 0o755);
    writeFileSync(path.join(pluginRoot, 'scripts', 'worker-service.cjs'), 'fixture worker');
    const scriptPath = path.join(root, 'run-installer.mjs');
    writeFileSync(scriptPath, `
      const said = [];
      console.log = () => {};
      console.error = (message) => said.push(String(message));
      globalThis.fetch = async () => new Response('owned context');
      const { readFileSync, existsSync } = await import('node:fs');
      const installer = await import(${JSON.stringify(INSTALLER)});
      const configPath = ${JSON.stringify(hooksPath)};
      const code = await installer.installWindsurfHooks();
      process.stdout.write(JSON.stringify({
        code, said: said.join('\\n'),
        file: existsSync(configPath) ? readFileSync(configPath, 'utf8') : null,
      }));
    `);
    const stdout = execFileSync(process.execPath, [scriptPath], {
      cwd: workspace,
      env: { ...process.env, HOME: home, USERPROFILE: home,
        CLAUDE_CONFIG_DIR: path.join(home, '.claude'), CLAUDE_MEM_DATA_DIR: path.join(home, '.claude-mem'),
        CLAUDE_PLUGIN_ROOT: pluginRoot, BUN_INSTALL: bunRoot,
        DO_NOT_TRACK: '1', CLAUDE_MEM_TELEMETRY: '0',
      },
      encoding: 'utf8', timeout: 20_000,
    });
    return JSON.parse(stdout);
  }

  it('refuses an array for "hooks" instead of writing hooks nobody will run', () => {
    const raw = JSON.stringify({ hooks: [] }, null, 2);
    const result = install(raw);
    expect(result.code).toBe(1);
    expect(result.file).toBe(raw);
    expect(result.said).toContain('refusing to overwrite');
  });

  it('names the event it cannot read instead of failing inside the merge', () => {
    const raw = JSON.stringify({ hooks: { post_run_command: 'audit --log' } }, null, 2);
    const result = install(raw);
    expect(result.code).toBe(1);
    expect(result.file).toBe(raw);
    expect(result.said).toContain('hooks.post_run_command');
    expect(result.said).not.toContain('.filter is not a function');
  });

  it('still merges when a foreign event holds a shape claude-mem never writes', () => {
    const result = install(JSON.stringify({ hooks: { pre_run_command: 'audit --log' } }, null, 2));
    expect(result.code).toBe(0);
    const written = JSON.parse(result.file!);
    // Copied back verbatim: the guard covers our five events, not the user's.
    expect(written.hooks.pre_run_command).toBe('audit --log');
    expect(written.hooks.post_run_command).toHaveLength(1);
  });
});