File size: 2,638 Bytes
063f5af
409f62b
063f5af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c96d1
409f62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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.50 }],
    igstRate: 18,
    freightCharges: 50,
    expected: { subtotal: 350.50, 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.20 }],
    igstRate: 10, // 9.92
    freightCharges: 0, // total = 109.12
    expected: { subtotal: 99.20, 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');