Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

13.6 KB
Raw Blame
import path from 'path';
import { logger } from '@librechat/data-schemas';
import type { TCustomConfig } from 'librechat-data-provider';
import { createCustomConfigLoader, ConfigReloadError, hashCustomConfig } from './loader';
import { loadYaml } from '~/utils/yaml';

const fixture = (name: string) => path.join(__dirname, '__fixtures__', 'theme', `${name}.yaml`);

const raw = (name: string) => loadYaml(fixture(name)) as TCustomConfig;

const createLoader = () =>
  createCustomConfigLoader({
    loadLocal: loadYaml,
    defaultConfigPath: fixture('valid'),
    redactConfig: (config: TCustomConfig) => config,
  });

const warnings = (spy: jest.SpyInstance): string =>
  spy.mock.calls.map(([message]) => String(message)).join('\n');

describe('createCustomConfigLoader interface.theme', () => {
  const originalConfigPath = process.env.CONFIG_PATH;
  let warn: jest.SpyInstance;
  let exit: jest.SpyInstance;

  beforeEach(() => {
    warn = jest.spyOn(logger, 'warn');
    jest.spyOn(logger, 'error').mockImplementation(() => logger);
    jest.spyOn(logger, 'info').mockImplementation(() => logger);
    exit = jest.spyOn(process, 'exit').mockImplementation((code) => {
      throw new Error(`process.exit(${code})`);
    });
  });

  afterEach(() => {
    jest.restoreAllMocks();
    if (originalConfigPath === undefined) {
      delete process.env.CONFIG_PATH;
    } else {
      process.env.CONFIG_PATH = originalConfigPath;
    }
  });

  const load = async (name: string, mode: 'startup' | 'reload' = 'startup') => {
    process.env.CONFIG_PATH = fixture(name);
    return createLoader()(false, { mode });
  };

  it('returns a valid theme exactly as written, without warnings', async () => {
    const config = await load('valid');

    expect(config?.interface?.theme).toEqual(raw('valid').interface?.theme);
    expect(config?.interface?.modelSelect).toBe(true);
    expect(warn).not.toHaveBeenCalled();
    expect(exit).not.toHaveBeenCalled();
  });

  it('keeps a theme naming a color token this version does not know, without that token', async () => {
    const config = await load('unknown-token');

    expect(exit).not.toHaveBeenCalled();
    expect(config?.interface?.theme).toEqual({
      version: 1,
      name: 'acme',
      modes: { light: { colors: { 'rgb-surface-primary': '240 244 255' } } },
    });
    expect(config?.interface?.modelSelect).toBe(true);
    expect(config?.cache).toBe(true);
    expect(warnings(warn)).toContain(
      'interface.theme.modes.light.colors.rgb-surfce-secondary: Unknown light color token ignored: rgb-surfce-secondary',
    );
    expect(warnings(warn)).toContain(
      'interface.theme.modes.light.colors.surface-tertiary: Unknown light color token ignored: surface-tertiary',
    );
    expect(warnings(warn)).not.toContain('the default theme applies instead');
  });

  it('still drops a theme whose unknown color token has an invalid value or name', async () => {
    const config = await load('unknown-token-invalid');

    expect(exit).not.toHaveBeenCalled();
    expect(config?.interface).not.toHaveProperty('theme');
    expect(config?.interface?.modelSelect).toBe(true);
    const logged = warnings(warn);
    expect(logged).toContain(
      'interface.theme.modes.light.colors.rgb-future-role: Invalid RGB value for rgb-future-role: red',
    );
    expect(logged).toContain(
      'interface.theme.modes.dark.colors.Surface Tertiary: Unknown color token: Surface Tertiary',
    );
    expect(logged).toContain('the default theme applies instead');
  });

  it('reports every bad value with its path and drops the theme', async () => {
    const config = await load('bad-value');

    expect(exit).not.toHaveBeenCalled();
    expect(config?.interface).not.toHaveProperty('theme');
    const logged = warnings(warn);
    expect(logged).toContain(
      'interface.theme.modes.dark.colors.rgb-surface-primary: Invalid RGB value for rgb-surface-primary: 300 16 32',
    );
    expect(logged).toContain(
      'interface.theme.modes.dark.appearance.controlRadius: Invalid appearance value for controlRadius: 4 pixels',
    );
  });

  it('drops a theme that is neither a name nor a definition', async () => {
    const config = await load('malformed');

    expect(exit).not.toHaveBeenCalled();
    expect(config?.interface).not.toHaveProperty('theme');
    expect(config?.interface?.modelSelect).toBe(true);
    expect(warnings(warn)).toContain(
      'interface.theme: Expected a bundled theme name or an inline theme definition',
    );
  });

  it('drops a value the client reads but the config schema rejects, citing the schema path', async () => {
    const config = await load('spacing');

    expect(exit).not.toHaveBeenCalled();
    expect(config?.interface).not.toHaveProperty('theme');
    expect(warnings(warn)).toContain('interface.theme.modes.light.colors.rgb-surface-primary:');
  });

  it('drops a bundled theme name that does not exist', async () => {
    const config = await load('unknown-name');

    expect(exit).not.toHaveBeenCalled();
    expect(config?.interface).not.toHaveProperty('theme');
    expect(config?.interface?.modelSelect).toBe(true);
    expect(warnings(warn)).toContain(
      'interface.theme: Unknown bundled theme "clickhous", expected one of: librechat, clickhouse',
    );
  });

  it('keeps a bundled theme name', async () => {
    const config = await load('bundled');

    expect(config?.interface?.theme).toBe('clickhouse');
    expect(warn).not.toHaveBeenCalled();
  });

  it('keeps a theme whose only problem is an appearance token this version ignores', async () => {
    const config = await load('unknown-appearance');

    expect(config?.interface?.theme).toEqual(raw('unknown-appearance').interface?.theme);
    expect(warnings(warn)).toContain(
      'interface.theme.modes.light.appearance.futureSpacing: Unknown light appearance token ignored: futureSpacing',
    );
    expect(warnings(warn)).not.toContain('the default theme applies instead');
  });

  it('still exits at startup when something other than the theme is invalid', async () => {
    await expect(load('unrelated-error')).rejects.toThrow('process.exit(1)');
    expect(exit).toHaveBeenCalledWith(1);
    expect(warnings(warn)).toContain('interface.theme.modes.light.colors.rgb-surfce-primary');
  });

  describe('reload mode', () => {
    it('applies the same fallback instead of rejecting the reload', async () => {
      const config = await load('bad-value', 'reload');

      expect(config?.interface).not.toHaveProperty('theme');
      expect(config?.interface?.modelSelect).toBe(true);
      expect(warnings(warn)).toContain(
        'interface.theme.modes.dark.colors.rgb-surface-primary: Invalid RGB value',
      );
    });

    it('returns a valid theme unchanged', async () => {
      const config = await load('valid', 'reload');

      expect(config?.interface?.theme).toEqual(raw('valid').interface?.theme);
      expect(warn).not.toHaveBeenCalled();
    });

    it('still rejects a reload whose other keys are invalid', async () => {
      await expect(load('unrelated-error', 'reload')).rejects.toBeInstanceOf(ConfigReloadError);
      expect(exit).not.toHaveBeenCalled();
    });
  });
});

