Tests/Test-AppJsonFile.Tests.ps1

Describe Test-AppJsonFile {
    InModuleScope Tecman.Tfs.Tools {
        function New-AppJson {
            Param(
                # path to create the new file in
                [Parameter(Mandatory=$true)]
                [string]
                $Path
            )

            Set-Content -Value '{"name": "testapp", "publisher": "test publisher", "brief": "this is the app brief"}' -Path (Join-Path $Path 'app.json')
        }

        function New-EnvironmentJson {
            Param(
                # path to create the new file in
                [Parameter(Mandatory=$true)]
                [string]
                $Path
            )
            
            Set-Content -Value '{}' -Path (Join-Path $Path 'environment.json')
        }

        Context "When the testapps is empty" {
            Mock Get-EnvironmentKeyValue -ParameterFilter {$KeyName -eq 'testapps'} {return $null}
            New-AppJson -Path $TestDrive
            New-EnvironmentJson -Path $TestDrive
            It "should return true" {
                Test-AppJsonFile -SourcePath $TestDrive | should be $true
            }
        }

        Context "When the testapps is not on app.json" {
            $testapps = @(
                            @{
                                appId = 'dd0be2ea-f733-4d65-bb34-a28f4624fb14' 
                                publisher = 'test publisher'
                                name = 'test app'
                            }
                        ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-EnvironmentKeyValue -ParameterFilter {$KeyName -eq 'testapps'} {return $testapps}

            $dependencies = @(
                                @{
                                    appId = '99feaa86-c421-491a-b585-7bea75fba33b' 
                                    publisher = 'dependency publisher'
                                    name = 'dependency app'
                                }
                            ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-AppKeyValue -ParameterFilter {$KeyName -eq 'dependencies'} {return $dependencies}
            New-AppJson -Path $TestDrive
            New-EnvironmentJson -Path $TestDrive
            It "should return true" {
                Test-AppJsonFile -SourcePath $TestDrive | should be $true
            }
        }

        Context "When the testapps is not on app.json - using id" {
            $testapps = @(
                            @{
                                id = 'dd0be2ea-f733-4d65-bb34-a28f4624fb14' 
                                publisher = 'test publisher'
                                name = 'test app'
                            }
                        ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-EnvironmentKeyValue -ParameterFilter {$KeyName -eq 'testapps'} {return $testapps}

            $dependencies = @(
                                @{
                                    id = '99feaa86-c421-491a-b585-7bea75fba33b' 
                                    publisher = 'dependency publisher'
                                    name = 'dependency app'
                                }
                            ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-AppKeyValue -ParameterFilter {$KeyName -eq 'dependencies'} {return $dependencies}
            New-AppJson -Path $TestDrive
            New-EnvironmentJson -Path $TestDrive
            It "should return true" {
                Test-AppJsonFile -SourcePath $TestDrive | should be $true
            }
        }

        Context "When a testapps is on app.json" {
            $testapps = @( 
                            @{
                                appId = 'dd0be2ea-f733-4d65-bb34-a28f4624fb14' 
                                publisher = 'test publisher'
                                name = 'test app'
                            }
                        ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-EnvironmentKeyValue -ParameterFilter {$KeyName -eq 'testapps'} {return $testapps}

            $dependencies = @(
                                @{
                                    appId = '99feaa86-c421-491a-b585-7bea75fba33b' 
                                    publisher = 'dependency publisher'
                                    name = 'dependency app'
                                },
                                @{
                                    appId = 'dd0be2ea-f733-4d65-bb34-a28f4624fb14' 
                                    publisher = 'test publisher'
                                    name = 'test app'
                                }
                            ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-AppKeyValue -ParameterFilter {$KeyName -eq 'dependencies'} {return $dependencies}

            New-AppJson -Path $TestDrive
            New-EnvironmentJson -Path $TestDrive
            It "should return false" {
                Test-AppJsonFile -SourcePath $TestDrive -SuppressError | should be $false
            }
        }
        Context "When a testapps is on app.json using id" {
            $testapps = @( 
                            @{
                                id = 'dd0be2ea-f733-4d65-bb34-a28f4624fb14' 
                                publisher = 'test publisher'
                                name = 'test app'
                            }
                        ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-EnvironmentKeyValue -ParameterFilter {$KeyName -eq 'testapps'} {return $testapps}

            $dependencies = @(
                                @{
                                    id = '99feaa86-c421-491a-b585-7bea75fba33b' 
                                    publisher = 'dependency publisher'
                                    name = 'dependency app'
                                },
                                @{
                                    id = 'dd0be2ea-f733-4d65-bb34-a28f4624fb14' 
                                    publisher = 'test publisher'
                                    name = 'test app'
                                }
                            ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-AppKeyValue -ParameterFilter {$KeyName -eq 'dependencies'} {return $dependencies}

            New-AppJson -Path $TestDrive
            New-EnvironmentJson -Path $TestDrive
            It "should return false" {
                Test-AppJsonFile -SourcePath $TestDrive -SuppressError | should be $false
            }
        }

        Context "When a Microsoft Tests-TestLibraries on app.json" {
            $dependencies = @(
                                @{
                                    appId = '99feaa86-c421-491a-b585-7bea75fba33b' 
                                    publisher = 'dependency publisher'
                                    name = 'dependency app'
                                },
                                @{
                                    appId = '5d86850b-0d76-4eca-bd7b-951ad998e997' 
                                    publisher = 'Microsoft'
                                    name = 'Tests-TestLibraries'
                                }
                            ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-AppKeyValue -ParameterFilter {$KeyName -eq 'dependencies'} {return $dependencies}
            
            New-AppJson -Path $TestDrive
            New-EnvironmentJson -Path $TestDrive
            It "should return false" {
                Test-AppJsonFile -SourcePath $TestDrive -SuppressError | should be $false
            }
        }

        Context "When a Microsoft Library Assert on app.json" {
            $dependencies = @(
                                @{
                                    appId = '99feaa86-c421-491a-b585-7bea75fba33b' 
                                    publisher = 'dependency publisher'
                                    name = 'dependency app'
                                },
                                @{
                                    appId = 'dd0be2ea-f733-4d65-bb34-a28f4624fb14' 
                                    publisher = 'Microsoft'
                                    name = 'Library Assert'
                                }
                            ) | ConvertTo-Json | ConvertFrom-Json
            Mock Get-AppKeyValue -ParameterFilter {$KeyName -eq 'dependencies'} {return $dependencies}
            
            New-AppJson -Path $TestDrive
            New-EnvironmentJson -Path $TestDrive
            It "should return false" {
                Test-AppJsonFile -SourcePath $TestDrive -SuppressError | should be $false
            }
        }

        Context "When a test key on app.json" {
            New-AppJson -Path $TestDrive
            Set-AppKeyValue -SourcePath $TestDrive -KeyName 'test' -KeyValue '11.0.0.0'
            New-EnvironmentJson -Path $TestDrive
            It "should return false" {
                Test-AppJsonFile -SourcePath $TestDrive -SuppressError | should be $false
            }
        }

        Context "App version matches release branch build number" {
            New-AppJson -Path $TestDrive
            Set-AppKeyValue -SourcePath $TestDrive -KeyName 'version' -KeyValue '1.2.0.0'
            It "should throw an error" {
                Test-AppJsonFile -SourcePath $TestDrive -BuildSourceBranch 'refs/heads/release/1.2' | should be $true
            }
        }

        Context "App version does not match release branch build number" {
            New-AppJson -Path $TestDrive
            Set-AppKeyValue -SourcePath $TestDrive -KeyName 'version' -KeyValue '1.1.0.0'
            It "should throw an error" {
                {Test-AppJsonFile -SourcePath $TestDrive -BuildSourceBranch 'refs/heads/release/1.2' } | should throw
            }
        }

        Context "App version does not match release branch build number, error suppressed" {
            New-AppJson -Path $TestDrive
            Set-AppKeyValue -SourcePath $TestDrive -KeyName 'version' -KeyValue '1.1.0.0'
            It "should throw an error" {
                Test-AppJsonFile -SourcePath $TestDrive -BuildSourceBranch 'refs/heads/release/1.2' -SuppressError | should be $false
            }
        }
    }
}