package main

import (
	"net/http"
	"net/http/httptest"
	"strings"
	"sync/atomic"
	"testing"
)

func TestLookupMissingDomain(t *testing.T) {
	s := &server{
		nrenASNs: make(map[uint]struct{}),
	}
	s.ready.Store(true)

	req := httptest.NewRequest(http.MethodGet, "/lookup", nil)
	rr := httptest.NewRecorder()

	s.lookupHandler(rr, req)

	if rr.Code != http.StatusBadRequest {
		t.Fatalf("expected 400, got %d", rr.Code)
	}

	if !strings.Contains(rr.Body.String(), "missing domain") {
		t.Fatalf("expected error message in response")
	}
}

func TestLookupServiceNotReady(t *testing.T) {
	s := &server{
		nrenASNs: make(map[uint]struct{}),
	}
	s.ready = atomic.Bool{}
	s.ready.Store(false)

	req := httptest.NewRequest(http.MethodGet, "/lookup?domain=example.com", nil)
	rr := httptest.NewRecorder()

	s.lookupHandler(rr, req)

	if rr.Code != http.StatusServiceUnavailable {
		t.Fatalf("expected 503, got %d", rr.Code)
	}
}
