1ea1e5b73c
A chat_prompt with a future send_at (epoch ms) is always queued, durable in chat_prompt_queue (new send_at column, added to older databases on Open), listed with send_at in chat_queue frames, and delivered by the room's runner once the time passes, waking on a timer. The queue stays FIFO: prompts queued after a scheduled one wait behind it. A past or zero send_at behaves as before; chat_unqueue cancels as for any queued prompt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
5.9 KiB
Go
139 lines
5.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func sendScheduled(t *testing.T, ctx context.Context, conn *websocket.Conn, id, text, operation string, at time.Time) protocol.Message {
|
|
t.Helper()
|
|
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: id, ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: text, OperationID: operation, Client: "Phone", SendAt: at.UnixMilli()}})
|
|
return readUntil(t, ctx, conn, protocol.ChatPrompt)
|
|
}
|
|
|
|
// A prompt scheduled for later is queued even on an idle session, listed with
|
|
// its send_at, held until that time, and a prompt queued after it waits
|
|
// behind it (FIFO).
|
|
func TestChatScheduledPromptWaitsForItsTime(t *testing.T) {
|
|
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
|
|
broker := newChannelChat()
|
|
server := startQueueServer(t, broker, store)
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
at := time.Now().Add(400 * time.Millisecond)
|
|
if ack := sendScheduled(t, server.ctx, conn, "p1", "later", "op-1", at); !ack.ChatQueued {
|
|
t.Fatalf("scheduled prompt not queued: %+v", ack)
|
|
}
|
|
if ack := sendPrompt(t, server.ctx, conn, "p2", "after", "op-2"); !ack.ChatQueued {
|
|
t.Fatalf("prompt behind a scheduled one not queued: %+v", ack)
|
|
}
|
|
rows, err := store.ChatQueuedPrompts(context.Background(), "ses_1")
|
|
if err != nil || len(rows) != 2 || rows[0].SendAt.UnixMilli() != at.UnixMilli() || !rows[1].SendAt.IsZero() {
|
|
t.Fatalf("stored rows = %+v %v", rows, err)
|
|
}
|
|
writeChat(t, server.ctx, conn, protocol.Message{Type: protocol.ChatSubscribe, ID: "sub", ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1"}})
|
|
sub := readUntil(t, server.ctx, conn, protocol.ChatSubscribe)
|
|
if sub.ChatQueue == nil || len(sub.ChatQueue.Prompts) != 2 || sub.ChatQueue.Prompts[0].SendAt != at.UnixMilli() || sub.ChatQueue.Prompts[1].SendAt != 0 {
|
|
t.Fatalf("subscribe queue = %+v", sub.ChatQueue)
|
|
}
|
|
time.Sleep(150 * time.Millisecond)
|
|
if n := broker.promptCount(); n != 0 {
|
|
t.Fatalf("scheduled prompt delivered early: %v", broker.promptTexts())
|
|
}
|
|
waitFor(t, "scheduled prompt delivered", func() bool { return broker.promptCount() == 1 })
|
|
if time.Now().Before(at) {
|
|
t.Fatalf("delivered before send_at")
|
|
}
|
|
if texts := broker.promptTexts(); !slices.Equal(texts, []string{"later"}) {
|
|
t.Fatalf("delivered = %v", texts)
|
|
}
|
|
endTurn(broker)
|
|
waitFor(t, "follow-up delivered", func() bool {
|
|
return slices.Equal(broker.promptTexts(), []string{"later", "after"})
|
|
})
|
|
waitFor(t, "rows deleted", func() bool { return len(queuedOps(t, store)) == 0 })
|
|
}
|
|
|
|
// A send_at already in the past sends exactly as an unscheduled prompt.
|
|
func TestChatScheduledPromptInThePastSendsNow(t *testing.T) {
|
|
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
|
|
broker := newChannelChat()
|
|
server := startQueueServer(t, broker, store)
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
if ack := sendScheduled(t, server.ctx, conn, "p1", "now", "op-1", time.Now().Add(-time.Minute)); ack.ChatQueued {
|
|
t.Fatalf("past send_at was queued: %+v", ack)
|
|
}
|
|
waitFor(t, "prompt delivered", func() bool { return broker.promptCount() == 1 })
|
|
if ops := queuedOps(t, store); len(ops) != 0 {
|
|
t.Fatalf("past send_at stored: %v", ops)
|
|
}
|
|
}
|
|
|
|
// chat_unqueue cancels a scheduled prompt: it is never delivered.
|
|
func TestChatScheduledPromptUnqueueCancels(t *testing.T) {
|
|
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
|
|
broker := newChannelChat()
|
|
server := startQueueServer(t, broker, store)
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
at := time.Now().Add(300 * time.Millisecond)
|
|
if ack := sendScheduled(t, server.ctx, conn, "p1", "later", "op-1", at); !ack.ChatQueued {
|
|
t.Fatalf("scheduled prompt not queued: %+v", ack)
|
|
}
|
|
writeChat(t, server.ctx, conn, protocol.Message{Type: protocol.ChatUnqueue, ID: "rm", ChatUnqueue: &protocol.ChatUnqueueRequest{SessionID: "ses_1", OperationID: "op-1"}})
|
|
removed := readUntil(t, server.ctx, conn, protocol.ChatUnqueue)
|
|
if removed.ChatQueue == nil || removed.ChatQueue.Removed != "op-1" || len(removed.ChatQueue.Prompts) != 0 {
|
|
t.Fatalf("unqueue reply = %+v", removed.ChatQueue)
|
|
}
|
|
time.Sleep(time.Until(at) + 200*time.Millisecond)
|
|
if n := broker.promptCount(); n != 0 {
|
|
t.Fatalf("cancelled scheduled prompt delivered: %v", broker.promptTexts())
|
|
}
|
|
if ops := queuedOps(t, store); len(ops) != 0 {
|
|
t.Fatalf("cancelled row kept: %v", ops)
|
|
}
|
|
}
|
|
|
|
// A scheduled prompt survives a restart and is delivered at its time, not at
|
|
// recovery.
|
|
func TestChatScheduledPromptSurvivesRestart(t *testing.T) {
|
|
previous := chatQueueRecoverySettle
|
|
chatQueueRecoverySettle = 10 * time.Millisecond
|
|
t.Cleanup(func() { chatQueueRecoverySettle = previous })
|
|
path := filepath.Join(t.TempDir(), "queue.db")
|
|
first := openQueueStore(t, path)
|
|
broker := newChannelChat()
|
|
server := startQueueServer(t, broker, first)
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
at := time.Now().Add(700 * time.Millisecond)
|
|
if ack := sendScheduled(t, server.ctx, conn, "p1", "later", "op-1", at); !ack.ChatQueued {
|
|
t.Fatalf("scheduled prompt not queued: %+v", ack)
|
|
}
|
|
server.stop()
|
|
first.Close()
|
|
|
|
reopened := openQueueStore(t, path)
|
|
rows, err := reopened.ChatQueuedPrompts(context.Background(), "ses_1")
|
|
if err != nil || len(rows) != 1 || rows[0].SendAt.UnixMilli() != at.UnixMilli() {
|
|
t.Fatalf("rows after restart = %+v %v", rows, err)
|
|
}
|
|
restarted := newChannelChat()
|
|
startQueueServer(t, restarted, reopened)
|
|
time.Sleep(100 * time.Millisecond)
|
|
if time.Now().Before(at) && restarted.promptCount() != 0 {
|
|
t.Fatalf("recovered scheduled prompt delivered early")
|
|
}
|
|
waitFor(t, "recovered scheduled prompt delivered", func() bool {
|
|
return slices.Equal(restarted.promptTexts(), []string{"later"})
|
|
})
|
|
if time.Now().Before(at) {
|
|
t.Fatalf("delivered before send_at")
|
|
}
|
|
waitFor(t, "recovered row deleted", func() bool { return len(queuedOps(t, reopened)) == 0 })
|
|
}
|