Skip to content

Commit

Permalink
remove pagexray tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fershad committed Mar 17, 2024
1 parent 41ee904 commit 1e6508a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 253 deletions.
234 changes: 3 additions & 231 deletions src/co2.test.js
Original file line number Diff line number Diff line change
@@ -1,154 +1,25 @@
"use strict";

import fs from "fs";
import path from "path";
import { MILLION, ONEBYTE, SWD } from "./constants/test-constants.js";

import pagexray from "pagexray";
import { MILLION, SWD } from "./constants/test-constants.js";

import CO2 from "./co2.js";
import { averageIntensity, marginalIntensity } from "./index.js";

const TwnGridIntensityValue = averageIntensity.data["TWN"];

describe("co2", () => {
let har, co2;
describe("1 byte model", () => {
const { TGWF_GREY_VALUE, TGWF_GREEN_VALUE, TGWF_MIXED_VALUE } = ONEBYTE;

beforeEach(() => {
co2 = new CO2({ model: "1byte" });
har = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
"utf8"
)
);
});

describe("perPage", () => {
it("returns CO2 for total transfer for page", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];

expect(co2.perPage(pageXrayRun).toFixed(5)).toBe(
TGWF_GREY_VALUE.toFixed(5)
);
});
it("returns lower CO2 for page served from green site", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];
let green = [
"www.thegreenwebfoundation.org",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
});
it("returns a lower CO2 number where *some* domains use green power", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];
// green can be true, or a array containing entries
let green = [
"www.thegreenwebfoundation.org",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];
expect(co2.perPage(pageXrayRun, green).toFixed(5)).toBe(
TGWF_MIXED_VALUE.toFixed(5)
);
});
});
describe("perDomain", () => {
it("shows object listing Co2 for each domain", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];
const res = co2.perDomain(pageXrayRun);

const domains = [
"thegreenwebfoundation.org",
"www.thegreenwebfoundation.org",
"maxcdn.bootstrapcdn.com",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];

for (let obj of res) {
expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);
expect(typeof obj.co2).toBe("number");
}
});
it("shows lower Co2 for green domains", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];

const greenDomains = [
"www.thegreenwebfoundation.org",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];
const res = co2.perDomain(pageXrayRun);
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);

for (let obj of res) {
expect(typeof obj.co2).toBe("number");
}
for (let obj of greenDomains) {
let index = 0;
expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
index++;
}
});
});
});
let co2;

describe("Sustainable Web Design model as simple option", () => {
// the SWD model should have slightly higher values as
// we include more of the system in calculations for the
// same levels of data transfer

const {
MILLION_GREEN,
MILLION_GREY,
TGWF_GREY_VALUE,
TGWF_MIXED_VALUE,
MILLION_PERVISIT_GREY,
MILLION_PERVISIT_GREEN,
} = SWD;
const { MILLION_PERVISIT_GREY, MILLION_PERVISIT_GREEN } = SWD;

// We're not passing in a model parameter here to check that SWD is used by default
beforeEach(() => {
co2 = new CO2();
har = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
"utf8"
)
);
});

describe("perVisit", () => {
Expand All @@ -173,105 +44,6 @@ describe("co2", () => {
});
});

describe("perPage", () => {
it("returns CO2 for total transfer for page", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];

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", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];
let green = [
"www.thegreenwebfoundation.org",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
});
it("returns a lower CO2 number where *some* domains use green power", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];
// green can be true, or a array containing entries
let green = [
"www.thegreenwebfoundation.org",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];
expect(
parseFloat(co2.perPage(pageXrayRun, green).toFixed(5))
).toBeCloseTo(parseFloat(TGWF_MIXED_VALUE.toFixed(5)), 3);
});
});
describe("perDomain", () => {
it("shows object listing Co2 for each domain", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];
const res = co2.perDomain(pageXrayRun);

const domains = [
"thegreenwebfoundation.org",
"www.thegreenwebfoundation.org",
"maxcdn.bootstrapcdn.com",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];

for (let obj of res) {
expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);
expect(typeof obj.co2).toBe("number");
}
});
it("shows lower Co2 for green domains", () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];

const greenDomains = [
"www.thegreenwebfoundation.org",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];
const res = co2.perDomain(pageXrayRun);
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);

for (let obj of res) {
expect(typeof obj.co2).toBe("number");
}
for (let obj of greenDomains) {
let index = 0;
expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
index++;
}
});
});
describe("Returning results by segment", () => {
const {
MILLION_GREY,
Expand Down
27 changes: 5 additions & 22 deletions src/hosting.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"use strict";

import fs from "fs";
import https from "https";
import path from "path";

import pagexray from "pagexray";

import hosting from "./hosting-node.js";

jest.mock("https");
Expand All @@ -25,35 +22,21 @@ describe("hosting", () => {
let har;
let httpsGetSpy;
beforeEach(() => {
har = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
"utf8"
)
);
httpsGetSpy = jest.spyOn(https, "get");
jest.clearAllMocks();
});
describe("checking all domains on a page object with #checkPage", () => {
it("returns a list of green domains, when passed a page object", async () => {
const pages = pagexray.convert(har);
const pageXrayRun = pages[0];
const db = await hosting.loadJSON(jsonPath);
const greenDomains = await hosting.checkPage(pageXrayRun, db);
const greenDomains = await hosting.checkPage(
["www.thegreenwebfoundation.org", "fonts.googleapis.com"],
db
);

expect(greenDomains).toHaveLength(11);
expect(greenDomains).toHaveLength(2);
const expectedGreendomains = [
"maxcdn.bootstrapcdn.com",
"thegreenwebfoundation.org",
"www.thegreenwebfoundation.org",
"fonts.googleapis.com",
"ajax.googleapis.com",
"assets.digitalclimatestrike.net",
"cdnjs.cloudflare.com",
"graphite.thegreenwebfoundation.org",
"analytics.thegreenwebfoundation.org",
"fonts.gstatic.com",
"api.thegreenwebfoundation.org",
];
greenDomains.forEach((dom) => {
expect(expectedGreendomains).toContain(dom);
Expand Down

0 comments on commit 1e6508a

Please sign in to comment.