11import { vol } from 'memfs' ;
2+ import { readFile } from 'node:fs/promises' ;
23import { join } from 'node:path' ;
4+ import { getPortalComparisonLink } from '@code-pushup/portal-client' ;
35import { Commit , Report , reportsDiffSchema } from '@code-pushup/models' ;
46import {
57 COMMIT_ALT_MOCK ,
@@ -13,6 +15,11 @@ import { Diff, fileExists, readJsonFile } from '@code-pushup/utils';
1315import { compareReportFiles , compareReports } from './compare' ;
1416
1517describe ( 'compareReportFiles' , ( ) => {
18+ const commitShas = {
19+ before : MINIMAL_REPORT_MOCK . commit ! . hash ,
20+ after : REPORT_MOCK . commit ! . hash ,
21+ } ;
22+
1623 beforeEach ( ( ) => {
1724 vol . fromJSON (
1825 {
@@ -30,6 +37,7 @@ describe('compareReportFiles', () => {
3037 after : join ( MEMFS_VOLUME , 'target-report.json' ) ,
3138 } ,
3239 { outputDir : MEMFS_VOLUME , filename : 'report' , format : [ 'json' ] } ,
40+ undefined ,
3341 ) ;
3442
3543 const reportsDiffPromise = readJsonFile (
@@ -48,6 +56,7 @@ describe('compareReportFiles', () => {
4856 after : join ( MEMFS_VOLUME , 'target-report.json' ) ,
4957 } ,
5058 { outputDir : MEMFS_VOLUME , filename : 'report' , format : [ 'json' , 'md' ] } ,
59+ undefined ,
5160 ) ;
5261
5362 await expect (
@@ -57,6 +66,116 @@ describe('compareReportFiles', () => {
5766 fileExists ( join ( MEMFS_VOLUME , 'report-diff.md' ) ) ,
5867 ) . resolves . toBeTruthy ( ) ;
5968 } ) ;
69+
70+ it ( 'should include portal link (fetched using upload config) in Markdown file' , async ( ) => {
71+ await compareReportFiles (
72+ {
73+ before : join ( MEMFS_VOLUME , 'source-report.json' ) ,
74+ after : join ( MEMFS_VOLUME , 'target-report.json' ) ,
75+ } ,
76+ { outputDir : MEMFS_VOLUME , filename : 'report' , format : [ 'json' , 'md' ] } ,
77+ {
78+ server : 'https://api.code-pushup.dev/graphql' ,
79+ apiKey : 'cp_XXXXX' ,
80+ organization : 'dunder-mifflin' ,
81+ project : 'website' ,
82+ } ,
83+ ) ;
84+
85+ await expect (
86+ readFile ( join ( MEMFS_VOLUME , 'report-diff.md' ) , 'utf8' ) ,
87+ ) . resolves . toContain (
88+ `[🕵️ See full comparison in Code PushUp portal 🔍](https://code-pushup.example.com/portal/dunder-mifflin/website/comparison/${ commitShas . before } /${ commitShas . after } )` ,
89+ ) ;
90+
91+ expect ( getPortalComparisonLink ) . toHaveBeenCalledWith <
92+ Parameters < typeof getPortalComparisonLink >
93+ > ( {
94+ server : 'https://api.code-pushup.dev/graphql' ,
95+ apiKey : 'cp_XXXXX' ,
96+ parameters : {
97+ organization : 'dunder-mifflin' ,
98+ project : 'website' ,
99+ before : commitShas . before ,
100+ after : commitShas . after ,
101+ } ,
102+ } ) ;
103+ } ) ;
104+
105+ it ( 'should not include portal link in Markdown if upload config is missing' , async ( ) => {
106+ await compareReportFiles (
107+ {
108+ before : join ( MEMFS_VOLUME , 'source-report.json' ) ,
109+ after : join ( MEMFS_VOLUME , 'target-report.json' ) ,
110+ } ,
111+ { outputDir : MEMFS_VOLUME , filename : 'report' , format : [ 'json' , 'md' ] } ,
112+ undefined ,
113+ ) ;
114+
115+ await expect (
116+ readFile ( join ( MEMFS_VOLUME , 'report-diff.md' ) , 'utf8' ) ,
117+ ) . resolves . not . toContain (
118+ '[🕵️ See full comparison in Code PushUp portal 🔍]' ,
119+ ) ;
120+
121+ expect ( getPortalComparisonLink ) . not . toHaveBeenCalled ( ) ;
122+ } ) ;
123+
124+ it ( 'should not include portal link in Markdown if report has no associated commits' , async ( ) => {
125+ vol . fromJSON (
126+ {
127+ 'source-report.json' : JSON . stringify ( {
128+ ...MINIMAL_REPORT_MOCK ,
129+ commit : null ,
130+ } satisfies Report ) ,
131+ 'target-report.json' : JSON . stringify ( REPORT_MOCK ) ,
132+ } ,
133+ MEMFS_VOLUME ,
134+ ) ;
135+ await compareReportFiles (
136+ {
137+ before : join ( MEMFS_VOLUME , 'source-report.json' ) ,
138+ after : join ( MEMFS_VOLUME , 'target-report.json' ) ,
139+ } ,
140+ { outputDir : MEMFS_VOLUME , filename : 'report' , format : [ 'json' , 'md' ] } ,
141+ {
142+ server : 'https://api.code-pushup.dev/graphql' ,
143+ apiKey : 'cp_XXXXX' ,
144+ organization : 'dunder-mifflin' ,
145+ project : 'website' ,
146+ } ,
147+ ) ;
148+
149+ await expect (
150+ readFile ( join ( MEMFS_VOLUME , 'report-diff.md' ) , 'utf8' ) ,
151+ ) . resolves . not . toContain (
152+ '[🕵️ See full comparison in Code PushUp portal 🔍]' ,
153+ ) ;
154+
155+ expect ( getPortalComparisonLink ) . not . toHaveBeenCalled ( ) ;
156+ } ) ;
157+
158+ it ( 'should not fetch portal link if Markdown not included in formats' , async ( ) => {
159+ await compareReportFiles (
160+ {
161+ before : join ( MEMFS_VOLUME , 'source-report.json' ) ,
162+ after : join ( MEMFS_VOLUME , 'target-report.json' ) ,
163+ } ,
164+ { outputDir : MEMFS_VOLUME , filename : 'report' , format : [ 'json' ] } ,
165+ {
166+ server : 'https://api.code-pushup.dev/graphql' ,
167+ apiKey : 'cp_XXXXX' ,
168+ organization : 'dunder-mifflin' ,
169+ project : 'website' ,
170+ } ,
171+ ) ;
172+
173+ expect ( getPortalComparisonLink ) . not . toHaveBeenCalled ( ) ;
174+
175+ await expect (
176+ fileExists ( join ( MEMFS_VOLUME , 'report-diff.md' ) ) ,
177+ ) . resolves . toBeFalsy ( ) ;
178+ } ) ;
60179} ) ;
61180
62181describe ( 'compareReports' , ( ) => {
0 commit comments