Files
NianAIGC/backend/internal/httpapi/openapi_contract_test.go

68 lines
2.1 KiB
Go

package httpapi
import (
"reflect"
"sort"
"testing"
)
func TestOpenAPIDocumentFreezesPublicSurface(t *testing.T) {
document := openAPIDocument("https://app.example.test")
if document["openapi"] != "3.1.0" {
t.Fatalf("openapi=%v", document["openapi"])
}
servers := document["servers"].([]any)
if got := servers[0].(map[string]any)["url"]; got != "https://app.example.test" {
t.Fatalf("server url=%v", got)
}
components := document["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
gotSchemas := make([]string, 0, len(schemas))
for name := range schemas {
gotSchemas = append(gotSchemas, name)
}
sort.Strings(gotSchemas)
wantSchemas := []string{
"Asset", "CreateJobRequest", "ErrorResponse", "GenerationCapability",
"GenerationJob", "GenerationStatus", "PromptMaterial", "RegisterAssetRequest",
"WebhookPayload",
}
sort.Strings(wantSchemas)
if !reflect.DeepEqual(gotSchemas, wantSchemas) {
t.Fatalf("schemas=%v", gotSchemas)
}
paths := document["paths"].(map[string]any)
gotPaths := make([]string, 0, len(paths))
for path := range paths {
gotPaths = append(gotPaths, path)
}
sort.Strings(gotPaths)
wantPaths := []string{
"/api/v1/assets", "/api/v1/assets/{id}", "/api/v1/assets/{id}/download",
"/api/v1/capabilities", "/api/v1/jobs", "/api/v1/jobs/{id}",
"/api/v1/jobs/{id}/cancel",
}
if !reflect.DeepEqual(gotPaths, wantPaths) {
t.Fatalf("paths=%v", gotPaths)
}
jobs := paths["/api/v1/jobs"].(map[string]any)
if jobs["get"] == nil || jobs["post"] == nil {
t.Fatalf("jobs verbs=%v", jobs)
}
assets := paths["/api/v1/assets"].(map[string]any)
requestBody := assets["post"].(map[string]any)["requestBody"].(map[string]any)
content := requestBody["content"].(map[string]any)
if content["application/json"] == nil || content["multipart/form-data"] == nil {
t.Fatalf("asset content=%v", content)
}
createJob := schemas["CreateJobRequest"].(map[string]any)
properties := createJob["properties"].(map[string]any)
if properties["materials"] == nil || properties["settings"] == nil || properties["webhookUrl"] == nil {
t.Fatalf("create job properties=%v", properties)
}
}