Add tests proving chat_interrupt stops a running chat turn

The chat_interrupt path was already wired end to end (protocol type,
server dispatch through handleChatSimple to broker.Interrupt, and the
adapter POST /api/session/{id}/interrupt), but nothing exercised it
against a live stream. Add the missing coverage:

- internal/opencode/chat_test.go: pin the adapter's outbound call
  exactly (POST /api/session/ses_1/interrupt, empty JSON body, one
  request, nil on 200) plus a non-nil error on HTTP 500.
- internal/server/chat_test.go: drive the real opencode.Chat adapter
  over an httptest SSE fixture that holds the turn open until the
  interrupt arrives, then assert the run ends, the interrupt is
  acknowledged, no error frame is sent, and the prompt/interrupt posts
  and interrupt path match. Also cover invalid-session rejection,
  the unconfigured-broker error, and broker-failure reporting, adding a
  spare interruptErr hook to the scripted fakeChat.

Production code is unchanged: verification found no gap in the existing
interrupt transport. Tests use httptest loopback fixtures only and do
not touch the live OpenCode service.
This commit is contained in:
2026-10-16 06:43:02 +01:00
parent e131c02fa1
commit b28f88be66
2 changed files with 233 additions and 1 deletions
+54
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -165,6 +166,59 @@ func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) {
}
}
// TestChatInterruptPostsInterruptEndpoint pins the outbound interrupt call: the
// adapter must POST an empty JSON body to /api/session/{id}/interrupt exactly
// once and return nil, and it must surface a non-nil error when the endpoint
// fails. One interrupter request, no probing beyond what the call needs.
func TestChatInterruptPostsInterruptEndpoint(t *testing.T) {
t.Run("success", func(t *testing.T) {
type request struct{ method, path, body string }
var mu sync.Mutex
var requests []request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, _ := io.ReadAll(r.Body)
mu.Lock()
requests = append(requests, request{method: r.Method, path: r.URL.Path, body: string(data)})
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{}`))
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := chat.Interrupt(ctx, "ses_1"); err != nil {
t.Fatalf("interrupt: %v", err)
}
mu.Lock()
got := append([]request(nil), requests...)
mu.Unlock()
if len(got) != 1 {
t.Fatalf("interrupt requests = %d, want 1: %+v", len(got), got)
}
if got[0].method != http.MethodPost || got[0].path != "/api/session/ses_1/interrupt" {
t.Fatalf("interrupt request = %+v", got[0])
}
if got[0].body != "{}" {
t.Fatalf("interrupt body = %q, want {}", got[0].body)
}
})
t.Run("failure", func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := chat.Interrupt(ctx, "ses_1"); err == nil {
t.Fatal("a failing interrupt endpoint must return an error")
}
})
}
// TestDecodeChatMessages loads a seeded user + assistant session in both
// response envelopes the two surfaces return and asserts the turns come back
// with role, id, part order, part ids and the completed tool's input, content