package http

import (
	"errors"
	stdhttp "net/http"
	"net/http/httptest"
	"strings"
	"testing"

	licenseclient "qycw/go-server/internal/license"
)

func TestWriteActivationErrorSanitizesResponse(t *testing.T) {
	tests := []struct {
		name       string
		err        error
		wantStatus int
	}{
		{name: "unavailable", err: licenseclient.ErrUnavailable, wantStatus: stdhttp.StatusBadRequest},
		{name: "instance limit", err: &licenseclient.HTTPError{StatusCode: stdhttp.StatusConflict}, wantStatus: stdhttp.StatusConflict},
		{name: "remote failure", err: errors.New("request contained AUTH-SECRET and inst_secret"), wantStatus: stdhttp.StatusBadGateway},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			recorder := httptest.NewRecorder()
			writeActivationError(recorder, tt.err)
			if recorder.Code != tt.wantStatus {
				t.Fatalf("status = %d, want %d", recorder.Code, tt.wantStatus)
			}
			body := recorder.Body.String()
			if strings.Contains(body, "AUTH-SECRET") || strings.Contains(body, "inst_secret") {
				t.Fatalf("response leaked internal data: %s", body)
			}
		})
	}
}
