1+ // Comprehensive test for the TypeScript to JavaScript mapping fix
2+ // This test simulates the full flow from language detection to query pack resolution
3+
4+ const languages = {
5+ javascript : [ "javascript" , "typescript" , "js" , "ts" , "jsx" , "tsx" ] ,
6+ python : [ "python" , "py" ] ,
7+ java : [ "java" ] ,
8+ csharp : [ "csharp" , "c#" , "cs" ] ,
9+ cpp : [ "cpp" , "c++" , "c" , "cc" , "cxx" ] ,
10+ go : [ "go" , "golang" ] ,
11+ ruby : [ "ruby" , "rb" ] ,
12+ swift : [ "swift" ] ,
13+ kotlin : [ "kotlin" , "kt" ] ,
14+ scala : [ "scala" ] ,
15+ } ;
16+
17+ function mapLanguagesToCodeQL ( inputLanguages ) {
18+ const results = [ ] ;
19+ const addedLanguages = new Set ( ) ;
20+
21+ for ( const language of inputLanguages ) {
22+ const lang = language . toLowerCase ( ) ;
23+
24+ // Direct match with CodeQL language
25+ if ( languages [ lang ] && ! addedLanguages . has ( lang ) ) {
26+ results . push ( lang ) ;
27+ addedLanguages . add ( lang ) ;
28+ continue ;
29+ }
30+
31+ // Check if it's an alias for a CodeQL language
32+ for ( const [ codeqlLang , aliases ] of Object . entries ( languages ) ) {
33+ if ( aliases . includes ( lang ) && ! addedLanguages . has ( codeqlLang ) ) {
34+ results . push ( codeqlLang ) ;
35+ addedLanguages . add ( codeqlLang ) ;
36+ break ;
37+ }
38+ }
39+ }
40+
41+ return [ ...new Set ( results ) ] ; // Remove any duplicates just in case
42+ }
43+
44+ // Simulate runLocalScan logic with the fix
45+ function simulateRunLocalScan ( configuredLanguages , githubLanguages ) {
46+ console . log ( '\n--- Simulating runLocalScan ---' ) ;
47+ console . log ( 'Configured languages:' , configuredLanguages ) ;
48+ console . log ( 'GitHub languages:' , githubLanguages ) ;
49+
50+ // Simulate the original logic
51+ let languages = configuredLanguages || [ ] ;
52+ if ( ! languages || languages . length === 0 ) {
53+ languages = mapLanguagesToCodeQL ( githubLanguages ) ;
54+ console . log ( 'Languages after GitHub mapping:' , languages ) ;
55+ }
56+
57+ // Apply the fix: ensure languages are always mapped
58+ languages = mapLanguagesToCodeQL ( languages ) ;
59+ console . log ( 'Final languages after fix:' , languages ) ;
60+
61+ // Simulate the processing loop
62+ for ( const language of languages ) {
63+ console . log ( `Processing language: ${ language } ` ) ;
64+
65+ // Simulate database path generation
66+ const databasePath = `/path/to/databases/repo/${ language } ` ;
67+ console . log ( ` Database path: ${ databasePath } ` ) ;
68+
69+ // Simulate output path generation
70+ const outputPath = `/path/to/results/repo-${ language } -abcd1234.sarif` ;
71+ console . log ( ` Output path: ${ outputPath } ` ) ;
72+
73+ // Simulate query pack resolution
74+ const queryPack = `codeql/${ language } -queries` ;
75+ console . log ( ` Query pack: ${ queryPack } ` ) ;
76+
77+ // Simulate suite path
78+ const suitePath = `${ queryPack } :codeql-suites/${ language } -code-scanning.qls` ;
79+ console . log ( ` Suite path: ${ suitePath } ` ) ;
80+ }
81+
82+ return languages ;
83+ }
84+
85+ console . log ( '=== Comprehensive Test for TypeScript to JavaScript Mapping Fix ===' ) ;
86+
87+ // Test case 1: The original problematic scenario
88+ console . log ( '\n🔍 Test Case 1: Original problematic scenario (auto-detection with TypeScript)' ) ;
89+ const result1 = simulateRunLocalScan ( [ ] , [ "TypeScript" ] ) ;
90+ console . log ( '✅ Expected: javascript-based paths' ) ;
91+ console . log ( '✅ Result:' , result1 . includes ( 'javascript' ) && ! result1 . includes ( 'typescript' ) ? 'PASS' : 'FAIL' ) ;
92+
93+ // Test case 2: Mixed languages from GitHub
94+ console . log ( '\n🔍 Test Case 2: Mixed GitHub languages' ) ;
95+ const result2 = simulateRunLocalScan ( [ ] , [ "TypeScript" , "Python" , "JavaScript" ] ) ;
96+ console . log ( '✅ Expected: javascript and python' ) ;
97+ console . log ( '✅ Result:' , JSON . stringify ( result2 . sort ( ) ) === '["javascript","python"]' ? 'PASS' : 'FAIL' ) ;
98+
99+ // Test case 3: Manual configuration with typescript (edge case)
100+ console . log ( '\n🔍 Test Case 3: Manual typescript configuration' ) ;
101+ const result3 = simulateRunLocalScan ( [ "typescript" ] , [ ] ) ;
102+ console . log ( '✅ Expected: javascript' ) ;
103+ console . log ( '✅ Result:' , JSON . stringify ( result3 ) === '["javascript"]' ? 'PASS' : 'FAIL' ) ;
104+
105+ // Test case 4: Manual configuration with javascript (should still work)
106+ console . log ( '\n🔍 Test Case 4: Manual javascript configuration' ) ;
107+ const result4 = simulateRunLocalScan ( [ "javascript" ] , [ ] ) ;
108+ console . log ( '✅ Expected: javascript' ) ;
109+ console . log ( '✅ Result:' , JSON . stringify ( result4 ) === '["javascript"]' ? 'PASS' : 'FAIL' ) ;
110+
111+ // Test case 5: Mixed manual configuration
112+ console . log ( '\n🔍 Test Case 5: Mixed manual configuration' ) ;
113+ const result5 = simulateRunLocalScan ( [ "typescript" , "python" , "javascript" ] , [ ] ) ;
114+ console . log ( '✅ Expected: javascript and python' ) ;
115+ console . log ( '✅ Result:' , JSON . stringify ( result5 . sort ( ) ) === '["javascript","python"]' ? 'PASS' : 'FAIL' ) ;
116+
117+ console . log ( '\n📋 Summary:' ) ;
118+ console . log ( '- Fix ensures TypeScript always maps to JavaScript' ) ;
119+ console . log ( '- Database paths use correct language (javascript)' ) ;
120+ console . log ( '- Output files use correct language (javascript)' ) ;
121+ console . log ( '- Query packs use correct language (javascript-queries)' ) ;
122+ console . log ( '- Suite paths use correct language (javascript-code-scanning.qls)' ) ;
123+ console . log ( '- Fix works for both auto-detection and manual configuration' ) ;
124+ console . log ( '- Prevents the "codeql/typescript-queries" error' ) ;
0 commit comments