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

5.5 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0

import { describe, expect, it } from 'bun:test';
import { readFileSync } from 'fs';
import { isClaudeMemObserverBaseUrl } from '../../src/ui/viewer/utils/observer-endpoint.js';
import {
  OPENAI_COMPAT_PRESET_OPTIONS,
  openAICompatPresetOption,
} from '../../src/ui/viewer/constants/openai-compat-presets.js';
import { OPENAI_COMPAT_PRESETS } from '../../src/shared/openai-compat-presets.js';

describe('openai-compatible settings surface', () => {
  const modalSource = () => readFileSync('src/ui/viewer/components/ContextSettingsModal.tsx', 'utf-8');

  it('lists the provider after the claude-mem observer, never ahead of it', () => {
    const source = modalSource();
    // The label goes through the UI catalog (t()), so match the option by its value and its English label.
    const observer = source.indexOf(`<option value="openrouter">{t('OpenRouter / claude-mem observer')}</option>`);
    const compat = source.indexOf('<option value="openai-compatible">');
    expect(observer).toBeGreaterThan(-1);
    expect(compat).toBeGreaterThan(observer);
  });

  // Wave 3 gate R4-4: #4216 listed Codex second, above the observer. Before the
  // sweep the select was Claude, Gemini, then the observer; the bring-your-own
  // options added since come after it.
  it('lists Codex after the claude-mem observer too, keeping the pre-sweep order ahead of it', () => {
    const source = modalSource();
    const position = (value: string) => source.indexOf(`<option value="${value}">`);
    expect(position('claude')).toBeGreaterThan(-1);
    expect(position('gemini')).toBeGreaterThan(position('claude'));
    expect(position('openrouter')).toBeGreaterThan(position('gemini'));
    expect(position('codex')).toBeGreaterThan(position('openrouter'));
  });

  it('edits the model while endpoint, preset and key stay outside the viewer', () => {
    const source = modalSource();
    expect(source).toContain("updateSetting('CLAUDE_MEM_OPENAI_COMPAT_MODEL'");
    for (const key of ['CLAUDE_MEM_OPENROUTER_BASE_URL', 'CLAUDE_MEM_OPENAI_COMPAT_PRESET', 'CLAUDE_MEM_OPENAI_COMPAT_BASE_URL', 'CLAUDE_MEM_OPENAI_COMPAT_API_KEY']) {
      expect(source).not.toContain(`updateSetting('${key}'`);
    }
    // Both base URLs and the preset show in read-only fields (ReadOnlyInput: a lock and a readOnly input).
    for (const key of ['CLAUDE_MEM_OPENROUTER_BASE_URL', 'CLAUDE_MEM_OPENAI_COMPAT_BASE_URL']) {
      expect(source.match(new RegExp(`<ReadOnlyInput[^>]*value=\\{formState\\.${key}[^>]*>`, 's'))?.[0]).toBeString();
    }
    expect(source.match(/<ReadOnlyInput[^>]*value=\{t\(presetOption\.label\)\}[^>]*>/s)?.[0]).toBeString();
    expect(source.match(/function ReadOnlyInput[\s\S]*?<input[^>]*>/)?.[0]).toContain('readOnly');

    const routeSource = readFileSync('src/services/worker/http/routes/SettingsRoutes.ts', 'utf-8');
    const writeList = routeSource.slice(routeSource.indexOf('const settingKeys = ['), routeSource.indexOf('];', routeSource.indexOf('const settingKeys = [')));
    expect(writeList).toContain("'CLAUDE_MEM_OPENAI_COMPAT_MODEL'");
    for (const key of ['CLAUDE_MEM_OPENROUTER_BASE_URL', 'CLAUDE_MEM_OPENAI_COMPAT_PRESET', 'CLAUDE_MEM_OPENAI_COMPAT_BASE_URL']) {
      expect(writeList).not.toContain(`'${key}'`);
    }
    expect(writeList).not.toContain("'CLAUDE_MEM_OPENAI_COMPAT_API_KEY'");
    expect(writeList).not.toContain("'CLAUDE_MEM_OPENAI_COMPAT_API_KEYS'");
  });

  it('exposes the implicit default override as an editable setting', () => {
    expect(modalSource()).toContain("updateSetting('CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL'");
    expect(modalSource()).toContain('effectiveDefault');
    // La copie locale doit exprimer les trois cas de la règle partagée, pas seulement exister.
    expect(modalSource()).toContain('.trim()');
    expect(modalSource()).toContain(".toLowerCase() === 'none'");
    expect(modalSource()).toContain('defaultOverride || presetOption.defaultModel');
    expect(modalSource()).toContain('last served model, then the preset default');
    const routeSource = readFileSync('src/services/worker/http/routes/SettingsRoutes.ts', 'utf-8');
    const writeList = routeSource.slice(routeSource.indexOf('const settingKeys = ['), routeSource.indexOf('];', routeSource.indexOf('const settingKeys = [')));
    expect(writeList).toContain("'CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL'");
  });

  it('offers exactly the worker\'s presets, in the same order', () => {
    expect(OPENAI_COMPAT_PRESET_OPTIONS.map(({ id, label, baseUrl, defaultModel }) => ({ id, label, baseUrl, defaultModel })))
      .toEqual(OPENAI_COMPAT_PRESETS.map(({ id, label, baseUrl, defaultModel }) => ({ id, label, baseUrl, defaultModel })));
  });

  it('reads an unknown or blank stored preset as custom, like the worker', () => {
    expect(openAICompatPresetOption('nvidia-nimm').id).toBe('custom');
    expect(openAICompatPresetOption('').id).toBe('custom');
    expect(openAICompatPresetOption(' NVIDIA-NIM ').id).toBe('nvidia-nim');
  });
});

describe('isClaudeMemObserverBaseUrl', () => {
  it('recognizes the observer gateway', () => {
    expect(isClaudeMemObserverBaseUrl('https://cmem.ai/api/inference/v1')).toBe(true);
    expect(isClaudeMemObserverBaseUrl(' https://CMEM.ai/api/inference/v1 ')).toBe(true);
  });

  it('does not label other endpoints as managed by the observer', () => {
    for (const value of [
      undefined, '', 'https://openrouter.ai/api/v1', 'https://api.deepseek.com',
      'http://localhost:1234/v1', 'https://cmem.ai.evil.example/v1', 'not a url',
    ]) {
      expect(isClaudeMemObserverBaseUrl(value)).toBe(false);
    }
  });
});