LibreChat/api/server/services/Threads/manage.spec.js
Danny Avila ab6cde0c79
⏳ fix: Separate Temporary and General Chat Retention (#15734)
* fix: separate temporary and general chat retention

* fix: inherit retention when branching messages

* fix: harden retention lifecycle boundaries

* fix: preserve subagent retention context

* fix: trust stored chat retention for uploads

* perf: overlap generated file retention lookup

* fix: derive retention from trusted chat state

* fix: preserve retention across the full turn lifecycle

* test: accept the conversation retention read adapter

* fix: carry retention deadlines through durable job saves

* fix: resolve retention at cached and new chat admission

* fix: resolve direct tool retention before execution

* fix: preserve retention across assistant writes and direct tools
2026-09-08 11:22:54 -04:00

91 lines
2.4 KiB
JavaScript

/**
* Tests for recordUsage - the assistants-side token spend path.
*
* `createTransaction` reads the guard out of caller-supplied data, so an
* omitted `transactions` is indistinguishable from an enabled one and the
* write proceeds even when the resolved config says `enabled: false`.
*/
const mockSpendTokens = jest.fn().mockResolvedValue();
jest.mock('@librechat/api', () => ({
countTokens: jest.fn().mockResolvedValue(0),
}));
jest.mock('@librechat/data-schemas', () => ({
escapeRegExp: jest.fn((str) => str),
}));
jest.mock('~/models', () => ({
saveMessage: jest.fn(),
getMessages: jest.fn(),
saveConvo: jest.fn(),
spendTokens: (...args) => mockSpendTokens(...args),
}));
jest.mock('~/server/services/Files/process', () => ({
retrieveAndProcessFile: jest.fn(),
}));
const { recordUsage } = require('./manage');
describe('recordUsage', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('forwards the resolved transactions config to spendTokens', async () => {
await recordUsage({
prompt_tokens: 100,
completion_tokens: 50,
model: 'gpt-4',
user: 'user-123',
conversationId: 'convo-123',
transactions: { enabled: false },
});
expect(mockSpendTokens).toHaveBeenCalledTimes(1);
expect(mockSpendTokens).toHaveBeenCalledWith(
{
user: 'user-123',
model: 'gpt-4',
context: 'message',
conversationId: 'convo-123',
transactions: { enabled: false },
},
{ promptTokens: 100, completionTokens: 50 },
);
});
it('forwards the config alongside an explicit context', async () => {
await recordUsage({
prompt_tokens: 10,
completion_tokens: 5,
model: 'gpt-4',
user: 'user-123',
conversationId: 'convo-123',
context: 'incomplete',
transactions: { enabled: true },
});
expect(mockSpendTokens).toHaveBeenCalledWith(
expect.objectContaining({ context: 'incomplete', transactions: { enabled: true } }),
expect.any(Object),
);
});
it('leaves the call unchanged when no config is supplied', async () => {
await recordUsage({
prompt_tokens: 10,
completion_tokens: 5,
model: 'gpt-4',
user: 'user-123',
conversationId: 'convo-123',
});
expect(mockSpendTokens).toHaveBeenCalledWith(
expect.objectContaining({ transactions: undefined }),
expect.any(Object),
);
});
});