Files
pipelines/internal/server/chat_prompt_origin_test.go
T
alan 9a6363b80e Chat: draw agent follow-ups and automatic continues apart from the operator
The server now names a user turn's origin (followup, auto_continue,
auto_switch, scheduled) on chat messages, user frames and queued prompts,
from the prompt's operation id while it holds the prompt and from its own
text prefixes in history read later. Origin is a new field, so older
clients keep treating these as ordinary user turns.

The app draws an agent follow-up, auto-continue and account-switch prompt
as a quiet outlined card on the agent's side with an icon, label and the
schedule facts from the preamble; the operator's own scheduled message
keeps its bubble with a small Scheduled mark. Find searches the drawn text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 15:40:34 +00:00

87 lines
3.6 KiB
Go

package server
import (
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatPromptOriginFromOperationID(t *testing.T) {
for _, tc := range []struct {
operationID string
scheduled bool
want string
}{
{chatFollowupPrefix + "abc", true, protocol.ChatOriginFollowup},
{chatRunFollowupPrefix + "7-finished-abc", true, protocol.ChatOriginFollowup},
{chatAutoContinuePrefix + "msg_1", true, protocol.ChatOriginAutoContinue},
{chatAutoSwitchPrefix + "msg_1", false, protocol.ChatOriginAutoSwitch},
{"op-1", true, protocol.ChatOriginScheduled},
{"op-1", false, ""},
} {
if got := chatPromptOrigin(tc.operationID, tc.scheduled); got != tc.want {
t.Errorf("chatPromptOrigin(%q, %v) = %q, want %q", tc.operationID, tc.scheduled, got, tc.want)
}
}
}
// A history page read after the server forgot the prompt still marks the
// server's own prompts by their text, and never the operator's or an
// assistant's message; the harness's slice is not changed.
func TestMarkChatOriginsRecognisesServerText(t *testing.T) {
text := func(role, s string) protocol.ChatMessage {
return protocol.ChatMessage{Role: role, Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: s}}}
}
page := []protocol.ChatMessage{
text(protocol.ChatRoleUser, "[Follow-up you scheduled at 2026-12-03T10:00:00Z with schedule_followup] check the run"),
text(protocol.ChatRoleUser, chatAutoSwitchText("a@example.com", "b@example.com")),
text(protocol.ChatRoleUser, "please look at [Follow-up you scheduled at noon]"),
text(protocol.ChatRoleAssistant, "[Follow-up you scheduled at 2026-12-03T10:00:00Z with schedule_followup] echo"),
{Role: protocol.ChatRoleUser, Origin: protocol.ChatOriginScheduled, Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: chatAutoSwitchTextPrefix}}},
}
got := markChatOrigins(page)
want := []string{protocol.ChatOriginFollowup, protocol.ChatOriginAutoSwitch, "", "", protocol.ChatOriginScheduled}
for i, origin := range want {
if got[i].Origin != origin {
t.Errorf("message %d origin = %q, want %q", i, got[i].Origin, origin)
}
}
if page[0].Origin != "" {
t.Fatalf("the harness's page was changed in place")
}
}
// The hub keeps a live user turn's origin, so a resume snapshot and a history
// page mapped to that turn carry it.
func TestChatHubKeepsUserOrigin(t *testing.T) {
hub := newChatHub()
hub.apply(protocol.ChatEventFrame{
SessionID: "ses_1", MessageID: "msg_f", Kind: protocol.ChatEventUser,
Client: chatFollowupClient, OperationID: chatFollowupPrefix + "abc", Origin: protocol.ChatOriginFollowup,
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: "later"},
})
snapshot := hub.snapshot("ses_1")
if len(snapshot) != 1 || snapshot[0].Origin != protocol.ChatOriginFollowup {
t.Fatalf("snapshot = %+v", snapshot)
}
got := hub.reconcileHistory("ses_1", []protocol.ChatMessage{{ID: "msg_f", Role: protocol.ChatRoleUser}})
if got[0].Origin != protocol.ChatOriginFollowup {
t.Fatalf("history origin = %+v", got[0])
}
}
// A queued prompt tells clients its origin.
func TestQueuedPromptWireOrigin(t *testing.T) {
later := time.Now().Add(time.Hour)
if got := (&chatQueuedPrompt{operationID: chatFollowupPrefix + "x", sendAt: later}).wire().Origin; got != protocol.ChatOriginFollowup {
t.Fatalf("follow-up origin = %q", got)
}
if got := (&chatQueuedPrompt{operationID: "op-1", sendAt: later}).wire().Origin; got != protocol.ChatOriginScheduled {
t.Fatalf("send-later origin = %q", got)
}
if got := (&chatQueuedPrompt{operationID: "op-1"}).wire().Origin; got != "" {
t.Fatalf("queued operator prompt origin = %q", got)
}
}