package cli import ( "bytes" "context" "encoding/json" "strings" "sync/atomic" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/wstest" ) // TestCLIChatAdoptSendsOneRequest pins the CLI parity for chat_adopt: exactly one // chat_adopt request carrying the id travels over the shared protocol client, // --json prints the response, and the human line is concise. func TestCLIChatAdoptSendsOneRequest(t *testing.T) { var calls int32 var got protocol.Message session := protocol.ChatSession{ID: "ses_abc", Title: "mine", Directory: "/work/mine", Tag: protocol.ChatSessionTagOperator} endpoint := wstest.Server(t, func(request protocol.Message) protocol.Message { atomic.AddInt32(&calls, 1) got = request return protocol.Message{Type: protocol.ChatAdopt, ChatSession: &session, ChatSessions: []protocol.ChatSession{session}} }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var out, errOut bytes.Buffer app := App{Out: &out, Err: &errOut, In: strings.NewReader("")} if err := app.Run(ctx, []string{"--url", endpoint, "chat-adopt", "ses_abc", "--json"}); err != nil { t.Fatal(err) } if n := atomic.LoadInt32(&calls); n != 1 { t.Fatalf("chat-adopt sent %d requests, want exactly one", n) } if got.Type != protocol.ChatAdopt || got.ChatAdopt == nil || got.ChatAdopt.SessionID != "ses_abc" { t.Fatalf("wire request = %+v, want chat_adopt with the id", got) } var decoded protocol.Message if err := json.Unmarshal(out.Bytes(), &decoded); err != nil { t.Fatalf("stdout is not parseable JSON: %v\n%s", err, out.String()) } if decoded.ChatSession == nil || decoded.ChatSession.ID != "ses_abc" { t.Fatalf("stdout lost the adopted session: %+v", decoded.ChatSession) } var humanOut, humanErr bytes.Buffer human := App{Out: &humanOut, Err: &humanErr, In: strings.NewReader("")} if err := human.Run(ctx, []string{"--url", endpoint, "chat-adopt", "ses_abc"}); err != nil { t.Fatal(err) } if !strings.Contains(humanOut.String(), "ses_abc") || !strings.Contains(humanOut.String(), "/work/mine") { t.Fatalf("human line = %q, want the id and the endpoint directory", humanOut.String()) } }