Personas were a misread requirement: SOUL is the chat identity and identities are per action. The selected persona's prompt never reached the model. Drops the resource, /personality, the chat selector, the settings page and ChatService.SetPersona, and deletes stored persona documents.
96 lines
3.1 KiB
Go
96 lines
3.1 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
)
|
|
|
|
type ownedChat struct{ messaging.ChatContract }
|
|
|
|
func (ownedChat) Snapshot(context.Context) (messaging.ChatSnapshot, error) {
|
|
return messaging.ChatSnapshot{CancellationAvailable: true}, nil
|
|
}
|
|
|
|
func (ownedChat) GetSession(context.Context, string) (messaging.SessionContext, bool, error) {
|
|
return messaging.SessionContext{SessionID: "private", Source: messaging.SessionSource{Platform: "web", ThreadID: "alice"}}, true, nil
|
|
}
|
|
|
|
func (ownedChat) RecentMessages(context.Context, string, int) ([]messaging.Message, error) {
|
|
return []messaging.Message{}, nil
|
|
}
|
|
|
|
func (ownedChat) RecentTurns(context.Context, string, int) ([]messaging.TurnRecord, error) {
|
|
return []messaging.TurnRecord{}, nil
|
|
}
|
|
|
|
func (ownedChat) Cancel(context.Context, string) (messaging.ChatCancellation, error) {
|
|
return messaging.ChatCancellation{Cancelled: true}, nil
|
|
}
|
|
|
|
func TestWebChatOwnership(t *testing.T) {
|
|
for _, route := range []struct {
|
|
name, body string
|
|
handler func(*Server, http.ResponseWriter, *http.Request)
|
|
}{
|
|
{"messages", "", (*Server).handleChatMessages},
|
|
{"turns", "", (*Server).handleChatTurns},
|
|
{"cancel", `{"session_id":"private"}`, (*Server).handleChatCancel},
|
|
} {
|
|
for _, user := range []string{"alice", "bob"} {
|
|
t.Run(route.name+"/"+user, func(t *testing.T) {
|
|
ctx := WithActingIdentity(t.Context(), identity.Identity{ID: identity.IdentityID(user)})
|
|
req := httptest.NewRequestWithContext(ctx, "POST", "https://archie.example/api/chat/test", strings.NewReader(route.body))
|
|
req.SetPathValue("id", "private")
|
|
rec := httptest.NewRecorder()
|
|
srv := &Server{Chat: &ChatService{Contract: ownedChat{}}}
|
|
route.handler(srv, rec, req)
|
|
want := 200
|
|
if user != "alice" {
|
|
want = 404
|
|
}
|
|
if rec.Code != want {
|
|
t.Fatalf("status=%d want=%d body=%s", rec.Code, want, rec.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWebChatSenderCannotBeSuppliedByBrowser(t *testing.T) {
|
|
ctx := WithActingIdentity(t.Context(), identity.Identity{ID: "alice"})
|
|
req := httptest.NewRequestWithContext(ctx, "POST", "https://archie.example/api/chat/message", strings.NewReader(`{"channel_id":"browser-channel","text":"hello","sender_id":"bob","thread_id":"bob"}`))
|
|
value, ok := (&Server{}).decodeChatMessage(httptest.NewRecorder(), req)
|
|
if !ok {
|
|
t.Fatal("message refused")
|
|
}
|
|
if value.Message.SenderID != "alice" || value.Message.ConversationID.ThreadID != "alice" {
|
|
t.Fatalf("browser selected sender: %+v", value.Message)
|
|
}
|
|
}
|
|
|
|
func TestChatQuestionBelongsToInitiatingPerson(t *testing.T) {
|
|
var asks chatAsks
|
|
id, answers := asks.open("alice")
|
|
defer asks.close(id)
|
|
if asks.answer(id, "approve", "bob") {
|
|
t.Fatal("another person answered the question")
|
|
}
|
|
select {
|
|
case <-answers:
|
|
t.Fatal("unauthorized answer delivered")
|
|
default:
|
|
}
|
|
if !asks.answer(id, "approve", "alice") {
|
|
t.Fatal("owner could not answer")
|
|
}
|
|
if answer := <-answers; answer != "approve" {
|
|
t.Fatalf("answer=%s", answer)
|
|
}
|
|
}
|