FILE / ScuroNeko/go-deepseek
methods_test.go
Исходный файл и его история в репозитории.
91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package deepseek
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCreateChatWithContext(t *testing.T) {
|
|
client := testAPI(func(r *http.Request) (*http.Response, error) {
|
|
if r.URL.Path != "/chat_session/create" {
|
|
t.Errorf("path = %q", r.URL.Path)
|
|
}
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
t.Errorf("read body: %v", err)
|
|
}
|
|
if string(body) != `{}` {
|
|
t.Errorf("body = %s, want {}", body)
|
|
}
|
|
return testHTTPResponse(`{"code":0,"data":{"biz_code":0,"biz_data":{"chat_session":{"id":"chat-id","seq_id":7,"model_type":"default"},"ttl_seconds":60}}}`), nil
|
|
})
|
|
|
|
got, err := client.CreateChatWithContext(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("CreateChatWithContext() error = %v", err)
|
|
}
|
|
if got.ID != "chat-id" || got.SeqID != 7 || got.ModelType != "default" {
|
|
t.Fatalf("session = %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestDeleteChatWithContext(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
body string
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "success",
|
|
body: `{"code":0,"data":{"biz_code":0,"biz_data":{}}}`,
|
|
},
|
|
{
|
|
name: "business error",
|
|
body: `{"code":0,"data":{"biz_code":42,"biz_msg":"delete denied"}}`,
|
|
wantErr: "delete denied (42)",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
responseBody := &trackingReadCloser{Reader: strings.NewReader(tt.body)}
|
|
client := testAPI(func(r *http.Request) (*http.Response, error) {
|
|
if r.Method != http.MethodPost {
|
|
t.Errorf("method = %q, want POST", r.Method)
|
|
}
|
|
if r.URL.Path != "/chat_session/delete" {
|
|
t.Errorf("path = %q, want /chat_session/delete", r.URL.Path)
|
|
}
|
|
|
|
var body deleteChatReq
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Errorf("decode request: %v", err)
|
|
}
|
|
if body.ChatSessionID != "chat-id" {
|
|
t.Errorf("chat_session_id = %q, want chat-id", body.ChatSessionID)
|
|
}
|
|
|
|
resp := testHTTPResponse("")
|
|
resp.Body = responseBody
|
|
return resp, nil
|
|
})
|
|
|
|
err := client.DeleteChatWithContext(context.Background(), "chat-id")
|
|
if tt.wantErr != "" {
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("DeleteChatWithContext() error = %v, want containing %q", err, tt.wantErr)
|
|
}
|
|
} else if err != nil {
|
|
t.Fatalf("DeleteChatWithContext() error = %v", err)
|
|
}
|
|
if !responseBody.closed {
|
|
t.Fatal("response body was not closed")
|
|
}
|
|
})
|
|
}
|
|
}
|