Skip to content

Commit

Permalink
Clean up SustainableWebDesign model perByte tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fershad authored Sep 1, 2023
2 parents 31952c9 + 1130722 commit a87c622
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
17 changes: 0 additions & 17 deletions src/co2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,6 @@ describe("co2", () => {
);
});

describe("perByte", () => {
it("returns a CO2 number for data transfer", () => {
co2.perByte(MILLION);
expect(co2.perByte(MILLION).toFixed(5)).toBe(MILLION_GREY.toFixed(5));
});

it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", () => {
expect(co2.perByte(MILLION, false).toFixed(5)).toBe(
MILLION_GREY.toFixed(5)
);

expect(co2.perByte(MILLION, true).toFixed(5)).toBe(
MILLION_GREEN.toFixed(5)
);
});
});

describe("perVisit", () => {
it("returns a CO2 number for data transfer per visit with caching assumptions from the Sustainable Web Design model", () => {
co2.perVisit(MILLION);
Expand Down
4 changes: 4 additions & 0 deletions src/sustainable-web-design.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ class SustainableWebDesign {
segmentResults = false,
options = {}
) {
if (bytes < 1) {
bytes = 0;
}

const energyBycomponent = this.energyPerByteByComponent(bytes, options);

// otherwise when faced with non numeric values throw an error
Expand Down
32 changes: 30 additions & 2 deletions src/sustainable-web-design.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SustainableWebDesign from "./sustainable-web-design.js";
import { MILLION, SWD } from "./constants/test-constants.js";

describe("sustainable web design model", () => {
const swd = new SustainableWebDesign();
Expand Down Expand Up @@ -26,8 +27,35 @@ describe("sustainable web design model", () => {
});

describe("perByte", () => {
it("should return a single number for CO2 emissions", () => {
expect(typeof swd.perByte(2257715.2)).toBe("number");
it("returns 0 for byte counts less than 1", () => {
expect(swd.perByte(0)).toBe(0);
expect(swd.perByte(0.99)).toBe(0);
expect(swd.perByte(-1)).toBe(0);

const segmented = swd.perByte(0.5, false, true);
expect(segmented.dataCenterCO2).toBe(0);
expect(segmented.consumerDeviceCO2).toBe(0);
expect(segmented.networkCO2).toBe(0);
expect(segmented.productionCO2).toBe(0);
expect(segmented.total).toBe(0);
});

it("returns a result for grey energy", () => {
expect(swd.perByte(MILLION)).toBeCloseTo(SWD.MILLION_GREY, 5);
});

it("returns a result for green energy", () => {
expect(swd.perByte(MILLION, true)).toBeCloseTo(SWD.MILLION_GREEN, 5);
});

it("can segment results", () => {
const result = swd.perByte(MILLION, false, true);

expect(result.dataCenterCO2).toBeCloseTo(0.05301, 5);
expect(result.consumerDeviceCO2).toBeCloseTo(0.18378, 5);
expect(result.networkCO2).toBeCloseTo(0.04948, 5);
expect(result.productionCO2).toBeCloseTo(0.06715, 5);
expect(result.total).toBeCloseTo(SWD.MILLION_GREY, 5);
});
});

Expand Down

0 comments on commit a87c622

Please sign in to comment.