package release import ( "fmt" "os" "path/filepath" "testing" "time" ) func TestHealthyRecordAndRead(t *testing.T) { bake := t.TempDir() at := time.Date(2026, 10, 6, 9, 0, 0, 0, time.UTC) b := HealthyBuild{SHA256: testSHA, Version: "f4478b15", How: "settle", At: at} if IsHealthy(bake, "bab", "console", testSHA) { t.Fatal("healthy before anything was recorded") } if err := RecordHealthy(bake, "bab", "console", b); err != nil { t.Fatal(err) } b.How, b.At = "live", at.Add(time.Hour) if err := RecordHealthy(bake, "bab", "console", b); err != nil { t.Fatal(err) } got := ReadHealthy(bake, "bab", "console") if len(got) != 1 || got[0].How != "settle" || !got[0].At.Equal(at) { t.Fatalf("history = %+v, want one entry, the first record kept", got) } if !IsHealthy(bake, "bab", "console", testSHA) || IsHealthy(bake, "bab", "other", testSHA) || IsHealthy(bake, "bab", "console", "") { t.Fatal("IsHealthy answers for the wrong app or an empty sha256") } } func TestHealthyKeepsTheNewestFifty(t *testing.T) { bake := t.TempDir() for i := 0; i < 55; i++ { if err := RecordHealthy(bake, "bab", "console", HealthyBuild{SHA256: fmt.Sprintf("%064x", i), How: "live"}); err != nil { t.Fatal(err) } } got := ReadHealthy(bake, "bab", "console") if len(got) != 50 || got[0].SHA256 != fmt.Sprintf("%064x", 54) || IsHealthy(bake, "bab", "console", fmt.Sprintf("%064x", 0)) { t.Fatalf("len = %d, first = %s; want the newest fifty", len(got), got[0].SHA256) } } func TestHealthyCorruptReadsEmpty(t *testing.T) { bake := t.TempDir() p := filepath.Join(bake, "healthy", "bab", "console.json") if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(p, []byte(`{"schema":1,"builds":[`), 0o644); err != nil { t.Fatal(err) } if got := ReadHealthy(bake, "bab", "console"); got != nil { t.Fatalf("corrupt history = %+v, want nil", got) } if err := RecordHealthy(bake, "bab", "console", HealthyBuild{SHA256: "nope"}); err == nil { t.Fatal("a non-hex sha256 was recorded") } }