import shell from 'shelljs' ; import { execSync } from 'child_process' ; import { getWithPromise , getVersionEffective , resolveApp , processError } from '../src/util' ; const mockExit = jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ) => { } ) ; test ( 'processError can exit process' , ( ) => { processError ( 'error' ) ; expect ( process . exit ) . toBeCalled ( ) ; } ) ; describe ( 'test getWithPromise' , ( ) => { test ( 'hope getWithPromise can get true result' , async ( ) => { const data = await getWithPromise ( 'https://registry.npm.taobao.org/ssr-with-js' ) ; expect ( data . name ) . toEqual ( 'ssr-with-js' ) ; } ) ; test ( 'hope timeout error can be invoke' , async ( ) => { try { await getWithPromise ( 'https://registry.npm.taobao.org/ssr-with-js' , 1 ) ; } catch ( error ) { expect ( error ) . toContain ( 'url request timeout' ) ; } } ) ; test ( 'request error url should reject' , async ( ) => { try { await getWithPromise ( 'https://encrypte1d.google.com/' ) ; } catch ( error ) { expect ( error . code ) . toEqual ( 'ENOTFOUND' ) ; } } ) ; } ) ; describe ( 'test getVersionEffective without cache' , ( ) => { test ( 'no cahe can return false' , async ( ) => { const data = await getVersionEffective ( { appName : 'app' , language : 'javascript' } ) ; expect ( data ) . toEqual ( false ) ; } ) ; } ) ; describe ( 'test getVersionEffective with cache' , ( ) => { beforeEach ( ( ) => { jest . resetModules ( ) ; shell . mkdir ( '-p' , resolveApp ( './cache/example/ssr-with-js' ) ) ; shell . touch ( resolveApp ( `./cache/example/ssr-with-js/package.json` ) ) ; } ) ; test ( 'cahe expired should return false' , async ( ) => { execSync ( `echo '{"version":"1.0.0"}' > ${ resolveApp ( `./cache/example/ssr-with-js/package.json` ) }` ) ; const data = await getVersionEffective ( { appName : 'app' , language : 'javascript' } ) ; expect ( data ) . toEqual ( false ) ; } ) ; test ( 'cahe not expire should return true' , async ( ) => { const { 'dist-tags' : { latest } } = await getWithPromise ( 'https://registry.npm.taobao.org/ssr-with-js' ) ; execSync ( `echo '{"version": "${ latest }"}' > ${ resolveApp ( `./cache/example/ssr-with-js/package.json` ) }` ) ; const data = await getVersionEffective ( { appName : 'app' , language : 'javascript' } ) ; expect ( data ) . toEqual ( true ) ; } ) ; test ( 'throw error should return true' , async ( ) => { const data = await getVersionEffective ( { appName : 'app' , language : 'javascript' } ) ; expect ( data ) . toEqual ( true ) ; } ) ; afterEach ( ( ) => { shell . rm ( '-rf' , resolveApp ( './cache' ) ) ; } ) ; } ) ;