mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-03 14:50:19 +01:00
📅 refactor: Replace Numeric Weekday Index with Named Day in Date Template Variables (#12022)
* feat(data-provider): include timezone and weekday label in current_datetime
* fix(data-provider): use named weekday for both date variables and single dayjs instance
Use a single `const now = dayjs()` instead of 5 separate instantiations,
apply named weekday to `{{current_date}}` (not just `{{current_datetime}}`),
simplify weekday format from `(weekday=Monday)` to `(Monday)`, and
harden test mock fallback to throw on unhandled format strings.
* chore(data-provider): remove dead day() mock from parsers spec
---------
Co-authored-by: Peter Rothlaender <peter.rothlaender@ginkgo.com>
This commit is contained in:
parent
a0a1749151
commit
2a5123bfa1
2 changed files with 22 additions and 21 deletions
|
|
@ -7,22 +7,24 @@ import type { TUser, TConversation } from '../src/types';
|
|||
|
||||
// Mock dayjs module with consistent date/time values regardless of environment
|
||||
jest.mock('dayjs', () => {
|
||||
// Create a mock implementation that returns fixed values
|
||||
const mockDayjs = () => ({
|
||||
format: (format: string) => {
|
||||
if (format === 'YYYY-MM-DD') {
|
||||
return '2024-04-29';
|
||||
}
|
||||
if (format === 'YYYY-MM-DD HH:mm:ss') {
|
||||
return '2024-04-29 12:34:56';
|
||||
if (format === 'YYYY-MM-DD HH:mm:ss Z') {
|
||||
return '2024-04-29 12:34:56 -04:00';
|
||||
}
|
||||
return format; // fallback
|
||||
if (format === 'dddd') {
|
||||
return 'Monday';
|
||||
}
|
||||
throw new Error(
|
||||
`Unhandled dayjs().format() call in mock: "${format}". Update the mock in parsers.spec.ts`,
|
||||
);
|
||||
},
|
||||
day: () => 1, // 1 = Monday
|
||||
toISOString: () => '2024-04-29T16:34:56.000Z',
|
||||
});
|
||||
|
||||
// Add any static methods needed
|
||||
mockDayjs.extend = jest.fn();
|
||||
|
||||
return mockDayjs;
|
||||
|
|
@ -47,13 +49,12 @@ describe('replaceSpecialVars', () => {
|
|||
|
||||
test('should replace {{current_date}} with the current date', () => {
|
||||
const result = replaceSpecialVars({ text: 'Today is {{current_date}}' });
|
||||
// dayjs().day() returns 1 for Monday (April 29, 2024 is a Monday)
|
||||
expect(result).toBe('Today is 2024-04-29 (1)');
|
||||
expect(result).toBe('Today is 2024-04-29 (Monday)');
|
||||
});
|
||||
|
||||
test('should replace {{current_datetime}} with the current datetime', () => {
|
||||
const result = replaceSpecialVars({ text: 'Now is {{current_datetime}}' });
|
||||
expect(result).toBe('Now is 2024-04-29 12:34:56 (1)');
|
||||
expect(result).toBe('Now is 2024-04-29 12:34:56 -04:00 (Monday)');
|
||||
});
|
||||
|
||||
test('should replace {{iso_datetime}} with the ISO datetime', () => {
|
||||
|
|
@ -90,7 +91,7 @@ describe('replaceSpecialVars', () => {
|
|||
user: mockUser,
|
||||
});
|
||||
expect(result).toBe(
|
||||
'Hello Test User! Today is 2024-04-29 (1) and the time is 2024-04-29 12:34:56 (1). ISO: 2024-04-29T16:34:56.000Z',
|
||||
'Hello Test User! Today is 2024-04-29 (Monday) and the time is 2024-04-29 12:34:56 -04:00 (Monday). ISO: 2024-04-29T16:34:56.000Z',
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -99,7 +100,7 @@ describe('replaceSpecialVars', () => {
|
|||
text: 'Date: {{CURRENT_DATE}}, User: {{Current_User}}',
|
||||
user: mockUser,
|
||||
});
|
||||
expect(result).toBe('Date: 2024-04-29 (1), User: Test User');
|
||||
expect(result).toBe('Date: 2024-04-29 (Monday), User: Test User');
|
||||
});
|
||||
|
||||
test('should confirm all specialVariables from config.ts get parsed', () => {
|
||||
|
|
@ -120,8 +121,8 @@ describe('replaceSpecialVars', () => {
|
|||
});
|
||||
|
||||
// Verify the expected replacements
|
||||
expect(result).toContain('2024-04-29 (1)'); // current_date
|
||||
expect(result).toContain('2024-04-29 12:34:56 (1)'); // current_datetime
|
||||
expect(result).toContain('2024-04-29 (Monday)'); // current_date
|
||||
expect(result).toContain('2024-04-29 12:34:56 -04:00 (Monday)'); // current_datetime
|
||||
expect(result).toContain('2024-04-29T16:34:56.000Z'); // iso_datetime
|
||||
expect(result).toContain('Test User'); // current_user
|
||||
});
|
||||
|
|
|
|||
|
|
@ -408,16 +408,16 @@ export function replaceSpecialVars({ text, user }: { text: string; user?: t.TUse
|
|||
return result;
|
||||
}
|
||||
|
||||
// e.g., "2024-04-29 (1)" (1=Monday)
|
||||
const currentDate = dayjs().format('YYYY-MM-DD');
|
||||
const dayNumber = dayjs().day();
|
||||
const combinedDate = `${currentDate} (${dayNumber})`;
|
||||
result = result.replace(/{{current_date}}/gi, combinedDate);
|
||||
const now = dayjs();
|
||||
const weekdayName = now.format('dddd');
|
||||
|
||||
const currentDatetime = dayjs().format('YYYY-MM-DD HH:mm:ss');
|
||||
result = result.replace(/{{current_datetime}}/gi, `${currentDatetime} (${dayNumber})`);
|
||||
const currentDate = now.format('YYYY-MM-DD');
|
||||
result = result.replace(/{{current_date}}/gi, `${currentDate} (${weekdayName})`);
|
||||
|
||||
const isoDatetime = dayjs().toISOString();
|
||||
const currentDatetime = now.format('YYYY-MM-DD HH:mm:ss Z');
|
||||
result = result.replace(/{{current_datetime}}/gi, `${currentDatetime} (${weekdayName})`);
|
||||
|
||||
const isoDatetime = now.toISOString();
|
||||
result = result.replace(/{{iso_datetime}}/gi, isoDatetime);
|
||||
|
||||
if (user && user.name) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue