package handler import "testing" // A4:limit 必须夹紧——负数(limit=-1 会让 gorm 取消 LIMIT 全表扫)、0、超大都要归位。 func TestClampLimit(t *testing.T) { cases := []struct { v string def, max int want int }{ {"", 50, 200, 50}, // 空 → 默认 {"-1", 50, 200, 50}, // 负数 → 默认(关键:堵住全表扫) {"0", 50, 200, 50}, // 0 → 默认 {"abc", 50, 200, 50}, // 非法 → 默认 {"100", 50, 200, 100}, // 合法 → 原值 {"999", 50, 200, 200}, // 超上界 → 夹到 max } for _, tc := range cases { if got := clampLimit(tc.v, tc.def, tc.max); got != tc.want { t.Fatalf("clampLimit(%q,%d,%d)=%d want %d", tc.v, tc.def, tc.max, got, tc.want) } } if clampOffset("-5") != 0 || clampOffset("abc") != 0 || clampOffset("7") != 7 { t.Fatal("clampOffset 应把负数/非法归 0、合法透传") } } // A1:safeCall 必须兜住 panic,不外抛(否则后台 goroutine 一 panic 崩整个进程)。 func TestSafeCallRecovers(t *testing.T) { done := false func() { defer func() { if r := recover(); r != nil { t.Fatalf("safeCall 未兜住 panic:%v", r) } }() safeCall("test", func() { panic("boom") }) done = true }() if !done { t.Fatal("safeCall 之后应正常继续") } }