package claude import ( "context" "encoding/json" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" ) func backgroundSession(t *testing.T) (*Chat, *session) { t.Helper() chat := New(context.Background()) return chat, chat.sessionFor("ses_bg", t.TempDir()) } func line(t *testing.T, value any) []byte { t.Helper() raw, err := json.Marshal(value) if err != nil { t.Fatal(err) } return raw } func toolUse(t *testing.T, id, name string, input map[string]any) []byte { return line(t, map[string]any{ "type": "assistant", "message": map[string]any{ "id": "msg_" + id, "role": "assistant", "content": []any{map[string]any{"type": "tool_use", "id": id, "name": name, "input": input}}, }, }) } func toolResult(t *testing.T, id, text string) []byte { return line(t, map[string]any{ "type": "user", "message": map[string]any{ "role": "user", "content": []any{map[string]any{"type": "tool_result", "tool_use_id": id, "content": text}}, }, }) } func running(t *testing.T, chat *Chat) []protocol.ChatShell { t.Helper() shells, err := chat.Shells(context.Background(), "ses_bg") if err != nil { t.Fatal(err) } return shells } func TestBackgroundBashStaysRunningUntilItsNotification(t *testing.T) { chat, s := backgroundSession(t) s.handleLine(nil, toolUse(t, "toolu_bg", "Bash", map[string]any{"command": "pipelines wait 191", "run_in_background": true})) s.handleLine(nil, toolResult(t, "toolu_bg", "Command running in background with ID: b123. Output is being written to: /tmp/b123.output")) shells := running(t, chat) if len(shells) != 1 || shells[0].Command != "pipelines wait 191" { t.Fatalf("a background command must stay listed while it runs, got %+v", shells) } s.handleLine(nil, line(t, map[string]any{ "type": "user", "message": map[string]any{ "role": "user", "content": "\nb123\ntoolu_bg\n" + "completed\nBackground command \"Wait\" completed (exit code 3)\n", }, })) if shells := running(t, chat); len(shells) != 0 { t.Fatalf("a notified task must leave the running list, got %+v", shells) } s.mu.Lock() exit := s.shells["toolu_bg"].shell.Exit s.mu.Unlock() if exit == nil || *exit != 3 { t.Fatalf("the notification's exit code is recorded, got %v", exit) } } func TestBackgroundAgentFinishesFromTheStructuredNotice(t *testing.T) { chat, s := backgroundSession(t) s.handleLine(nil, toolUse(t, "toolu_agent", "Agent", map[string]any{"description": "Review the lists", "prompt": "…"})) s.handleLine(nil, toolResult(t, "toolu_agent", "Async agent launched successfully.")) shells := running(t, chat) if len(shells) != 1 || shells[0].Command != "Agent: Review the lists" { t.Fatalf("a background agent is listed by its description, got %+v", shells) } s.handleLine(nil, line(t, map[string]any{ "type": "system", "subtype": "task_notification", "task_id": "a1", "tool_use_id": "toolu_agent", "status": "completed", "summary": "Agent finished", })) if shells := running(t, chat); len(shells) != 0 { t.Fatalf("the structured notice finishes the agent, got %+v", shells) } } func TestForegroundCommandsAndAgentsFinishWithTheirResult(t *testing.T) { chat, s := backgroundSession(t) s.handleLine(nil, toolUse(t, "toolu_fg", "Bash", map[string]any{"command": "ls"})) s.handleLine(nil, toolUse(t, "toolu_sync", "Agent", map[string]any{"description": "Quick look"})) if shells := running(t, chat); len(shells) != 2 { t.Fatalf("both are running while their calls are open, got %+v", shells) } s.handleLine(nil, toolResult(t, "toolu_fg", "file.txt")) s.handleLine(nil, toolResult(t, "toolu_sync", "Here is what I found.")) if shells := running(t, chat); len(shells) != 0 { t.Fatalf("a foreground command or agent finishes with its result, got %+v", shells) } // A stray notification for an unknown or finished task changes nothing. s.finishBackgroundTasks("toolu_fgcompleted") s.finishBackgroundTasks("toolu_unknown") if shells := running(t, chat); len(shells) != 0 { t.Fatalf("stray notifications list nothing, got %+v", shells) } }