tests/scour.tests.ps1

Describe "Index creation tests" {

    It "Initializes index" {

        $savedLocation = $pwd.Path

        Set-Location testDocuments

        Remove-Module Scour -ErrorAction Ignore
        Remove-Item __scour -Recurse -Force -ErrorAction Ignore
        Import-Module ..\..\scour

        Initialize-ScourIndex

        $result = Test-Path __scour
        $result | Should be $true

        Set-Location $savedLocation
    }
}

Describe "Location tests" {
    It "Gives good error message with no root directory" {

        $savedLocation = $pwd.Path
        Set-Location C:\

        { Search-ScourContent 'hello world' } |
            Should throw 'Scour has not yet analyzed the current directory or any of its parents. To create a Scour index, run Initialize-ScourIndex.'

        Set-Location $savedLocation
    }

    It "Filters results to a specific directory" {

        $savedLocation = $pwd.Path

        Set-Location testDocuments\Subdir2

        $results = Search-ScourContent final
        @($results).Count | Should be 1

        Set-Location $savedLocation
    }

    It "Filters results to a specific filename" {

        $savedLocation = $pwd.Path
        Set-Location testDocuments

        $result = Search-ScourContent final -Path '*Merriman.tx*'
        $result.Name | Should be 'William Merriman.txt'

        Set-Location $savedLocation
    }
}

Describe "Simple Searches" {

    It "Searches by single keyword, single result" {

        $savedLocation = $pwd.Path
        Set-Location testDocuments

        $result = Search-ScourContent Province
        $result.Name | Should be 'Suyu Parina.txt'

        Set-Location $savedLocation
    }

    It "Searches by single keyword, multiple results" {

        $savedLocation = $pwd.Path
        Set-Location testDocuments

        $result = Search-ScourContent final
        $result.Name -contains 'William Merriman.txt' | Should be $true
        $result.Name -contains 'Work All Week.txt' | Should be $true

        Set-Location $savedLocation
    }

    It "Searches with advanced syntax (fuzzy)" {

        $savedLocation = $pwd.Path
        Set-Location testDocuments

        $result = Search-ScourContent award~
        $result.Name | should be 'Work All Week.txt'

        Set-Location $savedLocation
    }

    It "Supports field searches" {

        $savedLocation = $pwd.Path
        Set-Location testDocuments

        $result = Search-ScourContent 'path:(all week)'
        $result.Name | should be 'Work All Week.txt'

        Set-Location $savedLocation        
    }

    It "Supports field searches" {

        $savedLocation = $pwd.Path
        Set-Location testDocuments

        $result = Search-ScourContent 'path:subdir1'
        @($result).Count | should be 2

        Set-Location $savedLocation        
    }
}