| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestHealthyRecordAndRead(t *testing.T) { |
| 12 | bake := t.TempDir() |
| 13 | at := time.Date(2026, 10, 6, 9, 0, 0, 0, time.UTC) |
| 14 | b := HealthyBuild{SHA256: testSHA, Version: "f4478b15", How: "settle", At: at} |
| 15 | if IsHealthy(bake, "bab", "console", testSHA) { |
| 16 | t.Fatal("healthy before anything was recorded") |
| 17 | } |
| 18 | if err := RecordHealthy(bake, "bab", "console", b); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | b.How, b.At = "live", at.Add(time.Hour) |
| 22 | if err := RecordHealthy(bake, "bab", "console", b); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | got := ReadHealthy(bake, "bab", "console") |
| 26 | if len(got) != 1 || got[0].How != "settle" || !got[0].At.Equal(at) { |
| 27 | t.Fatalf("history = %+v, want one entry, the first record kept", got) |
| 28 | } |
| 29 | if !IsHealthy(bake, "bab", "console", testSHA) || IsHealthy(bake, "bab", "other", testSHA) || IsHealthy(bake, "bab", "console", "") { |
| 30 | t.Fatal("IsHealthy answers for the wrong app or an empty sha256") |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestHealthyKeepsTheNewestFifty(t *testing.T) { |
| 35 | bake := t.TempDir() |
| 36 | for i := 0; i < 55; i++ { |
| 37 | if err := RecordHealthy(bake, "bab", "console", HealthyBuild{SHA256: fmt.Sprintf("%064x", i), How: "live"}); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | } |
| 41 | got := ReadHealthy(bake, "bab", "console") |
| 42 | if len(got) != 50 || got[0].SHA256 != fmt.Sprintf("%064x", 54) || IsHealthy(bake, "bab", "console", fmt.Sprintf("%064x", 0)) { |
| 43 | t.Fatalf("len = %d, first = %s; want the newest fifty", len(got), got[0].SHA256) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestHealthyCorruptReadsEmpty(t *testing.T) { |
| 48 | bake := t.TempDir() |
| 49 | p := filepath.Join(bake, "healthy", "bab", "console.json") |
| 50 | if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if err := os.WriteFile(p, []byte(`{"schema":1,"builds":[`), 0o644); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if got := ReadHealthy(bake, "bab", "console"); got != nil { |
| 57 | t.Fatalf("corrupt history = %+v, want nil", got) |
| 58 | } |
| 59 | if err := RecordHealthy(bake, "bab", "console", HealthyBuild{SHA256: "nope"}); err == nil { |
| 60 | t.Fatal("a non-hex sha256 was recorded") |
| 61 | } |
| 62 | } |
| 63 | |