diff --git a/src/tools/energy-computer/energy-computer.service.test.ts b/src/tools/energy-computer/energy-computer.service.test.ts new file mode 100644 index 00000000..a010df05 --- /dev/null +++ b/src/tools/energy-computer/energy-computer.service.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from 'vitest'; + +import { computeCost } from './energy-computer.service'; // Adjust the import path as needed + +describe('computeCost', () => { + it('should calculate the correct cost for valid inputs', () => { + const wattage = 1000; // 1000 watts = 1 kW + const duration = 5; // 5 hours + const kWhCost = 0.12; // $0.12 per kWh + const result = computeCost(wattage, duration, kWhCost); + expect(result).toBeCloseTo(0.60); // 1 kW * 5h * 0.12 = 0.60 + }); + + it('should return 0 when the duration is 0', () => { + const wattage = 1000; + const duration = 0; + const kWhCost = 0.12; + const result = computeCost(wattage, duration, kWhCost); + expect(result).toBe(0); + }); + + it('should return 0 when the wattage is 0', () => { + const wattage = 0; + const duration = 5; + const kWhCost = 0.12; + const result = computeCost(wattage, duration, kWhCost); + expect(result).toBe(0); + }); + + it('should return 0 when the cost per kWh is 0', () => { + const wattage = 1000; + const duration = 5; + const kWhCost = 0; + const result = computeCost(wattage, duration, kWhCost); + expect(result).toBe(0); + }); + + it('should handle fractional wattage and duration correctly', () => { + const wattage = 750; // 0.75 kW + const duration = 2.5; // 2.5 hours + const kWhCost = 0.10; // $0.10 per kWh + const result = computeCost(wattage, duration, kWhCost); + expect(result).toBeCloseTo(0.1875); // 0.75 kW * 2.5h * 0.10 = 0.1875 + }); + + it('should handle large numbers correctly', () => { + const wattage = 1000000; // 1 MW + const duration = 24; // 24 hours + const kWhCost = 0.15; // $0.15 per kWh + const result = computeCost(wattage, duration, kWhCost); + expect(result).toBe(3600); // 1000 kW * 24h * 0.15 = 3600 + }); +}); diff --git a/src/tools/energy-computer/energy-computer.service.ts b/src/tools/energy-computer/energy-computer.service.ts new file mode 100644 index 00000000..8702f5ab --- /dev/null +++ b/src/tools/energy-computer/energy-computer.service.ts @@ -0,0 +1,6 @@ +export function computeCost(wattage: number, durationHours: number, costPerKWh: number): number { + const kilowatts = wattage / 1000; + const energyConsumed = kilowatts * durationHours; + const totalCost = energyConsumed * costPerKWh; + return totalCost; +} diff --git a/src/tools/energy-computer/energy-computer.vue b/src/tools/energy-computer/energy-computer.vue new file mode 100644 index 00000000..74789183 --- /dev/null +++ b/src/tools/energy-computer/energy-computer.vue @@ -0,0 +1,26 @@ + + + diff --git a/src/tools/energy-computer/index.ts b/src/tools/energy-computer/index.ts new file mode 100644 index 00000000..25d0cc1b --- /dev/null +++ b/src/tools/energy-computer/index.ts @@ -0,0 +1,12 @@ +import { Engine } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Energy Consumption and Expense Computer', + path: '/energy-computer', + description: 'Compute energy consumption and expense', + keywords: ['energy', 'expense', 'watt', 'kwh', 'computer'], + component: () => import('./energy-computer.vue'), + icon: Engine, + createdAt: new Date('2024-08-15'), +}); diff --git a/src/tools/index.ts b/src/tools/index.ts index ed728e8e..cbba0f35 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -4,9 +4,9 @@ import { tool as basicAuthGenerator } from './basic-auth-generator'; import { tool as jsonEditor } from './json-editor'; import { tool as liveCode } from './live-code'; import { tool as emailNormalizer } from './email-normalizer'; +import { tool as energyComputer } from './energy-computer'; import { tool as peerShare } from './peer-share'; import { tool as asciiTextDrawer } from './ascii-text-drawer'; - import { tool as textToUnicode } from './text-to-unicode'; import { tool as safelinkDecoder } from './safelink-decoder'; import { tool as xmlToJson } from './xml-to-json'; @@ -177,7 +177,12 @@ export const toolsByCategory: ToolCategory[] = [ }, { name: 'Measurement', - components: [chronometer, temperatureConverter, benchmarkBuilder], + components: [ + chronometer, + temperatureConverter, + benchmarkBuilder, + energyComputer, + ], }, { name: 'Text',