LibreChat/scripts/eslint/chat.test.mjs
Marco Beretta 8e60a4185a
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/chat (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
Main Promotion Policy Tests / Main promotion denial and fast-forward tests (push) Waiting to run
🧱 refactor: Judge Chat Package Imports on the Resolved Module (#16975)
* refactor: Judge chat imports on the resolved target with one boundary rule

scripts/eslint/chat.mjs replaces the import patterns and load selectors: every
specifier is normalized (extension, trailing slash, /index, package name)
before it is judged, across static imports, re-exports, import(), require(),
import-equals and import types, so any spelling of the /react or /components
entry or a UI package is rejected from core. A RuleTester table and a
table-driven scenario cover the spellings.

* fix: Anchor the chat boundary root to the config directory

* fix: Keep bare app paths and bare core entries behind the chat boundary

* fix: Treat aliased react paths in core as the /react entry
2026-10-11 01:06:07 +02:00

153 lines
4.7 KiB
JavaScript

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 },
],
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' }],
},
{
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' }],
},
],
});