1+ const fs = require ( 'fs' ) ;
2+ const { execSync } = require ( 'child_process' ) ;
3+
4+ function parseInfoToml ( infoPath ) {
5+ const fs = require ( 'fs' ) ;
6+ const content = fs . readFileSync ( infoPath , 'utf8' ) ;
7+
8+ const exercises = [ ] ;
9+ const lines = content . split ( '\n' ) ;
10+
11+ let currentExercise = null ;
12+
13+ for ( const line of lines ) {
14+ const cleanLine = line . trim ( ) ;
15+
16+ if ( cleanLine . startsWith ( '[[exercises]]' ) ) {
17+ if ( currentExercise ) exercises . push ( currentExercise ) ;
18+ currentExercise = { } ;
19+ } else if ( cleanLine . startsWith ( 'name = ' ) ) {
20+ currentExercise . name = cleanLine . match ( / n a m e = " ( .+ ) " / ) [ 1 ] ;
21+ } else if ( cleanLine . startsWith ( 'path = ' ) ) {
22+ currentExercise . path = cleanLine . match ( / p a t h = " ( .+ ) " / ) [ 1 ] ;
23+ } else if ( cleanLine . startsWith ( 'mode = ' ) ) {
24+ currentExercise . mode = cleanLine . match ( / m o d e = " ( .+ ) " / ) [ 1 ] ;
25+ }
26+ }
27+
28+ if ( currentExercice ) exercises . push ( currentExercise ) ;
29+ return exercises ;
30+ }
31+
32+ async function callCairoCoderAPI ( exerciseContent ) {
33+ const response = await fetch ( 'http://localhost:3001/v1/chat/completions' , {
34+ method : 'POST' ,
35+ headers : {
36+ 'Content-Type' : 'application/json'
37+ } ,
38+ body : JSON . stringify ( {
39+ model : 'cairo-coder' ,
40+ messages : [
41+ {
42+ role : 'user' ,
43+ content : `Complete this Cairo exercise by fixing the code. Remove the "// I AM NOT DONE" comment and fix any compilation errors:\n\n${ exerciseContent } `
44+ }
45+ ]
46+ } )
47+ } ) ;
48+
49+ const data = await response . json ( ) ;
50+ return data . choices [ 0 ] . message . content ;
51+ }
52+
53+ async function main ( ) {
54+ // 1. Parser info.toml pour lister tous les exercices
55+ const exercises = parseInfoToml ( './starklings/info.toml' ) ;
56+
57+ let passed = 0 ;
58+ let total = exercises . length ;
59+
60+ for ( const exercise of exercises ) {
61+ console . log ( `Testing ${ exercise . name } ...` ) ;
62+ const exerciseContent = fs . readFileSync ( `./starklings/${ exercise . path } ` , 'utf8' ) ;
63+ const response = await callCairoCoderAPI ( exerciseContent ) ;
64+ fs . writeFileSync ( `./starklings/${ exercise . path } ` , response ) ;
65+
66+ // Tester avec starklings
67+ try {
68+ execSync ( `cd starklings && cargo run -r --bin starklings run ${ exercise . name } ` , { stdio : 'pipe' } ) ;
69+ console . log ( `✅ ${ exercise . name } ` ) ;
70+ passed ++ ;
71+ } catch ( error ) {
72+ console . log ( `❌ ${ exercise . name } ` ) ;
73+ }
74+ }
75+
76+ console . log ( `\nResults: ${ passed } /${ total } exercises passed (${ ( passed / total * 100 ) . toFixed ( 1 ) } %)` ) ;
77+
78+ // Fail CI si moins de 80% de réussite
79+ if ( passed / total < 0.8 ) {
80+ process . exit ( 1 ) ;
81+ }
82+ }
83+
84+ main ( ) . catch ( console . error ) ;
0 commit comments