diff --git a/packages/data-provider/specs/parsers.spec.ts b/packages/data-provider/specs/parsers.spec.ts index 522619a3b9..b980a5bc6a 100644 --- a/packages/data-provider/specs/parsers.spec.ts +++ b/packages/data-provider/specs/parsers.spec.ts @@ -7,7 +7,6 @@ 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') { @@ -19,13 +18,14 @@ jest.mock('dayjs', () => { if (format === 'dddd') { return 'Monday'; } - return format; // fallback + throw new Error( + `Unhandled dayjs().format() call in mock: "${format}". Update the mock in parsers.spec.ts`, + ); }, - day: () => 1, // 1 = Monday + day: () => 1, toISOString: () => '2024-04-29T16:34:56.000Z', }); - // Add any static methods needed mockDayjs.extend = jest.fn(); return mockDayjs; @@ -50,13 +50,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 -04:00 (weekday=Monday)'); + expect(result).toBe('Now is 2024-04-29 12:34:56 -04:00 (Monday)'); }); test('should replace {{iso_datetime}} with the ISO datetime', () => { @@ -93,7 +92,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 -04:00 (weekday=Monday). 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', ); }); @@ -102,7 +101,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', () => { @@ -123,8 +122,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 -04:00 (weekday=Monday)'); // 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 }); diff --git a/packages/data-provider/src/parsers.ts b/packages/data-provider/src/parsers.ts index 6fc28fff9c..3ec4221b62 100644 --- a/packages/data-provider/src/parsers.ts +++ b/packages/data-provider/src/parsers.ts @@ -408,17 +408,16 @@ export function replaceSpecialVars({ text, user }: { text: string; user?: t.TUse return result; } - // e.g., "2024-04-29 (1)" where dayjs().day() uses 0=Sunday..6=Saturday - 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 weekdayName = dayjs().format('dddd'); - const currentDatetime = dayjs().format('YYYY-MM-DD HH:mm:ss Z'); - result = result.replace(/{{current_datetime}}/gi, `${currentDatetime} (weekday=${weekdayName})`); + 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) {