Files
pipelines/internal/wstest/wstest.go
T
alan 8c1eea53a9 Report a stale client and refuse an unreadable newer server
Exchange a build/protocol identity so an older CLI can no longer talk to a
newer native server silently:

- protocol carries ProtocolVersion and every server frame (including the
  unsolicited welcome) is stamped with the server build and protocol, so a
  client that never pings still learns the server identity.
- buildinfo.Compare classifies the relationship as InSync/Warn/Refuse and
  names both builds; the client Watch reports one warning per connection and
  returns a hard error when the server speaks a newer, uninterpretable
  protocol so the caller never acts on the frame.
- client.ReceiveNotice decodes the raw frame so an unmodeled top-level field
  produces a one-time "unrecognized field(s)" notice instead of being dropped
  silently (the case that hid the artifacts field).
- CLI routes warnings to stderr, keeps machine JSON stdout clean and fails
  non-zero on a refused mismatch; ping/connection print the client/server
  relationship. The TUI surfaces the same notices through the shared log.
- scripts/setup-native.py now warns when an older `pipelines` earlier on PATH
  shadows prefix/bin/pipelines, naming both paths and versions, without
  installing or modifying anything else.
- Docs, the packaged debugging skill reference and README no longer imply the
  installed CLI always matches the server.

Tests cover the comparison table, unknown-field detection and caching, the
server identity stamping on pong and welcome, the CLI/TUI reporting paths and
the installer PATH shadowing warning.
2026-10-16 18:37:01 +01:00

61 lines
1.7 KiB
Go

// Package wstest provides the small websocket echo server shared by the client
// and CLI identity tests, so the request/reply harness is defined once.
package wstest
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Server starts a websocket endpoint that answers every request with
// reply(request), echoing the request ID, and returns the client URL.
func Server(t testing.TB, reply func(protocol.Message) protocol.Message) string {
t.Helper()
return serve(t, func(request protocol.Message) any {
response := reply(request)
response.ID = request.ID
return response
})
}
// Raw starts a websocket endpoint that answers every request with a JSON object
// from payload(request), with "id" set to the request ID, so tests can include
// top-level keys the client does not model.
func Raw(t testing.TB, payload func(protocol.Message) map[string]any) string {
t.Helper()
return serve(t, func(request protocol.Message) any {
body := payload(request)
body["id"] = request.ID
return body
})
}
func serve(t testing.TB, reply func(protocol.Message) any) string {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(w, r, nil)
if err != nil {
return
}
defer conn.CloseNow()
for {
var request protocol.Message
if err := wsjson.Read(r.Context(), conn, &request); err != nil {
return
}
if err := wsjson.Write(r.Context(), conn, reply(request)); err != nil {
return
}
}
}))
t.Cleanup(srv.Close)
return "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect"
}