Files
pipelines/internal/pipeline/chat_prompt_queue_test.go
T
alan 1ea1e5b73c Schedule chat prompts: send_at holds a queued prompt until its time
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>
2026-11-25 00:51:06 +00:00

100 lines
3.4 KiB
Go

package pipeline
import (
"context"
"errors"
"path/filepath"
"testing"
"time"
)
// A database whose queue table predates send_at gains the column on Open,
// keeps its rows unscheduled, and round-trips a scheduled prompt.
func TestChatPromptQueueSendAtMigratesAndRoundTrips(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "queue.db")
store, err := Open(path)
if err != nil {
t.Fatal(err)
}
// Rebuild the table in its pre-send_at shape with one existing row.
if _, err := store.db.Exec(`DROP TABLE chat_prompt_queue;
CREATE TABLE chat_prompt_queue (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
operation_id TEXT NOT NULL,
message_id TEXT NOT NULL,
native_message_id TEXT NOT NULL DEFAULT '',
text TEXT NOT NULL,
client TEXT NOT NULL DEFAULT '',
files_json TEXT NOT NULL DEFAULT '[]',
parts_json TEXT NOT NULL DEFAULT '[]',
created_at INTEGER NOT NULL,
UNIQUE(session_id, operation_id));
INSERT INTO chat_prompt_queue (session_id, operation_id, message_id, text, created_at) VALUES ('ses_1', 'old', 'm-old', 'old', 1000);`); err != nil {
t.Fatal(err)
}
store.Close()
store, err = Open(path)
if err != nil {
t.Fatal(err)
}
at := time.UnixMilli(time.Now().Add(time.Hour).UnixMilli())
if _, created, err := store.EnqueueChatPrompt(ctx, ChatQueuedPrompt{SessionID: "ses_1", OperationID: "new", MessageID: "m-new", Text: "new", SendAt: at}, 0); err != nil || !created {
t.Fatalf("enqueue scheduled: %v %v", created, err)
}
store.Close()
store, err = Open(path) // the migration is idempotent across Opens
if err != nil {
t.Fatal(err)
}
defer store.Close()
rows, err := store.ChatQueuedPrompts(ctx, "ses_1")
if err != nil || len(rows) != 2 {
t.Fatalf("rows = %+v %v", rows, err)
}
if rows[0].OperationID != "old" || !rows[0].SendAt.IsZero() {
t.Fatalf("migrated row = %+v", rows[0])
}
if rows[1].OperationID != "new" || !rows[1].SendAt.Equal(at) {
t.Fatalf("scheduled row = %+v, want send_at %v", rows[1], at)
}
}
func TestChatPromptQueueOrderDedupeAndBound(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "queue.db")
store, err := Open(path)
if err != nil {
t.Fatal(err)
}
for _, op := range []string{"a", "b"} {
if _, created, err := store.EnqueueChatPrompt(ctx, ChatQueuedPrompt{SessionID: "ses_1", OperationID: op, MessageID: "m-" + op, Text: op}, 2); err != nil || !created {
t.Fatalf("enqueue %s: created=%v err=%v", op, created, err)
}
}
if row, created, err := store.EnqueueChatPrompt(ctx, ChatQueuedPrompt{SessionID: "ses_1", OperationID: "a", Text: "again"}, 2); err != nil || created || row.Text != "a" {
t.Fatalf("duplicate operation: row=%+v created=%v err=%v", row, created, err)
}
if _, _, err := store.EnqueueChatPrompt(ctx, ChatQueuedPrompt{SessionID: "ses_1", OperationID: "c", Text: "c"}, 2); !errors.Is(err, ErrChatQueueFull) {
t.Fatalf("bound not enforced: %v", err)
}
store.Close()
store, err = Open(path)
if err != nil {
t.Fatal(err)
}
defer store.Close()
sessions, err := store.ChatQueuedSessions(ctx)
if err != nil || len(sessions) != 1 || sessions[0] != "ses_1" {
t.Fatalf("sessions = %v %v", sessions, err)
}
if removed, err := store.DeleteChatQueuedPrompt(ctx, "ses_1", "a"); err != nil || !removed {
t.Fatalf("delete: %v %v", removed, err)
}
rows, err := store.ChatQueuedPrompts(ctx, "ses_1")
if err != nil || len(rows) != 1 || rows[0].OperationID != "b" || rows[0].MessageID != "m-b" {
t.Fatalf("rows = %+v %v", rows, err)
}
}