quorate/services/frigate/tests.fsx

77 lines
2.6 KiB
FSharp

#load "model.fsx"
open System
open System.IO
open System.Security.Cryptography
open System.Text
let check condition message =
if not condition then
failwith message
let root =
Path.Combine(__SOURCE_DIRECTORY__, ".tests", Guid.NewGuid().ToString("N"))
let destination = Path.Combine(root, "model_cache", "model.onnx")
let bytes = Encoding.UTF8.GetBytes "synthetic model"
let expected = SHA256.HashData bytes |> Convert.ToHexString |> _.ToLowerInvariant()
let mutable downloads = 0
let download (output: Stream) =
downloads <- downloads + 1
output.Write bytes
let verify () =
check (File.ReadAllBytes destination = bytes) "installed bytes differ"
check (not (File.Exists(destination + ".incoming"))) "partial file remains"
try
check (Model.install expected destination download = Model.Installed) "empty cache was not installed"
verify ()
check (downloads = 1) "empty cache download count differs"
let offline (_: Stream) =
failwith "cached model attempted a download"
check (Model.install expected destination offline = Model.Cached) "valid cache was not reused"
verify ()
File.WriteAllText(destination, "corrupt model")
check (Model.install expected destination download = Model.Installed) "corrupt cache was not replaced"
verify ()
check (downloads = 2) "corrupt cache download count differs"
let stale = Encoding.UTF8.GetBytes "retained model"
File.WriteAllBytes(destination, stale)
let mismatch (output: Stream) =
output.Write(Encoding.UTF8.GetBytes "wrong model")
let mutable rejected = false
try
Model.install expected destination mismatch |> ignore
with :? InvalidOperationException ->
rejected <- true
check rejected "checksum mismatch was accepted"
check (File.ReadAllBytes destination = stale) "checksum mismatch replaced the retained model"
check (not (File.Exists(destination + ".incoming"))) "checksum mismatch left a partial file"
let interrupted (output: Stream) =
output.Write(bytes.AsSpan(0, 4))
raise (IOException "synthetic interrupted download")
rejected <- false
try
Model.install expected destination interrupted |> ignore
with :? IOException ->
rejected <- true
check rejected "download error was suppressed"
check (File.ReadAllBytes destination = stale) "download error replaced the retained model"
check (not (File.Exists(destination + ".incoming"))) "download error left a partial file"
printfn "PASS: empty cache, cached/offline model, corrupt cache, checksum mismatch and interrupted download"
finally
Directory.Delete(root, true)