describe('createCustomConfigLoader policy links', () => {
  const originalConfigPath = process.env.CONFIG_PATH;
  let warn: jest.SpyInstance;
  let exit: jest.SpyInstance;

  beforeEach(() => {
    warn = jest.spyOn(logger, 'warn');
    jest.spyOn(logger, 'error').mockImplementation(() => logger);
    jest.spyOn(logger, 'info').mockImplementation(() => logger);
    exit = jest.spyOn(process, 'exit').mockImplementation((code) => {
      throw new Error(`process.exit(${code})`);
    });
  });

  afterEach(() => {
    jest.restoreAllMocks();
    if (originalConfigPath === undefined) {
      delete process.env.CONFIG_PATH;
    } else {
      process.env.CONFIG_PATH = originalConfigPath;
    }
  });

  const load = async (name: string, mode: 'startup' | 'reload' = 'startup') => {
    process.env.CONFIG_PATH = path.join(__dirname, '__fixtures__', 'links', `${name}.yaml`);
    return createLoader()(false, { mode });
  };

  it.each(['startup', 'reload'] as const)(
    'drops a link that is not http(s), mailto:, tel: or relative and still loads on %s',
    async (mode) => {
      const config = await load('unsafe', mode);

      expect(exit).not.toHaveBeenCalled();
      expect(config?.interface).toEqual({
        modelSelect: true,
        privacyPolicy: { openNewTab: true },
        termsOfService: { modalAcceptance: true },
      });
      expect(warnings(warn)).toContain('- interface.privacyPolicy.externalUrl');
      expect(warnings(warn)).toContain('- interface.termsOfService.externalUrl');
      expect(warnings(warn)).not.toContain('javascript');
    },
  );

  it('keeps mailto: and http(s) links as written, without warnings', async () => {
    const config = await load('contact');

    expect(config?.interface).toEqual({
      privacyPolicy: { externalUrl: 'mailto:[email protected]' },
      termsOfService: { externalUrl: 'https://example.com/terms' },
    });
    expect(warn).not.toHaveBeenCalled();
  });
});

