From 940cc70f632a67580e2c606e26b235a330219ef8 Mon Sep 17 00:00:00 2001 From: Paul Coghlan Date: Fri, 4 Oct 2024 17:43:32 +0100 Subject: [PATCH] Added some tests --- .../llmclient/llmclient_test.go | 250 ++++++++++++++++++ 1 file changed, 250 insertions(+) diff --git a/packages/grafana-llm-app/llmclient/llmclient_test.go b/packages/grafana-llm-app/llmclient/llmclient_test.go index db81afa1..540d6e89 100644 --- a/packages/grafana-llm-app/llmclient/llmclient_test.go +++ b/packages/grafana-llm-app/llmclient/llmclient_test.go @@ -138,3 +138,253 @@ func TestChatCompletionsStream(t *testing.T) { t.Errorf("expected streamed content to be 'hello there', got '%s'", content) } } + +func TestCreateAssistant(t *testing.T) { + ctx := context.Background() + key := "test" + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/plugins/grafana-llm-app/resources/openai/v1/assistants" { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 page not found")) + } + if r.Method != http.MethodPost { + w.WriteHeader(http.StatusMethodNotAllowed) + } + if r.Header.Get("Authorization") != "Bearer "+key { + w.WriteHeader(http.StatusUnauthorized) + } + req := openai.AssistantRequest{} + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + w.WriteHeader(http.StatusOK) + response := openai.Assistant{ + ID: "test", + } + w.Header().Set("Content-Type", "application/json") + j, _ := json.Marshal(response) + w.Write(j) + }) + server := httptest.NewServer(handler) + client := NewOpenAI(server.URL, key) + // Test case: Create assistant request succeeds + req := AssistantRequest{ + AssistantRequest: openai.AssistantRequest{}, + Model: ModelBase, + } + _, err := client.CreateAssistant(ctx, req) + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } +} + +func TestCreateThread(t *testing.T) { + ctx := context.Background() + key := "test" + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/plugins/grafana-llm-app/resources/openai/v1/threads" { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 page not found")) + } + if r.Method != http.MethodPost { + w.WriteHeader(http.StatusMethodNotAllowed) + } + if r.Header.Get("Authorization") != "Bearer "+key { + w.WriteHeader(http.StatusUnauthorized) + } + req := openai.ThreadRequest{} + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + w.WriteHeader(http.StatusOK) + response := openai.Thread{ + ID: "test", + } + w.Header().Set("Content-Type", "application/json") + j, _ := json.Marshal(response) + w.Write(j) + }) + server := httptest.NewServer(handler) + client := NewOpenAI(server.URL, key) + // Test case: Create thread request succeeds + req := openai.ThreadRequest{} + _, err := client.CreateThread(ctx, req) + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } +} + +func TestCreateMessage(t *testing.T) { + ctx := context.Background() + key := "test" + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/plugins/grafana-llm-app/resources/openai/v1/threads/test/messages" { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 page not found")) + } + if r.Method != http.MethodPost { + w.WriteHeader(http.StatusMethodNotAllowed) + } + if r.Header.Get("Authorization") != "Bearer "+key { + w.WriteHeader(http.StatusUnauthorized) + } + req := openai.MessageRequest{} + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + w.WriteHeader(http.StatusOK) + response := openai.Message{ + ID: "test", + } + w.Header().Set("Content-Type", "application/json") + j, _ := json.Marshal(response) + w.Write(j) + }) + server := httptest.NewServer(handler) + client := NewOpenAI(server.URL, key) + // Test case: Create message request succeeds + req := openai.MessageRequest{} + _, err := client.CreateMessage(ctx, "test", req) + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } +} + +func TestCreateRun(t *testing.T) { + ctx := context.Background() + key := "test" + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/plugins/grafana-llm-app/resources/openai/v1/threads/test/runs" { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 page not found")) + } + if r.Method != http.MethodPost { + w.WriteHeader(http.StatusMethodNotAllowed) + } + if r.Header.Get("Authorization") != "Bearer "+key { + w.WriteHeader(http.StatusUnauthorized) + } + req := openai.RunRequest{} + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + w.WriteHeader(http.StatusOK) + response := openai.Run{ + ID: "test", + } + w.Header().Set("Content-Type", "application/json") + j, _ := json.Marshal(response) + w.Write(j) + }) + server := httptest.NewServer(handler) + client := NewOpenAI(server.URL, key) + // Test case: Create run request succeeds + req := openai.RunRequest{} + _, err := client.CreateRun(ctx, "test", req) + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } +} + +func TestRetrieveRun(t *testing.T) { + ctx := context.Background() + key := "test" + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/plugins/grafana-llm-app/resources/openai/v1/threads/test/runs/test" { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 page not found")) + } + if r.Method != http.MethodGet { + w.WriteHeader(http.StatusMethodNotAllowed) + } + if r.Header.Get("Authorization") != "Bearer "+key { + w.WriteHeader(http.StatusUnauthorized) + } + w.WriteHeader(http.StatusOK) + response := openai.Run{ + ID: "test", + } + w.Header().Set("Content-Type", "application/json") + j, _ := json.Marshal(response) + w.Write(j) + }) + server := httptest.NewServer(handler) + client := NewOpenAI(server.URL, key) + // Test case: Retrieve run request succeeds + _, err := client.RetrieveRun(ctx, "test", "test") + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } +} + +func TestSubmitToolOutputs(t *testing.T) { + ctx := context.Background() + key := "test" + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/plugins/grafana-llm-app/resources/openai/v1/threads/test/runs/test/submit_tool_outputs" { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 page not found")) + } + if r.Method != http.MethodPost { + w.WriteHeader(http.StatusMethodNotAllowed) + } + if r.Header.Get("Authorization") != "Bearer "+key { + w.WriteHeader(http.StatusUnauthorized) + } + req := openai.SubmitToolOutputsRequest{} + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + w.WriteHeader(http.StatusOK) + response := openai.Run{ + ID: "test", + } + w.Header().Set("Content-Type", "application/json") + j, _ := json.Marshal(response) + w.Write(j) + }) + server := httptest.NewServer(handler) + client := NewOpenAI(server.URL, key) + // Test case: Submit tool outputs request succeeds + req := openai.SubmitToolOutputsRequest{} + _, err := client.SubmitToolOutputs(ctx, "test", "test", req) + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } +} + +func TestListMessage(t *testing.T) { + ctx := context.Background() + key := "test" + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/plugins/grafana-llm-app/resources/openai/v1/threads/test/messages" { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 page not found")) + } + if r.Method != http.MethodGet { + w.WriteHeader(http.StatusMethodNotAllowed) + } + if r.Header.Get("Authorization") != "Bearer "+key { + w.WriteHeader(http.StatusUnauthorized) + } + w.WriteHeader(http.StatusOK) + response := openai.Run{ + ID: "test", + } + w.Header().Set("Content-Type", "application/json") + j, _ := json.Marshal(response) + w.Write(j) + }) + server := httptest.NewServer(handler) + client := NewOpenAI(server.URL, key) + // Test case: Retrieve run request succeeds + _, err := client.ListMessage(ctx, "test", nil, nil, nil, nil) + if err != nil { + t.Errorf("Expected no error, but got: %v", err) + } +}