Tests/Reporting.Tests.ps1

# Source the function to be tested
. "C:\Users\Dylan\m365\Office365itpros\O365-Toolkit\Public\Reporting.ps1"

Describe 'Get-O365MgAuditRecord' {
    BeforeAll {
        # Mock the Graph API cmdlets
        Mock New-MgBetaSecurityAuditLogQuery {
            return [pscustomobject]@{
                Id = '12345'
            }
        }

        $getMgBetaSecurityAuditLogQueryCallCount = 0
        Mock Get-MgBetaSecurityAuditLogQuery {
            $script:getMgBetaSecurityAuditLogQueryCallCount++
            if ($script:getMgBetaSecurityAuditLogQueryCallCount -eq 1) {
                return [pscustomobject]@{
                    status = 'running'
                }
            } else {
                return [pscustomobject]@{
                    status = 'succeeded'
                }
            }
        }

        Mock Get-MgBetaSecurityAuditLogQueryRecord {
            return @(
                [pscustomobject]@{
                    Service         = 'SharePoint'
                    CreatedDateTime = (Get-Date)
                    userPrincipalName = 'testuser@test.com'
                    operation       = 'FileModified'
                }
            )
        }
    }

    It 'should return an array of audit records' {
        $result = Get-O365MgAuditRecord -NoGridView
        $result | Should Not BeNullOrEmpty
        $result.Count | Should Be 1
        $result[0].UPN | Should Be 'testuser@test.com'
    }
}