package server import ( "context" "encoding/json" "net/http/httptest" "os" "path/filepath" "strings" "testing" "time" "github.com/coder/websocket" "github.com/coder/websocket/wsjson" "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) // TestChatLiveEndToEnd drives the real brokered chat path against the installed // OpenCode: it opens the server's single owned endpoint, creates an operator // session through chat_create in a chosen directory, lists it through // chat_sessions, prompts it, and reads its history through chat_messages — all on // a scratch store and an in-process handler. Its key proof is that a genuine // pipeline stage session (created as a raw OpenCode session and recorded in // run_steps/run_session_owners exactly as a run does) never appears in // chat_sessions. It is opt-in (real inference) and never touches the deployed // server, ntfy or any lane; it runs on a t.TempDir workspace. func TestChatLiveEndToEnd(t *testing.T) { if os.Getenv("PIPELINES_TEST_OPENCODE_CHAT_LIVE") != "1" { t.Skip("set PIPELINES_TEST_OPENCODE_CHAT_LIVE=1 to run the live OpenCode chat end-to-end check") } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() dir := t.TempDir() conn, err := opencode.OpenChat(ctx, opencode.Executable(), dir) if err != nil { t.Fatalf("OpenChat: %v", err) } defer conn.Close() t.Logf("owned OpenCode endpoint %s (backend %s)", conn.URL, conn.Backend) store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat-live.db")) if err != nil { t.Fatal(err) } defer store.Close() srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: opencode.NewChat(conn), OperatorRegistry: store})) defer srv.Close() ws, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil) if err != nil { t.Fatalf("dial: %v", err) } defer ws.CloseNow() // chat_directories returns the operator's home first. home, err := os.UserHomeDir() if err != nil { t.Fatal(err) } if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatDirectories, ID: "d1"}); err != nil { t.Fatal(err) } _, resp := readChatResponse(t, ctx, ws) if resp.Type != protocol.ChatDirectories || len(resp.ChatDirectories) == 0 || resp.ChatDirectories[0] != home { t.Fatalf("chat_directories = %+v, want home %q first", resp, home) } t.Logf("chat_directories = %+v", resp.ChatDirectories) // chat_create records the operator session in the chosen directory with the // operator tag. if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatCreate, ID: "c1", ChatCreate: &protocol.ChatCreateRequest{Title: "pipelines chat live check", Directory: dir}}); err != nil { t.Fatal(err) } _, resp = readChatResponse(t, ctx, ws) if resp.Type != protocol.ChatCreate || resp.ChatSession == nil { t.Fatalf("chat_create response = %+v", resp) } sid := resp.ChatSession.ID if sid == "" || resp.ChatSession.Directory != dir || resp.ChatSession.Tag != protocol.ChatSessionTagOperator { t.Fatalf("created operator session = %+v, want directory %q and tag operator", resp.ChatSession, dir) } defer conn.Call(context.Background(), "delete", "/api/session/"+sid, nil) t.Logf("chat_create created operator session %s in %s (tag %s)", sid, resp.ChatSession.Directory, resp.ChatSession.Tag) // chat_sessions lists exactly the created operator session, with its registry // directory and tag. sessions := listLiveSessions(t, ctx, ws) if len(sessions) != 1 || sessions[0].ID != sid || sessions[0].Tag != protocol.ChatSessionTagOperator || sessions[0].Directory != dir { t.Fatalf("chat_sessions = %+v, want the created operator session %s", sessions, sid) } t.Logf("chat_sessions listed %d operator session(s): %+v", len(sessions), sessions) // A genuine pipeline stage session is created as a raw OpenCode session and // recorded in run_steps/run_session_owners exactly as a run does; chat_sessions // must never list it. stageID := createRawLiveSession(t, ctx, conn, dir) defer conn.Call(context.Background(), "delete", "/api/session/"+stageID, nil) recordLiveStageSession(t, ctx, store, stageID, dir) sessions = listLiveSessions(t, ctx, ws) for _, session := range sessions { if session.ID == stageID { t.Fatalf("pipeline stage session %s was listed: %+v", stageID, sessions) } } if len(sessions) != 1 || sessions[0].ID != sid { t.Fatalf("chat_sessions = %+v, want only the operator session %s", sessions, sid) } t.Logf("chat_sessions still listed only the operator session after recording stage session %s", stageID) // chat_messages resumes retained/readback history without error. if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatMessages, ID: "m1", ChatQuery: &protocol.ChatQuery{SessionID: sid}}); err != nil { t.Fatal(err) } _, resp = readChatResponse(t, ctx, ws) if resp.Type != protocol.ChatMessages { t.Fatalf("chat_messages response = %+v", resp) } t.Logf("chat_messages returned %d turn(s)", len(resp.ChatMessages)) // A completed turn: the ack echoes the request id and the reply arrives as // chat_event frames with stable session/message ids. if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatPrompt, ID: "p1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: sid, Text: "Reply with exactly one word: pong"}}); err != nil { t.Fatal(err) } frames, ack := readChatResponse(t, ctx, ws) if ack.Type != protocol.ChatPrompt || ack.ID != "p1" { t.Fatalf("prompt ack = %+v, want the echoed request id p1", ack) } completed := collectUntilStepEnded(t, ctx, ws, frames) assertLiveFrames(t, sid, completed) t.Logf("completed turn: %d chat_event frame(s) [%s]; first text delta %q", len(completed), frameKinds(completed), firstDelta(completed)) } // listLiveSessions issues one chat_sessions request and returns the listed // sessions. func listLiveSessions(t *testing.T, ctx context.Context, ws *websocket.Conn) []protocol.ChatSession { t.Helper() if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); err != nil { t.Fatal(err) } _, resp := readChatResponse(t, ctx, ws) if resp.Type != protocol.ChatSessions { t.Fatalf("chat_sessions response = %+v", resp) } return resp.ChatSessions } // createRawLiveSession creates a bare OpenCode session in dir through the shared // connection, mirroring how a pipeline run creates the session a stage drives. func createRawLiveSession(t *testing.T, ctx context.Context, conn *opencode.Connection, dir string) string { t.Helper() created, err := conn.Call(ctx, "post", "/api/session", map[string]any{ "title": "pipelines stage session live check", "location": map[string]string{"directory": dir}, "permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}}, }) if err != nil { t.Fatalf("create raw session: %v", err) } var session struct { Data struct { ID string `json:"id"` } `json:"data"` } if err := json.Unmarshal(created, &session); err != nil || session.Data.ID == "" { t.Fatalf("raw session response %s (err %v)", created, err) } return session.Data.ID } // recordLiveStageSession records sessionID as a genuine pipeline stage session // through the same store calls a run uses (CreateRun/BeginStep/RunSession), so it // is present in run_steps and run_session_owners and proven to be excluded from // chat_sessions by the operator filter, not by being unrecorded. func recordLiveStageSession(t *testing.T, ctx context.Context, store *pipeline.Store, sessionID, dir string) { t.Helper() if _, err := store.Submit(ctx, "chat-live-stage", pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } items, err := store.List(ctx) if err != nil { t.Fatal(err) } definition, err := store.Get(ctx, items[0].ID, 1) if err != nil { t.Fatal(err) } run, _, err := store.CreateRun(ctx, definition, "chat-live-stage-run", dir, false, nil, map[string]any{"prompt": "task"}) if err != nil { t.Fatal(err) } if err := store.BeginStep(ctx, run, 1, "one", map[string]any{"prompt": "task"}); err != nil { t.Fatal(err) } if err := store.RunSession(ctx, run, 1, sessionID, true, "v2", dir); err != nil { t.Fatal(err) } } // readChatResponse reads until a non-event response message arrives, returning // that message and any interleaved chat_event frames. func readChatResponse(t *testing.T, ctx context.Context, ws *websocket.Conn) ([]protocol.ChatEventFrame, protocol.Message) { t.Helper() var frames []protocol.ChatEventFrame for { message := readChatMessage(t, ctx, ws) if message.Type == protocol.ChatEvent && message.ChatEvent != nil { frames = append(frames, *message.ChatEvent) continue } return frames, message } } // readChatMessage reads one frame and fails on a transport error or a server // error frame, so the test never silently accepts a failure. func readChatMessage(t *testing.T, ctx context.Context, ws *websocket.Conn) protocol.Message { t.Helper() var message protocol.Message if err := wsjson.Read(ctx, ws, &message); err != nil { t.Fatalf("read: %v", err) } if message.Type == protocol.Error { t.Fatalf("server error frame: %s", message.Error) } return message } // collectUntilStepEnded keeps reading event frames until the turn's step ends. func collectUntilStepEnded(t *testing.T, ctx context.Context, ws *websocket.Conn, frames []protocol.ChatEventFrame) []protocol.ChatEventFrame { t.Helper() for { message := readChatMessage(t, ctx, ws) if message.Type != protocol.ChatEvent || message.ChatEvent == nil { continue } frames = append(frames, *message.ChatEvent) if last := frames[len(frames)-1]; last.Kind == protocol.ChatEventStep && last.Step != nil && last.Step.Status == protocol.ChatStepEnded { return frames } } } // assertLiveFrames fails unless every frame carries the session id and a stable // message id, and the turn produced at least one text frame. func assertLiveFrames(t *testing.T, sid string, frames []protocol.ChatEventFrame) { t.Helper() sawText := false for _, frame := range frames { if frame.SessionID != sid { t.Fatalf("frame session %q, want %s: %+v", frame.SessionID, sid, frame) } if frame.MessageID == "" { t.Fatalf("frame missing a stable message id: %+v", frame) } if frame.Kind == protocol.ChatEventText { sawText = true } } if !sawText { t.Fatalf("the completed turn produced no text frame: %+v", frames) } } func firstDelta(frames []protocol.ChatEventFrame) string { for _, frame := range frames { if frame.Kind == protocol.ChatEventText && frame.Delta != "" { return frame.Delta } } return "" } // frameKinds summarises the frame sequence (step frames carry their status) so // the live run's observed output is legible evidence. func frameKinds(frames []protocol.ChatEventFrame) string { kinds := make([]string, 0, len(frames)) for _, frame := range frames { if frame.Kind == protocol.ChatEventStep && frame.Step != nil { kinds = append(kinds, frame.Kind+":"+frame.Step.Status) continue } kinds = append(kinds, frame.Kind) } return strings.Join(kinds, ",") }