From 2a5123bfa1481f9f1059c57c508ba02a742efadd Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Mon, 2 Mar 2026 19:22:11 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=85=20refactor:=20Replace=20Numeric=20?= =?UTF-8?q?Weekday=20Index=20with=20Named=20Day=20in=20Date=20Template=20V?= =?UTF-8?q?ariables=20(#12022)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- packages/data-provider/specs/parsers.spec.ts | 27 ++++++++++---------- packages/data-provider/src/parsers.ts | 16 ++++++------ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/data-provider/specs/parsers.spec.ts b/packages/data-provider/specs/parsers.spec.ts index 90359d6e6f..83c3500922 100644 --- a/packages/data-provider/specs/parsers.spec.ts +++ b/packages/data-provider/specs/parsers.spec.ts @@ -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 }); diff --git a/packages/data-provider/src/parsers.ts b/packages/data-provider/src/parsers.ts index ab6ff927f7..3ec4221b62 100644 --- a/packages/data-provider/src/parsers.ts +++ b/packages/data-provider/src/parsers.ts @@ -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) {