Files
gochat/backend/internal/handler/api/v1/widget_test_handler_test.go
T
Rogeeandrogee 2b182f9956 H-300: wire Captain Skills into Web runtime (#48)
* H-300: wire Captain Skills into Web runtime

* H-300: enforce effective model and conservative skill budget

* H-300: fix CI gosec step

* ci: extend golangci-lint timeout

* fix lint findings across backend

* fix(push): resolve delivery protocol blockers

* test(repository): close SQLite test databases

* test(repository): reuse SQLite schema per package

* H-307: restore backend Go cache in CI

* H-307: prefetch modules before cold lint

* H-307: resolve govulncheck security gate

* H-307: build lint with patched Go toolchain

* H-307: clear remaining security scan findings

---------

Co-authored-by: Rogee <rogee@ipao.vip>
2026-08-19 07:08:14 +08:00

68 lines
2.2 KiB
Go

package v1
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/suite"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
)
type WidgetTestHandlerTestSuite struct {
suite.Suite
db *gorm.DB
handler *WidgetTestHandler
router *gin.Engine
}
func (s *WidgetTestHandlerTestSuite) SetupSuite() {
s.db, _ = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err := s.db.AutoMigrate(&model.WidgetTest{}); err != nil {
panic(err)
}
repo := repository.NewWidgetTestRepo(s.db)
svc := service.NewWidgetTestService(repo)
s.handler = NewWidgetTestHandler(svc)
gin.SetMode(gin.TestMode)
r := gin.New()
group := r.Group("/platform/api/v1")
RegisterWidgetTestRoutes(group, s.handler)
s.router = r
}
func TestWidgetTestHandlerTestSuite(t *testing.T) {
suite.Run(t, new(WidgetTestHandlerTestSuite))
}
func (s *WidgetTestHandlerTestSuite) TestIndex_Success() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/platform/api/v1/widget_tests/", nil)
s.router.ServeHTTP(w, req)
// Gin trailing slash: RegisterWidgetTestRoutes uses "/" → need trailing slash or RedirectTrailingSlash
s.True(w.Code == http.StatusOK || w.Code == http.StatusMovedPermanently || w.Code == http.StatusTemporaryRedirect, "expected 200 or redirect, got %d", w.Code)
}
func (s *WidgetTestHandlerTestSuite) TestListByType_Success() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/platform/api/v1/widget_tests/conversation", nil)
s.router.ServeHTTP(w, req)
s.True(w.Code == http.StatusOK || w.Code == http.StatusUnprocessableEntity, "expected 200 or 500, got %d", w.Code)
}
func (s *WidgetTestHandlerTestSuite) TestListByType_EmptyType() {
// Route pattern /:type always matches some string — empty type won't hit this route
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/platform/api/v1/widget_tests/", nil)
s.router.ServeHTTP(w, req)
// This hits Index route (with trailing slash), not ListByType
s.True(w.Code == http.StatusOK || w.Code == http.StatusMovedPermanently, "got %d", w.Code)
}