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

8.0 KB
Raw Blame
import path from 'node:path';
import { describe, it } from 'node:test';
import tsParser from '@typescript-eslint/parser';
import { RuleTester } from 'eslint';
import chat from './chat.mjs';

RuleTester.describe = describe;
RuleTester.it = it;

const root = path.resolve(import.meta.dirname, '../..');
const options = [{ sourceRoot: 'packages/chat/src', packageName: '@librechat/chat' }];
const at = (file) => path.join(root, 'packages/chat/src', file);

const tester = new RuleTester({
  languageOptions: { parser: tsParser, parserOptions: { ecmaFeatures: { jsx: true } } },
});

/** Every spelling a source can use to load one target, in each syntactic form the rule reads. */
const forms = (specifier) => [
  `import x from '${specifier}';`,
  `import type { X } from '${specifier}';`,
  `export * from '${specifier}';`,
  `export { x } from '${specifier}';`,
  `export const load = () => import('${specifier}');`,
  `export const load = () => import(\`${specifier}\`);`,
  `export const load = () => require('${specifier}');`,
  `import x = require('${specifier}');`,
  `export type X = typeof import('${specifier}');`,
];

/** Ways of naming the `/react` and `/components` entries from `src/core/streaming/`. */
const entrySpellings = ['react', 'components'].flatMap((entry) => [
  `../../${entry}`,
  `../../${entry}/`,
  `../../${entry}.ts`,
  `../../${entry}.tsx`,
  `../../${entry}.js`,
  `../../${entry}.mjs`,
  `../../${entry}.cjs`,
  `../../${entry}/index`,
  `../../${entry}/index.js`,
  `../../${entry}/Part.tsx`,
  `../.././${entry}`,
  `../../core/../${entry}`,
]);

const uiSpellings = [
  'react',
  'react/jsx-runtime',
  'react-dom',
  'react-dom/client',
  'jotai',
  'jotai/utils',
  '@tanstack/react-query',
  '@librechat/client',
  '@librechat/client/theme.css',
];

const appSpellings = [
  'client/src/hooks',
  'src/client/src',
  '~/store',
  '~',
  '@librechat/frontend',
  '@librechat/frontend/src/store/filesDialog',
  '../../../../client/src/hooks',
  '../../../client/src',
  '../../package.json',
];

const selfSpellings = ['@librechat/chat', '@librechat/chat/react', '@librechat/chat/components'];

const cases = (file, specifiers, messageId) =>
  specifiers.flatMap((specifier) =>
    forms(specifier).map((code) => ({
      code,
      filename: at(file),
      options,
      errors: [{ messageId }],
    })),
  );

const allowed = (file, specifiers) =>
  specifiers.flatMap((specifier) =>
    forms(specifier).map((code) => ({ code, filename: at(file), options })),
  );

