1a9fc74999
A created chat's reply carried no mode, and the chat destination kept the first row it opened for the whole visit, ignoring later list re-reads, so the mode control only appeared after closing and reopening the chat. The server now names the mode on a created row, and the destination adopts each listed row that a re-read changed (excluding the live running flag), so the open conversation follows new, refreshed and other-device mode changes in place.
117 lines
4.4 KiB
Go
117 lines
4.4 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// modedChat records the modes the manager hands to the harness.
|
|
type modedChat struct {
|
|
*fakeChat
|
|
applied []string
|
|
}
|
|
|
|
func (c *modedChat) Modes(context.Context, string) ([]protocol.ChatMode, error) {
|
|
return []protocol.ChatMode{{ID: "default", Name: "Ask"}, {ID: "auto", Name: "Auto"}}, nil
|
|
}
|
|
|
|
func (c *modedChat) SetMode(_ context.Context, _, mode string) error {
|
|
if mode != "default" && mode != "auto" {
|
|
return errors.New("unknown mode " + mode)
|
|
}
|
|
c.applied = append(c.applied, mode)
|
|
return nil
|
|
}
|
|
|
|
func TestChatModeIsStoredAppliedAndSurvivesRestart(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "chat.db")
|
|
store, err := pipeline.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id := "01900000-0000-7000-8000-000000000002"
|
|
if err := store.RecordOperatorSession(ctx, id, "/fixture", "chat", protocol.ChatSessionTagOperator); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.SetOperatorSessionBackend(ctx, id, "claude"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake := &modedChat{fakeChat: scriptedChat()}
|
|
fake.sessionRecord = protocol.ChatSession{ID: id, Backend: "claude"}
|
|
fake.sessionFound = true
|
|
fake.sessions = []protocol.ChatSession{fake.sessionRecord}
|
|
manager := &chatManager{claude: fake, registry: store, cache: map[string]string{}}
|
|
var reply protocol.Message
|
|
capture := func(msg protocol.Message) error { reply = msg; return nil }
|
|
|
|
if session, _, err := manager.Session(ctx, id); err != nil || session.Mode != "default" || session.ModeName != "Ask" {
|
|
t.Fatalf("fresh session names %q/%q, want the harness default: %v", session.Mode, session.ModeName, err)
|
|
}
|
|
handleChatModes(ctx, protocol.Message{ID: "list", ChatQuery: &protocol.ChatQuery{SessionID: id}}, manager, capture)
|
|
if reply.Type != protocol.ChatModes || len(reply.ChatModes) != 2 || reply.ModeRequest == nil || reply.ModeRequest.Mode != "" {
|
|
t.Fatalf("modes reply = %+v", reply)
|
|
}
|
|
handleChatModeSwitch(ctx, protocol.Message{ID: "bad", ModeRequest: &protocol.ChatModeRequest{SessionID: id, Mode: "yolo"}}, manager, capture)
|
|
if reply.Type != protocol.Error {
|
|
t.Fatalf("unknown mode accepted: %+v", reply)
|
|
}
|
|
handleChatModeSwitch(ctx, protocol.Message{ID: "auto", ModeRequest: &protocol.ChatModeRequest{SessionID: id, Mode: "auto"}}, manager, capture)
|
|
if reply.Type != protocol.ChatModeSelect || reply.ModeRequest.Mode != "auto" || len(fake.applied) != 1 {
|
|
t.Fatalf("switch reply = %+v, applied %v", reply, fake.applied)
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err = pipeline.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
|
|
// A restarted server forgot the harness state; the stored mode is listed and
|
|
// re-applied before the next turn.
|
|
fake.applied = nil
|
|
manager = &chatManager{claude: fake, registry: store, cache: map[string]string{}}
|
|
session, found, err := manager.Session(ctx, id)
|
|
if err != nil || !found || session.Mode != "auto" || session.ModeName != "Auto" {
|
|
t.Fatalf("restarted session = %+v, %v", session, err)
|
|
}
|
|
list, err := manager.Sessions(ctx)
|
|
if err != nil || len(list) != 1 || list[0].Mode != "auto" {
|
|
t.Fatalf("restarted list = %+v, %v", list, err)
|
|
}
|
|
handleChatModes(ctx, protocol.Message{ID: "again", ChatQuery: &protocol.ChatQuery{SessionID: id}}, manager, capture)
|
|
if reply.ModeRequest == nil || reply.ModeRequest.Mode != "auto" {
|
|
t.Fatalf("current mode after restart = %+v", reply)
|
|
}
|
|
if err := manager.Prompt(ctx, id, "hello"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := manager.PromptWithMessageIDFiles(ctx, id, "again", "msg", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(fake.applied) != 2 || fake.applied[0] != "auto" || fake.applied[1] != "auto" {
|
|
t.Fatalf("stored mode not re-applied before turns: %v", fake.applied)
|
|
}
|
|
}
|
|
|
|
// A created chat names its mode like a listed row, so the client shows the
|
|
// mode control without re-reading the list.
|
|
func TestCreatedChatNamesItsMode(t *testing.T) {
|
|
fake := &modedChat{fakeChat: scriptedChat()}
|
|
manager := &chatManager{claude: fake, cache: map[string]string{}}
|
|
session, err := manager.CreateBackend(context.Background(), "chat", "/fixture", "claude")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if session.Mode != "default" || session.ModeName != "Ask" {
|
|
t.Fatalf("created session names %q/%q, want the harness default", session.Mode, session.ModeName)
|
|
}
|
|
}
|