Files
pipelines/internal/server/chat_tools_followup_test.go
T

68 lines
2.9 KiB
Go

package server
import (
"context"
"encoding/json"
"net/http"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// schedule_followup queues a durable, removable prompt under the agent's
// follow-up label, and a pending follow-up never holds the operator's own
// prompt back.
func TestScheduleFollowupQueuesWithoutBlockingTheOperator(t *testing.T) {
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
broker := newChannelChat()
server := startQueueServer(t, broker, store)
token, err := store.ChatToolToken(context.Background(), chatToolSessionScope+"ses_1")
if err != nil {
t.Fatal(err)
}
call := func(arguments string) map[string]any {
body := `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"schedule_followup","arguments":` + arguments + `}}`
request, _ := http.NewRequestWithContext(server.ctx, http.MethodPost, server.srv.URL+chatToolPath+token, strings.NewReader(body))
request.Header.Set("Content-Type", "application/json")
response, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatal(err)
}
defer response.Body.Close()
var out struct {
Result map[string]any `json:"result"`
}
_ = json.NewDecoder(response.Body).Decode(&out)
return out.Result
}
if result := call(`{"message":"check the run","after_minutes":30}`); result["isError"] != false {
t.Fatalf("schedule_followup = %v", result)
}
rows, err := store.ChatQueuedPrompts(context.Background(), "ses_1")
if err != nil || len(rows) != 1 || !strings.HasPrefix(rows[0].OperationID, chatFollowupPrefix) || rows[0].Client != chatFollowupClient || rows[0].SendAt.IsZero() || !strings.Contains(rows[0].Text, "check the run") {
t.Fatalf("stored follow-up = %+v %v", rows, err)
}
conn := dialChatConn(t, server.ctx, server.srv)
if ack := sendPrompt(t, server.ctx, conn, "p1", "hello", "op-1"); ack.ChatQueued {
t.Fatalf("the operator's prompt waited behind a pending follow-up: %+v", ack)
}
waitFor(t, "operator prompt delivered", func() bool { return broker.promptCount() == 1 })
endTurn(broker)
call(`{"message":"two","after_minutes":60}`)
call(`{"message":"three","after_minutes":90}`)
if result := call(`{"message":"four","after_minutes":120}`); result["isError"] != true {
t.Fatalf("a fourth pending follow-up must be refused: %v", result)
}
writeChat(t, server.ctx, conn, protocol.Message{Type: protocol.ChatUnqueue, ID: "rm", ChatUnqueue: &protocol.ChatUnqueueRequest{SessionID: "ses_1", OperationID: rows[0].OperationID}})
if removed := readUntil(t, server.ctx, conn, protocol.ChatUnqueue); removed.ChatQueue == nil || removed.ChatQueue.Removed != rows[0].OperationID || len(removed.ChatQueue.Prompts) != 2 {
t.Fatalf("the operator removes a follow-up: %+v", removed.ChatQueue)
}
if n := broker.promptCount(); n != 1 {
t.Fatalf("no follow-up is delivered before its time: %v", broker.promptTexts())
}
}