LibreChat/client/src/utils/__tests__/cleanupPreset.integration.test.ts
Danny Avila 467df0f07a
🎭 feat: Override Custom Endpoint Schema with Specified Params Endpoint (#11788)
* 🔧 refactor: Simplify payload parsing and enhance getSaveOptions logic

- Removed unused bedrockInputSchema from payloadParser, streamlining the function.
- Updated payloadParser to handle optional chaining for model parameters.
- Enhanced getSaveOptions to ensure runOptions defaults to an empty object if parsing fails, improving robustness.
- Adjusted the assignment of maxContextTokens to use the instance variable for consistency.

* 🔧 fix: Update maxContextTokens assignment logic in initializeAgent function

- Enhanced the maxContextTokens assignment to allow for user-defined values, ensuring it defaults to a calculated value only when not provided or invalid. This change improves flexibility in agent initialization.

* 🧪 test: Add unit tests for initializeAgent function

- Introduced comprehensive unit tests for the initializeAgent function, focusing on maxContextTokens behavior.
- Tests cover scenarios for user-defined values, fallback calculations, and edge cases such as zero and negative values, enhancing overall test coverage and reliability of agent initialization logic.

* refactor: default params Endpoint Configuration Handling

- Integrated `getEndpointsConfig` to fetch endpoint configurations, allowing for dynamic handling of `defaultParamsEndpoint`.
- Updated `buildEndpointOption` to pass `defaultParamsEndpoint` to `parseCompactConvo`, ensuring correct parameter handling based on endpoint type.
- Added comprehensive unit tests for `buildDefaultConvo` and `cleanupPreset` to validate behavior with `defaultParamsEndpoint`, covering various scenarios and edge cases.
- Refactored related hooks and utility functions to support the new configuration structure, improving overall flexibility and maintainability.

* refactor: Centralize defaultParamsEndpoint retrieval

- Introduced `getDefaultParamsEndpoint` function to streamline the retrieval of `defaultParamsEndpoint` across various hooks and middleware.
- Updated multiple files to utilize the new function, enhancing code consistency and maintainability.
- Removed redundant logic for fetching `defaultParamsEndpoint`, simplifying the codebase.
2026-02-13 23:04:51 -05:00

119 lines
3.4 KiB
TypeScript

import { EModelEndpoint } from 'librechat-data-provider';
import cleanupPreset from '../cleanupPreset';
/**
* Integration tests for cleanupPreset — NO mocks.
* Uses the real parseConvo to verify actual schema behavior
* with defaultParamsEndpoint for custom endpoints.
*/
describe('cleanupPreset - real parsing with defaultParamsEndpoint', () => {
it('should preserve maxOutputTokens when defaultParamsEndpoint is anthropic', () => {
const preset = {
presetId: 'test-id',
title: 'Claude Opus',
endpoint: 'AnthropicClaude',
endpointType: EModelEndpoint.custom,
model: 'anthropic/claude-opus-4.5',
temperature: 0.7,
maxOutputTokens: 8192,
topP: 0.9,
maxContextTokens: 50000,
};
const result = cleanupPreset({
preset,
defaultParamsEndpoint: EModelEndpoint.anthropic,
});
expect(result.maxOutputTokens).toBe(8192);
expect(result.topP).toBe(0.9);
expect(result.temperature).toBe(0.7);
expect(result.maxContextTokens).toBe(50000);
expect(result.model).toBe('anthropic/claude-opus-4.5');
});
it('should strip maxOutputTokens without defaultParamsEndpoint (OpenAI schema)', () => {
const preset = {
presetId: 'test-id',
title: 'GPT Custom',
endpoint: 'MyOpenRouter',
endpointType: EModelEndpoint.custom,
model: 'gpt-4o',
temperature: 0.7,
maxOutputTokens: 8192,
max_tokens: 4096,
};
const result = cleanupPreset({ preset });
expect(result.maxOutputTokens).toBeUndefined();
expect(result.max_tokens).toBe(4096);
expect(result.temperature).toBe(0.7);
});
it('should strip OpenAI-specific fields when using anthropic params', () => {
const preset = {
presetId: 'test-id',
title: 'Claude Custom',
endpoint: 'AnthropicClaude',
endpointType: EModelEndpoint.custom,
model: 'anthropic/claude-3-opus',
max_tokens: 4096,
top_p: 0.9,
presence_penalty: 0.5,
frequency_penalty: 0.3,
temperature: 0.7,
};
const result = cleanupPreset({
preset,
defaultParamsEndpoint: EModelEndpoint.anthropic,
});
expect(result.max_tokens).toBeUndefined();
expect(result.top_p).toBeUndefined();
expect(result.presence_penalty).toBeUndefined();
expect(result.frequency_penalty).toBeUndefined();
expect(result.temperature).toBe(0.7);
});
it('should not carry bedrock region to custom endpoint', () => {
const preset = {
presetId: 'test-id',
title: 'Custom',
endpoint: 'MyEndpoint',
endpointType: EModelEndpoint.custom,
model: 'gpt-4o',
temperature: 0.7,
region: 'us-east-1',
};
const result = cleanupPreset({ preset });
expect(result.region).toBeUndefined();
expect(result.temperature).toBe(0.7);
});
it('should preserve Google-specific fields when defaultParamsEndpoint is google', () => {
const preset = {
presetId: 'test-id',
title: 'Gemini Custom',
endpoint: 'MyGoogleEndpoint',
endpointType: EModelEndpoint.custom,
model: 'gemini-pro',
temperature: 0.7,
maxOutputTokens: 8192,
topP: 0.9,
topK: 40,
};
const result = cleanupPreset({
preset,
defaultParamsEndpoint: EModelEndpoint.google,
});
expect(result.maxOutputTokens).toBe(8192);
expect(result.topP).toBe(0.9);
expect(result.topK).toBe(40);
});
});