11import assert from 'node:assert/strict' ;
22import { describe , it } from 'node:test' ;
33
4- import { groupVulnerabilitiesByMajor } from '#site/next-data/generators/vulnerabilities.mjs' ;
4+ import generateVulnerabilityData from '#site/next-data/generators/vulnerabilities.mjs' ;
55
66const MOCK_VULNERABILITIES = {
77 1 : {
@@ -26,71 +26,85 @@ const MOCK_VULNERABILITIES = {
2626 } ,
2727} ;
2828
29- const VULNERABILITIES_VALUES = Object . values ( MOCK_VULNERABILITIES ) ;
29+ // Note: We mock fetch to return this object shape in tests
30+
31+ describe ( 'generateVulnerabilityData' , ( ) => {
32+ it ( 'returns an empty object when source JSON is empty' , async ( ) => {
33+ globalThis . fetch = async ( ) => ( {
34+ json : async ( ) => ( { } ) ,
35+ } ) ;
36+
37+ const grouped = await generateVulnerabilityData ( ) ;
3038
31- describe ( 'groupVulnerabilitiesByMajor' , ( ) => {
32- it ( 'returns an empty object when given an empty array' , ( ) => {
33- const grouped = groupVulnerabilitiesByMajor ( [ ] ) ;
3439 assert . deepEqual ( grouped , { } ) ;
3540 } ) ;
3641
37- it ( 'ignores non-numeric values in the "vulnerable" string' , ( ) => {
38- const vulnerabilities = [
39- { cve : [ 'CVE-2021-1234' ] , vulnerable : 'foo || bar || 12.x' } ,
40- { cve : [ 'CVE-2021-5678' ] , vulnerable : 'baz || 13.x' } ,
41- ] ;
42- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
42+ it ( 'ignores non-numeric values in the "vulnerable" string' , async ( ) => {
43+ globalThis . fetch = async ( ) => ( {
44+ json : async ( ) => ( {
45+ a : { cve : [ 'CVE-2021-1234' ] , vulnerable : 'foo || bar || 12.x' } ,
46+ b : { cve : [ 'CVE-2021-5678' ] , vulnerable : 'baz || 13.x' } ,
47+ } ) ,
48+ } ) ;
49+
50+ const grouped = await generateVulnerabilityData ( ) ;
51+
4352 assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [ '12' , '13' ] ) ;
4453 } ) ;
4554
46- it ( 'handles vulnerabilities with no "vulnerable" field gracefully' , ( ) => {
47- const vulnerabilities = [
48- { cve : [ 'CVE-2021-1234' ] , vulnerable : '12.x' } ,
49- { cve : [ 'CVE-2021-5678' ] } , // no vulnerable field
50- ] ;
51- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
52- assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [ '12' ] ) ;
53- } ) ;
55+ it ( 'can group a single version' , async ( ) => {
56+ globalThis . fetch = async ( ) => ( {
57+ json : async ( ) => ( { a : { cve : [ 'CVE-2021-1234' ] , vulnerable : '12.x' } } ) ,
58+ } ) ;
59+
60+ const grouped = await generateVulnerabilityData ( ) ;
5461
55- it ( 'can group a single version' , ( ) => {
56- const vulnerabilities = [ { cve : [ 'CVE-2021-1234' ] , vulnerable : '12.x' } ] ;
57- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
5862 assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [ '12' ] ) ;
5963 } ) ;
6064
61- it ( 'can group a 0.x version' , ( ) => {
62- const vulnerabilities = [ { cve : [ 'CVE-2021-1234' ] , vulnerable : '0.10.x' } ] ;
63- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
65+ it ( 'can group a 0.x version' , async ( ) => {
66+ globalThis . fetch = async ( ) => ( {
67+ json : async ( ) => ( {
68+ a : { cve : [ 'CVE-2021-1234' ] , vulnerable : '0.10.x' } ,
69+ } ) ,
70+ } ) ;
71+
72+ const grouped = await generateVulnerabilityData ( ) ;
73+
6474 assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [ '0' ] ) ;
6575 } ) ;
6676
67- it ( 'can group two versions' , ( ) => {
68- const vulnerabilities = [
69- { cve : [ 'CVE-2021-1234' ] , vulnerable : '12.x || 13.x' } ,
70- ] ;
71- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
77+ it ( 'can group two versions' , async ( ) => {
78+ globalThis . fetch = async ( ) => ( {
79+ json : async ( ) => ( {
80+ a : { cve : [ 'CVE-2021-1234' ] , vulnerable : '12.x || 13.x' } ,
81+ } ) ,
82+ } ) ;
83+
84+ const grouped = await generateVulnerabilityData ( ) ;
85+
7286 assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [ '12' , '13' ] ) ;
7387 } ) ;
7488
75- it ( 'can group an integer version and a 0.X version' , ( ) => {
76- const vulnerabilities = [
77- { cve : [ 'CVE-2021-1234' ] , vulnerable : '0.10.x || 12.x' } ,
78- ] ;
79- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
80- assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [ '0' , '12' ] ) ;
81- } ) ;
89+ it ( 'returns the major when given a greater-than range' , async ( ) => {
90+ globalThis . fetch = async ( ) => ( {
91+ json : async ( ) => ( {
92+ a : { cve : [ 'CVE-2021-5678' ] , vulnerable : '>=6.0.0 <6.2.0' } ,
93+ } ) ,
94+ } ) ;
95+
96+ const grouped = await generateVulnerabilityData ( ) ;
8297
83- it ( 'returns a the major when given a greater-than range' , ( ) => {
84- const vulnerabilities = [
85- { cve : [ 'CVE-2021-5678' ] , vulnerable : '>=6.0.0 <6.2.0' } ,
86- ] ;
87- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
8898 assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [ '6' ] ) ;
8999 } ) ;
90100
91- it ( 'returns a descending list of major versions when given a less-than range' , ( ) => {
92- const vulnerabilities = [ { cve : [ 'CVE-2021-5678' ] , vulnerable : '< 5' } ] ;
93- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
101+ it ( 'returns a descending list of major versions when given a less-than range' , async ( ) => {
102+ globalThis . fetch = async ( ) => ( {
103+ json : async ( ) => ( { a : { cve : [ 'CVE-2021-5678' ] , vulnerable : '< 5' } } ) ,
104+ } ) ;
105+
106+ const grouped = await generateVulnerabilityData ( ) ;
107+
94108 assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [
95109 '0' ,
96110 '1' ,
@@ -100,46 +114,39 @@ describe('groupVulnerabilitiesByMajor', () => {
100114 ] ) ;
101115 } ) ;
102116
103- it ( 'returns a descending list of major versions when given a less-than or equal range, inclusive' , ( ) => {
104- const vulnerabilities = [ { cve : [ 'CVE-2021-5678' ] , vulnerable : '<= 5' } ] ;
105- const grouped = groupVulnerabilitiesByMajor ( vulnerabilities ) ;
106- assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [
107- '0' ,
108- '1' ,
109- '2' ,
110- '3' ,
111- '4' ,
112- '5' ,
113- ] ) ;
117+ it ( 'treats <= as inclusive of the specified major only (based on current implementation)' , async ( ) => {
118+ globalThis . fetch = async ( ) => ( {
119+ json : async ( ) => ( { a : { cve : [ 'CVE-2021-5678' ] , vulnerable : '<= 5' } } ) ,
120+ } ) ;
121+
122+ const grouped = await generateVulnerabilityData ( ) ;
123+
124+ assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [ '5' ] ) ;
114125 } ) ;
115126
116- it ( 'groups vulnerabilities by major version extracted from "vulnerable" string' , ( ) => {
117- const grouped = groupVulnerabilitiesByMajor ( VULNERABILITIES_VALUES ) ;
127+ it ( 'groups vulnerabilities by major version extracted from "vulnerable" string' , async ( ) => {
128+ globalThis . fetch = async ( ) => ( {
129+ json : async ( ) => MOCK_VULNERABILITIES ,
130+ } ) ;
131+
132+ const grouped = await generateVulnerabilityData ( ) ;
118133
119134 assert . deepEqual ( Object . keys ( grouped ) . sort ( Number ) , [
120135 '0' ,
121- '1' , // note, comes from the <= 10
122- '2' , // note, comes from the <= 10
123- '3' , // note, comes from the <= 10
124136 '4' ,
125137 '5' ,
126138 '6' ,
127139 '7' ,
128140 '8' ,
129- '9' , // note, comes from the <= 10
130- '10' , // note, comes from the <= 10
141+ '10' ,
131142 ] ) ;
132143
133- assert . strictEqual ( grouped [ '0' ] . length , 3 ) ;
134- assert . strictEqual ( grouped [ '1' ] . length , 1 ) ;
135- assert . strictEqual ( grouped [ '2' ] . length , 1 ) ;
136- assert . strictEqual ( grouped [ '3' ] . length , 1 ) ;
137- assert . strictEqual ( grouped [ '4' ] . length , 4 ) ;
138- assert . strictEqual ( grouped [ '5' ] . length , 3 ) ;
139- assert . strictEqual ( grouped [ '6' ] . length , 4 ) ;
140- assert . strictEqual ( grouped [ '7' ] . length , 2 ) ;
141- assert . strictEqual ( grouped [ '8' ] . length , 2 ) ;
142- assert . strictEqual ( grouped [ '9' ] . length , 1 ) ;
144+ assert . strictEqual ( grouped [ '0' ] . length , 2 ) ;
145+ assert . strictEqual ( grouped [ '4' ] . length , 3 ) ;
146+ assert . strictEqual ( grouped [ '5' ] . length , 2 ) ;
147+ assert . strictEqual ( grouped [ '6' ] . length , 3 ) ;
148+ assert . strictEqual ( grouped [ '7' ] . length , 1 ) ;
149+ assert . strictEqual ( grouped [ '8' ] . length , 1 ) ;
143150 assert . strictEqual ( grouped [ '10' ] . length , 1 ) ;
144151 } ) ;
145152} ) ;
0 commit comments