RezinaLMS-BackEnd/internal/web_server/handlers/response_contracts_test.go

50 lines
1.6 KiB
Go

package handlers
import (
"encoding/json"
"reflect"
"testing"
"Rezina-Backend/internal/domain"
vimeoservice "Rezina-Backend/internal/services/vimeo"
"Rezina-Backend/internal/web_server/response"
)
func responseJSONFields(t *testing.T, value any) map[string]any {
t.Helper()
raw, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
var fields map[string]any
if err = json.Unmarshal(raw, &fields); err != nil {
t.Fatal(err)
}
return fields
}
// Documentation-only payloads must preserve existing wire names, including the
// PascalCase fields returned by raw service/domain structs. This prevents a
// documentation improvement from silently advertising a new JSON API contract.
func TestResponseDocumentationMatchesRuntimeJSONFields(t *testing.T) {
for _, tc := range []struct {
name string
runtime, documented any
}{
{"message domain envelope", domain.Response{}, MessageResponse{}},
{"message legacy envelope", response.APIResponse{}, MessageAPIResponse{}},
{"raw assessment question set", domain.QuestionSet{}, RawQuestionSetResponse{}},
{"raw Vimeo upload result", vimeoservice.UploadResult{}, VimeoUploadResultData{}},
{"notification count", map[string]int{"unread": 0}, CountUnreadNotificationsResponse{}},
{"video status", map[string]string{"video_id": "", "status": ""}, VimeoTranscodeStatusData{}},
} {
t.Run(tc.name, func(t *testing.T) {
runtime := responseJSONFields(t, tc.runtime)
documented := responseJSONFields(t, tc.documented)
if !reflect.DeepEqual(runtime, documented) {
t.Fatalf("runtime JSON=%v; documented JSON=%v", runtime, documented)
}
})
}
}