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
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
⚡ perf: Cache Package Builds and Make Client Coverage Opt-In (#16709)
Marco Beretta9ff53ba5b5a1
diff --git a/client/jest.config.cjs b/client/jest.config.cjsindex 28b5f842cb..74b6110538 100644--- a/client/jest.config.cjs+++ b/client/jest.config.cjs@@ -7,7 +7,7 @@ module.exports = {testEnvironmentOptions: {url: 'http://localhost:3080',},- collectCoverage: true,+ collectCoverage: process.env.COVERAGE === 'true',collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}','!<rootDir>/node_modules/',diff --git a/client/package.json b/client/package.jsonindex 048dceec69..6831d650cd 100644--- a/client/package.json+++ b/client/package.json@@ -12,7 +12,7 @@"dev": "cross-env NODE_ENV=development vite","preview-prod": "cross-env NODE_ENV=development vite preview","test": "cross-env NODE_ENV=development jest --watch",- "test:ci": "cross-env NODE_ENV=development jest --ci --logHeapUsage",+ "test:ci": "cross-env NODE_ENV=development COVERAGE=true jest --ci --logHeapUsage","b:test": "NODE_ENV=test bunx jest --watch","b:build": "NODE_ENV=production bun --bun vite build","b:dev": "NODE_ENV=development bunx vite"diff --git a/e2e/specs/mock/scenarios/turbo-cache.spec.ts b/e2e/specs/mock/scenarios/turbo-cache.spec.tsnew file mode 100644index 0000000000..c020ce21b3--- /dev/null+++ b/e2e/specs/mock/scenarios/turbo-cache.spec.ts@@ -0,0 +1,165 @@+import { spawnSync } from 'node:child_process';+import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';+import { expect, test } from '@playwright/test';+import { inOneProject, repoRoot } from './lint.helpers';++/**+ * `npm run build` restores package `dist/` from Turbo's cache, so every input a+ * build bakes in has to reach its hash. `npm run test:turbo` runs the unit suites+ * in parallel but never replays a result: a suite here reads fixtures, env files+ * and source from other workspaces, so no input list could make a cached pass+ * trustworthy. With no cache to key, every caller variable is passed through. These scenarios read Turbo's own dry-run plan for the checkout;+ * nothing runs and nothing is written.+ */++type DryTask = {+ taskId: string;+ hash: string;+ dependencies: string[];+ inputs: Record<string, string>;+ resolvedTaskDefinition: { cache: boolean };+ environmentVariables: { passthrough: string[] | null };+};++const TURBO = resolve(repoRoot, 'node_modules/.bin/turbo');+const rootScripts = (+ JSON.parse(readFileSync(resolve(repoRoot, 'package.json'), 'utf8')) as {+ scripts: Record<string, string>;+ }+).scripts;+/** The workspaces `npm run test:turbo` selects, read from the script itself. */+const SUITES = [...rootScripts['test:turbo'].matchAll(/--filter=(\S+)/g)].map((match) => match[1]);++function plan(+ task: string,+ env: Record<string, string> = {},+ filters: string[] = SUITES,+): Map<string, DryTask> {+ const base = { ...process.env };+ delete base.VITE_ENABLE_LOGGER;+ const result = spawnSync(+ TURBO,+ ['run', task, '--dry=json', ...filters.map((name) => `--filter=${name}`)],+ {+ cwd: repoRoot,+ encoding: 'utf8',+ maxBuffer: 64 * 1024 * 1024,+ env: { ...base, TURBO_TELEMETRY_DISABLED: '1', ...env },+ },+ );+ expect(result.status, result.stderr).toBe(0);+ const { tasks } = JSON.parse(result.stdout.slice(result.stdout.indexOf('{'))) as {+ tasks: DryTask[];+ };+ return new Map(tasks.map((entry) => [entry.taskId, entry]));+}++function get(tasks: Map<string, DryTask>, id: string): DryTask {+ const entry = tasks.get(id);+ expect(entry, `${id} is in the plan`).toBeDefined();+ return entry!;+}++test.describe('the parallel test runner', () => {+ test.beforeEach(() => {+ inOneProject();+ test.setTimeout(120_000);+ });++ test('the runner covers every workspace with a test suite @scenario:test-turbo-covers-every-workspace-suite', () => {+ const everySuite = [...plan('test:ci', {}, []).keys()].filter((id) => id.endsWith('#test:ci'));+ expect(SUITES.map((name) => `${name}#test:ci`).sort()).toEqual(everySuite.sort());+ });++ test('every suite runs instead of replaying a cached result @scenario:test-turbo-always-runs-every-suite', () => {+ const tasks = plan('test:ci');+ for (const name of SUITES) {+ expect(get(tasks, `${name}#test:ci`).resolvedTaskDefinition.cache, name).toBe(false);+ }+ });++ test('each suite starts after the builds its specs import @scenario:each-suite-waits-for-the-builds-it-imports', () => {+ const tasks = plan('test:ci');+ expect(get(tasks, '@librechat/data-schemas#test:ci').dependencies).toContain(+ 'librechat-data-provider#build',+ );+ expect(get(tasks, '@librechat/api#test:ci').dependencies).toEqual(+ expect.arrayContaining(['librechat-data-provider#build', '@librechat/data-schemas#build']),+ );+ expect(get(tasks, '@librechat/backend#test:ci').dependencies).toEqual(+ expect.arrayContaining([+ 'librechat-data-provider#build',+ '@librechat/data-schemas#build',+ '@librechat/api#build',+ ]),+ );+ expect(get(tasks, '@librechat/frontend#test:ci').dependencies).toEqual(+ expect.arrayContaining(['librechat-data-provider#build', '@librechat/client#build']),+ );+ });++ test('every variable the caller sets reaches jest @scenario:live-suite-env-reaches-jest', () => {+ const names = [+ 'RUN_USAGE_LIVE_TESTS',+ 'OPENWEATHER_API_KEY',+ 'MONGOMS_SYSTEM_BINARY',+ 'TMPDIR',+ ];+ const tasks = plan('test:ci', Object.fromEntries(names.map((name) => [name, '/tmp/x'])));+ for (const suite of SUITES) {+ const passed = (get(tasks, `${suite}#test:ci`).environmentVariables.passthrough ?? []).map(+ (entry) => entry.slice(0, entry.indexOf('=')),+ );+ expect(passed, suite).toEqual(expect.arrayContaining(names));+ }+ });+});++test.describe('the package build cache', () => {+ test.beforeEach(() => {+ inOneProject();+ test.setTimeout(120_000);+ });++ test('a package build hashes its shipped assets but not its specs @scenario:a-spec-edit-keeps-the-package-build-cached', () => {+ const inputs = Object.keys(get(plan('build'), '@librechat/api#build').inputs);+ expect(inputs).toContain('openapi/agents.openapi.json');+ expect(inputs.some((file) => /\.spec\.|\.test\.|__tests__\//.test(file))).toBe(false);+ expect(inputs.some((file) => file.startsWith('src/'))).toBe(true);+ });++ test('the client package rebuilds when its baked logger setting changes @scenario:client-package-build-tracks-baked-env', () => {+ const off = get(plan('build', { VITE_ENABLE_LOGGER: 'false' }), '@librechat/client#build');+ const on = get(plan('build', { VITE_ENABLE_LOGGER: 'true' }), '@librechat/client#build');+ expect(on.hash).not.toBe(off.hash);+ });+});++test.describe('client coverage', () => {+ test.beforeEach(() => inOneProject());++ test('collects coverage only when the run asks for it @scenario:client-coverage-runs-only-when-requested', () => {+ const config = resolve(repoRoot, 'client/jest.config.cjs');+ const read = (coverage?: string) => {+ const env: Record<string, string | undefined> = { ...process.env };+ delete env.COVERAGE;+ if (coverage !== undefined) {+ env.COVERAGE = coverage;+ }+ const result = spawnSync(+ process.execPath,+ ['-e', `process.stdout.write(String(require(${JSON.stringify(config)}).collectCoverage))`],+ { cwd: repoRoot, encoding: 'utf8', env },+ );+ expect(result.status, result.stderr).toBe(0);+ return result.stdout.trim();+ };+ expect(read()).toBe('false');+ expect(read('true')).toBe('true');++ const manifest = readFileSync(resolve(repoRoot, 'client/package.json'), 'utf8');+ const scripts = (JSON.parse(manifest) as { scripts: Record<string, string> }).scripts;+ expect(scripts['test:ci']).toContain('COVERAGE=true');+ });+});diff --git a/package.json b/package.jsonindex a8d83d848f..a7f2a65b81 100644--- a/package.json+++ b/package.json@@ -109,6 +109,7 @@"test:packages:data-schemas": "cd packages/data-schemas && npm run test:ci","test:config": "jest --config config/jest.config.js","test:all": "npm run test:client && npm run test:api && npm run test:packages:api && npm run test:packages:data-provider && npm run test:packages:data-schemas",+ "test:turbo": "turbo run test:ci --concurrency=2 --filter=@librechat/frontend --filter=@librechat/client --filter=@librechat/backend --filter=@librechat/api --filter=librechat-data-provider --filter=@librechat/data-schemas","e2e:update": "npm run e2e:prepare && playwright test --config=e2e/playwright.config.local.ts --update-snapshots","e2e:report": "npx playwright show-report e2e/playwright-report","lint:fix": "npm run build:client-package && eslint . --fix --pass-on-unpruned-suppressions",diff --git a/turbo.json b/turbo.jsonindex 09459aa69e..9a3ee708a4 100644--- a/turbo.json+++ b/turbo.json@@ -22,14 +22,54 @@],"outputs": ["dist/**"]},+ "test:ci": {+ "dependsOn": ["^build"],+ "cache": false,+ "passThroughEnv": ["*"]+ },+ "@librechat/data-schemas#test:ci": {+ "dependsOn": ["^build", "librechat-data-provider#build"],+ "cache": false,+ "passThroughEnv": ["*"]+ },+ "@librechat/api#test:ci": {+ "dependsOn": ["^build", "librechat-data-provider#build", "@librechat/data-schemas#build"],+ "cache": false,+ "passThroughEnv": ["*"]+ },"@librechat/data-schemas#build": {- "dependsOn": ["^build", "librechat-data-provider#build"]+ "dependsOn": ["^build", "librechat-data-provider#build"],+ "inputs": [+ "$TURBO_DEFAULT$",+ "!**/__tests__/**",+ "!**/__mocks__/**",+ "!**/*.test.*",+ "!**/*.spec.*"+ ],+ "outputs": ["dist/**"]},"@librechat/api#build": {- "dependsOn": ["^build", "librechat-data-provider#build", "@librechat/data-schemas#build"]+ "dependsOn": ["^build", "librechat-data-provider#build", "@librechat/data-schemas#build"],+ "inputs": [+ "$TURBO_DEFAULT$",+ "!**/__tests__/**",+ "!**/__mocks__/**",+ "!**/*.test.*",+ "!**/*.spec.*"+ ],+ "outputs": ["dist/**"]},"@librechat/client#build": {- "dependsOn": ["^build", "librechat-data-provider#build"]+ "dependsOn": ["^build", "librechat-data-provider#build"],+ "inputs": [+ "$TURBO_DEFAULT$",+ "!**/__tests__/**",+ "!**/__mocks__/**",+ "!**/*.test.*",+ "!**/*.spec.*"+ ],+ "outputs": ["dist/**"],+ "env": ["NODE_ENV", "VITE_ENABLE_LOGGER", "VITE_LOGGER_FILTER"]}}}