package server import ( "context" "encoding/json" "net/url" "path/filepath" "slices" "testing" "time" "git.a-wan.net/alan/pipelines/internal/pipeline" ) func fastQueueRecovery(t *testing.T) { t.Helper() settle, retry, retryMax := chatQueueRecoverySettle, chatQueueRecoveryRetry, chatQueueRecoveryRetryMax chatQueueRecoverySettle, chatQueueRecoveryRetry, chatQueueRecoveryRetryMax = 10*time.Millisecond, 50*time.Millisecond, 100*time.Millisecond t.Cleanup(func() { chatQueueRecoverySettle, chatQueueRecoveryRetry, chatQueueRecoveryRetryMax = settle, retry, retryMax }) } func queuedPromptWithFile(t *testing.T, store *pipeline.Store, op, text, path string) { t.Helper() files, err := json.Marshal([]chatStoredFile{{URI: (&url.URL{Scheme: "file", Path: path}).String(), Name: filepath.Base(path)}}) if err != nil { t.Fatal(err) } if _, _, err := store.EnqueueChatPrompt(context.Background(), pipeline.ChatQueuedPrompt{SessionID: "ses_1", OperationID: op, MessageID: "msg_" + op, Text: text, FilesJSON: string(files)}, 0); err != nil { t.Fatal(err) } } // A queued prompt whose attachment directory cannot be resolved at restart is // kept and restored on a later retry, not deleted. func TestChatQueuedPromptKeptOnTransientAttachmentFailure(t *testing.T) { fastQueueRecovery(t) dir := t.TempDir() path := filepath.Join(t.TempDir(), "queue.db") store := openQueueStore(t, path) queuedPromptWithFile(t, store, "op-1", "with file", filepath.Join(dir, "a.txt")) t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", "relative/dir") // unresolvable for now broker := newChannelChat() startQueueServer(t, broker, store) time.Sleep(200 * time.Millisecond) if ops := queuedOps(t, store); !slices.Equal(ops, []string{"op-1"}) { t.Fatalf("queued prompt dropped on a transient failure: %v", ops) } if texts := broker.promptTexts(); len(texts) != 0 { t.Fatalf("prompt delivered while unrestorable: %v", texts) } t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir) waitFor(t, "prompt restored on retry", func() bool { return slices.Equal(broker.promptTexts(), []string{"with file"}) }) waitFor(t, "delivered row deleted", func() bool { return len(queuedOps(t, store)) == 0 }) } // A queued prompt that can never be rebuilt is still dropped, and the rest of // the session's queue is delivered. func TestChatQueuedPromptDroppedOnPermanentFailure(t *testing.T) { fastQueueRecovery(t) dir := t.TempDir() t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir) path := filepath.Join(t.TempDir(), "queue.db") store := openQueueStore(t, path) queuedPromptWithFile(t, store, "op-1", "outside", filepath.Join(t.TempDir(), "elsewhere.txt")) if _, _, err := store.EnqueueChatPrompt(context.Background(), pipeline.ChatQueuedPrompt{SessionID: "ses_1", OperationID: "op-2", MessageID: "msg_op-2", Text: "plain"}, 0); err != nil { t.Fatal(err) } broker := newChannelChat() startQueueServer(t, broker, store) waitFor(t, "valid prompt delivered", func() bool { return slices.Equal(broker.promptTexts(), []string{"plain"}) }) waitFor(t, "rows cleared", func() bool { return len(queuedOps(t, store)) == 0 }) }