Skip to content

Commit

Permalink
Added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcoghlan committed Nov 4, 2024
1 parent ace59f1 commit 940cc70
Showing 1 changed file with 250 additions and 0 deletions.
250 changes: 250 additions & 0 deletions packages/grafana-llm-app/llmclient/llmclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 940cc70

Please sign in to comment.