import { readFile } from 'fs' ; import { resolve as resolvePath } from 'path' ; import { compareURLPatterns } from '../../src/utils/url' ; import { parseArray , formatArray , getTextDiffIndex , getTextPositionMessage } from '../../src/utils/text' ; import { parseInversionFixes , formatInversionFixes } from '../../src/generators/css-filter' ; import { parseDynamicThemeFixes , formatDynamicThemeFixes } from '../../src/generators/dynamic-theme' ; import { parseStaticThemes , formatStaticThemes } from '../../src/generators/static-theme' ; import type { StaticTheme } from '../../src/definitions' ; import { ParseColorSchemeConfig } from '../../src/utils/colorscheme-parser' ; function readConfig ( fileName ) { return new Promise < string > ( ( resolve , reject ) => { readFile ( resolvePath ( __dirname , '../../src/config/' , fileName ) , { encoding : 'utf-8' } , ( err , data ) => { if ( err ) { reject ( err ) ; return ; } resolve ( data ) ; } ) ; } ) ; } function isURLPatternValid ( url ) { return url . length > 0 && url . indexOf ( '://' ) < 0 ; } function throwIfDifferent ( input , expected , message ) { return ( ) => { const diffIndex = getTextDiffIndex ( input , expected ) ; if ( diffIndex >= 0 ) { throw new Error ( `${ message }\n${ getTextPositionMessage ( input , diffIndex ) }` ) ; } } ; } test ( 'Dark Sites list' , async ( ) => { const file = await readConfig ( 'dark-sites.config' ) ; expect ( file . indexOf ( '\r' ) ) . toEqual ( - 1 ) ; const sites = parseArray ( file ) ; expect ( sites . length ) . toBeGreaterThan ( 0 ) ; expect ( sites . every ( isURLPatternValid ) ) . toBe ( true ) ; expect ( sites ) . toEqual ( sites . slice ( ) . sort ( compareURLPatterns ) ) ; expect ( throwIfDifferent ( file , formatArray ( sites ) , 'Dark Sites list format error' ) ) . not . toThrow ( ) ; } ) ; test ( 'Dynamic Theme Fixes config' , async ( ) => { const file = await readConfig ( 'dynamic-theme-fixes.config' ) ; expect ( file . indexOf ( '\r' ) ) . toEqual ( - 1 ) ; const fixes = parseDynamicThemeFixes ( file ) ; expect ( fixes [ 0 ] . url [ 0 ] ) . toEqual ( '*' ) ; expect ( fixes . every ( ( { url } ) => url . every ( isURLPatternValid ) ) ) . toBe ( true ) ; expect ( fixes . map ( ( { url } ) => url [ 0 ] ) ) . toEqual ( fixes . map ( ( { url } ) => url [ 0 ] ) . sort ( compareURLPatterns ) ) ; const commaSelector = /\,(?![^\(|\"]*(\)|\"))/ ; expect ( fixes . every ( ( { invert , ignoreInlineStyle , ignoreImageAnalysis } ) => ( invert || [ ] ) . concat ( ignoreInlineStyle || [ ] ) . concat ( ignoreImageAnalysis || [ ] ) . every ( ( s ) => ! commaSelector . test ( s ) ) ) ) . toBe ( true ) ; expect ( throwIfDifferent ( file , formatDynamicThemeFixes ( fixes ) , 'Dynamic fixes format error' ) ) . not . toThrow ( ) ; expect ( parseDynamicThemeFixes ( '' ) ) . toEqual ( [ ] ) ; expect ( parseDynamicThemeFixes ( [ 'inbox.google.com' , 'mail.google.com' , 'INVERT' , 'a' , 'b' , 'CSS' , '.x { color: white !important; }' , 'UNSUPPORTED' , 'c' , 'd' , '========' , 'twitter.com' , 'UNSUPPORTED' , 'a' , 'b' , 'INVERT' , 'c' , 'd' , '========' , 'wikipedia.org' , 'IGNORE INLINE STYLE' , 'a' , 'b' , '========' , 'duckduckgo.com' , 'IGNORE IMAGE ANALYSIS' , 'img[alt="Logo"]' , 'canvas' , ] . join ( '\n' ) ) ) . toEqual ( [ { url : [ 'inbox.google.com' , 'mail.google.com' ] , invert : [ 'a' , 'b' ] , css : '.x { color: white !important; }' } , { url : [ 'twitter.com' ] , invert : [ 'c' , 'd' ] } , { url : [ 'wikipedia.org' ] , ignoreInlineStyle : [ 'a' , 'b' ] } , { url : [ 'duckduckgo.com' ] , ignoreImageAnalysis : [ 'img[alt="Logo"]' , 'canvas' ] } , ] ) ; } ) ; test ( 'Inversion Fixes config' , async ( ) => { const file = await readConfig ( 'inversion-fixes.config' ) ; expect ( file . indexOf ( '\r' ) ) . toEqual ( - 1 ) ; const fixes = parseInversionFixes ( file ) ; expect ( fixes [ 0 ] . url [ 0 ] ) . toEqual ( '*' ) ; expect ( fixes . every ( ( { url } ) => url . every ( isURLPatternValid ) ) ) . toBe ( true ) ; expect ( fixes . map ( ( { url } ) => url [ 0 ] ) ) . toEqual ( fixes . map ( ( { url } ) => url [ 0 ] ) . sort ( compareURLPatterns ) ) ; expect ( fixes . every ( ( { invert , noinvert , removebg } ) => ( invert || [ ] ) . concat ( noinvert || [ ] ) . concat ( removebg || [ ] ) . every ( ( s ) => s . indexOf ( ',' ) < 0 ) ) ) . toBe ( true ) ; expect ( throwIfDifferent ( file , formatInversionFixes ( fixes ) , 'Inversion fixes format error' ) ) . not . toThrow ( ) ; } ) ; test ( 'Static Themes config' , async ( ) => { const file = await readConfig ( 'static-themes.config' ) ; expect ( file . indexOf ( '\r' ) ) . toEqual ( - 1 ) ; const themes = parseStaticThemes ( file ) ; expect ( themes [ 0 ] . url [ 0 ] ) . toEqual ( '*' ) ; expect ( themes . every ( ( { url } ) => url . every ( isURLPatternValid ) ) ) . toBe ( true ) ; expect ( themes . map ( ( { url } ) => url [ 0 ] ) ) . toEqual ( themes . map ( ( { url } ) => url [ 0 ] ) . sort ( compareURLPatterns ) ) ; expect ( themes . every ( ( t ) => ( Object . keys ( t ) as Array < keyof StaticTheme > ) . filter ( ( prop ) => [ 'url' , 'noCommon' ] . indexOf ( prop ) < 0 ) . every ( ( prop ) => ( t [ prop ] as string [ ] ) . every ( ( s ) => s . indexOf ( ',' ) < 0 ) ) ) ) . toBe ( true ) ; expect ( throwIfDifferent ( file , formatStaticThemes ( themes ) , 'Static theme format error' ) ) . not . toThrow ( ) ; } ) ; test ( 'Colorscheme config' , async ( ) => { const file = await readConfig ( 'color-schemes.drconf' ) ; expect ( file . indexOf ( '\r' ) ) . toEqual ( - 1 ) ; const { result : schemes , error } = ParseColorSchemeConfig ( file ) ; expect ( error ) . toBeNull ( ) ; expect ( schemes . dark [ 'Default' ] ) . toBeDefined ( ) ; expect ( schemes . light [ 'Default' ] ) . toBeDefined ( ) ; } ) ;