@@ -6,11 +6,24 @@ import { afterAll, assert, beforeAll, describe, expect, test, vi } from 'vitest'
66import { ShowcaseScraper } from '../scripts/libs/showcaseScrapper' ;
77import type { ShowcaseGitHubRepoLink } from '../src/content.config' ;
88
9- // Define a mock query function that will be used by the GraphQL client during the test to return fake data.
10- const { queryMock } = vi . hoisted ( ( ) => ( { queryMock : vi . fn ( ) } ) ) ;
9+ // Define mocks used by the GraphQL client during tests.
10+ const { MockGraphqlResponseError, queryMock } = vi . hoisted ( ( ) => ( {
11+ queryMock : vi . fn ( ) ,
12+ MockGraphqlResponseError : class extends Error {
13+ errors : Array < { type ?: string } > ;
14+
15+ constructor ( errors : Array < { type ?: string } > ) {
16+ super ( 'Mock GraphQL response error' ) ;
17+ this . errors = errors ;
18+ }
19+ } ,
20+ } ) ) ;
1121
1222// Mock the entire GraphQL client to avoid hitting the GitHub API.
13- vi . mock ( '@octokit/graphql' , ( ) => ( { graphql : { defaults : ( ) => queryMock } } ) ) ;
23+ vi . mock ( '@octokit/graphql' , ( ) => ( {
24+ GraphqlResponseError : MockGraphqlResponseError ,
25+ graphql : { defaults : ( ) => queryMock } ,
26+ } ) ) ;
1427
1528// Mock the fs module to avoid writing showcase files to the file system during tests.
1629vi . mock ( 'node:fs/promises' ) ;
@@ -78,6 +91,19 @@ test('should identify GitHub repo links', async () => {
7891 expect ( showcases . at ( 0 ) ?. links ) . toMatchObject ( [ { url : link , type : 'github_repo' } ] ) ;
7992} ) ;
8093
94+ test ( 'should skip GitHub repo links when repository is not found' , async ( ) => {
95+ const missingRepoLink = getTestGitHubLink ( 'user_1' , 'repo_missing' ) ;
96+ const validRepoLink = getTestGitHubLink ( 'user_2' , 'repo_valid' ) ;
97+
98+ const scraper = getTestScrapper ( [ [ { notFound : true , url : missingRepoLink } , validRepoLink ] ] ) ;
99+
100+ const showcases = await scraper . run ( ) ;
101+
102+ expect ( showcases ) . toHaveLength ( 1 ) ;
103+ expect ( showcases . at ( 0 ) ?. links ) . toHaveLength ( 1 ) ;
104+ expect ( showcases . at ( 0 ) ?. links . at ( 0 ) ) . toMatchObject ( { type : 'github_repo' , url : validRepoLink } ) ;
105+ } ) ;
106+
81107test ( 'should handle GitLab links as unknown' , async ( ) => {
82108 const link = getTestGitLabLink ( 'user' ) ;
83109
@@ -350,7 +376,7 @@ describe('GitHub repo link languages', () => {
350376function getTestScrapper ( commentsLinks : TestCommentLinks [ ] ) {
351377 const scraper = new ShowcaseScraper ( 'test-org' , 'test-repo' , 0 , [ ] ) ;
352378
353- let ghRepoLinks : { languages : TestRepoLanguage [ ] ; name : string ; owner : string ; url : string } [ ] = [ ] ;
379+ let ghRepoLinks : { languages : TestRepoLanguage [ ] ; name : string ; notFound ?: boolean ; owner : string ; url : string } [ ] = [ ] ;
354380
355381 const commentsNodes = commentsLinks . map ( ( commentLinks , commentIndex ) => {
356382 const isFlatLinks = Array . isArray ( commentLinks ) ;
@@ -370,7 +396,13 @@ function getTestScrapper(commentsLinks: TestCommentLinks[]) {
370396 const ghLink = gh ( url ) ;
371397
372398 if ( ghLink ?. name && ghLink ?. owner ) {
373- ghRepoLinks . push ( { languages : languages ?? [ ] , name : ghLink . name , owner : ghLink . owner , url } ) ;
399+ ghRepoLinks . push ( {
400+ languages : languages ?? [ ] ,
401+ name : ghLink . name ,
402+ notFound : typeof link === 'string' ? false : link . notFound ,
403+ owner : ghLink . owner ,
404+ url,
405+ } ) ;
374406 }
375407
376408 return `<a href="${ url } ">${ url } </a>` ;
@@ -391,6 +423,11 @@ function getTestScrapper(commentsLinks: TestCommentLinks[]) {
391423
392424 // Each GitHub repository link will trigger a GraphQL query to fetch the repository data so we mock each of them.
393425 for ( const ghRepoLink of ghRepoLinks ) {
426+ if ( ghRepoLink . notFound ) {
427+ queryMock . mockRejectedValueOnce ( new MockGraphqlResponseError ( [ { type : 'NOT_FOUND' } ] ) ) ;
428+ continue ;
429+ }
430+
394431 queryMock . mockReturnValueOnce ( {
395432 repository : {
396433 description : faker . lorem . paragraph ( ) ,
@@ -441,7 +478,13 @@ function getTestRepoLanguageSize() {
441478 return faker . number . int ( 1_000_000 ) ;
442479}
443480
444- type TestCommentLink = string | { languages ?: TestRepoLanguage [ ] ; url : string } ;
481+ type TestGithubRepoLinkOptions = {
482+ languages ?: TestRepoLanguage [ ] ;
483+ notFound ?: boolean ;
484+ url : string ;
485+ } ;
486+
487+ type TestCommentLink = string | TestGithubRepoLinkOptions ;
445488type TestCommentLinks = string [ ] | { author ?: string ; links : TestCommentLink [ ] } ;
446489
447490interface TestRepoLanguage {
0 commit comments