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) } }