11"use strict" ;
22
3- import fs from "fs" ;
4- import path from "path" ;
5- import { MILLION , ONEBYTE , SWD } from "./constants/test-constants.js" ;
6-
7- import pagexray from "pagexray" ;
3+ import { MILLION , SWD } from "./constants/test-constants.js" ;
84
95import CO2 from "./co2.js" ;
106import { averageIntensity , marginalIntensity } from "./index.js" ;
117
128const TwnGridIntensityValue = averageIntensity . data [ "TWN" ] ;
139
1410describe ( "co2" , ( ) => {
15- let har , co2 ;
16- describe ( "1 byte model" , ( ) => {
17- const { TGWF_GREY_VALUE , TGWF_GREEN_VALUE , TGWF_MIXED_VALUE } = ONEBYTE ;
18-
19- beforeEach ( ( ) => {
20- co2 = new CO2 ( { model : "1byte" } ) ;
21- har = JSON . parse (
22- fs . readFileSync (
23- path . resolve ( __dirname , "../data/fixtures/tgwf.har" ) ,
24- "utf8"
25- )
26- ) ;
27- } ) ;
28-
29- describe ( "perPage" , ( ) => {
30- it ( "returns CO2 for total transfer for page" , ( ) => {
31- const pages = pagexray . convert ( har ) ;
32- const pageXrayRun = pages [ 0 ] ;
33-
34- expect ( co2 . perPage ( pageXrayRun ) . toFixed ( 5 ) ) . toBe (
35- TGWF_GREY_VALUE . toFixed ( 5 )
36- ) ;
37- } ) ;
38- it ( "returns lower CO2 for page served from green site" , ( ) => {
39- const pages = pagexray . convert ( har ) ;
40- const pageXrayRun = pages [ 0 ] ;
41- let green = [
42- "www.thegreenwebfoundation.org" ,
43- "fonts.googleapis.com" ,
44- "ajax.googleapis.com" ,
45- "assets.digitalclimatestrike.net" ,
46- "cdnjs.cloudflare.com" ,
47- "graphite.thegreenwebfoundation.org" ,
48- "analytics.thegreenwebfoundation.org" ,
49- "fonts.gstatic.com" ,
50- "api.thegreenwebfoundation.org" ,
51- ] ;
52- expect ( co2 . perPage ( pageXrayRun , green ) ) . toBeLessThan ( TGWF_GREY_VALUE ) ;
53- } ) ;
54- it ( "returns a lower CO2 number where *some* domains use green power" , ( ) => {
55- const pages = pagexray . convert ( har ) ;
56- const pageXrayRun = pages [ 0 ] ;
57- // green can be true, or a array containing entries
58- let green = [
59- "www.thegreenwebfoundation.org" ,
60- "fonts.googleapis.com" ,
61- "ajax.googleapis.com" ,
62- "assets.digitalclimatestrike.net" ,
63- "cdnjs.cloudflare.com" ,
64- "graphite.thegreenwebfoundation.org" ,
65- "analytics.thegreenwebfoundation.org" ,
66- "fonts.gstatic.com" ,
67- "api.thegreenwebfoundation.org" ,
68- ] ;
69- expect ( co2 . perPage ( pageXrayRun , green ) . toFixed ( 5 ) ) . toBe (
70- TGWF_MIXED_VALUE . toFixed ( 5 )
71- ) ;
72- } ) ;
73- } ) ;
74- describe ( "perDomain" , ( ) => {
75- it ( "shows object listing Co2 for each domain" , ( ) => {
76- const pages = pagexray . convert ( har ) ;
77- const pageXrayRun = pages [ 0 ] ;
78- const res = co2 . perDomain ( pageXrayRun ) ;
79-
80- const domains = [
81- "thegreenwebfoundation.org" ,
82- "www.thegreenwebfoundation.org" ,
83- "maxcdn.bootstrapcdn.com" ,
84- "fonts.googleapis.com" ,
85- "ajax.googleapis.com" ,
86- "assets.digitalclimatestrike.net" ,
87- "cdnjs.cloudflare.com" ,
88- "graphite.thegreenwebfoundation.org" ,
89- "analytics.thegreenwebfoundation.org" ,
90- "fonts.gstatic.com" ,
91- "api.thegreenwebfoundation.org" ,
92- ] ;
93-
94- for ( let obj of res ) {
95- expect ( domains . indexOf ( obj . domain ) ) . toBeGreaterThan ( - 1 ) ;
96- expect ( typeof obj . co2 ) . toBe ( "number" ) ;
97- }
98- } ) ;
99- it ( "shows lower Co2 for green domains" , ( ) => {
100- const pages = pagexray . convert ( har ) ;
101- const pageXrayRun = pages [ 0 ] ;
102-
103- const greenDomains = [
104- "www.thegreenwebfoundation.org" ,
105- "fonts.googleapis.com" ,
106- "ajax.googleapis.com" ,
107- "assets.digitalclimatestrike.net" ,
108- "cdnjs.cloudflare.com" ,
109- "graphite.thegreenwebfoundation.org" ,
110- "analytics.thegreenwebfoundation.org" ,
111- "fonts.gstatic.com" ,
112- "api.thegreenwebfoundation.org" ,
113- ] ;
114- const res = co2 . perDomain ( pageXrayRun ) ;
115- const resWithGreen = co2 . perDomain ( pageXrayRun , greenDomains ) ;
116-
117- for ( let obj of res ) {
118- expect ( typeof obj . co2 ) . toBe ( "number" ) ;
119- }
120- for ( let obj of greenDomains ) {
121- let index = 0 ;
122- expect ( resWithGreen [ index ] . co2 ) . toBeLessThan ( res [ index ] . co2 ) ;
123- index ++ ;
124- }
125- } ) ;
126- } ) ;
127- } ) ;
11+ let co2 ;
12812
12913 describe ( "Sustainable Web Design model as simple option" , ( ) => {
13014 // the SWD model should have slightly higher values as
13115 // we include more of the system in calculations for the
13216 // same levels of data transfer
13317
134- const {
135- MILLION_GREEN ,
136- MILLION_GREY ,
137- TGWF_GREY_VALUE ,
138- TGWF_MIXED_VALUE ,
139- MILLION_PERVISIT_GREY ,
140- MILLION_PERVISIT_GREEN ,
141- } = SWD ;
18+ const { MILLION_PERVISIT_GREY , MILLION_PERVISIT_GREEN } = SWD ;
14219
14320 // We're not passing in a model parameter here to check that SWD is used by default
14421 beforeEach ( ( ) => {
14522 co2 = new CO2 ( ) ;
146- har = JSON . parse (
147- fs . readFileSync (
148- path . resolve ( __dirname , "../data/fixtures/tgwf.har" ) ,
149- "utf8"
150- )
151- ) ;
15223 } ) ;
15324
15425 describe ( "perVisit" , ( ) => {
@@ -173,105 +44,6 @@ describe("co2", () => {
17344 } ) ;
17445 } ) ;
17546
176- describe ( "perPage" , ( ) => {
177- it ( "returns CO2 for total transfer for page" , ( ) => {
178- const pages = pagexray . convert ( har ) ;
179- const pageXrayRun = pages [ 0 ] ;
180-
181- expect ( parseFloat ( co2 . perPage ( pageXrayRun ) . toFixed ( 5 ) ) ) . toBeCloseTo (
182- parseFloat ( TGWF_GREY_VALUE . toFixed ( 5 ) ) ,
183- 3
184- ) ;
185- } ) ;
186- it ( "returns lower CO2 for page served from green site" , ( ) => {
187- const pages = pagexray . convert ( har ) ;
188- const pageXrayRun = pages [ 0 ] ;
189- let green = [
190- "www.thegreenwebfoundation.org" ,
191- "fonts.googleapis.com" ,
192- "ajax.googleapis.com" ,
193- "assets.digitalclimatestrike.net" ,
194- "cdnjs.cloudflare.com" ,
195- "graphite.thegreenwebfoundation.org" ,
196- "analytics.thegreenwebfoundation.org" ,
197- "fonts.gstatic.com" ,
198- "api.thegreenwebfoundation.org" ,
199- ] ;
200- expect ( co2 . perPage ( pageXrayRun , green ) ) . toBeLessThan ( TGWF_GREY_VALUE ) ;
201- } ) ;
202- it ( "returns a lower CO2 number where *some* domains use green power" , ( ) => {
203- const pages = pagexray . convert ( har ) ;
204- const pageXrayRun = pages [ 0 ] ;
205- // green can be true, or a array containing entries
206- let green = [
207- "www.thegreenwebfoundation.org" ,
208- "fonts.googleapis.com" ,
209- "ajax.googleapis.com" ,
210- "assets.digitalclimatestrike.net" ,
211- "cdnjs.cloudflare.com" ,
212- "graphite.thegreenwebfoundation.org" ,
213- "analytics.thegreenwebfoundation.org" ,
214- "fonts.gstatic.com" ,
215- "api.thegreenwebfoundation.org" ,
216- ] ;
217- expect (
218- parseFloat ( co2 . perPage ( pageXrayRun , green ) . toFixed ( 5 ) )
219- ) . toBeCloseTo ( parseFloat ( TGWF_MIXED_VALUE . toFixed ( 5 ) ) , 3 ) ;
220- } ) ;
221- } ) ;
222- describe ( "perDomain" , ( ) => {
223- it ( "shows object listing Co2 for each domain" , ( ) => {
224- const pages = pagexray . convert ( har ) ;
225- const pageXrayRun = pages [ 0 ] ;
226- const res = co2 . perDomain ( pageXrayRun ) ;
227-
228- const domains = [
229- "thegreenwebfoundation.org" ,
230- "www.thegreenwebfoundation.org" ,
231- "maxcdn.bootstrapcdn.com" ,
232- "fonts.googleapis.com" ,
233- "ajax.googleapis.com" ,
234- "assets.digitalclimatestrike.net" ,
235- "cdnjs.cloudflare.com" ,
236- "graphite.thegreenwebfoundation.org" ,
237- "analytics.thegreenwebfoundation.org" ,
238- "fonts.gstatic.com" ,
239- "api.thegreenwebfoundation.org" ,
240- ] ;
241-
242- for ( let obj of res ) {
243- expect ( domains . indexOf ( obj . domain ) ) . toBeGreaterThan ( - 1 ) ;
244- expect ( typeof obj . co2 ) . toBe ( "number" ) ;
245- }
246- } ) ;
247- it ( "shows lower Co2 for green domains" , ( ) => {
248- const pages = pagexray . convert ( har ) ;
249- const pageXrayRun = pages [ 0 ] ;
250-
251- const greenDomains = [
252- "www.thegreenwebfoundation.org" ,
253- "fonts.googleapis.com" ,
254- "ajax.googleapis.com" ,
255- "assets.digitalclimatestrike.net" ,
256- "cdnjs.cloudflare.com" ,
257- "graphite.thegreenwebfoundation.org" ,
258- "analytics.thegreenwebfoundation.org" ,
259- "fonts.gstatic.com" ,
260- "api.thegreenwebfoundation.org" ,
261- ] ;
262- const res = co2 . perDomain ( pageXrayRun ) ;
263- const resWithGreen = co2 . perDomain ( pageXrayRun , greenDomains ) ;
264-
265- for ( let obj of res ) {
266- expect ( typeof obj . co2 ) . toBe ( "number" ) ;
267- }
268- for ( let obj of greenDomains ) {
269- let index = 0 ;
270- expect ( resWithGreen [ index ] . co2 ) . toBeLessThan ( res [ index ] . co2 ) ;
271- index ++ ;
272- }
273- } ) ;
274- } ) ;
27547 describe ( "Returning results by segment" , ( ) => {
27648 const {
27749 MILLION_GREY ,
0 commit comments