From 3e3f905ea50bbabbe38ad3ddfc4e013a3ff446c4 Mon Sep 17 00:00:00 2001 From: Alexander Rossmanith Date: Mon, 20 Jul 2026 21:42:27 +0530 Subject: [PATCH] test: add unit tests for getOrdinalDay function --- src/calendar.mjs | 41 +++++++++++++++++++++++++++++++++++++++++ src/index.mjs | 41 +---------------------------------------- tests/calendar.spec.mjs | 29 ++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 41 deletions(-) diff --git a/src/calendar.mjs b/src/calendar.mjs index d0a45f8..2fef6a5 100644 --- a/src/calendar.mjs +++ b/src/calendar.mjs @@ -28,3 +28,44 @@ export function getMalayalamDate(gregorianDate) { naksatra: naksatraName.trim() }; } + + +/** + * @returns {Date} - Current date and time in IST (Indian Standard Time) + */ +export function getIstDate() { + // Construct date string formatted explicitly for India timezone + const now = new Date(); + const formatter = new Intl.DateTimeFormat('en-US', { + timeZone: 'Asia/Kolkata', + year: 'numeric', + month: '2-digit', + day: '2-digit' + }); + + // Formats into MM/DD/YYYY in IST + const [{ value: month }, , { value: day }, , { value: year }] = formatter.formatToParts(now); + + // Create UTC date anchored at 0:00:00 for that IST date + // Month is 0-indexed in JavaScript Date constructor, so subtract 1. + // Year and date don't need adjustment. + return new Date(Date.UTC(year, month - 1, day, 0, 0, 0)); +} + + +/** + * Converts a number to its ordinal string representation (1st, 2nd, 3rd, 4th, etc.) + * @param {number} day - The day number (1-31) + * @returns {string} - Ordinal string (e.g., "4th") + */ +export function getOrdinalDay(day) { + const remainder = day % 100; + if (remainder >= 11 && remainder <= 13) return `${day}th`; + + const lastDigit = day % 10; + if (lastDigit === 1) return `${day}st`; + if (lastDigit === 2) return `${day}nd`; + if (lastDigit === 3) return `${day}rd`; + + return `${day}th`; +} diff --git a/src/index.mjs b/src/index.mjs index 2077072..4f11288 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,5 +1,5 @@ import Alexa from 'ask-sdk-core'; -import { getMalayalamDate } from './calendar.mjs'; +import { getMalayalamDate, getOrdinalDay, getIstDate } from './calendar.mjs'; // 1. Core Logic: Handles explicit date/star question requests const GetMalayalamDateIntentHandler = { @@ -74,45 +74,6 @@ const ErrorHandler = { } }; -// Get current gregorian date according to Indian Standard Time (IST) -function getIstDate() { - // Construct date string formatted explicitly for India timezone - const now = new Date(); - const formatter = new Intl.DateTimeFormat('en-US', { - timeZone: 'Asia/Kolkata', - year: 'numeric', - month: '2-digit', - day: '2-digit' - }); - - // Formats into MM/DD/YYYY in IST - const [{ value: month }, , { value: day }, , { value: year }] = formatter.formatToParts(now); - - // Create UTC date anchored at 0:00:00 for that IST date - // Month is 0-indexed in JavaScript Date constructor, so subtract 1. - // Year and date don't need adjustment. - return new Date(Date.UTC(year, month - 1, day, 0, 0, 0)); -} - - -/** - * Converts a number to its ordinal string representation (1st, 2nd, 3rd, 4th, etc.) - * @param {number} day - The day number (1-31) - * @returns {string} - Ordinal string (e.g., "4th") - */ -function getOrdinalDay(day) { - const remainder = day % 100; - if (remainder >= 11 && remainder <= 13) return `${day}th`; - - const lastDigit = day % 10; - if (lastDigit === 1) return `${day}st`; - if (lastDigit === 2) return `${day}nd`; - if (lastDigit === 3) return `${day}rd`; - - return `${day}th`; -} - - // 3. Central Response Builder function generateCalendarResponse(handlerInput) { const date = getMalayalamDate(getIstDate()); diff --git a/tests/calendar.spec.mjs b/tests/calendar.spec.mjs index 7e1d0c3..80b6a12 100644 --- a/tests/calendar.spec.mjs +++ b/tests/calendar.spec.mjs @@ -1,4 +1,4 @@ -import { getMalayalamDate } from '../src/calendar.mjs'; +import { getMalayalamDate, getOrdinalDay } from '../src/calendar.mjs'; import assert from 'node:assert'; describe('getMalayalamDate unit test', () => { @@ -12,3 +12,30 @@ describe('getMalayalamDate unit test', () => { }); }); }); + +describe('getOrdinalDay function unit test', () => { + it('Returns correct ordinal for various days', () => { + const testCases = [ + { day: 1, expected: '1st' }, + { day: 2, expected: '2nd' }, + { day: 3, expected: '3rd' }, + { day: 4, expected: '4th' }, + { day: 5, expected: '5th' }, + { day: 11, expected: '11th' }, + { day: 12, expected: '12th' }, + { day: 13, expected: '13th' }, + { day: 14, expected: '14th' }, + { day: 20, expected: '20th' }, + { day: 21, expected: '21st' }, + { day: 22, expected: '22nd' }, + { day: 23, expected: '23rd' }, + { day: 24, expected: '24th' }, + { day: 30, expected: '30th' }, + { day: 31, expected: '31st' } + ]; + + testCases.forEach(({ day, expected }) => { + assert.strictEqual(getOrdinalDay(day), expected); + }); + }); +});