|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { computeCost } from './energy-computer.service'; // Adjust the import path as needed |
| 4 | + |
| 5 | +describe('computeCost', () => { |
| 6 | + it('should calculate the correct cost for valid inputs', () => { |
| 7 | + const wattage = 1000; // 1000 watts = 1 kW |
| 8 | + const duration = 5; // 5 hours |
| 9 | + const kWhCost = 0.12; // $0.12 per kWh |
| 10 | + const result = computeCost(wattage, duration, kWhCost); |
| 11 | + expect(result).toBeCloseTo(0.60); // 1 kW * 5h * 0.12 = 0.60 |
| 12 | + }); |
| 13 | + |
| 14 | + it('should return 0 when the duration is 0', () => { |
| 15 | + const wattage = 1000; |
| 16 | + const duration = 0; |
| 17 | + const kWhCost = 0.12; |
| 18 | + const result = computeCost(wattage, duration, kWhCost); |
| 19 | + expect(result).toBe(0); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return 0 when the wattage is 0', () => { |
| 23 | + const wattage = 0; |
| 24 | + const duration = 5; |
| 25 | + const kWhCost = 0.12; |
| 26 | + const result = computeCost(wattage, duration, kWhCost); |
| 27 | + expect(result).toBe(0); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return 0 when the cost per kWh is 0', () => { |
| 31 | + const wattage = 1000; |
| 32 | + const duration = 5; |
| 33 | + const kWhCost = 0; |
| 34 | + const result = computeCost(wattage, duration, kWhCost); |
| 35 | + expect(result).toBe(0); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should handle fractional wattage and duration correctly', () => { |
| 39 | + const wattage = 750; // 0.75 kW |
| 40 | + const duration = 2.5; // 2.5 hours |
| 41 | + const kWhCost = 0.10; // $0.10 per kWh |
| 42 | + const result = computeCost(wattage, duration, kWhCost); |
| 43 | + expect(result).toBeCloseTo(0.1875); // 0.75 kW * 2.5h * 0.10 = 0.1875 |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle large numbers correctly', () => { |
| 47 | + const wattage = 1000000; // 1 MW |
| 48 | + const duration = 24; // 24 hours |
| 49 | + const kWhCost = 0.15; // $0.15 per kWh |
| 50 | + const result = computeCost(wattage, duration, kWhCost); |
| 51 | + expect(result).toBe(3600); // 1000 kW * 24h * 0.15 = 3600 |
| 52 | + }); |
| 53 | +}); |
0 commit comments