105 lines
3.9 KiB
Go
105 lines
3.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
type selectedModelChat struct {
|
|
*fakeChat
|
|
model, nativeID string
|
|
overrides int
|
|
}
|
|
|
|
func (c *selectedModelChat) PromptWithMessageIDModel(_ context.Context, _, _, nativeID, model string) error {
|
|
c.model, c.nativeID = model, nativeID
|
|
c.overrides++
|
|
return nil
|
|
}
|
|
|
|
func TestCodexModelSelectionSurvivesServerRestart(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-000000000001"
|
|
if err := store.RecordOperatorSession(ctx, id, "/fixture", "chat", protocol.ChatSessionTagOperator); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.SetOperatorSessionBackend(ctx, id, "codex"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake := &selectedModelChat{fakeChat: scriptedChat()}
|
|
fake.models = []protocol.ChatModel{{ID: "fixture-model", Provider: "fixture-provider"}}
|
|
fake.sessionRecord = protocol.ChatSession{ID: id, Backend: "codex", Model: "fixture-current", ModelProvider: "fixture-provider"}
|
|
fake.sessionFound = true
|
|
fake.sessions = []protocol.ChatSession{fake.sessionRecord}
|
|
manager := &chatManager{codex: fake, registry: store, cache: map[string]string{}}
|
|
var reply protocol.Message
|
|
handleChatModelSwitch(ctx, protocol.Message{ID: "choose", ModelRequest: &protocol.ChatModelRequest{SessionID: id, Provider: "fixture-provider", Model: "fixture-model"}}, manager, func(msg protocol.Message) error { reply = msg; return nil })
|
|
if reply.Type != protocol.ChatModelSelect || reply.ModelRequest.Selection != "next_turn" {
|
|
t.Fatalf("selection reply = %+v", reply)
|
|
}
|
|
if fake.overrides != 0 {
|
|
t.Fatal("selection started a turn")
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err = pipeline.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
// A new manager has neither backend nor model cached in memory.
|
|
manager = &chatManager{codex: fake, registry: store, cache: map[string]string{}}
|
|
session, found, err := manager.Session(ctx, id)
|
|
if err != nil || !found || session.Model != "fixture-model" || session.ModelSelection != "next_turn" {
|
|
t.Fatalf("restarted session = %+v, %v", session, err)
|
|
}
|
|
list, err := manager.Sessions(ctx)
|
|
if err != nil || len(list) != 1 || list[0].Model != "fixture-model" || list[0].ModelSelection != "next_turn" {
|
|
t.Fatalf("restarted list = %+v, %v", list, err)
|
|
}
|
|
if err := manager.PromptWithMessageID(ctx, id, "hello", "msg_native"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fake.model != "fixture-model" || fake.nativeID != "msg_native" {
|
|
t.Fatalf("override = %s/%s", fake.model, fake.nativeID)
|
|
}
|
|
if err := manager.Prompt(ctx, id, "again"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fake.overrides != 2 {
|
|
t.Fatal("plain prompt bypassed saved choice")
|
|
}
|
|
if err := manager.PromptWithMessageIDFiles(ctx, id, "no attachments", "msg_empty", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fake.overrides != 3 || fake.nativeID != "msg_empty" {
|
|
t.Fatal("empty attachments bypassed saved choice")
|
|
}
|
|
fake.sessionRecord.ModelProvider = "different-provider"
|
|
if err := manager.Prompt(ctx, id, "must not send"); err == nil {
|
|
t.Fatal("saved model sent to a different provider")
|
|
}
|
|
if fake.overrides != 3 {
|
|
t.Fatal("mismatched provider started a turn")
|
|
}
|
|
// A failed durable save must never acknowledge the next-turn selection.
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reply = protocol.Message{}
|
|
handleChatModelSwitch(ctx, protocol.Message{ID: "failed-save", ModelRequest: &protocol.ChatModelRequest{SessionID: id, Provider: "fixture-provider", Model: "fixture-model"}}, manager, func(msg protocol.Message) error { reply = msg; return nil })
|
|
if reply.Type != protocol.Error || reply.ModelRequest != nil {
|
|
t.Fatalf("failed save acknowledged: %+v", reply)
|
|
}
|
|
}
|