|
| 1 | +import { blog, docs, learn } from '../../../src/lib/source'; |
| 2 | + |
| 3 | +export interface StaticRoute { |
| 4 | + url: string; |
| 5 | + params: Record<string, string | string[]>; |
| 6 | + type: 'docs' | 'blog' | 'learn'; |
| 7 | +} |
| 8 | + |
| 9 | +/** |
| 10 | + * Extract all static routes that should be generated by generateStaticParams |
| 11 | + */ |
| 12 | +export function getAllStaticRoutes(): StaticRoute[] { |
| 13 | + const routes: StaticRoute[] = []; |
| 14 | + |
| 15 | + // Get docs routes - use the actual URLs from the source |
| 16 | + try { |
| 17 | + const docsPages = docs.getPages(); |
| 18 | + for (const page of docsPages) { |
| 19 | + routes.push({ |
| 20 | + url: page.url, |
| 21 | + params: { slug: page.slugs }, |
| 22 | + type: 'docs', |
| 23 | + }); |
| 24 | + } |
| 25 | + } catch (error) { |
| 26 | + console.error('Failed to get docs pages:', error); |
| 27 | + } |
| 28 | + |
| 29 | + // Get blog routes |
| 30 | + try { |
| 31 | + const blogPages = blog.getPages(); |
| 32 | + for (const page of blogPages) { |
| 33 | + routes.push({ |
| 34 | + url: page.url, |
| 35 | + params: { slug: page.slugs }, |
| 36 | + type: 'blog', |
| 37 | + }); |
| 38 | + } |
| 39 | + } catch (error) { |
| 40 | + console.error('Failed to get blog pages:', error); |
| 41 | + } |
| 42 | + |
| 43 | + // Get learn routes - use the actual URLs from the source |
| 44 | + try { |
| 45 | + const learnPages = learn.getPages(); |
| 46 | + for (const page of learnPages) { |
| 47 | + routes.push({ |
| 48 | + url: page.url, |
| 49 | + params: { slug: page.slugs }, |
| 50 | + type: 'learn', |
| 51 | + }); |
| 52 | + } |
| 53 | + } catch (error) { |
| 54 | + console.error('Failed to get learn pages:', error); |
| 55 | + } |
| 56 | + |
| 57 | + return routes; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Group routes by type for easier testing |
| 62 | + */ |
| 63 | +export function getRoutesByType() { |
| 64 | + const allRoutes = getAllStaticRoutes(); |
| 65 | + |
| 66 | + return { |
| 67 | + docs: allRoutes.filter((route) => route.type === 'docs'), |
| 68 | + blog: allRoutes.filter((route) => route.type === 'blog'), |
| 69 | + learn: allRoutes.filter((route) => route.type === 'learn'), |
| 70 | + all: allRoutes, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Get a sample of routes for quick testing |
| 76 | + */ |
| 77 | +export function getSampleRoutes(limit = 10): StaticRoute[] { |
| 78 | + const routes = getAllStaticRoutes(); |
| 79 | + |
| 80 | + // Get a balanced sample from each type |
| 81 | + const sampleSize = Math.ceil(limit / 3); |
| 82 | + const routesByType = getRoutesByType(); |
| 83 | + |
| 84 | + return [ |
| 85 | + ...routesByType.docs.slice(0, sampleSize), |
| 86 | + ...routesByType.blog.slice(0, sampleSize), |
| 87 | + ...routesByType.learn.slice(0, sampleSize), |
| 88 | + ].slice(0, limit); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Clean URL by removing fragments that might cause navigation issues |
| 93 | + */ |
| 94 | +export function cleanUrl(url: string): string { |
| 95 | + // Remove URL fragments (#) that can cause timeouts in tests |
| 96 | + return url.split('#')[0]; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Check if a URL has fragments that might cause navigation issues |
| 101 | + */ |
| 102 | +export function hasUrlFragments(url: string): boolean { |
| 103 | + return url.includes('#'); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Get routes suitable for e2e testing (clean URLs without fragments) |
| 108 | + */ |
| 109 | +export function getTestableRoutes(): StaticRoute[] { |
| 110 | + return getAllStaticRoutes().map((route) => ({ |
| 111 | + ...route, |
| 112 | + url: cleanUrl(route.url), |
| 113 | + })); |
| 114 | +} |
0 commit comments