diff --git a/src/constants/test-constants.js b/src/constants/test-constants.js index 0b1067b..f1dd344 100644 --- a/src/constants/test-constants.js +++ b/src/constants/test-constants.js @@ -18,6 +18,22 @@ export const SWD = { TGWF_GREEN_VALUE: 0.54704, TGWF_MIXED_VALUE: 0.21896, + MILLION_ENERGY_PERBYTE_DEVICE: 0.00042, + MILLION_ENERGY_PERBYTE_NETWORK: 0.00011, + MILLION_ENERGY_PERBYTE_DATACENTER: 0.00012, + MILLION_ENERGY_PERBYTE_PRODUCTION: 0.00015, + MILLION_ENERGY_PERBYTE_TOTAL: 0.00081, + + MILLION_ENERGY_PERVISIT_DEVICE_FIRST: 0.0003159, + MILLION_ENERGY_PERVISIT_DEVICE_SECOND: 0.000002106, + MILLION_ENERGY_PERVISIT_NETWORK_FIRST: 0.00008505, + MILLION_ENERGY_PERVISIT_NETWORK_SECOND: 0.000000567, + MILLION_ENERGY_PERVISIT_DATACENTER_FIRST: 0.000091125, + MILLION_ENERGY_PERVISIT_DATACENTER_SECOND: 0.0000006075, + MILLION_ENERGY_PERVISIT_PRODUCTION_FIRST: 0.000115425, + MILLION_ENERGY_PERVISIT_PRODUCTION_SECOND: 0.0000007695, + MILLION_ENERGY_PERVISIT_TOTAL: 0.00061155, + MILLION_GREY_DEVICES: 0.18378, MILLION_GREY_NETWORKS: 0.04948, MILLION_GREY_DATACENTERS: 0.05301, diff --git a/src/sustainable-web-design.test.js b/src/sustainable-web-design.test.js index 9662880..0e9660b 100644 --- a/src/sustainable-web-design.test.js +++ b/src/sustainable-web-design.test.js @@ -1,88 +1,67 @@ import SustainableWebDesign from "./sustainable-web-design.js"; +import { MILLION, SWD } from "./constants/test-constants.js"; describe("sustainable web design model", () => { const swd = new SustainableWebDesign(); - const averageWebsiteInBytes = 2257715.2; describe("energyPerByteByComponent", () => { it("should return a object with numbers for each system component", () => { - // Compare these with the carbon intensity tab C7-C10. - // https://docs.google.com/spreadsheets/d/1eFlHhSBus_HqmoXqX237eAYr0PREUhTr6YBxznQC4jI/edit#gid=0 + // Compare these with the carbon intensity tab B7-B10. + // https://docs.google.com/spreadsheets/d/1qC1f8jWKucXitQ9OnhhFcGO9NWwaSa35B4xcNvC5Mqs/edit#gid=0 // These numbers should match the spreadsheet - const groupedEnergy = swd.energyPerByteByComponent(averageWebsiteInBytes); - expect(groupedEnergy.consumerDeviceEnergy).toBeCloseTo(0.00095095, 8); - expect(groupedEnergy.networkEnergy).toBeCloseTo(0.00025602, 7); - expect(groupedEnergy.dataCenterEnergy).toBeCloseTo(0.00027431, 8); - expect(groupedEnergy.productionEnergy).toBeCloseTo(0.0003475, 7); + const groupedEnergy = swd.energyPerByteByComponent(MILLION); + expect(groupedEnergy.consumerDeviceEnergy).toBeCloseTo(SWD.MILLION_ENERGY_PERBYTE_DEVICE, 5); + expect(groupedEnergy.networkEnergy).toBeCloseTo(SWD.MILLION_ENERGY_PERBYTE_NETWORK, 5); + expect(groupedEnergy.dataCenterEnergy).toBeCloseTo(SWD.MILLION_ENERGY_PERBYTE_DATACENTER, 5); + expect(groupedEnergy.productionEnergy).toBeCloseTo(SWD.MILLION_ENERGY_PERBYTE_PRODUCTION, 5); }); }); describe("energyPerByte", () => { it("should return a number in kilowatt hours for the given data transfer in bytes", () => { - const energyForTransfer = swd.energyPerByte(averageWebsiteInBytes); - expect(energyForTransfer).toBeCloseTo(0.00182874, 7); + const energyForTransfer = swd.energyPerByte(MILLION); + expect(energyForTransfer).toBeCloseTo(SWD.MILLION_ENERGY_PERBYTE_TOTAL, 5); }); }); describe("perByte", () => { it("should return a single number for CO2 emissions", () => { - expect(typeof swd.perByte(2257715.2)).toBe("number"); + expect(typeof swd.perByte(MILLION)).toBe("number"); }); }); describe("perVisit", () => { it("should return a single number for CO2 emissions", () => { - expect(typeof swd.perVisit(2257715.2)).toBe("number"); + expect(typeof swd.perVisit(MILLION)).toBe("number"); }); }); describe("energyPerVisit", () => { it("should return a number", () => { - expect(typeof swd.energyPerVisit(averageWebsiteInBytes)).toBe("number"); + expect(typeof swd.energyPerVisit(MILLION)).toBe("number"); }); it("should calculate the correct energy", () => { - expect(swd.energyPerVisit(averageWebsiteInBytes)).toBe( - 0.0013807057305600004 + expect(swd.energyPerVisit(MILLION)).toBeCloseTo( + SWD.MILLION_ENERGY_PERVISIT_TOTAL, 5 ); }); }); describe("emissionsPerVisitInGrams", () => { it("should calculate the correct co2 per visit", () => { - const energy = swd.energyPerVisit(averageWebsiteInBytes); - expect(swd.emissionsPerVisitInGrams(energy)).toEqual(0.6); + const energy = swd.energyPerVisit(MILLION); + expect(swd.emissionsPerVisitInGrams(energy)).toBeCloseTo( + SWD.MILLION_PERVISIT_GREY, 2 + ); }); it("should accept a dynamic KwH value", () => { - const energy = swd.energyPerVisit(averageWebsiteInBytes); - expect(swd.emissionsPerVisitInGrams(energy, 245)).toEqual(0.34); - }); - }); - - describe("annualEnergyInKwh", () => { - it("should calculate the correct energy in kWh", () => { - expect(swd.annualEnergyInKwh(averageWebsiteInBytes)).toBe(27092582400); - }); - }); - - describe("annualEmissionsInGrams", () => { - it("should calculate the corrent energy in grams", () => { - expect(swd.annualEmissionsInGrams(averageWebsiteInBytes)).toBe( - 27092582400 + const energy = swd.energyPerVisit(MILLION); + expect(swd.emissionsPerVisitInGrams(energy, 436)).toBeCloseTo( + SWD.MILLION_PERVISIT_GREY, 2 ); }); }); - - describe("annualSegmentEnergy", () => { - it("should return the correct values", () => { - expect(swd.annualSegmentEnergy(averageWebsiteInBytes)).toEqual({ - consumerDeviceEnergy: 1174011.9, - dataCenterEnergy: 338657.28, - networkEnergy: 316080.13, - productionEnergy: 428965.89, - }); - }); - }); });