|
| 1 | +namespace Grace.CLI.Tests |
| 2 | + |
| 3 | +open FsUnit |
| 4 | +open Grace.CLI |
| 5 | +open Grace.Shared |
| 6 | +open Grace.Shared.Client |
| 7 | +open Grace.Shared.Utilities |
| 8 | +open NodaTime |
| 9 | +open NUnit.Framework |
| 10 | +open System |
| 11 | +open System.IO |
| 12 | +open System.Text.Json |
| 13 | + |
| 14 | +[<NonParallelizable>] |
| 15 | +module HistoryStorageTests = |
| 16 | + |
| 17 | + let private withFileBackup (path: string) (action: unit -> unit) = |
| 18 | + let backupPath = path + ".testbackup" |
| 19 | + let hadExisting = File.Exists(path) |
| 20 | + |
| 21 | + if hadExisting then File.Copy(path, backupPath, true) |
| 22 | + |
| 23 | + try |
| 24 | + action () |
| 25 | + finally |
| 26 | + if hadExisting then |
| 27 | + File.Copy(backupPath, path, true) |
| 28 | + File.Delete(backupPath) |
| 29 | + elif File.Exists(path) then |
| 30 | + File.Delete(path) |
| 31 | + |
| 32 | + let private randomString (random: Random) = |
| 33 | + let length = random.Next(8, 32) |
| 34 | + let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 35 | + let buffer = Array.zeroCreate<char> length |
| 36 | + |
| 37 | + for i in 0 .. length - 1 do |
| 38 | + buffer[i] <- chars[random.Next(chars.Length)] |
| 39 | + |
| 40 | + String(buffer) |
| 41 | + |
| 42 | + [<Test>] |
| 43 | + let ``redacts --token values`` () = |
| 44 | + let config = UserConfiguration.UserConfiguration() |
| 45 | + let random = Random(1337) |
| 46 | + |
| 47 | + for _ in 0..49 do |
| 48 | + let value = randomString random |
| 49 | + let args = [| "--token"; value |] |
| 50 | + let redacted, _ = HistoryStorage.redactArguments args config.History |
| 51 | + redacted |> Array.exists (fun arg -> arg = value) |> should equal false |
| 52 | + |
| 53 | + [<Test>] |
| 54 | + let ``redacts --token=value values`` () = |
| 55 | + let config = UserConfiguration.UserConfiguration() |
| 56 | + let random = Random(7331) |
| 57 | + |
| 58 | + for _ in 0..49 do |
| 59 | + let value = randomString random |
| 60 | + let args = [| $"--token={value}" |] |
| 61 | + let redacted, _ = HistoryStorage.redactArguments args config.History |
| 62 | + |
| 63 | + redacted |
| 64 | + |> Array.exists (fun arg -> arg.Contains(value, StringComparison.Ordinal)) |
| 65 | + |> should equal false |
| 66 | + |
| 67 | + [<TestCase("30s", 30.0)>] |
| 68 | + [<TestCase("10m", 600.0)>] |
| 69 | + [<TestCase("24h", 86400.0)>] |
| 70 | + [<TestCase("7d", 604800.0)>] |
| 71 | + let ``parses valid durations`` (input: string, expectedSeconds: float) = |
| 72 | + match HistoryStorage.tryParseDuration input with |
| 73 | + | Ok duration -> duration.TotalSeconds |> should equal expectedSeconds |
| 74 | + | Error error -> Assert.Fail($"Expected Ok, got Error: {error}") |
| 75 | + |
| 76 | + [<TestCase("")>] |
| 77 | + [<TestCase("10x")>] |
| 78 | + [<TestCase("abc")>] |
| 79 | + [<TestCase("10")>] |
| 80 | + let ``rejects invalid durations`` (input: string) = |
| 81 | + match HistoryStorage.tryParseDuration input with |
| 82 | + | Ok _ -> Assert.Fail("Expected Error, got Ok.") |
| 83 | + | Error _ -> Assert.Pass() |
| 84 | + |
| 85 | + [<Test>] |
| 86 | + let ``readHistoryEntries skips corrupt lines`` () = |
| 87 | + let historyPath = HistoryStorage.getHistoryFilePath () |
| 88 | + let historyDir = Path.GetDirectoryName(historyPath) |
| 89 | + Directory.CreateDirectory(historyDir) |> ignore |
| 90 | + |
| 91 | + let options = JsonSerializerOptions(Constants.JsonSerializerOptions) |
| 92 | + options.WriteIndented <- false |
| 93 | + |
| 94 | + let entry: HistoryStorage.HistoryEntry = |
| 95 | + { id = Guid.NewGuid() |
| 96 | + timestampUtc = getCurrentInstant () |
| 97 | + argvOriginal = [| "branch"; "status" |] |
| 98 | + argvNormalized = [| "branch"; "status" |] |
| 99 | + commandLine = "branch status" |
| 100 | + cwd = Environment.CurrentDirectory |
| 101 | + repoRoot = None |
| 102 | + repoName = None |
| 103 | + repoBranch = None |
| 104 | + graceVersion = "0.1" |
| 105 | + exitCode = 0 |
| 106 | + durationMs = 10L |
| 107 | + parseSucceeded = true |
| 108 | + redactions = List.empty |
| 109 | + source = None } |
| 110 | + |
| 111 | + let json = JsonSerializer.Serialize(entry, options) |
| 112 | + |
| 113 | + withFileBackup historyPath (fun () -> |
| 114 | + File.WriteAllLines(historyPath, [| json; "not json" |]) |
| 115 | + let result = HistoryStorage.readHistoryEntries () |
| 116 | + result.Entries.Length |> should equal 1 |
| 117 | + result.CorruptCount |> should equal 1) |
| 118 | + |
| 119 | + [<Test>] |
| 120 | + let ``prunes history to max entries`` () = |
| 121 | + let historyPath = HistoryStorage.getHistoryFilePath () |
| 122 | + let configPath = UserConfiguration.getUserConfigurationPath () |
| 123 | + let historyDir = Path.GetDirectoryName(historyPath) |
| 124 | + Directory.CreateDirectory(historyDir) |> ignore |
| 125 | + |
| 126 | + withFileBackup configPath (fun () -> |
| 127 | + withFileBackup historyPath (fun () -> |
| 128 | + let configuration = UserConfiguration.UserConfiguration() |
| 129 | + configuration.History.Enabled <- true |
| 130 | + configuration.History.MaxEntries <- 2 |
| 131 | + configuration.History.MaxFileBytes <- 1024L * 1024L |
| 132 | + configuration.History.RetentionDays <- 365 |
| 133 | + |
| 134 | + match UserConfiguration.saveUserConfiguration configuration with |
| 135 | + | Ok _ -> () |
| 136 | + | Error error -> Assert.Fail(error) |
| 137 | + |
| 138 | + File.WriteAllText(historyPath, String.Empty) |
| 139 | + |
| 140 | + let start = getCurrentInstant () |
| 141 | + |
| 142 | + for offset in [ 0..2 ] do |
| 143 | + let timestamp = start.Plus(Duration.FromMinutes(float offset)) |
| 144 | + |
| 145 | + HistoryStorage.tryRecordInvocation |
| 146 | + { argvOriginal = [| "branch"; "status" |] |
| 147 | + argvNormalized = [| "branch"; "status" |] |
| 148 | + cwd = Environment.CurrentDirectory |
| 149 | + exitCode = 0 |
| 150 | + durationMs = 5L |
| 151 | + parseSucceeded = true |
| 152 | + timestampUtc = timestamp |
| 153 | + source = None } |
| 154 | + |
| 155 | + let result = HistoryStorage.readHistoryEntries () |
| 156 | + result.Entries.Length |> should equal 2)) |
0 commit comments