package postgres import ( "context" "errors" "fmt" "io" "net" "testing" "github.com/jackc/pgx/v5/pgconn" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab4/internal/store" ) // TestNormalizeUnavailable classifies dependency failures while retaining their causes. func TestNormalizeUnavailable(t *testing.T) { cases := []struct { name string err error }{ {"deadline", context.DeadlineExceeded}, {"closed stream", io.EOF}, {"truncated stream", io.ErrUnexpectedEOF}, {"network", &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("refused")}}, {"connection exception", &pgconn.PgError{Code: "08006"}}, {"connection limit", &pgconn.PgError{Code: "53300"}}, {"shutdown", &pgconn.PgError{Code: "57P01"}}, {"crash", &pgconn.PgError{Code: "57P02"}}, {"starting", &pgconn.PgError{Code: "57P03"}}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { err := normalize(fmt.Errorf("operation failed: %w", tc.err)) if !errors.Is(err, store.ErrUnavailable) || !errors.Is(err, tc.err) { t.Fatalf("failure category or cause lost: %T", tc.err) } }) } } // TestNormalizeUnexpected preserves programming errors instead of misclassifying them as outages. func TestNormalizeUnexpected(t *testing.T) { for _, code := range []string{"42601", "23503", "42P01"} { t.Run(code, func(t *testing.T) { err := &pgconn.PgError{Code: code} if normalize(err) != err { t.Fatal("unexpected SQL error was reclassified") } }) } }