//go:build integration

package inttest

import (
	"context"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"go.uber.org/zap/zaptest"

	"scrabble/backend/internal/server"
	"scrabble/backend/internal/session"
)

// TestReadyzWithRealDatabase exercises the assembled server against the live
// container: /readyz answers 200 once the database pings and the session cache
// is warmed, closing the gap the unit test (nil database) cannot cover.
func TestReadyzWithRealDatabase(t *testing.T) {
	svc := session.NewService(session.NewStore(testDB), session.NewCache())
	if err := svc.Warm(context.Background()); err != nil {
		t.Fatalf("warm session cache: %v", err)
	}

	srv := server.New(":0", server.Deps{
		Logger:        zaptest.NewLogger(t),
		DB:            testDB,
		PingTimeout:   5 * time.Second,
		SessionsReady: svc.Ready,
	})

	for _, path := range []string{"/healthz", "/readyz"} {
		rec := httptest.NewRecorder()
		srv.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
		if rec.Code != http.StatusOK {
			t.Errorf("%s status = %d, want 200", path, rec.Code)
		}
	}
}
