chore(frontend): engineering baseline per guide ch.8
- toolchain: vitest+jsdom (15 tests green: permission pure functions exhaustive, storage migration), prettier (printWidth 120 to fit the dense existing style + 700-line page budget), commitlint conventional - npm scripts: typecheck / format / test / lint:fix; build fingerprint __APP_VERSION__ (version+git hash+build time) shown in Settings > about - git hooks via core.hooksPath=frontend/.husky: pre-commit lint-staged (prettier + eslint --fix best-effort), commit-msg commitlint - permission pure functions extracted to lib/permissions.ts (single source consumed by useAuth/useLogin); AuditLogsPage filters on URL - backend: resp package unit tests; health version constant aligned with frontend package.json
This commit is contained in:
1 parent
ac498c7ba2
commit
d867c276d4
8 files changed
+1761
-23
No files matched your search
@@ -14,6 +14,9 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AppVersion 与前端 package.json 版本保持同步
|
||||
const AppVersion = "0.4.0"
|
||||
|
||||
func main() {
|
||||
// Load config
|
||||
config.Init()
|
||||
@@ -73,7 +76,7 @@ func main() {
|
||||
|
||||
// Health check
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "ok", "version": "0.3.1", "backend": "go"})
|
||||
c.JSON(200, gin.H{"status": "ok", "version": AppVersion, "backend": "go"})
|
||||
})
|
||||
|
||||
// Routes
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package resp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func setupRouter() *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
return r
|
||||
}
|
||||
|
||||
func decode(t *testing.T, w *httptest.ResponseRecorder) map[string]any {
|
||||
t.Helper()
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &m); err != nil {
|
||||
t.Fatalf("response not json: %v (%s)", err, w.Body.String())
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func TestFailEnvelope(t *testing.T) {
|
||||
r := setupRouter()
|
||||
r.GET("/x", func(c *gin.Context) { BadRequest(c, "参数不合法") })
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", w.Code)
|
||||
}
|
||||
m := decode(t, w)
|
||||
if m["detail"] != "参数不合法" {
|
||||
t.Fatalf("detail = %v", m["detail"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestInternalErrSanitizes(t *testing.T) {
|
||||
r := setupRouter()
|
||||
dbErr := errors.New("SQL logic error: no such table: users (database is locked)")
|
||||
r.GET("/x", func(c *gin.Context) { InternalErr(c, dbErr) })
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("status = %d, want 500", w.Code)
|
||||
}
|
||||
m := decode(t, w)
|
||||
// 客户端不得看到 DB 错误原文
|
||||
if m["detail"] == dbErr.Error() {
|
||||
t.Fatalf("internal error leaked to client: %v", m["detail"])
|
||||
}
|
||||
if m["code"] != "internal" {
|
||||
t.Fatalf("code = %v, want internal", m["code"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageEnvelope(t *testing.T) {
|
||||
r := setupRouter()
|
||||
r.GET("/x", func(c *gin.Context) {
|
||||
Page(c, []string{"a", "b"}, 42, 2, 10)
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
m := decode(t, w)
|
||||
if m["total"].(float64) != 42 || m["page"].(float64) != 2 || m["page_size"].(float64) != 10 {
|
||||
t.Fatalf("pagination envelope wrong: %v", m)
|
||||
}
|
||||
items, ok := m["items"].([]any)
|
||||
if !ok || len(items) != 2 {
|
||||
t.Fatalf("items wrong: %v", m["items"])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user