tester.run('chat/boundary', chat.rules.boundary, {
  valid: [
    ...allowed('core/streaming/probe.ts', [
      'librechat-data-provider',
      './types',
      '../types.js',
      '../../core/types',
      './react-utils',
      '../reactive.js',
      '../../index',
    ]),
    ...allowed('index.ts', ['./core', './core/index.ts']),
    ...allowed('react/probe.tsx', [
      'react',
      'jotai',
      '@tanstack/react-query',
      '../core',
      '../components',
    ]),
    ...allowed('components/Probe.tsx', ['react', '@librechat/client', '../react']),
    ...allowed('__tests__/core.spec.ts', ['react']),
    {
      code: "const name = 'react'; export const load = () => import(name);",
      filename: at('react/probe.ts'),
      options,
    },
    { code: "import x from '~/store';", filename: path.join(root, 'client/src/x.ts'), options },
    ...[
      "import { cn } from '@librechat/client'; export const C = () => cn('a');",
      "import { cn } from '@librechat/client'; export function f() { return cn('a'); }",
      "import { cn } from '@librechat/client'; export const C = memo(function C() { return cn('a'); });",
      "import { cn } from '@librechat/client'; class A { value = cn('a'); }",
      "import type { TShowToast } from '@librechat/client'; export const t = TShowToast;",
      "import { cn } from './utils'; export const classes = cn('a');",
      "import { cn } from '@librechat/client'; export const f = (cn: (v: string) => string) => cn('a');",
      "import { memo, createContext } from 'react'; export const C = memo(() => null); export const Ctx = createContext(null);",
      "import { atom } from 'jotai'; import { atomFamily } from 'jotai/utils'; export const a = atom(0); export const f = atomFamily(() => a);",
      "import { FileSources } from 'librechat-data-provider'; export const icon = (s: string) => ({ [FileSources.local]: 'x' })[s];",
      "import { Search } from 'lucide-react'; export const icons = { search: Search, list: [Search] };",
    ].map((code) => ({ code, filename: at('components/Probe.tsx'), options })),
    {
      code: "import { cn } from '@librechat/client'; const classes = cn('a');",
      filename: at('components/__tests__/Probe.spec.tsx'),
      options,
    },
    {
      code: "import path from 'node:path'; export const dir = path.join('a', 'b');",
      filename: at('__fixtures__/i18n.ts'),
      options,
    },
    {
      code: "import { cn } from '@librechat/client'; export const classes = cn('a');",
      filename: at('components/__mocks__/Probe.tsx'),
      options,
    },
    {
      code: "import path from 'node:path'; export const dir = path.join('a', 'b');",
      filename: at('react/probe.ts'),
      options,
    },
  ],
  invalid: [
    ...cases('core/streaming/probe.ts', entrySpellings, 'entry'),
    ...cases('core/streaming/probe.ts', uiSpellings, 'ui'),
    ...cases('index.ts', ['react'], 'ui'),
    ...cases(
      'core/probe.ts',
      ['components', 'components/Part', 'src/components', 'src/react', '@/react', '@/components/'],
      'entry',
    ),
    ...cases('index.ts', ['./react', './components/'], 'entry'),
    ...cases('core/probe.ts', appSpellings.slice(0, 4), 'app'),
    ...cases('react/probe.ts', appSpellings, 'app'),
    ...cases('components/Probe.tsx', ['recoil', 'recoil/x'], 'recoil'),
    ...cases('react/probe.ts', selfSpellings, 'self'),
    ...cases('core/probe.ts', selfSpellings, 'self'),
    {
      // An absolute root holds when ESLint runs from another directory, as a workspace script does.
      code: "import x from 'react';",
      filename: at('core/probe.ts'),
      options: [
        { sourceRoot: path.join(root, 'packages/chat/src'), packageName: '@librechat/chat' },
      ],
      errors: [{ messageId: 'ui' }],
    },
    ...[
      ["import { cn } from '@librechat/client'; export const classes = cn('a');", 'cn'],
      ["import { cn as join } from '@librechat/client'; export const classes = join('a');", 'join'],
      [
        "import * as Client from '@librechat/client'; export const c = Client['cn']('a');",
        'Client',
      ],
      ["import { cva } from '@librechat/client'; export const variants = cva('a', {});", 'cva'],
      ["import { Store } from '@librechat/client'; export const store = new Store();", 'Store'],
      ["import { css } from '@librechat/client'; export const rule = css`color: red;`;", 'css'],
      ["import { cn } from '@librechat/client'; class A { static value = cn('a'); }", 'cn'],
      ["export const classes = cn('a'); import { cn } from '@librechat/client';", 'cn'],
      [
        "import { FileSources } from 'librechat-data-provider'; export const icons = { [FileSources.local]: 'x' };",
        'FileSources',
        'librechat-data-provider',
      ],
      ["import { v4 } from 'uuid'; export const id = v4();", 'v4', 'uuid'],
      [
        "import { atom } from 'jotai'; export const a = atom(0);",
        'atom',
        'jotai',
        { loadSafe: [] },
      ],
    ].flatMap(([code, name, source = '@librechat/client', extra = {}]) =>
      ['components/Probe.tsx', 'react/probe.ts'].map((file) => ({
        code,
        filename: at(file),
        options: [{ ...options[0], ...extra }],
        errors: [{ messageId: 'load', data: { name, source } }],
      })),
    ),
    {
      code: 'export const load = (name: string) => import(name);',
      filename: at('core/probe.ts'),
      options,
      errors: [{ messageId: 'opaque' }],
    },
    {
      code: 'export const load = (name: string) => require(`./${name}`);',
      filename: at('core/probe.ts'),
      options,
      errors: [{ messageId: 'opaque' }],
    },
  ],
});