package apperr import ( "testing" "github.com/carlosframework/platform/internal/applog" ) func TestIsAvailability(t *testing.T) { for _, c := range []struct { r Record want bool }{ {Record{Source: SourceEdge, Kind: KindUnavailable, Status: 502}, true}, {Record{Source: SourceEdge, Kind: KindHTTP, Status: 503}, true}, // written before the amendment {Record{Source: SourceEdge, Kind: KindHTTP, Status: 504}, true}, {Record{Source: SourceEdge, Kind: KindHTTP, Status: 500}, false}, // the app's own failure {Record{Source: SourceEdge, Kind: KindHTTP, Status: 505}, false}, {Record{Source: SourceLog, Stream: applog.StreamPlatform, Level: "error"}, true}, {Record{Source: SourceLog, Stream: applog.StreamApp, Level: "error"}, false}, {Record{Source: SourceServer, Status: 503}, false}, } { if got := IsAvailability(c.r); got != c.want { t.Errorf("IsAvailability(%+v) = %v, want %v", c.r, got, c.want) } } } // TestAsAvailabilityRegroupsHistory: records the edge filed before the // amendment, one per path, collapse into one row per status. func TestAsAvailabilityRegroupsHistory(t *testing.T) { a := AsAvailability(Record{Source: SourceEdge, Kind: KindHTTP, Status: 502, Name: "502 GET /.env", Path: "/.env", FP: "old1"}) b := AsAvailability(Record{Source: SourceEdge, Kind: KindHTTP, Status: 502, Name: "502 GET /phpinfo.php", Path: "/phpinfo.php", FP: "old2"}) c := AsAvailability(Record{Source: SourceEdge, Kind: KindHTTP, Status: 503, Name: "503 GET /.env", Path: "/.env", FP: "old3"}) if a.FP != b.FP || a.FP == c.FP { t.Fatalf("fps = %s %s %s: want one per status", a.FP, b.FP, c.FP) } if a.Name != "502 · cause not recorded" || a.Kind != KindUnavailable || a.Path != "/.env" { t.Fatalf("regrouped = %+v", a) } // A new record is already in shape; a 500 is not Availability at all. fresh := Record{Source: SourceEdge, Kind: KindUnavailable, Status: 502, Cause: CauseRefused, Name: AvailabilityName(502, CauseRefused), FP: "keep"} if got := AsAvailability(fresh); got.FP != "keep" { t.Fatalf("a new record was regrouped: %+v", got) } if got := AsAvailability(Record{Source: SourceEdge, Kind: KindHTTP, Status: 500, FP: "keep"}); got.FP != "keep" { t.Fatalf("a 500 was regrouped: %+v", got) } if got := AvailabilityName(503, "martian"); got != "503 · martian" { t.Fatalf("an unknown cause renders as itself, got %q", got) } }