describe('createCustomConfigLoader remote fetch failures', () => {
  const originalConfigPath = process.env.CONFIG_PATH;

  beforeEach(() => {
    process.env.CONFIG_PATH = 'https://config.example/librechat.yaml';
    jest.spyOn(logger, 'error').mockImplementation(() => logger);
  });

  afterEach(() => {
    jest.restoreAllMocks();
    if (originalConfigPath === undefined) {
      delete process.env.CONFIG_PATH;
    } else {
      process.env.CONFIG_PATH = originalConfigPath;
    }
  });

  const loaderFor = (error: unknown) =>
    createCustomConfigLoader({
      loadLocal: loadYaml,
      defaultConfigPath: fixture('valid'),
      redactConfig: (config: TCustomConfig) => config,
      fetchRemote: async () => {
        throw error;
      },
    });

  it('fails startup when the remote config fetch times out', async () => {
    const timeout = Object.assign(new Error('timeout of 10000ms exceeded'), {
      code: 'ECONNABORTED',
    });
    await expect(loaderFor(timeout)()).rejects.toThrow(/Timed out fetching the remote config/);
  });

  it('keeps returning null for other remote fetch failures', async () => {
    const refused = Object.assign(new Error('connect ECONNREFUSED'), { code: 'ECONNREFUSED' });
    await expect(loaderFor(refused)()).resolves.toBeNull();
  });
});

describe('createCustomConfigLoader onLoad', () => {
  const originalConfigPath = process.env.CONFIG_PATH;

  beforeEach(() => {
    jest.spyOn(logger, 'error').mockImplementation(() => logger);
    jest.spyOn(logger, 'info').mockImplementation(() => logger);
  });

  afterEach(() => {
    jest.restoreAllMocks();
    if (originalConfigPath === undefined) {
      delete process.env.CONFIG_PATH;
    } else {
      process.env.CONFIG_PATH = originalConfigPath;
    }
  });

  /** The loader parses a string source as YAML, so comments and layout reach the digest input. */
  const createTextLoader = (text: string, onLoad: jest.Mock) => {
    process.env.CONFIG_PATH = '/virtual/librechat.yaml';
    return createCustomConfigLoader({
      loadLocal: () => text,
      defaultConfigPath: '/virtual/librechat.yaml',
      redactConfig: (value: TCustomConfig) => value,
      onLoad,
    });
  };

  const loadText = async (text: string) => {
    const onLoad = jest.fn();
    const config = await createTextLoader(text, onLoad)(false);
    return { config, onLoad };
  };

  const digestOf = async (text: string): Promise<string> => {
    const { onLoad } = await loadText(text);
    expect(onLoad).toHaveBeenCalledTimes(1);
    return onLoad.mock.calls[0][0].digest;
  };

  it('hands the loaded config and a short digest to onLoad', async () => {
    const { config, onLoad } = await loadText(
      'version: 1.3.18\ntelemetry:\n  stackTraceLimit: 40\n',
    );

    expect(config?.telemetry?.stackTraceLimit).toBe(40);
    expect(onLoad).toHaveBeenCalledWith({
      config,
      digest: expect.stringMatching(/^[0-9a-f]{12}$/),
    });
  });

  it('keeps the digest across comments, layout and key order, and moves it on a value change', async () => {
    const base = await digestOf('version: 1.3.18\ncache: true\ninterface:\n  modelSelect: true\n');
    const reordered = await digestOf(
      '# comment\ninterface: { modelSelect: true }\n\ncache: true\nversion: 1.3.18\n',
    );
    const changed = await digestOf(
      'version: 1.3.18\ncache: false\ninterface:\n  modelSelect: true\n',
    );

    expect(reordered).toBe(base);
    expect(changed).not.toBe(base);
  });

  it('hashes the file as written, not the modelSpecs the schema fills in', async () => {
    const text =
      'version: 1.3.18\nmodelSpecs:\n  list:\n    - name: spec\n      label: Spec\n' +
      '      preset:\n        endpoint: openAI\n        model: gpt-4o\n';
    const { config, onLoad } = await loadText(text);

    expect(config?.modelSpecs).toBeDefined();
    expect(onLoad.mock.calls[0][0].digest).toBe(
      hashCustomConfig({
        version: '1.3.18',
        modelSpecs: {
          list: [{ name: 'spec', label: 'Spec', preset: { endpoint: 'openAI', model: 'gpt-4o' } }],
        },
      }),
    );
  });

  it('does not call onLoad when a reload is rejected', async () => {
    const onLoad = jest.fn();
    const load = createTextLoader('version: 1.3.18\nnotAKey: true\n', onLoad);

    await expect(load(false, { mode: 'reload' })).rejects.toBeInstanceOf(ConfigReloadError);
    expect(onLoad).not.toHaveBeenCalled();
  });
});

describe('hashCustomConfig', () => {
  it('ignores key order and keeps array order', () => {
    expect(hashCustomConfig({ a: 1, b: { c: 2, d: [1, 2] } })).toBe(
      hashCustomConfig({ b: { d: [1, 2], c: 2 }, a: 1 }),
    );
    expect(hashCustomConfig({ b: { d: [2, 1], c: 2 }, a: 1 })).not.toBe(
      hashCustomConfig({ a: 1, b: { c: 2, d: [1, 2] } }),
    );
  });
});