package handlers

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

// Ensures the /projects/{id}/sub-projects alias from F-48 lands on the
// canonical /projects/{id}/children URL. The redirect is wired on the OUTER
// mux so it fires before auth middleware.
func TestRegisterLegacyRedirects_SubProjectsAlias(t *testing.T) {
	mux := http.NewServeMux()
	registerLegacyRedirects(mux)

	cases := []struct {
		path string
		want string
	}{
		{"/projects/abc-123/sub-projects", "/projects/abc-123/children"},
		{"/projects/abc-123/sub-projects?foo=bar", "/projects/abc-123/children?foo=bar"},
	}
	for _, tc := range cases {
		req := httptest.NewRequest(http.MethodGet, tc.path, nil)
		w := httptest.NewRecorder()
		mux.ServeHTTP(w, req)
		if w.Code != http.StatusMovedPermanently {
			t.Fatalf("%s: status = %d, want %d", tc.path, w.Code, http.StatusMovedPermanently)
		}
		if got := w.Header().Get("Location"); got != tc.want {
			t.Fatalf("%s: Location = %q, want %q", tc.path, got, tc.want)
		}
	}
}
