@@ -892,6 +892,105 @@ eq("403 is blocked", classify("https://a.test/", 403, "https://a.test/").kind, "
892892eq ( "404 is dead" , classify ( "https://a.test/" , 404 , "https://a.test/" ) . kind , "dead" ) ;
893893eq ( "report url is an inert code span" , reportUrl ( "https://a.test/@x](y)`z" ) , "`https://a.test/@x](y)%60z`" ) ;
894894
895+ /* transient link-check failures (#275) ------------------------------------ */
896+ await section ( "link-check transient classification (#275)" , async ( ) => {
897+ const url = "https://assets.test/pack" ;
898+ for ( const status of [ 502 , 503 , 504 ] ) {
899+ const result = classify ( url , status , url ) ;
900+ eq ( `HTTP ${ status } is unavailable` , result . kind , "unavailable" ) ;
901+ eq ( `HTTP ${ status } keeps its status` , result . detail , `HTTP ${ status } ` ) ;
902+ eq ( `HTTP ${ status } after a redirect is still unavailable` , classify ( url , status , "https://other.test/" ) . kind , "unavailable" ) ;
903+ }
904+ for ( const status of [ 400 , 404 , 405 , 410 , 422 , 500 , 501 ] ) {
905+ eq ( `HTTP ${ status } stays dead` , classify ( url , status , url ) . kind , "dead" ) ;
906+ }
907+ for ( const status of [ 401 , 403 , 429 ] ) {
908+ eq ( `HTTP ${ status } stays blocked` , classify ( url , status , url ) . kind , "blocked" ) ;
909+ }
910+ eq ( "HTTP 200 stays ok" , classify ( url , 200 , url ) . kind , "ok" ) ;
911+ } ) ;
912+
913+ await section ( "link-check transient report (#275)" , async ( ) => {
914+ const { execFileSync } = await import ( "node:child_process" ) ;
915+ const { pathToFileURL } = await import ( "node:url" ) ;
916+ const root = nodeFs . mkdtempSync ( nodePath . join ( nodeOs . tmpdir ( ) , "fgda-links-" ) ) ;
917+ try {
918+ // Run the real CLI against a tiny catalog, with all network I/O stubbed
919+ // in a child process so neither the real catalog nor other tests change.
920+ nodeFs . cpSync ( new URL ( "../" , import . meta. url ) , nodePath . join ( root , "site" ) , {
921+ recursive : true ,
922+ filter : ( src ) => nodeFs . statSync ( src ) . isDirectory ( )
923+ ? ! [ "public" , "dist" ] . includes ( nodePath . basename ( src ) )
924+ : src . endsWith ( ".mjs" ) || nodePath . basename ( src ) === "config.json" ,
925+ } ) ;
926+ const catalog = nodePath . join ( root , "catalog" ) ;
927+ nodeFs . mkdirSync ( catalog ) ;
928+ const put = ( id , url , evidence = "" , heading = "Evidence" ) => nodeFs . writeFileSync ( nodePath . join ( catalog , `${ id } .md` ) ,
929+ `---\nid: ${ id } \nurl: ${ url } \nstatus: active\n---\n\n## ${ heading } \n\n${ evidence } \n` ) ;
930+ for ( const status of [ 502 , 503 , 504 ] ) {
931+ put ( `source-${ status } ` , `https://assets.test/source-${ status } ` , `https://assets.test/licence-${ status } ` , status === 504 ? "Notes" : "Evidence" ) ;
932+ }
933+ put ( "recovered" , "https://assets.test/recovered-503" ) ;
934+ const preload = nodePath . join ( root , "network.mjs" ) ;
935+ nodeFs . writeFileSync ( preload , `
936+ import dns from "node:dns/promises";
937+ import { syncBuiltinESMExports } from "node:module";
938+ dns.lookup = async () => [{ address: "8.8.8.8", family: 4 }];
939+ syncBuiltinESMExports();
940+ const calls = new Map();
941+ globalThis.fetch = async (url) => {
942+ const u = new URL(url);
943+ if (u.hostname === "api.github.com") return new Response(null, { status: 503 });
944+ if (u.hostname !== "assets.test") throw new Error("unexpected test host");
945+ const n = (calls.get(url) || 0) + 1;
946+ calls.set(url, n);
947+ const status = u.pathname.includes("recovered") && n > 1 ? 200 : Number(u.pathname.match(/(\\d+)$/)[1]);
948+ return new Response(null, { status });
949+ };
950+ ` ) ;
951+ const output = nodePath . join ( root , "github-output" ) ;
952+ const reportPath = nodePath . join ( root , "report.md" ) ;
953+ const run = ( ) => {
954+ nodeFs . writeFileSync ( output , "" ) ;
955+ execFileSync ( process . execPath , [ "--import" , pathToFileURL ( preload ) . href , nodePath . join ( root , "site/check-links.mjs" ) , "--report" , reportPath ] , {
956+ env : { ...process . env , GITHUB_OUTPUT : output , GITHUB_TOKEN : "" } ,
957+ stdio : [ "ignore" , "pipe" , "pipe" ] , timeout : 10000 ,
958+ } ) ;
959+ return nodeFs . readFileSync ( reportPath , "utf8" ) ;
960+ } ;
961+ const transient = run ( ) ;
962+ has ( "transient report: no dead sources" , transient , "### Dead (0)" ) ;
963+ has ( "transient report: no dead licence pages" , transient , "### Pages quoted in Evidence or linked in Notes that no longer load (0)" ) ;
964+ has ( "transient report: sources and licence pages could not be checked" , transient , "### Could not check (6 skipped)" ) ;
965+ const skipped = transient . split ( "### Could not check" ) [ 1 ] || "" ;
966+ for ( const status of [ 502 , 503 , 504 ] ) {
967+ has ( `transient report: source HTTP ${ status } ` , skipped , `\`https://assets.test/source-${ status } \` (HTTP ${ status } )` ) ;
968+ has ( `transient report: licence HTTP ${ status } ` , skipped , `\`https://assets.test/licence-${ status } \` (HTTP ${ status } )` ) ;
969+ }
970+ lacks ( "transient report: a successful retry is not reported" , transient , "https://assets.test/recovered-503" ) ;
971+ eq ( "transient report: no sources need a look" , nodeFs . readFileSync ( output , "utf8" ) , "problems=0\n" ) ;
972+
973+ for ( const status of [ 404 , 410 ] ) {
974+ put ( `dead-${ status } ` , `https://assets.test/source-${ status } ` , `https://assets.test/licence-${ status } ` ) ;
975+ }
976+ put ( "api-failure" , "https://github.com/o/r" ) ;
977+ // GitHub's page is healthy, but its API is temporarily unavailable.
978+ nodeFs . appendFileSync ( preload , `
979+ const stubFetch = globalThis.fetch;
980+ globalThis.fetch = async (url) => new URL(url).hostname === "github.com"
981+ ? new Response(null, { status: 200 }) : stubFetch(url);
982+ ` ) ;
983+ const mixed = run ( ) ;
984+ has ( "mixed report: 404/410 sources stay dead" , mixed , "### Dead (2)" ) ;
985+ has ( "mixed report: 404/410 licence pages stay dead" , mixed , "### Pages quoted in Evidence or linked in Notes that no longer load (2)" ) ;
986+ has ( "mixed report: transient answers and API failures stay visible" , mixed , "### Could not check (7 skipped)" ) ;
987+ has ( "mixed report: API failure keeps its reason" , mixed , "GitHub API HTTP 503 for o/r" ) ;
988+ eq ( "mixed report: only dead sources and licence pages need a look" , nodeFs . readFileSync ( output , "utf8" ) , "problems=4\n" ) ;
989+ } finally {
990+ nodeFs . rmSync ( root , { recursive : true , force : true } ) ;
991+ }
992+ } ) ;
993+
895994// #64: one entry walker; hidden and scratch folders are not entries.
896995const tmp = nodeFs . mkdtempSync ( nodePath . join ( nodeOs . tmpdir ( ) , "entries-" ) ) ;
897996for ( const rel of [ "2d/a.md" , "2d/README.md" , "2d/TEMPLATE.md" , "2d/.hidden/b.md" , "_scratch/c.md" , "3d/sub/d.md" , "3d/e.txt" ] ) {
0 commit comments