From 0632e7d68614d1b0d7a0b9dbd6e9462f0d30202e Mon Sep 17 00:00:00 2001 From: fershad <27988517+fershad@users.noreply.github.com> Date: Tue, 7 Nov 2023 12:44:46 +0800 Subject: [PATCH 1/2] update tests for updated grid intensities --- src/co2.test.js | 320 ++++++++++++++++++----------- src/constants/test-constants.js | 2 +- src/sustainable-web-design.test.js | 14 +- 3 files changed, 210 insertions(+), 126 deletions(-) diff --git a/src/co2.test.js b/src/co2.test.js index ad72b4f..b6344c4 100644 --- a/src/co2.test.js +++ b/src/co2.test.js @@ -153,18 +153,21 @@ describe("co2", () => { describe("perVisit", () => { it("returns a CO2 number for data transfer per visit with caching assumptions from the Sustainable Web Design model", () => { co2.perVisit(MILLION); - expect(co2.perVisit(MILLION).toFixed(5)).toBe( - MILLION_PERVISIT_GREY.toFixed(5) + expect(parseFloat(co2.perVisit(MILLION).toFixed(5))).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY.toFixed(5)), + 3 ); }); it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", () => { - expect(co2.perVisit(MILLION, false).toFixed(5)).toBe( - MILLION_PERVISIT_GREY.toFixed(5) + expect(parseFloat(co2.perVisit(MILLION, false).toFixed(5))).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY.toFixed(5)), + 3 ); - expect(co2.perVisit(MILLION, true).toFixed(5)).toBe( - MILLION_PERVISIT_GREEN.toFixed(5) + expect(parseFloat(co2.perVisit(MILLION, true).toFixed(5))).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREEN.toFixed(5)), + 3 ); }); }); @@ -174,8 +177,9 @@ describe("co2", () => { const pages = pagexray.convert(har); const pageXrayRun = pages[0]; - expect(co2.perPage(pageXrayRun).toFixed(5)).toBe( - TGWF_GREY_VALUE.toFixed(5) + expect(parseFloat(co2.perPage(pageXrayRun).toFixed(5))).toBeCloseTo( + parseFloat(TGWF_GREY_VALUE.toFixed(5)), + 3 ); }); it("returns lower CO2 for page served from green site", () => { @@ -209,9 +213,9 @@ describe("co2", () => { "fonts.gstatic.com", "api.thegreenwebfoundation.org", ]; - expect(co2.perPage(pageXrayRun, green).toFixed(5)).toBe( - TGWF_MIXED_VALUE.toFixed(5) - ); + expect( + parseFloat(co2.perPage(pageXrayRun, green).toFixed(5)) + ).toBeCloseTo(parseFloat(TGWF_MIXED_VALUE.toFixed(5)), 3); }); }); describe("perDomain", () => { @@ -291,83 +295,127 @@ describe("co2", () => { it("returns an object with devices, networks, data centers, and production emissions shown separately, as well as the total emissions", () => { co2 = new CO2({ results: "segment" }); const res = co2.perVisit(MILLION); - expect(res["consumerDeviceCO2 - first"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_DEVICES_FIRST.toFixed(5) + expect( + parseFloat(res["consumerDeviceCO2 - first"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_DEVICES_FIRST.toFixed(5)) ); - expect(res["networkCO2 - first"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_NETWORKS_FIRST.toFixed(5) + expect(parseFloat(res["networkCO2 - first"].toFixed(5))).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_NETWORKS_FIRST.toFixed(5)) ); - expect(res["dataCenterCO2 - first"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_DATACENTERS_FIRST.toFixed(5) + expect( + parseFloat(res["dataCenterCO2 - first"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_DATACENTERS_FIRST.toFixed(5)) ); - expect(res["productionCO2 - first"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_PRODUCTION_FIRST.toFixed(5) + expect( + parseFloat(res["productionCO2 - first"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_PRODUCTION_FIRST.toFixed(5)) ); - expect(res["consumerDeviceCO2 - subsequent"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_DEVICES_SECOND.toFixed(5) + expect( + parseFloat(res["consumerDeviceCO2 - subsequent"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_DEVICES_SECOND.toFixed(5)) ); - expect(res["networkCO2 - subsequent"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_NETWORKS_SECOND.toFixed(5) + expect( + parseFloat(res["networkCO2 - subsequent"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_NETWORKS_SECOND.toFixed(5)) ); - expect(res["dataCenterCO2 - subsequent"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_DATACENTERS_SECOND.toFixed(5) + expect( + parseFloat(res["dataCenterCO2 - subsequent"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_DATACENTERS_SECOND.toFixed(5)) ); - expect(res["productionCO2 - subsequent"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_PRODUCTION_SECOND.toFixed(5) + expect( + parseFloat(res["productionCO2 - subsequent"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_PRODUCTION_SECOND.toFixed(5)) + ); + expect(parseFloat(res.total.toFixed(5))).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY.toFixed(5)), + 3 ); - expect(res.total.toFixed(5)).toBe(MILLION_PERVISIT_GREY.toFixed(5)); }); it("returns adjusted data center and total emissions for when green, other values remain the same as grey", () => { co2 = new CO2({ results: "segment" }); const res = co2.perVisit(MILLION, true); // Since the data center emissions are the only ones that change, we can just check those // and the total. To check the rest stay the same as grey, we can just check the device results. - expect(res["consumerDeviceCO2 - first"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_DEVICES_FIRST.toFixed(5) + expect( + parseFloat(res["consumerDeviceCO2 - first"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_DEVICES_FIRST.toFixed(5)), + 3 ); - expect(res["dataCenterCO2 - first"].toFixed(5)).toBe( - MILLION_PERVISIT_GREEN_DATACENTERS_FIRST.toFixed(5) + expect( + parseFloat(res["dataCenterCO2 - first"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREEN_DATACENTERS_FIRST.toFixed(5)), + 3 ); - expect(res["consumerDeviceCO2 - subsequent"].toFixed(5)).toBe( - MILLION_PERVISIT_GREY_DEVICES_SECOND.toFixed(5) + expect( + parseFloat(res["consumerDeviceCO2 - subsequent"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREY_DEVICES_SECOND.toFixed(5)), + 3 ); - expect(res["dataCenterCO2 - subsequent"].toFixed(5)).toBe( - MILLION_PERVISIT_GREEN_DATACENTERS_SECOND.toFixed(5) + expect( + parseFloat(res["dataCenterCO2 - subsequent"].toFixed(5)) + ).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREEN_DATACENTERS_SECOND.toFixed(5)), + 3 ); - expect(res.total.toFixed(5)).toBe(MILLION_PERVISIT_GREEN.toFixed(5)); + expect(parseFloat(res.total.toFixed(5))).toBeCloseTo( + parseFloat(MILLION_PERVISIT_GREEN.toFixed(5)), + 3 + ); }); }); describe("perByte", () => { it("returns adjusted data center and total emissions for when green, other values remain the same as grey", () => { co2 = new CO2({ results: "segment" }); const res = co2.perByte(MILLION, true); - expect(res["consumerDeviceCO2"].toFixed(5)).toBe( - MILLION_GREY_DEVICES.toFixed(5) + expect(parseFloat(res["consumerDeviceCO2"].toFixed(5))).toBeCloseTo( + parseFloat(MILLION_GREY_DEVICES.toFixed(5)), + 3 ); - expect(res["dataCenterCO2"].toFixed(5)).toBe( - MILLION_GREEN_DATACENTERS.toFixed(5) + expect(parseFloat(res["dataCenterCO2"].toFixed(5))).toBeCloseTo( + parseFloat(MILLION_GREEN_DATACENTERS.toFixed(5)), + 3 + ); + expect(parseFloat(res.total.toFixed(5))).toBeCloseTo( + parseFloat(MILLION_GREEN.toFixed(5)), + 3 ); - expect(res.total.toFixed(5)).toBe(MILLION_GREEN.toFixed(5)); }); it("returns an object with devices, networks, data centers, and production emissions shown separately, as well as the total emissions", () => { co2 = new CO2({ results: "segment" }); const res = co2.perByte(MILLION); - expect(res["consumerDeviceCO2"].toFixed(5)).toBe( - MILLION_GREY_DEVICES.toFixed(5) + expect(parseFloat(res["consumerDeviceCO2"].toFixed(5))).toBeCloseTo( + parseFloat(MILLION_GREY_DEVICES.toFixed(5)), + 3 + ); + expect(parseFloat(res["networkCO2"].toFixed(5))).toBeCloseTo( + parseFloat(MILLION_GREY_NETWORKS.toFixed(5)), + 3 ); - expect(res["networkCO2"].toFixed(5)).toBe( - MILLION_GREY_NETWORKS.toFixed(5) + expect(parseFloat(res["dataCenterCO2"].toFixed(5))).toBeCloseTo( + parseFloat(MILLION_GREY_DATACENTERS.toFixed(5)), + 3 ); - expect(res["dataCenterCO2"].toFixed(5)).toBe( - MILLION_GREY_DATACENTERS.toFixed(5) + expect(parseFloat(res["productionCO2"].toFixed(5))).toBeCloseTo( + parseFloat(MILLION_GREY_PRODUCTION.toFixed(5)), + 3 ); - expect(res["productionCO2"].toFixed(5)).toBe( - MILLION_GREY_PRODUCTION.toFixed(5) + expect(parseFloat(res.total.toFixed(5))).toBeCloseTo( + parseFloat(MILLION_GREY.toFixed(5)), + 3 ); - expect(res.total.toFixed(5)).toBe(MILLION_GREY.toFixed(5)); }); }); }); @@ -473,7 +521,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBeCloseTo(MILLION_PERVISIT_GREY, 5); + ).toBeCloseTo(MILLION_PERVISIT_GREY, 3); expect( parseFloat( @@ -485,29 +533,43 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBeCloseTo(MILLION_GREY, 5); + ).toBeCloseTo(MILLION_GREY, 3); }); it("uses a number correctly", () => { expect( - co2 - .perVisitTrace(MILLION, false, { - gridIntensity: { - device: 561, - }, - }) - .co2.toPrecision(5) - ).toBe(MILLION_PERVISIT_GREY_DEVICE_GRID_INTENSITY_CHANGE.toPrecision(5)); + parseFloat( + co2 + .perVisitTrace(MILLION, false, { + gridIntensity: { + device: 561, + }, + }) + .co2.toPrecision(5) + ) + ).toBeCloseTo( + parseFloat( + MILLION_PERVISIT_GREY_DEVICE_GRID_INTENSITY_CHANGE.toPrecision(5) + ), + 3 + ); expect( - co2 - .perByteTrace(MILLION, false, { - gridIntensity: { - device: 561, - }, - }) - .co2.toPrecision(4) - ).toBe(MILLION_PERBYTE_GREY_DEVICE_GRID_INTENSITY_CHANGE.toPrecision(4)); + parseFloat( + co2 + .perByteTrace(MILLION, false, { + gridIntensity: { + device: 561, + }, + }) + .co2.toPrecision(4) + ) + ).toBeCloseTo( + parseFloat( + MILLION_PERBYTE_GREY_DEVICE_GRID_INTENSITY_CHANGE.toPrecision(4) + ), + 3 + ); }); it("uses an object correctly", () => { @@ -523,7 +585,7 @@ describe("co2", () => { }) .co2.toFixed(5) ) - ).toBeCloseTo(MILLION_PERVISIT_GREY_DEVICE_GRID_INTENSITY_CHANGE, 4); + ).toBeCloseTo(MILLION_PERVISIT_GREY_DEVICE_GRID_INTENSITY_CHANGE, 3); expect( parseFloat( co2 @@ -536,7 +598,7 @@ describe("co2", () => { }) .co2.toPrecision(4) ) - ).toBeCloseTo(MILLION_PERBYTE_GREY_DEVICE_GRID_INTENSITY_CHANGE, 4); + ).toBeCloseTo(MILLION_PERBYTE_GREY_DEVICE_GRID_INTENSITY_CHANGE, 3); }); }); @@ -559,7 +621,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBe(MILLION_PERVISIT_GREY); + ).toBeCloseTo(MILLION_PERVISIT_GREY, 3); expect( parseFloat( co2 @@ -570,31 +632,41 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBe(MILLION_GREY); + ).toBeCloseTo(MILLION_GREY, 3); }); it("uses a number correctly", () => { expect( - co2 - .perVisitTrace(MILLION, false, { - gridIntensity: { - dataCenter: 561, - }, - }) - .co2.toPrecision(5) - ).toBe( - MILLION_PERVISIT_GREY_DATACENTER_GRID_INTENSITY_CHANGE.toPrecision(5) + parseFloat( + co2 + .perVisitTrace(MILLION, false, { + gridIntensity: { + dataCenter: 561, + }, + }) + .co2.toPrecision(5) + ) + ).toBeCloseTo( + parseFloat( + MILLION_PERVISIT_GREY_DATACENTER_GRID_INTENSITY_CHANGE.toPrecision(5) + ), + 3 ); expect( - co2 - .perByteTrace(MILLION, false, { - gridIntensity: { - dataCenter: 561, - }, - }) - .co2.toPrecision(5) - ).toBe( - MILLION_PERBYTE_GREY_DATACENTER_GRID_INTENSITY_CHANGE.toPrecision(5) + parseFloat( + co2 + .perByteTrace(MILLION, false, { + gridIntensity: { + dataCenter: 561, + }, + }) + .co2.toPrecision(5) + ) + ).toBeCloseTo( + parseFloat( + MILLION_PERBYTE_GREY_DATACENTER_GRID_INTENSITY_CHANGE.toPrecision(5) + ), + 3 ); }); @@ -611,7 +683,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBeCloseTo(MILLION_PERVISIT_GREY_DATACENTER_GRID_INTENSITY_CHANGE, 4); + ).toBeCloseTo(MILLION_PERVISIT_GREY_DATACENTER_GRID_INTENSITY_CHANGE, 3); expect( parseFloat( co2 @@ -624,7 +696,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBeCloseTo(MILLION_PERBYTE_GREY_DATACENTER_GRID_INTENSITY_CHANGE, 4); + ).toBeCloseTo(MILLION_PERBYTE_GREY_DATACENTER_GRID_INTENSITY_CHANGE, 3); }); }); @@ -647,7 +719,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBe(MILLION_PERVISIT_GREY); + ).toBeCloseTo(MILLION_PERVISIT_GREY, 3); expect( parseFloat( @@ -659,30 +731,42 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBe(MILLION_GREY); + ).toBeCloseTo(MILLION_GREY, 3); }); it("uses a number correctly", () => { expect( - co2 - .perVisitTrace(MILLION, false, { - gridIntensity: { - network: 561, - }, - }) - .co2.toPrecision(5) - ).toBe( - MILLION_PERVISIT_GREY_NETWORK_GRID_INTENSITY_CHANGE.toPrecision(5) + parseFloat( + co2 + .perVisitTrace(MILLION, false, { + gridIntensity: { + network: 561, + }, + }) + .co2.toPrecision(5) + ) + ).toBeCloseTo( + parseFloat( + MILLION_PERVISIT_GREY_NETWORK_GRID_INTENSITY_CHANGE.toPrecision(5) + ), + 3 ); expect( - co2 - .perByteTrace(MILLION, false, { - gridIntensity: { - network: 561, - }, - }) - .co2.toPrecision(5) - ).toBe(MILLION_PERBYTE_GREY_NETWORK_GRID_INTENSITY_CHANGE.toPrecision(5)); + parseFloat( + co2 + .perByteTrace(MILLION, false, { + gridIntensity: { + network: 561, + }, + }) + .co2.toPrecision(5) + ) + ).toBeCloseTo( + parseFloat( + MILLION_PERBYTE_GREY_NETWORK_GRID_INTENSITY_CHANGE.toPrecision(5) + ), + 3 + ); }); it("uses an object correctly", () => { @@ -698,7 +782,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBeCloseTo(MILLION_PERVISIT_GREY_NETWORK_GRID_INTENSITY_CHANGE, 4); + ).toBeCloseTo(MILLION_PERVISIT_GREY_NETWORK_GRID_INTENSITY_CHANGE, 3); expect( parseFloat( co2 @@ -711,7 +795,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBeCloseTo(MILLION_PERBYTE_GREY_NETWORK_GRID_INTENSITY_CHANGE, 4); + ).toBeCloseTo(MILLION_PERBYTE_GREY_NETWORK_GRID_INTENSITY_CHANGE, 3); }); }); @@ -733,7 +817,7 @@ describe("co2", () => { .perVisitTrace(1000000, false, { dataReloadRatio: "0.5" }) .co2.toPrecision(5) ) - ).toBe(MILLION_PERVISIT_GREY); + ).toBeCloseTo(MILLION_PERVISIT_GREY, 3); }); it("expects a number between 0 and 1", () => { @@ -745,7 +829,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBe(MILLION_PERVISIT_GREY); + ).toBeCloseTo(MILLION_PERVISIT_GREY, 3); expect( parseFloat( co2 @@ -754,7 +838,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBe(MILLION_PERVISIT_GREY); + ).toBeCloseTo(MILLION_PERVISIT_GREY, 3); expect( co2.perVisitTrace(1000000, false, { dataReloadRatio: 0, @@ -784,7 +868,7 @@ describe("co2", () => { }) .co2.toPrecision(5) ) - ).toBe(MILLION_GREY); + ).toBeCloseTo(MILLION_GREY, 3); }); it("expects firstVisitPercentage to be a number", () => { diff --git a/src/constants/test-constants.js b/src/constants/test-constants.js index a475269..1ea4cbc 100644 --- a/src/constants/test-constants.js +++ b/src/constants/test-constants.js @@ -12,7 +12,7 @@ export const ONEBYTE = { export const SWD = { MILLION_GREY: 0.35418, MILLION_GREEN: 0.30713, - MILLION_PERVISIT_GREY: 0.26741, + MILLION_PERVISIT_GREY: 0.26766, MILLION_PERVISIT_GREEN: 0.23188, TGWF_GREY_VALUE: 0.24963, diff --git a/src/sustainable-web-design.test.js b/src/sustainable-web-design.test.js index 2b2356b..af19ad8 100644 --- a/src/sustainable-web-design.test.js +++ b/src/sustainable-web-design.test.js @@ -41,21 +41,21 @@ describe("sustainable web design model", () => { }); it("returns a result for grey energy", () => { - expect(swd.perByte(MILLION)).toBeCloseTo(SWD.MILLION_GREY, 5); + expect(swd.perByte(MILLION)).toBeCloseTo(SWD.MILLION_GREY, 3); }); it("returns a result for green energy", () => { - expect(swd.perByte(MILLION, true)).toBeCloseTo(SWD.MILLION_GREEN, 5); + expect(swd.perByte(MILLION, true)).toBeCloseTo(SWD.MILLION_GREEN, 3); }); it("can segment results", () => { const result = swd.perByte(MILLION, false, true); - expect(result.dataCenterCO2).toBeCloseTo(SWD.MILLION_GREY_DATACENTERS, 5); - expect(result.consumerDeviceCO2).toBeCloseTo(SWD.MILLION_GREY_DEVICES, 5); - expect(result.networkCO2).toBeCloseTo(SWD.MILLION_GREY_NETWORKS, 5); - expect(result.productionCO2).toBeCloseTo(SWD.MILLION_GREY_PRODUCTION, 5); - expect(result.total).toBeCloseTo(SWD.MILLION_GREY, 5); + expect(result.dataCenterCO2).toBeCloseTo(SWD.MILLION_GREY_DATACENTERS, 3); + expect(result.consumerDeviceCO2).toBeCloseTo(SWD.MILLION_GREY_DEVICES, 3); + expect(result.networkCO2).toBeCloseTo(SWD.MILLION_GREY_NETWORKS, 3); + expect(result.productionCO2).toBeCloseTo(SWD.MILLION_GREY_PRODUCTION, 3); + expect(result.total).toBeCloseTo(SWD.MILLION_GREY, 3); }); }); 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 2/2] 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; }