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

3.6 KB
Raw Blame
import { describe, expect, test } from 'bun:test';
import { atomcodeAdapter, claudeCodeAdapter, getPlatformAdapter } from '../../../src/cli/adapters/index.js';
import { AdapterRejectedInput } from '../../../src/cli/adapters/errors.js';

// AtomCode hook payloads, in the shapes built by
// atomcode crates/atomcode-capabilities/src/cc_hooks.rs.
const BASE = {
  session_id: 'atomcode-session-0001',
  cwd: process.cwd(),
};
const PRE_READ = {
  ...BASE,
  hook_event_name: 'PreToolUse',
  call_id: 'tc-42',
  tool_name: 'read_file',
  tool_input: { file_path: 'notes.txt' },
};
// PostToolUse carries the result but no tool_input.
const POST_BASH = {
  ...BASE,
  hook_event_name: 'PostToolUse',
  call_id: 'tc-43',
  tool_name: 'bash',
  tool_response: 'ok\n',
};

describe('atomcodeAdapter', () => {
  test('is the adapter for the atomcode platform', () => {
    expect(getPlatformAdapter('atomcode')).toBe(atomcodeAdapter);
  });

  test('maps AtomCode tool names and keeps the native name', () => {
    const mapped: Array<[string, string]> = [
      ['bash', 'Bash'], ['read_file', 'Read'], ['write_file', 'Write'], ['edit_file', 'Edit'],
      ['grep', 'Grep'], ['glob', 'Glob'], ['web_search', 'WebSearch'], ['web_fetch', 'WebFetch'],
    ];
    for (const [native, recorded] of mapped) {
      const input = atomcodeAdapter.normalizeInput({ ...POST_BASH, tool_name: native });
      expect(input.toolName).toBe(recorded);
      expect(input.nativeToolName).toBe(native);
    }
  });

  test('passes unmapped tools through unchanged with no native name', () => {
    for (const name of ['task', 'mcp__x__y', 'toString']) {
      const input = atomcodeAdapter.normalizeInput({ ...POST_BASH, tool_name: name });
      expect(input.toolName).toBe(name);
      expect(input.nativeToolName).toBeUndefined();
    }
  });

  test('uses call_id as the tool use id so repeat tool events dedupe', () => {
    expect(atomcodeAdapter.normalizeInput(POST_BASH).toolUseId).toBe('tc-43');
    expect(atomcodeAdapter.normalizeInput(PRE_READ).toolUseId).toBe('tc-42');
    expect(atomcodeAdapter.normalizeInput({ ...POST_BASH, tool_use_id: 'tu-1' }).toolUseId).toBe('tu-1');
  });

  test('reads a PostToolUse without tool_input: the result only', () => {
    const input = atomcodeAdapter.normalizeInput(POST_BASH);
    expect(input.toolInput).toBeUndefined();
    expect(input.toolResponse).toBe('ok\n');
  });

  test('keeps the read_file input the file-context hook reads', () => {
    expect(atomcodeAdapter.normalizeInput(PRE_READ).toolInput).toEqual({ file_path: 'notes.txt' });
  });

  test('rejects a payload without a session id', () => {
    expect(() => atomcodeAdapter.normalizeInput({ ...POST_BASH, session_id: '' })).toThrow(AdapterRejectedInput);
    expect(() => atomcodeAdapter.normalizeInput({ ...POST_BASH, session_id: undefined })).toThrow('missing_session_id');
  });

  // A cwd inherited from elsewhere (CLAUDE_PROJECT_DIR) must not replace AtomCode's.
  test("keeps AtomCode's cwd, and falls back to the process cwd only when it is missing", () => {
    expect(atomcodeAdapter.normalizeInput({ ...POST_BASH, cwd: '/home/example/project' }).cwd).toBe('/home/example/project');
    expect(atomcodeAdapter.normalizeInput({ ...POST_BASH, cwd: '' }).cwd).toBe(process.cwd());
    expect(atomcodeAdapter.normalizeInput({ ...POST_BASH, cwd: 42 }).cwd).toBe(process.cwd());
  });

  test('returns context in the Claude Code envelope', () => {
    const result = { hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: 'memory' } };
    expect(atomcodeAdapter.formatOutput(result)).toEqual(claudeCodeAdapter.formatOutput(result));
  });
});