package server import ( "context" "fmt" "net/http" "net/http/httptest" "strings" "sync/atomic" "testing" "time" "github.com/coder/websocket" "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/protocol" ) type shellOutputTestBroker struct { *fakeChat outputCalls int } func (b *shellOutputTestBroker) ShellOutput(context.Context, protocol.ChatShellOutputQuery) (protocol.ChatShellOutputPage, error) { b.outputCalls++ return protocol.ChatShellOutputPage{Output: "unexpected"}, nil } func TestChatShellOutputRejectsBoundsBeforeRegistryOrProvider(t *testing.T) { store := newOperatorStore(t) broker := &shellOutputTestBroker{fakeChat: scriptedChat()} queries := []protocol.ChatShellOutputQuery{ {SessionID: "ses_1", ShellID: "sh_1", Limit: 0}, {SessionID: "ses_1", ShellID: "sh_1", Limit: -1}, {SessionID: "ses_1", ShellID: "sh_1", Cursor: protocol.ChatShellOutputMaxCursor + 1, Limit: 1}, {SessionID: "ses_1", ShellID: "sh_1/../x", Limit: 1}, {SessionID: "ses_1", ShellID: "sh_1%2f", Limit: 1}, {SessionID: "ses_1", ShellID: "sh_1?cursor=0", Limit: 1}, {SessionID: "ses_1", ShellID: "sh_1#fragment", Limit: 1}, {SessionID: "ses_1", ShellID: "sh_%ZZ", Limit: 1}, {SessionID: "ses_1", ShellID: "sh_" + string(make([]byte, 126)), Limit: 1}, } for _, query := range queries { var response protocol.Message handleChatShellOutput(context.Background(), protocol.Message{ChatShellOutputQuery: &query}, broker, store, func(m protocol.Message) error { response = m return nil }) if response.Type != protocol.Error { t.Fatalf("query %+v response = %+v", query, response) } } if broker.outputCalls != 0 { t.Fatalf("invalid queries reached provider %d times", broker.outputCalls) } } // TestChatShellOutputRealHandlerWebsocketOwnedAndForeign drives the real // authenticated adapter, HTTP fixture, HandlerWithOptions, and /connect path. // The foreign detail deliberately has a marker that would be returned by its // output route; the session metadata check must reject it first. func TestChatShellOutputRealHandlerWebsocketOwnedAndForeign(t *testing.T) { testChatShellOutputOwnedAndForeign(t, false) } func TestChatShellOutputRealHandlerExitedReadback(t *testing.T) { testChatShellOutputOwnedAndForeign(t, true) } func testChatShellOutputOwnedAndForeign(t *testing.T, exited bool) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() var providerRequests atomic.Int32 fixtureHandler := LargeChatFixtureHandlerWithShell(t.TempDir(), exited) fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { providerRequests.Add(1) fixtureHandler.ServeHTTP(w, r) })) defer fixture.Close() attached, err := opencode.OpenEndpointAttached(ctx, fixture.URL, LargeChatFixturePassword, "", "") if err != nil { t.Fatal(err) } store := newOperatorStore(t) if err := store.RecordOperatorSession(ctx, LargeChatFixtureSessionID, t.TempDir(), "owned", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } handler := HandlerWithOptions(ctx, Options{Chat: opencode.NewChat(attached.Connection), OperatorRegistry: store}).(*runHandler) srv := httptest.NewServer(handler) select { case <-handler.hub.recoveryDone: case <-ctx.Done(): t.Fatal("startup recovery did not finish") } defer srv.Close() ws := dialShellOutputWebsocket(t, ctx, srv) defer ws.CloseNow() writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShells, ID: "owned-shells", ChatQuery: &protocol.ChatQuery{SessionID: LargeChatFixtureSessionID}}) owned := readShellOutputMessage(t, ctx, ws) if owned.Type != protocol.ChatShells || (exited && len(owned.ChatShells) != 0) || (!exited && (len(owned.ChatShells) != 1 || owned.ChatShells[0].ID != LargeChatFixtureShellID || owned.ChatShells[0].Status != protocol.ChatShellRunning)) { t.Fatalf("owned shell listing = %+v", owned) } wantStatus := protocol.ChatShellRunning if exited { wantStatus = protocol.ChatShellExited } writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShellOutput, ID: "owned-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: LargeChatFixtureSessionID, ShellID: LargeChatFixtureShellID, Limit: protocol.ChatShellOutputLimit}}) page := readShellOutputMessage(t, ctx, ws) if page.Type != protocol.ChatShellOutput || page.ChatShellOutput == nil || page.ChatShellOutput.Output != LargeChatFixtureShellOutput || page.ChatShellOutput.Cursor != int64(len(LargeChatFixtureShellOutput)) || page.ChatShellOutput.Shell.Status != wantStatus { t.Fatalf("owned output page = %+v", page) } if exited { if page.ChatShellOutput.Shell.Exit == nil || *page.ChatShellOutput.Shell.Exit != 0 { t.Fatalf("selected exited shell readback = %+v", page.ChatShellOutput.Shell) } } else if page.ChatShellOutput.Shell.Exit != nil { t.Fatalf("running shell unexpectedly has exit = %+v", page.ChatShellOutput.Shell.Exit) } if page.ChatShellOutput.Cursor != int64(len(page.ChatShellOutput.Output)) || page.ChatShellOutput.Size != page.ChatShellOutput.Cursor { t.Fatalf("non-deterministic output cursor/size = %+v", page.ChatShellOutput) } refusals := []struct { name string request protocol.Message }{ {name: "foreign-output", request: protocol.Message{Type: protocol.ChatShellOutput, ID: "foreign-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: LargeChatFixtureSessionID, ShellID: LargeChatFixtureForeignShellID, Limit: protocol.ChatShellOutputLimit}}}, {name: "foreign-list", request: protocol.Message{Type: protocol.ChatShells, ID: "foreign-list", ChatQuery: &protocol.ChatQuery{SessionID: LargeChatFixtureForeignSessionID}}}, {name: "syntax-invalid-list", request: protocol.Message{Type: protocol.ChatShells, ID: "invalid-list", ChatQuery: &protocol.ChatQuery{SessionID: "ses_bad/invalid"}}}, {name: "unknown-output", request: protocol.Message{Type: protocol.ChatShellOutput, ID: "unknown-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: "ses_unregistered_fixture", ShellID: LargeChatFixtureShellID, Limit: protocol.ChatShellOutputLimit}}}, } for _, refusal := range refusals { beforeRefusal := providerRequests.Load() writeChat(t, ctx, ws, refusal.request) response := readShellOutputMessage(t, ctx, ws) if response.Type != protocol.Error || strings.Contains(response.Error, LargeChatFixtureForeignShellOutput) { t.Fatalf("%s response leaked or succeeded: %+v", refusal.name, response) } if got := providerRequests.Load(); got != beforeRefusal { t.Fatalf("%s reached provider: requests %d -> %d", refusal.name, beforeRefusal, got) } } } func TestChatShellOutputRealHandlerUnavailableAndReconnect(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() fixture := httptest.NewServer(LargeChatFixtureHandlerWithShell(t.TempDir(), false)) defer fixture.Close() attached, err := opencode.OpenEndpointAttached(ctx, fixture.URL, LargeChatFixturePassword, "", "") if err != nil { t.Fatal(err) } store := newOperatorStore(t) if err := store.RecordOperatorSession(ctx, LargeChatFixtureSessionID, t.TempDir(), "owned", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: opencode.NewChat(attached.Connection), OperatorRegistry: store})) defer srv.Close() ws := dialShellOutputWebsocket(t, ctx, srv) writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShellOutput, ID: "missing-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: LargeChatFixtureSessionID, ShellID: "sh_missing_fixture", Limit: 64}}) missing := readShellOutputMessage(t, ctx, ws) if missing.Type != protocol.ChatShellOutput || missing.ChatShellOutput == nil || !missing.ChatShellOutput.Unavailable { t.Fatalf("404 shell output = %+v", missing) } ws = dialShellOutputWebsocket(t, ctx, srv) defer ws.CloseNow() writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShells, ID: "reconnect", ChatQuery: &protocol.ChatQuery{SessionID: LargeChatFixtureSessionID}}) if got := readShellOutputMessage(t, ctx, ws); got.Type != protocol.ChatShells { t.Fatalf("reconnect shell listing = %+v", got) } if _, err := opencode.OpenEndpointAttached(ctx, fixture.URL, "wrong-password", "", ""); err == nil { t.Fatal("wrong provider authentication unexpectedly succeeded") } } func TestChatShellOutputRejectsMalformedAndOversizedProviderPages(t *testing.T) { for _, tc := range []struct { name string output string }{ {name: "malformed", output: `{"data":{"output":"x"}}`}, {name: "oversized", output: fmt.Sprintf(`{"data":{"output":%q,"cursor":%d,"size":%d,"truncated":false}}`, strings.Repeat("x", protocol.ChatShellOutputLimit+1), protocol.ChatShellOutputLimit+1, protocol.ChatShellOutputLimit+1)}, } { t.Run(tc.name, func(t *testing.T) { fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !largeChatFixtureAuthenticated(w, r) { return } w.Header().Set("Content-Type", "application/json") switch r.URL.Path { case "/api/info": _, _ = w.Write([]byte(`{"version":"fixture","urls":[]}`)) case "/api/event": w.Header().Set("Content-Type", "text/event-stream") case "/api/shell/sh_malformed_fixture": _, _ = w.Write([]byte(`{"data":{"id":"sh_malformed_fixture","status":"running","metadata":{"sessionID":"ses_malformed_fixture"}}}`)) case "/api/shell/sh_malformed_fixture/output": _, _ = w.Write([]byte(tc.output)) default: if r.URL.Path == "/api/shell" { _, _ = w.Write([]byte(`{"data":[]}`)) return } http.NotFound(w, r) } })) defer fixture.Close() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() attached, err := opencode.OpenEndpointAttached(ctx, fixture.URL, LargeChatFixturePassword, "", "") if err != nil { t.Fatal(err) } _, err = opencode.NewChat(attached.Connection).ShellOutput(ctx, protocol.ChatShellOutputQuery{SessionID: "ses_malformed_fixture", ShellID: "sh_malformed_fixture", Limit: protocol.ChatShellOutputLimit}) if err == nil { t.Fatalf("%s provider page was accepted", tc.name) } }) } } func TestChatShellOutputRealHandlerCancellationStopsProvider(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() started := make(chan struct{}) canceled := make(chan struct{}) fixtureHandler := LargeChatFixtureHandlerWithShell(t.TempDir(), false) fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/shell/sh_cancel_fixture" { if !largeChatFixtureAuthenticated(w, r) { return } close(started) <-r.Context().Done() close(canceled) return } fixtureHandler.ServeHTTP(w, r) })) defer fixture.Close() attached, err := opencode.OpenEndpointAttached(ctx, fixture.URL, LargeChatFixturePassword, "", "") if err != nil { t.Fatal(err) } store := newOperatorStore(t) if err := store.RecordOperatorSession(ctx, "ses_cancel_fixture", t.TempDir(), "cancel", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } serverCtx, serverCancel := context.WithCancel(ctx) defer serverCancel() srv := httptest.NewServer(HandlerWithOptions(serverCtx, Options{Chat: opencode.NewChat(attached.Connection), OperatorRegistry: store})) defer srv.Close() ws := dialShellOutputWebsocket(t, ctx, srv) writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShellOutput, ID: "cancel-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: "ses_cancel_fixture", ShellID: "sh_cancel_fixture", Limit: 64}}) select { case <-started: case <-ctx.Done(): t.Fatal("provider detail request did not start") } serverCancel() select { case <-canceled: case <-ctx.Done(): t.Fatal("canceling handler did not cancel provider request") } } func dialShellOutputWebsocket(t *testing.T, ctx context.Context, srv *httptest.Server) *websocket.Conn { t.Helper() ws, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil) if err != nil { t.Fatal(err) } return ws } func readShellOutputMessage(t *testing.T, ctx context.Context, ws *websocket.Conn) protocol.Message { t.Helper() var message protocol.Message if err := readTestFrame(ctx, ws, &message); err != nil { t.Fatal(err) } return message }