test: add unit tests for getOrdinalDay function

This commit is contained in:
2026-07-20 21:42:27 +05:30
parent 1ac9ead1d5
commit 3e3f905ea5
3 changed files with 70 additions and 41 deletions
+41
View File
@@ -28,3 +28,44 @@ export function getMalayalamDate(gregorianDate) {
naksatra: naksatraName.trim() 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`;
}
+1 -40
View File
@@ -1,5 +1,5 @@
import Alexa from 'ask-sdk-core'; 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 // 1. Core Logic: Handles explicit date/star question requests
const GetMalayalamDateIntentHandler = { 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 // 3. Central Response Builder
function generateCalendarResponse(handlerInput) { function generateCalendarResponse(handlerInput) {
const date = getMalayalamDate(getIstDate()); const date = getMalayalamDate(getIstDate());
+28 -1
View File
@@ -1,4 +1,4 @@
import { getMalayalamDate } from '../src/calendar.mjs'; import { getMalayalamDate, getOrdinalDay } from '../src/calendar.mjs';
import assert from 'node:assert'; import assert from 'node:assert';
describe('getMalayalamDate unit test', () => { 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);
});
});
});