113 lines
3.4 KiB
Go
113 lines
3.4 KiB
Go
package exec_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
iexec "github.com/mathiasbq/supervisor/internal/exec"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func validLiteLLMResult() iexec.Result {
|
|
return iexec.Result{
|
|
Status: "pass",
|
|
Phase: "review",
|
|
Skill: "review",
|
|
ModelUsed: "ollama/devstral",
|
|
Message: "looks good",
|
|
}
|
|
}
|
|
|
|
func chatResponseFor(t *testing.T, result iexec.Result) []byte {
|
|
t.Helper()
|
|
content, err := json.Marshal(result)
|
|
require.NoError(t, err)
|
|
resp := map[string]any{
|
|
"choices": []map[string]any{
|
|
{"message": map[string]any{"role": "assistant", "content": string(content)}},
|
|
},
|
|
}
|
|
data, err := json.Marshal(resp)
|
|
require.NoError(t, err)
|
|
return data
|
|
}
|
|
|
|
func TestLiteLLMParsesValidResult(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, "/v1/chat/completions", r.URL.Path)
|
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write(chatResponseFor(t, validLiteLLMResult()))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
ex := iexec.NewLiteLLM(srv.URL, "", 5*time.Second)
|
|
result, err := ex.Run(context.Background(), iexec.Request{
|
|
SkillPrompt: "review rules",
|
|
TaskPrompt: "review the code",
|
|
Model: "ollama/devstral",
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "pass", result.Status)
|
|
assert.Equal(t, "review", result.Skill)
|
|
}
|
|
|
|
func TestLiteLLMSendsAuthHeader(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, "Bearer secret", r.Header.Get("Authorization"))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write(chatResponseFor(t, validLiteLLMResult()))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
ex := iexec.NewLiteLLM(srv.URL, "secret", 5*time.Second)
|
|
_, err := ex.Run(context.Background(), iexec.Request{Model: "x", TaskPrompt: "t", SkillPrompt: "s"})
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestLiteLLMErrorOnNonOKStatus(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
ex := iexec.NewLiteLLM(srv.URL, "", 5*time.Second)
|
|
_, err := ex.Run(context.Background(), iexec.Request{Model: "x", TaskPrompt: "t"})
|
|
assert.ErrorContains(t, err, "503")
|
|
}
|
|
|
|
func TestLiteLLMErrorOnUnparsableJSON(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
resp := map[string]any{
|
|
"choices": []map[string]any{
|
|
{"message": map[string]any{"role": "assistant", "content": "not json at all"}},
|
|
},
|
|
}
|
|
data, _ := json.Marshal(resp)
|
|
_, _ = w.Write(data)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
ex := iexec.NewLiteLLM(srv.URL, "", 5*time.Second)
|
|
_, err := ex.Run(context.Background(), iexec.Request{Model: "x", TaskPrompt: "t"})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestLiteLLMRespectsContextCancellation(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // Cancel immediately
|
|
|
|
ex := iexec.NewLiteLLM("http://invalid.example.com", "", 1*time.Second)
|
|
_, err := ex.Run(ctx, iexec.Request{Model: "x", TaskPrompt: "t"})
|
|
assert.Error(t, err)
|
|
}
|