Skip to content

Commit

Permalink
Fix to return proper results when variables with 0 value are passed i…
Browse files Browse the repository at this point in the history
…nto function
  • Loading branch information
fershad authored Nov 7, 2023
2 parents 0632e7d + 4148331 commit b1c0679
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
74 changes: 73 additions & 1 deletion src/co2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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);
});
});
});
12 changes: 6 additions & 6 deletions src/sustainable-web-design.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit b1c0679

Please sign in to comment.