Files
gochat/backend/internal/handler/api/v1/widget_test_handler_test.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

66 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{})
s.db.AutoMigrate(&model.WidgetTest{})
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)
}