46 lines
1.7 KiB
Go
46 lines
1.7 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func TestChatFormsReadbackUsesSessionOwnership(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/session/ses_otherdir/form" {
|
|
t.Errorf("wrong session path: %s", r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"data":[{"id":"f_1","sessionID":"ses_otherdir","title":"Pending before connect","metadata":{"kind":"question"},"fields":[{"key":"answer","type":"text","required":true}]},{"id":"foreign","sessionID":"ses_foreign"}]}`))
|
|
}))
|
|
defer srv.Close()
|
|
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL, Directory: "/unrelated"})
|
|
forms, err := chat.Forms(context.Background(), "ses_otherdir")
|
|
if err != nil || len(forms) != 1 || forms[0].ID != "f_1" || len(forms[0].Fields) != 1 || !forms[0].Fields[0].Required {
|
|
t.Fatalf("forms=%+v err=%v", forms, err)
|
|
}
|
|
}
|
|
|
|
func TestChatQuestionCancellationDoesNotSendAnAnswer(t *testing.T) {
|
|
calls := 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls++
|
|
if r.Method != "DELETE" || r.URL.Path != "/api/session/ses_test/form/form_test" {
|
|
t.Errorf("unexpected cancellation: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
|
if err := chat.Answer(context.Background(), protocol.ChatAnswerRequest{SessionID: "ses_test", FormID: "form_test", Action: "cancel"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls != 1 {
|
|
t.Fatalf("calls=%d", calls)
|
|
}
|
|
}
|