Spaces:
Running
Running
const assert = require("assert"); | |
const { numberToWords, calculateQuotation } = require("../script.js"); | |
// Test cases for Indian number-to-words conversion (Crore/Lakh system) | |
const cases = [ | |
{ num: 0, expected: "Zero" }, | |
{ num: 5, expected: "Five" }, | |
{ num: 15, expected: "Fifteen" }, | |
{ num: 75, expected: "Seventy Five" }, | |
{ num: 100, expected: "One Hundred" }, | |
{ num: 569, expected: "Five Hundred and Sixty Nine" }, | |
{ num: 1000, expected: "One Thousand" }, | |
{ num: 1100, expected: "One Thousand One Hundred" }, | |
{ num: 1234, expected: "One Thousand Two Hundred and Thirty Four" }, | |
{ num: 10000, expected: "Ten Thousand" }, | |
{ num: 54000, expected: "Fifty Four Thousand" }, | |
{ num: 100000, expected: "One Lakh" }, | |
{ num: 510000, expected: "Five Lakh Ten Thousand" }, | |
{ | |
num: 9999999, | |
expected: | |
"Ninety Nine Lakh Ninety Nine Thousand Nine Hundred and Ninety Nine", | |
}, | |
{ num: 10000000, expected: "One Crore" }, | |
{ num: 12500000, expected: "One Crore Twenty Five Lakh" }, | |
]; | |
cases.forEach(({ num, expected }) => { | |
const actual = numberToWords(num); | |
assert.strictEqual( | |
actual, | |
expected, | |
`${num} => "${actual}" (expected "${expected}")`, | |
); | |
}); | |
console.log("β All numberToWords tests passed"); | |
// --- Tests for calculateQuotation --- | |
const calculationCases = [ | |
{ | |
name: "Basic case with one item", | |
items: [{ amount: 100 }], | |
igstRate: 18, | |
freightCharges: 50, | |
expected: { | |
subtotal: 100, | |
igstAmount: 18, | |
total: 168, | |
finalTotal: 168, | |
rOff: 0, | |
}, | |
}, | |
{ | |
name: "Multiple items", | |
items: [{ amount: 100 }, { amount: 250.5 }], | |
igstRate: 18, | |
freightCharges: 50, | |
expected: { | |
subtotal: 350.5, | |
igstAmount: 63.09, | |
total: 463.59, | |
finalTotal: 464, | |
rOff: -0.41, | |
}, | |
}, | |
{ | |
name: "No IGST or freight", | |
items: [{ amount: 500 }], | |
igstRate: 0, | |
freightCharges: 0, | |
expected: { | |
subtotal: 500, | |
igstAmount: 0, | |
total: 500, | |
finalTotal: 500, | |
rOff: 0, | |
}, | |
}, | |
{ | |
name: "Rounding down", | |
items: [{ amount: 99.2 }], | |
igstRate: 10, // 9.92 | |
freightCharges: 0, // total = 109.12 | |
expected: { | |
subtotal: 99.2, | |
igstAmount: 9.92, | |
total: 109.12, | |
finalTotal: 109, | |
rOff: 0.12, | |
}, | |
}, | |
]; | |
calculationCases.forEach( | |
({ name, items, igstRate, freightCharges, expected }) => { | |
const actual = calculateQuotation(items, igstRate, freightCharges); | |
// Need to compare floating point numbers with a tolerance | |
Object.keys(expected).forEach((key) => { | |
assert( | |
Math.abs(actual[key] - expected[key]) < 0.001, | |
`[${name}] ${key} => ${actual[key]} (expected ${expected[key]})`, | |
); | |
}); | |
}, | |
); | |
console.log("β All calculateQuotation tests passed"); | |