Add explorer AI chat and context endpoints

This commit is contained in:
defiQUG
2026-03-27 13:37:53 -07:00
parent 01e0633124
commit c9e792d55f
4 changed files with 1272 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package rest_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -109,8 +110,8 @@ func TestSearchEndpoint(t *testing.T) {
_, mux := setupTestServer(t)
testCases := []struct {
name string
query string
name string
query string
wantCode int
}{
{"block number", "?q=1000", http.StatusOK},
@@ -179,7 +180,7 @@ func TestErrorHandling(t *testing.T) {
mux.ServeHTTP(w, req)
assert.True(t, w.Code >= http.StatusBadRequest)
var errorResponse map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &errorResponse)
if err == nil {
@@ -213,6 +214,33 @@ func TestPagination(t *testing.T) {
}
}
func TestAIContextEndpoint(t *testing.T) {
_, mux := setupTestServer(t)
req := httptest.NewRequest("GET", "/api/v1/ai/context?q=cUSDT+bridge", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var response map[string]interface{}
err := json.Unmarshal(w.Body.Bytes(), &response)
require.NoError(t, err)
assert.NotNil(t, response["context"])
}
func TestAIChatEndpointRequiresOpenAIKey(t *testing.T) {
_, mux := setupTestServer(t)
body := bytes.NewBufferString(`{"messages":[{"role":"user","content":"What is live on Chain 138?"}]}`)
req := httptest.NewRequest("POST", "/api/v1/ai/chat", body)
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
}
// TestRequestTimeout tests request timeout handling
func TestRequestTimeout(t *testing.T) {
// This would test timeout behavior
@@ -225,11 +253,10 @@ func BenchmarkListBlocks(b *testing.B) {
_, mux := setupTestServer(&testing.T{})
req := httptest.NewRequest("GET", "/api/v1/blocks?limit=10&page=1", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
}
}