69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package application_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/application"
|
|
)
|
|
|
|
func TestLocalApplicationServesFoundationHealthAndReadiness(t *testing.T) {
|
|
app, err := application.New(application.Options{
|
|
Getenv: func(name string) string {
|
|
if name == "ZHINIAN_DATA_BACKEND" {
|
|
return "local"
|
|
}
|
|
return ""
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New() error = %v", err)
|
|
}
|
|
t.Cleanup(app.Close)
|
|
|
|
for _, test := range []struct {
|
|
path string
|
|
wantCode int
|
|
}{
|
|
{path: "/api/health", wantCode: http.StatusOK},
|
|
{path: "/api/ready", wantCode: http.StatusOK},
|
|
} {
|
|
t.Run(test.path, func(t *testing.T) {
|
|
response := httptest.NewRecorder()
|
|
app.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodGet, test.path, nil))
|
|
if response.Code != test.wantCode {
|
|
t.Fatalf("status = %d, want %d", response.Code, test.wantCode)
|
|
}
|
|
var payload struct {
|
|
OK bool `json:"ok"`
|
|
Database struct {
|
|
Backend string `json:"backend"`
|
|
Configured bool `json:"configured"`
|
|
} `json:"database"`
|
|
}
|
|
if err := json.NewDecoder(response.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if !payload.OK || payload.Database.Backend != "local" || !payload.Database.Configured {
|
|
t.Fatalf("payload = %+v", payload)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplicationRejectsInvalidProductionDatabaseConfiguration(t *testing.T) {
|
|
_, err := application.New(application.Options{
|
|
Getenv: func(name string) string {
|
|
if name == "NODE_ENV" {
|
|
return "production"
|
|
}
|
|
return ""
|
|
},
|
|
})
|
|
if err == nil {
|
|
t.Fatal("New() error = nil, want fail-closed database configuration error")
|
|
}
|
|
}
|