@@ -3,15 +3,20 @@ import * as fs from 'fs';
33import * as path from 'path' ;
44import * as vscode from 'vscode' ;
55
6- const RETRY_DELAY = 100 ; // 0.1 seconds
6+ interface TestCase {
7+ input : string | null ;
8+ output : string | null ;
9+ }
10+
11+ const RETRY_DELAY = 3000 ; // 0.1 seconds
712const MAX_RETRIES = 5 ; // Maximum retry attempts
813
914export async function fetchTestCases ( url : string ) : Promise < void > {
1015 const browser = await puppeteer . launch ( { headless : false } ) ; // Run in visible mode for debugging
1116 const page = await browser . newPage ( ) ;
1217
1318 let attempts = 0 ;
14- let testCases = [ ] ;
19+ let testCases : TestCase [ ] = [ ] ;
1520
1621 const baseDirectory = path . join ( __dirname , 'dist' , 'test_cases' ) ;
1722
@@ -83,7 +88,6 @@ export async function fetchTestCases(url: string): Promise<void> {
8388 console . log ( 'Failed to fetch test cases after maximum retries.' ) ;
8489 }
8590}
86-
8791async function fetchTestCasesWithRetry ( page : puppeteer . Page , url : string ) : Promise < any [ ] > {
8892 // Load cookies if available
8993 await loadCookies ( page ) ;
@@ -96,24 +100,50 @@ async function fetchTestCasesWithRetry(page: puppeteer.Page, url: string): Promi
96100 }
97101
98102 console . log ( 'Waiting for test cases to load...' ) ;
103+ let testCases : TestCase [ ] = [ ] ;
99104 try {
100105 await page . waitForSelector ( 'pre' , { timeout : 20000 } ) ; // Wait up to 20 seconds for the <pre> tags
106+ // Extract test cases
107+ testCases = await page . $$eval ( 'pre' , ( elements ) => {
108+ return elements . map ( pre => {
109+ const inputMatch = pre . innerText . match ( / I n p u t : \s * ( .+ ) / ) ;
110+ const outputMatch = pre . innerText . match ( / O u t p u t : \s * ( .+ ) / ) ;
111+ return {
112+ input : inputMatch ? inputMatch [ 1 ] . trim ( ) : null ,
113+ output : outputMatch ? outputMatch [ 1 ] . trim ( ) : null ,
114+ } ;
115+ } ) ;
116+ } ) ;
101117 } catch ( error ) {
102- console . error ( 'Failed to find <pre> tags in time:' , error ) ;
103- return [ ] ; // Return empty array if fetching fails
118+ console . error ( 'Failed to extract test cases using <pre> selector:' , error ) ;
104119 }
105120
106- // Extract test cases
107- const testCases = await page . $$eval ( 'pre' , ( elements ) => {
108- return elements . map ( pre => {
109- const inputMatch = pre . innerText . match ( / I n p u t : \s * ( .+ ) / ) ;
110- const outputMatch = pre . innerText . match ( / O u t p u t : \s * ( .+ ) / ) ;
111- return {
112- input : inputMatch ? inputMatch [ 1 ] . trim ( ) : null ,
113- output : outputMatch ? outputMatch [ 1 ] . trim ( ) : null ,
114- } ;
115- } ) ;
116- } ) ;
121+ if ( testCases . length === 0 ) {
122+ try {
123+ // Fallback to the .example-block structure
124+ console . log ( 'Trying to extract test cases using .example-block structure...' ) ;
125+ await page . waitForSelector ( '.example-block' , { timeout : 5000 } ) ; // Adjust timeout as needed
126+ testCases = await page . $$eval ( '.example-block' , ( blocks ) => {
127+ return blocks . map ( block => {
128+ const inputElement = block . querySelector ( 'strong:contains("Input:") + span.example-io' ) ;
129+ const outputElement = block . querySelector ( 'strong:contains("Output:") + span.example-io' ) ;
130+
131+ return {
132+ input : inputElement ? ( inputElement as HTMLElement ) . innerText . trim ( ) : null ,
133+ output : outputElement ? ( outputElement as HTMLElement ) . innerText . trim ( ) : null ,
134+ } ;
135+ } ) ;
136+ } ) ;
137+
138+ if ( testCases . length > 0 ) {
139+ console . log ( 'Test cases successfully extracted using .example-block structure.' ) ;
140+ } else {
141+ console . log ( 'No test cases found using .example-block structure.' ) ;
142+ }
143+ } catch ( error ) {
144+ console . error ( 'Failed to extract test cases using .example-block structure:' , error ) ;
145+ }
146+ }
117147
118148 return testCases ;
119149}
0 commit comments