LibreChat/api/server/services/Schedules/index.spec.js
lia-by-librechat[bot] 0eadbc45b1
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 Vote Gate / Gate (push) Waiting to run
Codegraph E2E Votes / vote (shard 1/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 10/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 2/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 3/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 4/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 5/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 6/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 7/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 8/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 9/10) (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
🪸 refactor: Separate Shared-Link and Schedule Persistence (#18031)
Co-authored-by: Lia <lia@librechat.ai>
2026-11-10 15:44:13 -05:00

90 lines
3.2 KiB
JavaScript

const mockRecordScheduleOutcome = jest.fn();
const mockCreateSchedulesService = jest.fn(() => ({
recordScheduleOutcome: mockRecordScheduleOutcome,
}));
jest.mock('@librechat/api', () => ({
createSchedulesService: (...args) => mockCreateSchedulesService(...args),
}));
jest.mock('~/server/services/Config/app', () => ({ getAppConfig: jest.fn() }));
jest.mock('~/server/services/Agents/triggers', () => ({ enqueueAgentTrigger: jest.fn() }));
jest.mock('./mcp', () => jest.fn());
jest.mock('./access', () => ({ resolveAgentFireAccess: jest.fn() }));
jest.mock('~/models', () => ({
isAgentTriggerPrincipalActive: jest.fn(),
findScheduleOwner: jest.fn(),
initializeNullBalance: jest.fn(),
}));
const { recordExpiredScheduleApproval } = require('./index');
describe('recordExpiredScheduleApproval', () => {
beforeEach(() => {
mockRecordScheduleOutcome.mockReset().mockResolvedValue(true);
});
it('ignores ordinary generation approvals', async () => {
await expect(
recordExpiredScheduleApproval('conversation-1', { createdAt: 1000 }),
).resolves.toBeUndefined();
expect(mockRecordScheduleOutcome).not.toHaveBeenCalled();
});
it('settles the exact scheduled generation as interrupted', async () => {
await recordExpiredScheduleApproval('conversation-1', {
createdAt: 1000,
conversationId: 'conversation-1',
scheduleId: 'schedule-1',
scheduledFor: '2026-08-17T12:00:00.000Z',
});
expect(mockRecordScheduleOutcome).toHaveBeenCalledWith({
scheduleId: 'schedule-1',
scheduledFor: '2026-08-17T12:00:00.000Z',
streamId: 'conversation-1',
jobCreatedAt: 1000,
status: 'interrupted',
conversationId: 'conversation-1',
error: 'Approval expired before a decision was made',
});
});
it('surfaces a failed durable settlement instead of reporting false success', async () => {
mockRecordScheduleOutcome.mockResolvedValue(false);
await expect(
recordExpiredScheduleApproval('conversation-1', {
createdAt: 1000,
scheduleId: 'schedule-1',
scheduledFor: '2026-08-17T12:00:00.000Z',
}),
).rejects.toThrow('Failed to settle expired scheduled approval schedule-1');
});
});
describe('schedule owner lookup', () => {
it('selects the terms acceptance flag the fire-time terms check reads', async () => {
const methods = require('~/models');
const owner = { _id: 'user-1', termsAccepted: false };
methods.findScheduleOwner.mockResolvedValue(owner);
mockRecordScheduleOutcome.mockResolvedValue(true);
let facade;
jest.isolateModules(() => {
facade = require('./index');
});
// The facade builds the service on first use and hands it the host adapters.
await facade.recordExpiredScheduleApproval('conversation-1', {
createdAt: 1000,
scheduleId: 'schedule-1',
scheduledFor: '2026-08-17T12:00:00.000Z',
});
const { findUserById } = mockCreateSchedulesService.mock.calls.at(-1)[0];
await expect(findUserById('user-1')).resolves.toEqual(owner);
expect(methods.findScheduleOwner).toHaveBeenCalledWith('user-1');
expect(mockCreateSchedulesService.mock.calls.at(-1)[0].initializeNullBalance).toBe(
methods.initializeNullBalance,
);
});
});