137 lines
3.2 KiB
Go
137 lines
3.2 KiB
Go
//go:build integration
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAPIV0ChatBot(t *testing.T) {
|
|
defer goTestMain(t)()
|
|
|
|
body := url.Values{}
|
|
body.Set(`Prompt`, "Text transcript of a never ending dialogue between a middle manager and his direct reports. The middle manager works in a middle sized corporation and must tell employees what he thinks of employees' work. Middle manager always replies to bad news with an overly optimistic observation prefixed with 'Perhaps, but have you considered'.")
|
|
body.Set(`Message`, `I lost keys to the company car in my couch, boss.`)
|
|
|
|
t.Run("put over post", func(t *testing.T) {
|
|
resp := httpDo(t, http.MethodPost, "/api/v0/chatbot", body.Encode())
|
|
got, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("(%d) %s", resp.StatusCode, got)
|
|
if len(got) < 100 {
|
|
t.Error(string(got))
|
|
}
|
|
|
|
resp2 := httpDo(t, http.MethodPut, "/api/v0/chatbot", body.Encode())
|
|
got2, err := io.ReadAll(resp2.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("(%d) %s", resp2.StatusCode, got2)
|
|
if len(got2) < 100 {
|
|
t.Error(string(got2))
|
|
}
|
|
|
|
if bytes.Equal(got, got2) {
|
|
t.Error("dupe generation")
|
|
}
|
|
})
|
|
|
|
t.Run("post over post", func(t *testing.T) {
|
|
resp := httpDo(t, http.MethodPost, "/api/v0/chatbot", body.Encode())
|
|
got, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("(%d) %s", resp.StatusCode, got)
|
|
if len(got) < 100 {
|
|
t.Error(string(got))
|
|
}
|
|
|
|
resp2 := httpDo(t, http.MethodPost, "/api/v0/chatbot", body.Encode())
|
|
got2, err := io.ReadAll(resp2.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("(%d) %s", resp2.StatusCode, got2)
|
|
if len(got2) < 100 {
|
|
t.Error(string(got))
|
|
}
|
|
|
|
if bytes.Equal(got, got2) {
|
|
t.Error("dupe generation")
|
|
}
|
|
})
|
|
|
|
t.Run("put over zero", func(t *testing.T) {
|
|
resp := httpDo(t, http.MethodPut, "/api/v0/chatbot", body.Encode())
|
|
got, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("(%d) %s", resp.StatusCode, got)
|
|
if len(got) > 100 {
|
|
t.Error(string(got))
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
func goTestMain(t *testing.T) func() {
|
|
log.SetOutput(io.Discard)
|
|
|
|
ctx, can := context.WithCancel(context.Background())
|
|
ctx, cleanup := contextWithCleanup(ctx)
|
|
config(ctx)
|
|
Config.Port += 10
|
|
Config.ChatBot.N = 32
|
|
wg := &sync.WaitGroup{}
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
listenAndServe(ctx)
|
|
}()
|
|
httpDo(t, http.MethodGet, "/", "")
|
|
return func() {
|
|
cleanup()
|
|
can()
|
|
wg.Wait()
|
|
}
|
|
}
|
|
|
|
func httpDo(t *testing.T, method, path, body string) *http.Response {
|
|
req, _ := http.NewRequest(
|
|
method,
|
|
fmt.Sprintf("http://localhost:%d/%s", Config.Port, strings.TrimLeft(path, "/")),
|
|
nil,
|
|
)
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
cookie := Cookie{Name: t.Name()}
|
|
req.Header.Set("Cookie", "root="+cookie.Serialize())
|
|
for {
|
|
req.Body = io.NopCloser(strings.NewReader(body))
|
|
if resp, err := http.DefaultClient.Do(req.Clone(context.Background())); err == nil {
|
|
defer resp.Body.Close()
|
|
b, _ := io.ReadAll(resp.Body)
|
|
resp.Body = io.NopCloser(bytes.NewReader(b))
|
|
return resp
|
|
}
|
|
time.Sleep(time.Millisecond * 25)
|
|
t.Logf("retrying %s", req.URL.String())
|
|
}
|
|
t.Fatalf("failed to ever %s %s", method, path)
|
|
panic(nil)
|
|
}
|