photoprism/pkg/i18n/response_test.go
Michael Mayer f5373ccec6 UX: Render backend error responses in the current UI locale #5682
The i18n.Response envelope now carries the untranslated source id and params; the frontend (api.js) renders them via Tp in the user's UI locale, with the server-rendered error/message as the instance-locale fallback. Common not-found/conflict/bad-request handlers were switched from hardcoded literals and raw err.Error() to Abort/i18n.Message helpers, so they benefit automatically.
2026-06-23 12:06:49 +00:00

92 lines
2.9 KiB
Go

package i18n
import (
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewResponse(t *testing.T) {
t.Run("AlreadyExists", func(t *testing.T) {
resp := NewResponse(http.StatusConflict, ErrAlreadyExists, "A cat")
assert.Equal(t, http.StatusConflict, resp.Code)
assert.Equal(t, "A cat already exists", resp.Err)
assert.Equal(t, "", resp.Msg)
assert.Equal(t, "%s already exists", resp.ID)
assert.Equal(t, []any{"A cat"}, resp.Params)
})
t.Run("UnexpectedError", func(t *testing.T) {
resp := NewResponse(http.StatusInternalServerError, ErrUnexpected, "A cat")
assert.Equal(t, http.StatusInternalServerError, resp.Code)
assert.Equal(t, "Something went wrong, try again", resp.Err)
assert.Equal(t, "", resp.Msg)
assert.Equal(t, "Something went wrong, try again", resp.ID)
})
t.Run("ChangesSaved", func(t *testing.T) {
resp := NewResponse(http.StatusOK, MsgChangesSaved)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "", resp.Err)
assert.Equal(t, "Changes successfully saved", resp.Msg)
assert.Equal(t, "Changes successfully saved", resp.ID)
if s, err := json.Marshal(resp); err != nil {
t.Fatal(err)
} else {
assert.Equal(t, `{"code":200,"message":"Changes successfully saved","id":"Changes successfully saved"}`, string(s))
}
})
}
func TestResponse_String(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resp := Response{Code: 404, Err: "Not found", Msg: "page not found", Details: "xyz"}
assert.Equal(t, "Not found", resp.String())
})
t.Run("NoError", func(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resp := Response{Code: 200, Msg: "Ok", Details: "xyz"}
assert.Equal(t, "Ok", resp.String())
})
})
}
func TestResponse_LowerString(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resp := Response{Code: 404, Err: "Not found", Msg: "page not found", Details: "xyz"}
assert.Equal(t, "not found", resp.LowerString())
})
t.Run("NoError", func(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resp := Response{Code: 200, Msg: "Ok", Details: "xyz"}
assert.Equal(t, "ok", resp.LowerString())
})
})
}
func TestResponse_Error(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resp := Response{Code: 404, Err: "Not found", Msg: "page not found", Details: "xyz"}
assert.Equal(t, "Not found", resp.Error())
})
t.Run("NoError", func(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resp := Response{Code: 200, Msg: "Ok", Details: "xyz"}
assert.Equal(t, "", resp.Error())
})
})
}
func TestResponse_Success(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resp := Response{Code: 404, Err: "Not found", Msg: "page not found", Details: "xyz"}
assert.Equal(t, false, resp.Success())
})
t.Run("NoError", func(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resp := Response{Code: 200, Msg: "Ok", Details: "xyz"}
assert.Equal(t, true, resp.Success())
})
})
}