package router import ( "net/http" "net/http/httptest" "strings" "testing" "hfb_sys/backend/internal/config" "hfb_sys/backend/internal/database" "hfb_sys/backend/internal/model" "go.uber.org/zap" ) func testConfig() config.Config { return config.Config{AppEnv: "test", JWTSecret: "test-secret"} } func TestHealthRoutesSeparateLivenessAndReadiness(t *testing.T) { engine := New(testConfig(), Dependencies{}, zap.NewNop()) for _, tt := range []struct { path string wantStatus int }{ {path: "/health", wantStatus: http.StatusOK}, {path: "/health/ready", wantStatus: http.StatusServiceUnavailable}, {path: "/api/health", wantStatus: http.StatusServiceUnavailable}, } { recorder := httptest.NewRecorder() request := httptest.NewRequest(http.MethodGet, tt.path, nil) engine.ServeHTTP(recorder, request) if recorder.Code != tt.wantStatus { t.Fatalf("GET %s status = %d, want %d; body=%s", tt.path, recorder.Code, tt.wantStatus, recorder.Body.String()) } } } func TestExtractedAdminRoutesRemainRegisteredWithoutDuplicates(t *testing.T) { db := database.NewTestDB() engine := New(testConfig(), Dependencies{DB: db}, zap.NewNop()) routes := engine.Routes() registered := make(map[string]struct{}, len(routes)) for _, route := range routes { key := route.Method + " " + route.Path if _, exists := registered[key]; exists { t.Fatalf("duplicate route: %s", key) } registered[key] = struct{}{} } for _, key := range []string{ "GET /api/admin/crash/products", "GET /api/admin/mohong/products", "POST /api/admin/chats/qrcodes/batch", "PUT /api/admin/chat-support-groups/:id/members", "PUT /api/admin/roles/:id/permissions", "PUT /api/admin/admin-users/:id/password", "POST /api/admin/announcements/:id/publish", "GET /api/site/context", "GET /api/site/check-domain", "GET /api/distribution/sites/:id/listings", "GET /api/distribution/sites/:id/wallet", } { if _, exists := registered[key]; !exists { t.Errorf("missing route after extraction: %s", key) } } } func TestSiteContextRouteUsesTenantHost(t *testing.T) { db := database.NewTestDB() tenant := model.Tenant{ Code: "jieyou", Name: "解忧商行", Status: "active", ProfileJSON: []byte(`{"site_name":"解忧商行","logo_url":"https://cdn.example/logo.png"}`), FeaturesJSON: []byte( `{"rental_listing":true}`, ), } if err := db.Create(&tenant).Error; err != nil { t.Fatalf("create tenant: %v", err) } if err := db.Create(&model.TenantDomain{ TenantID: tenant.ID, Host: "zuhaox.com", DomainType: "storefront", Status: "active", }).Error; err != nil { t.Fatalf("create tenant domain: %v", err) } if err := db.Create(&model.DistributionSite{ SiteCode: "legacy_dist", SiteName: "历史分销站", Domain: "legacy.example.com", Status: "active", }).Error; err != nil { t.Fatalf("create legacy distribution site: %v", err) } engine := New(testConfig(), Dependencies{DB: db}, zap.NewNop()) req := httptest.NewRequest(http.MethodGet, "/api/site/context", nil) req.Host = "zuhaox.com" rec := httptest.NewRecorder() engine.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String()) } body := rec.Body.String() for _, want := range []string{`"tenant_code":"jieyou"`, `"site_name":"解忧商行"`, `"rental_listing":true`} { if !strings.Contains(body, want) { t.Fatalf("body missing %s: %s", want, body) } } req = httptest.NewRequest(http.MethodGet, "/api/site/context", nil) req.Host = "unknown.example.com" rec = httptest.NewRecorder() engine.ServeHTTP(rec, req) if rec.Code != http.StatusNotFound { t.Fatalf("unknown host status = %d, want 404", rec.Code) } for domain, wantStatus := range map[string]int{ "zuhaox.com": http.StatusOK, "legacy.example.com": http.StatusOK, "unknown.example.com": http.StatusNotFound, } { req = httptest.NewRequest(http.MethodGet, "/api/site/check-domain?domain="+domain, nil) rec = httptest.NewRecorder() engine.ServeHTTP(rec, req) if rec.Code != wantStatus { t.Fatalf("check domain %s status = %d, want %d; body=%s", domain, rec.Code, wantStatus, rec.Body.String()) } } }