import 'mocha' ; import { expect } from 'chai' ; import { IBrowserOsDetector } from '@/application/Environment/BrowserOs/IBrowserOsDetector' ; import { OperatingSystem } from '@/domain/OperatingSystem' ; import { Environment , IEnvironmentVariables } from '@/application/Environment/Environment' ; import { DesktopOsTestCases } from './DesktopOsTestCases' ; interface EnvironmentVariables { window ? : unknown ; process ? : unknown ; navigator ? : unknown ; } class SystemUnderTest extends Environment { constructor ( variables , browserOsDetector ? ) { super ( variables as unknown as IEnvironmentVariables , browserOsDetector ) ; } } describe ( 'Environment' , ( ) => { describe ( 'isDesktop' , ( ) => { it ( 'returns true if process type is renderer' , ( ) => { const window = { process : { type : 'renderer' , } , } ; const sut = new SystemUnderTest ( { window } ) ; expect ( sut . isDesktop ) . to . equal ( true ) ; } ) ; it ( 'returns true if electron is defined as process version' , ( ) => { const process = { versions : { electron : true , } , } ; const sut = new SystemUnderTest ( { process } ) ; expect ( sut . isDesktop ) . to . equal ( true ) ; } ) ; it ( 'returns true if navigator user agent has electron' , ( ) => { const navigator = { userAgent : 'Electron' , } ; const sut = new SystemUnderTest ( { navigator } ) ; expect ( sut . isDesktop ) . to . equal ( true ) ; } ) ; it ( 'returns false as default' , ( ) => { const sut = new SystemUnderTest ( { } ) ; expect ( sut . isDesktop ) . to . equal ( false ) ; } ) ; } ) ; describe ( 'os' , ( ) => { it ( 'returns undefined without user agent' , ( ) => { const expected = undefined ; const mock = { detect : ( ) => { throw new Error ( 'should not reach here' ) ; } , } ; const sut = new SystemUnderTest ( { } , mock ) ; const actual = sut . os ; expect ( actual ) . to . equal ( expected ) ; } ) ; it ( 'browser os from BrowserOsDetector' , ( ) => { const givenUserAgent = 'testUserAgent' ; const expected = OperatingSystem . macOS ; const window = { navigator : { userAgent : givenUserAgent , } , } ; const mock = { detect : ( agent ) => { if ( agent !== givenUserAgent ) { throw new Error ( 'Unexpected user agent' ) ; } return expected ; } , } ; const sut = new SystemUnderTest ( { window } , mock ) ; const actual = sut . os ; expect ( actual ) . to . equal ( expected ) ; } ) ; describe ( 'desktop os' , ( ) => { const navigator = { userAgent : 'Electron' , } ; for ( const testCase of DesktopOsTestCases ) { it ( testCase . processPlatform , ( ) => { const process = { platform : testCase . processPlatform , } ; const sut = new SystemUnderTest ( { navigator , process } ) ; expect ( sut . os ) . to . equal ( testCase . expectedOs , printMessage ( ) ) ; function printMessage ( ) { return `Expected: "${ OperatingSystem [ testCase . expectedOs ] }"\n` + `Actual: "${ OperatingSystem [ sut . os ] }"\n` + `Platform: "${ testCase . processPlatform }"` ; } } ) ; } } ) ; } ) ; } ) ;