From 41483310241e5a139f0ff9c8c8526b7223047744 Mon Sep 17 00:00:00 2001 From: fershad <27988517+fershad@users.noreply.github.com> Date: Tue, 7 Nov 2023 14:03:05 +0800 Subject: [PATCH] fix for 0 grid intensity and other variables --- src/co2.test.js | 74 ++++++++++++++++++++++++++++++++++- src/sustainable-web-design.js | 12 +++--- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/src/co2.test.js b/src/co2.test.js index b6344c4..9a7af24 100644 --- a/src/co2.test.js +++ b/src/co2.test.js @@ -947,7 +947,7 @@ describe("co2", () => { }); describe("Using values equal to 0", () => { - const co2 = new CO2(); + const co2 = new CO2({ results: "segment" }); it("expects perByteTrace to support values equal to 0", () => { const perByteTraceResult = co2.perByteTrace(1000000, false, { @@ -986,5 +986,77 @@ describe("co2", () => { expect(network).toBe(0); expect(device).toBe(0); }); + it("expects perByteTrace segments to be 0 when grid intensity is 0", () => { + const perByteTraceResult = co2.perByteTrace(1000000, false, { + gridIntensity: { + dataCenter: 0, + network: 0, + device: 0, + }, + }); + const co2Result = perByteTraceResult.co2; + // Less than 0.1 because there's still a small production component that's calculated + expect(co2Result.total).toBeLessThan(0.1); + expect(co2Result["dataCenterCO2"]).toBe(0); + expect(co2Result["networkCO2"]).toBe(0); + expect(co2Result["consumerDeviceCO2"]).toBe(0); + }); + + it("expects perVisitTrace segments to be 0 when grid intensity is 0", () => { + const perVisitTraceResult = co2.perVisitTrace(1000000, false, { + gridIntensity: { + dataCenter: 0, + network: 0, + device: 0, + }, + }); + const co2Result = perVisitTraceResult.co2; + const { dataCenter, network, device } = + perVisitTraceResult.variables.gridIntensity; + // Less than 0.1 because there's still a small production component that's calculated + expect(co2Result.total).toBeLessThan(0.1); + expect(co2Result["dataCenterCO2 - first"]).toBe(0); + expect(co2Result["networkCO2 - first"]).toBe(0); + expect(co2Result["consumerDeviceCO2 - first"]).toBe(0); + expect(co2Result["dataCenterCO2 - subsequent"]).toBe(0); + expect(co2Result["networkCO2 - subsequent"]).toBe(0); + expect(co2Result["consumerDeviceCO2 - subsequent"]).toBe(0); + }); + + it("expects perVisitTrace segments to be 0 when there are 0 first and returning visitors", () => { + const perVisitTraceResult = co2.perVisitTrace(1000000, false, { + firstVisitPercentage: 0, + returnVisitPercentage: 0, + }); + const co2Result = perVisitTraceResult.co2; + // Less than 0.1 because there's still a small production component that's calculated + expect(co2Result.total).toBe(0); + }); + + it("expects perVisitTrace subsequent segments to be 0 when returning visitors is 0", () => { + const perVisitTraceResult = co2.perVisitTrace(1000000, false, { + firstVisitPercentage: 100, + returnVisitPercentage: 0, + }); + const co2Result = perVisitTraceResult.co2; + expect(co2Result["dataCenterCO2 - first"]).toBeGreaterThan(0); + expect(co2Result["networkCO2 - first"]).toBeGreaterThan(0); + expect(co2Result["consumerDeviceCO2 - first"]).toBeGreaterThan(0); + expect(co2Result["dataCenterCO2 - subsequent"]).toBe(0); + expect(co2Result["networkCO2 - subsequent"]).toBe(0); + expect(co2Result["consumerDeviceCO2 - subsequent"]).toBe(0); + }); + it("expects perVisitTrace subsequent segments to be 0 when data reload is 0", () => { + const perVisitTraceResult = co2.perVisitTrace(1000000, false, { + dataReloadRatio: 0, + }); + const co2Result = perVisitTraceResult.co2; + expect(co2Result["dataCenterCO2 - first"]).toBeGreaterThan(0); + expect(co2Result["networkCO2 - first"]).toBeGreaterThan(0); + expect(co2Result["consumerDeviceCO2 - first"]).toBeGreaterThan(0); + expect(co2Result["dataCenterCO2 - subsequent"]).toBe(0); + expect(co2Result["networkCO2 - subsequent"]).toBe(0); + expect(co2Result["consumerDeviceCO2 - subsequent"]).toBe(0); + }); }); }); diff --git a/src/sustainable-web-design.js b/src/sustainable-web-design.js index d2737a0..613afd8 100644 --- a/src/sustainable-web-design.js +++ b/src/sustainable-web-design.js @@ -70,14 +70,14 @@ class SustainableWebDesign { if (options?.gridIntensity) { const { device, network, dataCenter } = options.gridIntensity; - if (device?.value) { + if (device?.value || device?.value === 0) { deviceCarbonIntensity = device.value; } - if (network?.value) { + if (network?.value || network?.value === 0) { networkCarbonIntensity = network.value; } // If the user has set a carbon intensity value for the datacentre, then that overrides everything and is used - if (dataCenter?.value) { + if (dataCenter?.value || dataCenter?.value === 0) { dataCenterCarbonIntensity = dataCenter.value; } } @@ -245,15 +245,15 @@ class SustainableWebDesign { returnView = RETURNING_VISITOR_PERCENTAGE, dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD ) { - if (options.dataReloadRatio) { + if (options.dataReloadRatio || options.dataReloadRatio === 0) { dataReloadRatio = options.dataReloadRatio; } - if (options.firstVisitPercentage) { + if (options.firstVisitPercentage || options.firstVisitPercentage === 0) { firstView = options.firstVisitPercentage; } - if (options.returnVisitPercentage) { + if (options.returnVisitPercentage || options.returnVisitPercentage === 0) { returnView = options.returnVisitPercentage; }